Clear a field's value on input clear in react-final-form

后端 未结 3 1947
刺人心
刺人心 2021-01-20 19:08

I\'m using a custom component with react-final-form. On input change it sets the value to the address field. But when the input is cleared it doesn

相关标签:
3条回答
  • 2021-01-20 19:26

    All the default callback are handle by the component. If you want to do a clear with a button click, you can create a custom component and use native callback methods do your thing.

    onChange = (event) => {
      this.setState({
        address:event.target.value
      });
    }
    
    onClear = () => {
      this.setState({
        address:''
      });
    }
    <div>
      <Field name="address">
          <div>
            <input
              value={this.state.address}
              onChange={this.onChange}
            />
          </div>
      </Field>
      <button onClick={this.onClear}>Clear</button>
    </div>

    0 讨论(0)
  • 2021-01-20 19:27

    The problem is not with the react-final-form in your code, it is due to the react-da data, I have played a lot around your code within 1 day, and checked reset is working with fine with the react-final-form

    Just update your render with this code and see reset is working absolutely fine. Yeah! the problem is with react da data. I am not sure about what it does due to less official information for this package.

     <div className="App">
          <h2>Dadata test</h2>
          <Form
            mutators={{
              clear: ([address], state, { changeValue }) => {
                changeValue(state, "address", () => undefined);
              }
            }}
            onSubmit={onSubmit}
            render={({ form, handleSubmit, pristine, invalid, values, errors }) => (
              <form onSubmit={handleSubmit} noValidate>
                <Field name="address" component="input" />
                  {/* {props => (
                    <div>
                      <ReactDadata
                        token="d19c6d0b94e64b21d8168f9659f64f7b8c1acd1f"
                        onChange={event =>
                          props.input.onChange !== undefined
                            ? props.input.onChange({ value: event })
                            : form.mutators.clear
                        }
                      />
                    </div>
                  )}
                </Field> */}
                <button type="submit" disabled={invalid}>
                  Submit
                </button>
                <button type="button" onClick={form.reset}>
                  Clear
                </button>
                <Fields names={["address"]}>
                  {fieldsState => (
                    <pre>{JSON.stringify(fieldsState, undefined, 2)}</pre>
                  )}
                </Fields>
              </form>
            )}
          />
        </div>

    Hope it will help you to resolve the problem.

    0 讨论(0)
  • 2021-01-20 19:47

    You can just call form.change('address', undefined) at any time that you'd like to clear the value.

    0 讨论(0)
提交回复
热议问题