Skip to content

Commit

Permalink
Add support to oneOf/anyOf with constant schemas in StringField
Browse files Browse the repository at this point in the history
  • Loading branch information
fbessou committed Jun 12, 2017
1 parent d1a515c commit cc6d17d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/components/fields/StringField.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import PropTypes from "prop-types";
import {
getWidget,
getUiOptions,
isSelect,
optionsList,
getDefaultRegistry,
} from "../../utils";
Expand All @@ -25,7 +26,7 @@ function StringField(props) {
} = props;
const { title, format } = schema;
const { widgets, formContext } = registry;
const enumOptions = Array.isArray(schema.enum) && optionsList(schema);
const enumOptions = isSelect(schema) && optionsList(schema);
const defaultWidget = format || (enumOptions ? "select" : "text");
const { widget = defaultWidget, placeholder = "", ...options } = getUiOptions(
uiSchema
Expand Down
18 changes: 11 additions & 7 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,20 +299,24 @@ export function toConstant(schema) {
}
}

export function isMultiSelect(schema, definitions = {}) {
if (!schema.uniqueItems || !schema.items) {
return false;
}
const itemsSchema = retrieveSchema(schema.items, definitions);
const altSchemas = itemsSchema.oneOf || itemsSchema.anyOf;
if (Array.isArray(itemsSchema.enum)) {
export function isSelect(_schema, definitions = {}) {
const schema = retrieveSchema(_schema, definitions);
const altSchemas = schema.oneOf || schema.anyOf;
if (Array.isArray(schema.enum)) {
return true;
} else if (Array.isArray(altSchemas)) {
return altSchemas.every(altSchemas => isConstant(altSchemas));
}
return false;
}

export function isMultiSelect(schema, definitions = {}) {
if (!schema.uniqueItems || !schema.items) {
return false;
}
return isSelect(schema.items, definitions);
}

export function isFilesArray(schema, uiSchema, definitions = {}) {
if (uiSchema["ui:widget"] === "files") {
return true;
Expand Down

0 comments on commit cc6d17d

Please sign in to comment.