简体   繁体   English

react-final-form 与自动完成组件

[英]react-final-form with autocomplite component

I have i dynamic form and having issue with Autocomplite component from material-ui when i connected with react-final-form-arrays, cant get the selected item value当我与 react-final-form-arrays 连接时,我有动态表单并且 material-ui 的 Autocomplite 组件存在问题,无法获取所选项目值

here is form code这是表单代码

<Field
   name={`${name}`.product}
   list={products}
   component={AutocompleteField}
   label={'Products'}
/>
function ProductSelectField({list, label, dealer_id, stock_id, all_stocks, input, ...rest}) {

    const {name, onChange, ...restInput} = input;

    const [value, setValue] = useState([]);
    const [inputValue, setInputValue] = useState('');

    const getProducts = (event, newValue) => {
        setValue(newValue);
    }
    return (
        <Autocomplete
            {...rest}
            id="product"
            options={list}
            getOptionLabel={(option) => option.name}
            defaultValue={list[0]}
            onChange={getProducts}
            inputValue={inputValue}
            onInputChange={(event, newInputValue) => {
                setInputValue(newInputValue);
            }}
            renderInput={(params) =>
                <TextField
                    {...restInput}
                    {...params}
                    label={label}
                    variant="outlined"
                />
            }
        />
    );
}

Without any extra info or code box to go off of, it seems that you are not invoking the input's onChange hook to update the fields state.如果没有任何额外的信息或代码框到 go 关闭,您似乎没有调用输入的onChange挂钩来更新字段 state。 In your Autocomplete prop.onChange , you are invoking the getProducts hook but not where are you passing the value to the onChange hook.在您的 Autocomplete prop.onChange中,您正在调用getProducts钩子,但不是在哪里将值传递给onChange钩子。

- const {name, onChange, ...restInput} = input; //delete
     <TextField
       - {...restInput} //delete
         {...params}
         label={label}
         variant="outlined"
     />
// spread out the entire input prop into the Autocomplete
<Autocomplete
    {...input}
    {... rest of your props}
/>

These Docs on React-Final-Form shows you what the input prop passes down and it shows how it does all the work for you. 这些关于 React-Final-Form 的文档向您展示了input prop 传递的内容,并展示了它如何为您完成所有工作。

However, I use the Autocomplete from material-ui as well and understand you want to control the local state at the same time.但是,我也使用 material-ui 的自动完成功能,并且了解您想同时控制本地 state。 Refactor your getProducts hook to update both.重构您的getProducts挂钩以更新两者。

const getProducts = (event, newValue) => {
        setValue(newValue);
        input.onChange(event); // the event will carry over because 'text' and 'select' inputs both return the 'event.target.value' without any extra logic for the Field component.
    }

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

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