使用最终形式,我有第三方输入组件。我已经写了一个适配器。它也具有验证器,但是meta.touched
始终为假。我试过传播onFocus事件到输入,但没有运气。我在做什么错了?
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>
);
}
}
答案 0 :(得分:7)
像往常一样,我回答自己的问题。
我还必须传播onBlur()
事件,这是有道理的,因为touched
文档说只有在用户输入并专注于输入之后才是真的。
<FloatingLabelInput
...
onBlur={(event) => input.onBlur(event)}
/>