react-json-schema-form-extras icon indicating copy to clipboard operation
react-json-schema-form-extras copied to clipboard

add DidUpdate support to typeahead

Open WpSlt opened this issue 4 years ago • 0 comments

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)
			: [],
		});
	  }
	}
....

WpSlt avatar Mar 03 '20 10:03 WpSlt