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

Method to refresh form inputs on model update?

Open matias-sandell opened this issue 9 years ago • 10 comments

Is there a method for refreshing form values or re-rendering the SchemaForm when the model updates? It seems the SchemaForm doesn't react (no pun intended) to model changes (for example when routing to another item, which in turn updates the model).

Not sure if it's an issue with material-ui or react-schema-form though.

Excellent work, by the way!

matias-sandell avatar Feb 25 '16 09:02 matias-sandell

Frankly, I never thought about your use case before. And all the built in fields are optimised for performance. The worst case would to build a customized field that supports two way binding given that you only have one or two fields that need to be updated from outside.

stevehu avatar Feb 25 '16 12:02 stevehu

OK, I probably need to add a refresh method to ComposedComponent since there could be a lot of fields depending on the editor type. Will get back to you with a PR when I have a solution.

matias-sandell avatar Feb 26 '16 13:02 matias-sandell

Actually I'm struggling with the same issue, I think it can be a really popular use case. In order to achieve that, In ComposedComponent you can add componentWillReceiveProps and set the state, based on new model. Also props have to be changed from defaultValue to value, in some components like Text or Number. I think it would be a great feature to have, and I can submit a pull request at any moment. @stevehu what do you think about that?

agrishun avatar May 20 '16 11:05 agrishun

Sounds good to me. We just need to make sure that array and fieldset work with refresh. Thanks

stevehu avatar May 20 '16 11:05 stevehu

I've also ran into a situation where I need my form to update, because of a model change. What is the status of this feature?

Haggus avatar Aug 18 '16 11:08 Haggus

Hello! I ran into this one too: my form model needs to be updated from outside to allow a "reset button" feature (on click on the button reset select fields to a specific value).

I worked out a solution by altering the Select component's state when it receives props. Let me know if that solution would suit you, and I'll have a PR ready!

Eventually, other components could be updated that way too.

foxdb avatar Aug 17 '17 00:08 foxdb

@foxdb We are looking for a solution to this problem. Is anybody working on it?

3thanHead avatar Aug 18 '17 17:08 3thanHead

@foxdb I think your solution should work if it applies to all types of field. I am looking forward to the PR. Thanks.

stevehu avatar Aug 18 '17 17:08 stevehu

@foxdb Thanks for your help. We have several PRs recently and one of the PR got a small issue. I am still waiting for the developer to take a look and get it fixed. I don't know if we should have a release with a small issue. If someone is waiting for this feature, I will release it immediately with a comment.

#58

stevehu avatar Aug 21 '17 23:08 stevehu

In waiting, you can use a specific schemaform property for forcing rerendering.

For example, use a reset key which changes of value and is associated to the schemaform.

...
handleReset = () => this.setState({ reset: ! this.state.reset, model: this.props.model /* default model */ });
...
// rendering method
const { reset, model } = this.state;
return (
<SchemaForm key={this.state.reset} model={this.state.model} />
);
...

b3j0f avatar Sep 11 '17 12:09 b3j0f