简体   繁体   English

在子组件中使用DropDown的React-Final-Form,如何?

[英]React-Final-Form with DropDown in Child Component, how?

I'm trying to use React-final-form with a DropDown in a Child Component. 我正在尝试在子组件中使用带有DropDown的React-final-form。 Can't get this to work. 无法使它正常工作。

All of my text fields are already in a Child Component and this works like a charm. 我所有的文本字段都已经在子组件中,这就像一个超级按钮。 The field in the parent looks like this: 父级中的字段如下所示:

<Field
    name="lastName"
    placeholder="Last Name"
    validate={required}
>
    {({input, meta, placeholder}) => (
        <MyField meta={meta} input={input} placeholder={placeholder}/>
    )}
</Field>

The Child Component looks like this: 子组件看起来像这样:

export const MyField = (props) => {
    return (
        <Form.Field className={props.meta.active ? 'active' : ''}>
            <Label>{props.label ? props.label : props.placeholder}</Label>
            <Form.Input
                {...props.input}
                placeholder={props.placeholder}
                className={(props.meta.error && props.meta.touched ? 'error' : '')}
            />
        </Form.Field>
    )
};

The "Form.Field" and "Label" are coming from semantic-ui-react “ Form.Field”和“ Label”来自语义UI反应

But now I want to do the same with a DropDown. 但是现在我想对DropDown做同样的事情。 The standard DropDown, taken from an example on the React-Final-Form site , looks like this: 从React-Final-Form网站上的示例中提取的标准DropDown看起来像这样:

<Field name="toppingsA" component="select">
    <option value="chicken">Chicken</option>
    <option value="ham">Ham</option>
    <option value="mushrooms">Mushrooms</option>
    <option value="cheese">Cheese</option>
    <option value="tuna">Tuna</option>
    <option value="pineapple">Pineapple</option>
</Field>

And it works in a sense that I'm getting the value in my react-final-form values onSubmit. 从某种意义上说,我的反应最终形式的值是onSubmit。 then I'm trying to offload the Dropdown itself to the Child Component (with the intention to use the semantic-ui-react version of a Dropdown, but first things first and get the Dropdown to work :-) ) 然后我试图将Dropdown本身卸载到Child Component(目的是使用Dropdown的语义ui反应版本,但首先要使Dropdown起作用:-))

Parent Component: 父组件:

const eatOptions = [
    {key: 'c', text: 'Chicken', value: 'chicken'},
    {key: 'h', text: 'Ham', value: 'ham'},
    {key: 'm', text: 'Mushrooms', value: 'mushrooms'},
    {key: 't', text: 'Tuna', value: 'tuna'}
];

// And in the Form:
<Field name="toppingsB" component="select" options={eatOptions}>
    { ({input,  meta, options}) => {
        return (
            <Opts options={options} name={input.name}/>
        )
    }}
</Field>

And in the Child Component: 在子组件中:

export const Opts = (props) => {
    return (
        <select name={props.name}>
            {props.options.map((x) => {
                return (
                    <option key={x.key} value={x.value}>{x.text}</option>
                )
            })}
        </select>
    )
};

Result is that the HTML looks the same (which does not say that much I guess), ToppingsA is picked up in the values (on onSubmit) and ToppingsB is not. 结果是HTML看起来相同(我猜并没有说太多),ToppingsA的值(在onSubmit上)被拾取,而ToppingsB则不是。 I can't figure out what am I missing here and your help would be very much appreciated. 我不知道我在这里想念什么,非常感谢您的帮助。

Thanks in advance, Bert 预先感谢,伯特

If you are using render-props for toppingsB then the Field component prop should not be type "select" as the children of Field will be a function and not multiple tags. 如果您正在使用render-props作为toppingsB,则Field component prop不应键入“ select”,因为Field的子级将是一个函数,而不是多个标签。

It also looks like you are not letting your form know of any changes that occur inside the child component. 看起来您也没有让表单知道子组件内部发生的任何更改。 Try passing the Opts component an onChange function as a prop: 尝试向Opts组件传递onChange函数作为道具:

 <Opts 
    options={options}
    name={input.name}
    onChange={ (value:string) => input.onChange(value)}
  />

@S.Taylor, Thanks for your help!! @ S.Taylor,谢谢您的帮助! It works. 有用。

As a reference the working code: 作为参考,工作代码:

The Field in Parent Component: 父组件中的字段:

<Field name="toppingsB" options={eatOptions} >
    { ({input,  meta, options}) => {
        return (
            <Opts
                options={options}
                name={input.name}
                onChange={ (value) => input.onChange(value)}
            />
        )
    }}
</Field>

And the code in the Child Component: 以及子组件中的代码:

export const Opts = (props) => {
    return (
        <select name={props.name} onChange={props.onChange}>
            {props.options.map((x) => {
                return (
                    <option key={x.key} value={x.value}>{x.text}</option>
                )
            })}
        </select>
    )
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM