Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get which field changed on the event onChange #651

Closed
eddyzhang1986 opened this issue Jul 28, 2017 · 2 comments
Closed

Get which field changed on the event onChange #651

eddyzhang1986 opened this issue Jul 28, 2017 · 2 comments
Labels
possibly close To confirm if this issue can be closed

Comments

@eddyzhang1986
Copy link

eddyzhang1986 commented Jul 28, 2017

Description

when the field value change,I just want to known which field i changed
how can i do?

Steps to Reproduce

  1. when the field value change,I just want to known which field i changed
    how can i do?
@manahga
Copy link

manahga commented Aug 1, 2017

I was able to accomplish this through the creation of a custom SchemaField component which acts as a wrapper to the original SchemaField and intercepts the onChange event of the underlying field. It then forwards the changed field value, along with the field name, to a callback function provided in the formContext of the form. I used the formContext as I couldn't find another way of passing a custom callback down to the custom SchemaField component.

import React from 'react';
import SchemaField from 'react-jsonschema-form/lib/components/fields/SchemaField';

const CustomSchemaField = function (props) {

    const customProps = {};

    //Only process if we are dealing with a field, not the parent object
    if (props.name) {

        const formContext = props.registry.formContext;        
        
        //Store the original onChange event provided to the SchemaField
        //as well as the name of the field
        const { onChange, name } = props;

        //Provide a new onChange event for the SchemaField
        customProps.onChange = function(formData) {

            //Call the custom handler provided in the formContext, if it exists,
            //with the field name and new value
            if (formContext && formContext.onFieldChange && 
                typeof formContext.onFieldChange === 'function') {

                formContext.onFieldChange(name, formData);
            }

            //Call the original onChange handler
            onChange(formData);
        };

    }

    return (
        <SchemaField {...props} {...customProps} />
    );
};

The above would be utilized like so:

class MyComponent extends React.Component {

    constructor(props) {
        super(props);
    }

    onFieldChange = (name, formData) => {
        //Handle individual field change here...
    }

    render() {
       const formContext = {
           onFieldChange: this.onFieldChange
       };
       return <Form ... fields={{ SchemaField: CustomSchemaField }} formContext={formContext} />
    }

}

There certainly may be an easier way...but this has worked for me.

@heath-freenome
Copy link
Member

This was fixed in 5.x with #3173

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
possibly close To confirm if this issue can be closed
Projects
None yet
Development

No branches or pull requests

4 participants