Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions example/ExamplePage.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import {
Select
} from "@material-ui/core";
import Localizer from "./data/tests/localizer";
import SelectLabel from "./data/selectlabel";
import ErrorBoundary from "./ErrorBoundary";
// RcSelect is still in migrating process so it's excluded for now
// import RcSelect from 'react-schema-form-rc-select/lib/RcSelect';
const examples = {
localizer: Localizer
localizer: Localizer,
selectLabel: SelectLabel
};

type State = {
Expand Down Expand Up @@ -48,7 +50,7 @@ class ExamplePage extends React.Component<void, State> {
{ label: "Readonly", value: "data/readonly.json" },
{ label: "Array", value: "data/array.json" },
{ label: "Object", value: "data/object.json" },
{ label: "Select with label", value: "data/selectlabel.json" },
{ label: "Select", value: "selectLabel" },
{ label: "ArraySelect", value: "data/arrayselect.json" },
{ label: "Conditional Array", value: "data/conditionalarray.json" },
{
Expand Down
51 changes: 51 additions & 0 deletions example/data/selectlabel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
export default {
schema: {
type: "object",
required: ["name", "region"],
title: "Create Wallet",
properties: {
name: {
title: "Name",
type: "string"
},
region: {
title: "Region",
type: "string",
enum: ["0000", "0001", "0002"]
},
bank: {
title: "Bank",
type: "string",
enum: [
{ id: 1, name: "First" },
{ id: 2, name: "Second" },
{ id: 3, name: "Third" }
],
isObject: true,
displayFn: each => each.name
}
}
},
form: [
"name",
{
key: "region",
type: "select",
titleMap: [
{
value: "0000",
name: "Americas"
},
{
value: "0001",
name: "Asia, Oceania"
},
{
value: "0002",
name: "Europe, Africa"
}
]
},
"bank"
]
};
46 changes: 0 additions & 46 deletions example/data/selectlabel.json

This file was deleted.

57 changes: 48 additions & 9 deletions src/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ type Props = {
model: any,
form: any,
onChangeValidate: any,
localization: Localization
localization: Localization,
onChange: any
};

type State = {
Expand All @@ -39,10 +40,38 @@ class Select extends Component<Props, State> {
}

onSelected = event => {
const { onChangeValidate } = this.props;
const {
onChangeValidate,
onChange,
form: {
key,
schema: { isObject, enum: values, findFn }
}
} = this.props;
const currentValue = event.target.value;
this.setState({ currentValue });
onChangeValidate(event);
if (isObject) {
const item = values.find(each =>
findFn ? findFn(each, currentValue) : each === currentValue
);
onChange(key, item);
} else {
onChangeValidate(event);
}
};

getLabel = each => {
const {
form: {
schema: { displayFn, noLocalization }
},
localization: { getLocalizedString }
} = this.props;
if (displayFn) {
return displayFn(each);
}
if (noLocalization) return each.name;
return getLocalizedString(each.name);
};

render() {
Expand All @@ -51,12 +80,22 @@ class Select extends Component<Props, State> {
localization: { getLocalizedString }
} = this.props;
const { currentValue } = this.state;
const menuItems = form.titleMap.map((item, idx) => (
// eslint-disable-next-line react/no-array-index-key
<MenuItem key={idx} value={item.value}>
{item.name && getLocalizedString(item.name)}
</MenuItem>
));
let menuItems = [];
if (form.schema.isObject) {
menuItems = form.schema.enum.map((item, idx) => (
// eslint-disable-next-line react/no-array-index-key
<MenuItem key={idx} value={item}>
{this.getLabel(item)}
</MenuItem>
));
} else {
menuItems = form.titleMap.map((item, idx) => (
// eslint-disable-next-line react/no-array-index-key
<MenuItem key={idx} value={item.value}>
{this.getLabel(item)}
</MenuItem>
));
}
return (
<FormControl fullWidth>
<InputLabel required={form.required}>
Expand Down
2 changes: 1 addition & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ const select = (name, schema, options) => {
const f = stdFormObj(name, schema, options);
f.key = options.path;
f.type = "select";
if (!f.titleMap) {
if (!f.titleMap && !schema.isObject) {
f.titleMap = enumToTitleMap(schema.enum);
}
options.lookup[ObjectPath.stringify(options.path)] = f;
Expand Down