0

I cannot integrate react-dates with react-final-form.

If I add an onChange callback to react-dates' component, using change(name, value) function from the form, the name is not saved to the form state (I suppose because the field has to be registered before using change).

Is it possible to register manually a field?

edit: I found a way, adding an hidden Field, but I don't like that...

edit 2: adding a test form:

Edit  React Final Form - Simple Example

avalla
  • 560
  • 4
  • 19

3 Answers3

0

You must use form from FormRenderProps to do this.

「form.change」

<Form
  onSubmit={onSubmit}
  initialValues={{ a: "a" }}
  render={({
    form, // Focus here and removed 「change」
    handleSubmit,
    reset,
    submitting,
    pristine,
    values
  }) => (
    <form onSubmit={handleSubmit}>
      <div>
        <label>A Field</label>
        <Field name="a" component="input" type="text" />
      </div>
      <div>
        <label>A not Field</label>
        <input
          name="a"
          type="text"
          onChange={event => {
            console.log("changing A to", event.target.value);
            form.change("a", event.target.value); // Use form.change instead of change
          }}
        />
      </div>
      <div>
        <label>B not Field</label>
        <input
          name="b"
          type="text"
          onChange={event => {
            console.log("changing b to", event.target.value);
            form.change("b", event.target.value); // Use form.change instead of change
          }}
        />
      </div>
      <div className="buttons">
        <button type="submit" disabled={submitting || pristine}>
          Submit
        </button>
        <button
          type="button"
          onClick={reset}
          disabled={submitting || pristine}
        >
          Reset
        </button>
      </div>
      <pre>{JSON.stringify(values, 0, 2)}</pre>
    </form>
  )}
/>
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer) – Lakmi Aug 30 '19 at 04:23
-1

You can wrap it inside of Field to make final-form's state aware of the b field:

<Field
  name="b"
  render={() => {
    return (
      <input
        type="text"
        onChange={event => {
          console.log("changing b to", event.target.value);
          change("b", event.target.value);
        }}
      />
    );
  }}
/>

in the case above you don't need to specify special onChange handler:

<Field
  name="b"
  render={({ input }) => {
    return <input type="text" {...input} />;
  }}
/>

or even

<Field name="b" component="input" />
Evgeny Timoshenko
  • 3,119
  • 5
  • 33
  • 53
-1

This worked for me:

import { SingleDatePicker } from 'react-dates'


// set a state
const [ date, setDate ] = useState(null);


// inside your render inside the react-final-form component
<Field name="date">
    {({ input, meta }) => (
        <>
            <SingleDatePicker
                readOnly
                onOutsideClick={true}
                numberOfMonths={1}
                focused={focused}
                date={date}
                onDateChange={value => {
                    // you need to set the value to a formatted date
                    // because react-dates returns a moment object as the value
                    input.onChange(value.format('YYYY-MM-DD'))
                    setDate(value);
                }}
                onFocusChange={({ focused }) => {
                    setFocused(focused);
                }}
            />
        </>
    )}
</Field>
Eddsters
  • 458
  • 1
  • 4
  • 16