简体   繁体   English

多选反应最终形式

[英]react-final-form with multiple select

I'm trying to build a form with a multiple value Select component by Material-UI using react-final-form.我正在尝试使用 react-final-form 通过 Material-UI 构建一个具有多值 Select 组件的表单。 Somehow with single Select, I can get the value but with multiple, it doesn't.不知何故,使用单个选择,我可以获得值,但使用多个,则不能。 Somehow it seems like react-final-form is holding its own value internally.不知何故,似乎 react-final-form 在内部拥有自己的价值。

Here's a the guiding link from Material-UI for building multiple Select:这是 Material-UI 中用于构建多个 Select 的指导链接:

https://codesandbox.io/s/sr6pf https://codesandbox.io/s/sr6pf

I tried to replicate the very first example (without using react hook) in my form and I still miss something ?我试图在我的表单中复制第一个示例(不使用 react 钩子),但我仍然错过了什么?

https://codesandbox.io/embed/react-final-form-material-ui-example-jfmoe https://codesandbox.io/embed/react-final-form-material-ui-example-jfmoe

What should I add to my Component to make this work ?我应该向我的组件添加什么才能使这项工作?

Thanks,谢谢,

For some reasons I've managed to figure out the solution for my own question.由于某些原因,我设法为我自己的问题找出了解决方案。 The proper answer is to create a custom MultiSelect component instead of reusing the one from final-form-material-ui .正确的答案是创建一个自定义的 MultiSelect 组件,而不是重用来自final-form-material-ui

Notes: I've tried to use <Select /> from final-form-material-ui but adding multiple prop to the component will not be passed to , this is weird.注意:我尝试使用<Select /> from final-form-material-ui但是向组件添加multiple prop 不会传递给 ,这很奇怪。

So, my custom component would look like this, almost similar to the one from their github with multiple prop added.所以,我的自定义组件看起来像这样,几乎类似于他们 github 中添加了multiple prop 的组件。

 import React from 'react'; import FormControl from '@material-ui/core/FormControl'; import FormHelperText from '@material-ui/core/FormHelperText'; import InputLabel from '@material-ui/core/InputLabel'; import Select from '@material-ui/core/Select'; function SelectMulti({ input: { name, value, onChange, ...restInput }, meta, label, formControlProps, ...rest }) { const showError = ((meta.submitError && !meta.dirtySinceLastSubmit) || meta.error) && meta.touched; return ( <FormControl {...formControlProps} error={showError}> <InputLabel htmlFor={name} shrink> {label} </InputLabel> <Select {...rest} multiple name={name} value={value} onChange={onChange} inputProps={restInput} /> {showError && ( <FormHelperText>{meta.error || meta.submitError}</FormHelperText> )} </FormControl> ); } SelectMulti.propTypes = {}; export default SelectMulti;

Hope this help someone in the future希望这对将来的人有所帮助

I was able to solve this with by setting the fomat as such我能够通过将fomat设置为这样来解决这个问题

<Field
 name="concepts"
 component={Select}
 displayEmpty={trie}
 multiple={true}
 value={[]}
 format={value => value || []}
 />

As per https://github.com/erikras/redux-form-material-ui/issues/212#issuecomment-358376925根据https://github.com/erikras/redux-form-material-ui/issues/212#issuecomment-358376925

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

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