22

using final-form, i have a third party input component. i've written an adapter for it. it has a validator as well, but meta.touched is always false. i've tried propagating the onFocus event up to the input, but no luck. what am i doing wrong?

const requiredValidator = value => (value ? undefined : 'is required');

const FloatingLabelInputAdapter = ({ input, meta, ...rest }) => (
  <FloatingLabelInput
    {...rest}
    onChange={(event) => input.onChange(event)}
    onFocus={(event) => input.onFocus(event)}
    errorText={meta.touched ? meta.error : ''}
  />
)

// used like this:

<Field
  component={FloatingLabelInputAdapter}
  label="Email"
  name="email"
  type="text"
  validate={requiredValidator}
/>

// and here's the render() of the component


  render() {
    const { children, label } = this.props;
    const { focussing, used } = this.state;

    console.log('FloatingLabelInput.props', this.props);

    return (
      <Group {...this.props} >
        <TextInput
          focussing={focussing}
          innerRef={(comp) => { this.input = comp }}
          onFocus={this.onFocusHandle}
          onBlur={this.onBlurHandle}
          onChange={this.onChange}
          type={this.props.type} />

        <Label
          focussing={focussing}
          used={used}>

          {label}
        </Label>

        <Bar focussing={focussing} />
      </Group>
    );
  }
}
Adam Krawesky
  • 1,313
  • 11
  • 23

1 Answers1

36

annnnd as usual i answer my own question.

i had to propagate the onBlur() event as well, which makes sense since touched docs say it's true only after user has entered and left focus on the input.

<FloatingLabelInput
   ...
   onBlur={(event) => input.onBlur(event)}
/>
Adam Krawesky
  • 1,313
  • 11
  • 23