Giter Site home page Giter Site logo

Comments (19)

n1k0 avatar n1k0 commented on May 3, 2024 4

onFieldChange=(name, value, data) => {

To be noted, we should allow intercepting any deeply nested updates here.

from react-jsonschema-form.

OliverColeman avatar OliverColeman commented on May 3, 2024 4

@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.

acouch avatar acouch commented on May 3, 2024 1

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.

lovato avatar lovato commented on May 3, 2024 1

@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.

Natim avatar Natim commented on May 3, 2024

I remember this was done in the formbuilder to compute the field id from the label.

See: https://github.com/Kinto/formbuilder/blob/8ceae7585108c3fe8d0ed64882f9d856cf2ef9e7/formbuilder/components/EditableField.js#L43-L62

from react-jsonschema-form.

n1k0 avatar n1k0 commented on May 3, 2024

Do you guys have a dream API in mind for this here?

from react-jsonschema-form.

Natim avatar Natim commented on May 3, 2024

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.

leplatrem avatar leplatrem commented on May 3, 2024

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.

apkoponen avatar apkoponen commented on May 3, 2024

Jdorns Json-editor uses the templates for this:

https://github.com/jdorn/json-editor#templates

from react-jsonschema-form.

n1k0 avatar n1k0 commented on May 3, 2024

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.

apkoponen avatar apkoponen commented on May 3, 2024

Yup, templates do have their limitations.

from react-jsonschema-form.

maartenth avatar maartenth commented on May 3, 2024

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.

maartenth avatar maartenth commented on May 3, 2024

@n1k0 Bumping this question because you probably missed my comment above. :)

from react-jsonschema-form.

n1k0 avatar n1k0 commented on May 3, 2024

@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.

maartenth avatar maartenth commented on May 3, 2024

Thank you for your reply and your thoughts. I will try to find a more elegant API.

from react-jsonschema-form.

knilink avatar knilink commented on May 3, 2024

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.

n1k0 avatar n1k0 commented on May 3, 2024

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.

elisechant avatar elisechant commented on May 3, 2024

@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.

stale avatar stale commented on May 3, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.