Comments (19)
onFieldChange=(name, value, data) => {
To be noted, we should allow intercepting any deeply nested updates here.
from react-jsonschema-form.
@acouch I was having a similar issue (I think). The values of some fields on my form are constrained by others - eg can only add up to some maximum value. In Form.onChange I grab the formData and store it in some state. If I try modifying some of the values of the formData to satisfy the constraint before putting it into state then the other fields would always be one step behind. The solution for me was to do a deep clone of the formData property before mutating it. Potentially inefficient, but effective.
from react-jsonschema-form.
Anyone find a decent solution for this? If I intercept the state change similar to https://github.com/Kinto/formbuilder/blob/8ceae7585108c3fe8d0ed64882f9d856cf2ef9e7/formbuilder/components/EditableField.js#L43-L62 my slug field is always one character behind.
from react-jsonschema-form.
@OliverColeman I am having the VERY SAME issue you had. And your suggestion fixed my problem. Thanks man. "would always be one step behind".
from react-jsonschema-form.
I remember this was done in the formbuilder to compute the field id from the label.
from react-jsonschema-form.
Do you guys have a dream API in mind for this here?
from react-jsonschema-form.
UISchema set to something like:
{
"slug": {"populate_from": "title"},
}
Where slug
and title
are JSONSchema properties maybe?
populate_from
could also be slugify_from
from react-jsonschema-form.
After discussion:
- We should keep this library as minimalist and focused as possible
- For the created date use-case, it is possible to use the
onSubmit()
function to alter the final object - For the slug use-case, the idea would be to introduce a
onFieldChange
hook that can alter the field value (or other fields)
<Form schema={schema}
uiSchema={uiSchema}
onFieldChange=(name, value, data) => {
switch(name) {
case 'title':
return {...data, [name]: value, slug: slugify(value)};
default:
return {...data: [name]: value}
}
} />
from react-jsonschema-form.
Jdorns Json-editor uses the templates for this:
https://github.com/jdorn/json-editor#templates
from react-jsonschema-form.
Hmm, nice though most often you're likely to want a little more than just templating... functions are probably better for advanced computations :)
from react-jsonschema-form.
Yup, templates do have their limitations.
from react-jsonschema-form.
onFieldChange=(name, value, data) => {
Could I help with implementing this feature? I'm currently working on a project where this would be very welcome due to better integration possibilities with Redux reducers.
How do you see this in combination with the current onChange()
function? Would all fields call onFieldChange instead, or both? And which one would come first?
from react-jsonschema-form.
@n1k0 Bumping this question because you probably missed my comment above. :)
from react-jsonschema-form.
@maartenth Form onChange
should work as before, but reflect computed field values in formData
.
As mentioned before, the trickiest part imo is to find a nice API for hooking into deeply nested fields. Using strings to identify these isn't an option in my world. All I can see atm is to introduce yet another new hooks
prop which would be an object registering nested hook functions:
const schema = {
type: "object",
properties: {
foo: {
type: "object",
properties: {
bar: {type: "string"}
}
}
}
}
const hooks = {
foo: {
bar: (value) => value + "!"
}
}
render(<Form schema={schema} hooks={hooks}/>)
We'll have to be careful with precedence here though. This should also work with array items as well.
Thoughts? /cc @leplatrem @olzraiti @frassinier and people interested
from react-jsonschema-form.
Thank you for your reply and your thoughts. I will try to find a more elegant API.
from react-jsonschema-form.
How about
//SchemaField.js
function getFieldComponent(schema, uiSchema, fields) {
//....
return (uiSchema["ui:enhancer"] || a=>a)(FieldComponent);
}
import propsMapper from "recompose/propsMapper";
//...
const uiSchema = {
title: {
'ui:enhancer': propsMapper(({value, onChange, ...rest})=>({
onChange: value => onChange(slugify(value)),
value: unslugify(value),
...rest
}))
}
};
For this case, I personally prefer simply
import propsMapper from "recompose/propsMapper";
import TextWidget from "react-jsonschema-form/TextWidget";
const uiSchema = {
title: {
'ui:widget': propsMapper(({value, onChange, ...rest})=>({
onChange: value => onChange(slugify(value)),
value: unslugify(value),
...rest
}))(TextWidget)
}
};
from react-jsonschema-form.
It would feel odd to add data processing logic to uiSchema, which has always been intended to deal with presentation only.
Also, I'm still pretty much convinced that post-processing form data could (and should) be achieved in the form's onSubmit
event handler.
from react-jsonschema-form.
@n1k0 I would agree. I normalize payload before post. Or you might be able to use a controlled component to compute inline?
I feel like these solutions are pretty heavy on perf?
from react-jsonschema-form.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Please leave a comment if this is still an issue for you. Thank you.
from react-jsonschema-form.
Related Issues (20)
- DOCUMENTATION Issue: Wrong method signature? HOT 1
- Schema defines standalone checkbox as mandatory, but asterisk is not rendered. HOT 2
- I would like to make a default change to the look of all input fields HOT 5
- Adding required field in nested if/then/else not working correctly HOT 2
- FormData is not received based on schema properties HOT 1
- Select bug when multiple oneOf are nested HOT 2
- How can I cite this? HOT 6
- RJSF anyOf customization HOT 3
- Validation error when multiple $ref references point to a single definition HOT 3
- not working about validation HOT 3
- Passing ui options down to array items HOT 3
- MUI number input label UI issue with non-numeric input HOT 1
- Publish v6 release candidate with support for Bootstrap 5 HOT 1
- Can not replace "integer" widget HOT 4
- Omit Extra Data not behaving as expected on submit for allOf (if then) schemas along with additionalProperties set to false blocking form submission HOT 1
- `anyOf` form validation error messages confusing when `anyOf` items contain properties found in other items HOT 4
- How to reference an array field from an enum field?
- Array Length dependant on FormContext Value
- Add support for tremor UI library
- Issue with data of files array when we have validation required and minItems HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-jsonschema-form.