react-json-schema-form-extras
react-json-schema-form-extras copied to clipboard
add DidUpdate support to typeahead
I would like to propose to add DidUpdate support to your typeahead field. Somenthing like it:
function isValidOptions(options) {
return !!options && options.constructor === Array;
}
export class AsyncTypeaheadField extends BaseTypeaheadField {
constructor(props) {
super(props);
let { schema, uiSchema: { asyncTypeahead }, formData } = this.props;
this.state = {
options: isValidOptions(asyncTypeahead.options) ? asyncTypeahead.options : [],
isLoading: false,
selected: isValidFormData(formData)
? toSelected(formData, schema, asyncTypeahead.mapping, asyncTypeahead.options)
: [],
};
}
componentDidUpdate(prevProps, prevState) {
if (this.props.uiSchema !== prevProps.uiSchema) {
let { schema, uiSchema: { asyncTypeahead }, formData} = this.props;
this.setState({
options: isValidOptions(asyncTypeahead.options) ? asyncTypeahead.options : [],
isLoading: false,
selected: isValidFormData(formData)
? toSelected(formData, schema, asyncTypeahead.mapping, asyncTypeahead.options)
: [],
});
}
}
....