简体   繁体   English

从控制器中的表单中选择不起作用

[英]Select from the Form in a Controller not working

I have a Form, I use React toolkit, but I have a problem, all my fields are updated when I change them it works correctly for me, with Https PUT, but when I have a select, it doesn't change it for me.我有一个表单,我使用 React 工具包,但是我有一个问题,当我更改它们时,我的所有字段都会更新,它对我来说可以正常工作,使用 Https PUT,但是当我有一个选择时,它不会为我改变.

For the selected data I made a call to the API for the entity I wanted, and I displayed them in the dropbox.对于选定的数据,我调用了所需实体的 API,并将它们显示在 Dropbox 中。

    const dispatch = useDispatch();
    const categories = useSelector(selectCategories);
    const status = useSelector(selectStatus);
    const priority = useSelector(selectPriority);
    const methods = useFormContext();
    const { control, formState } = methods;
    const { errors } = formState;
    const [selectedCategory, setSelectedCategory] = useState('all');
    const [selectedStatus, setSelectedStatus] = useState('all');
    const [selectedPriority, setSelectedPriority] = useState('all');

This is for set这是为集

    function handleSelectedStatus(event) {
    setSelectedStatus(event.target.value);
}

And here is Controller这是控制器

    <Controller
            name="status.id"
            control={control}
            defaultValue={[]}
            value={selectedStatus}
            onChange={handleSelectedStatus}
            render={({ field }) => (
                <Select
                    value={selectedStatus}
                    onChange={handleSelectedStatus}
                    multiline
                    rows={5}
                    className="mt-8 mb-16 w-1/2"
                >
                    {status.map(categ => {
                        if (categ.id === field.value) {
                            return (
                                <MenuItem value="all">
                                    <em value={field.value}>{field.value} </em>
                                </MenuItem>
                            );
                        }
                    })}
                    {status.map(category => (
                        <MenuItem value={category.name} key={category.id}>
                            {category.name}
                        </MenuItem>
                    ))}
                </Select>
            )}
        />

Maybe someone else will consume it and this question will be useful也许其他人会消费它,这个问题会很有用

Using Select.Option should work instead of using MenuItem.使用 Select.Option 应该可以工作,而不是使用 MenuItem。

 <Select
            value={selectedStatus}
            onChange={handleSelectedStatus}
            multiline
            rows={5}
            className="mt-8 mb-16 w-1/2"
        >
            {status.map(categ => {
                if (categ.id === field.value) {
                    return (
                        <Select.Option value={field.value}>
                            {field.value}
                        </Select.Option>
                    );
                }
            })}
        </Select>

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

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