具有多个 select 的反应最终形式

react-final-form with multiple select

我正在尝试使用 react-final-form 通过 Material-UI 构建具有多个值 Select 组件的表单。不知何故,对于单个 Select,我可以获得值,但对于多个,则不能。不知何故,react-final-form 似乎在内部拥有自己的价值。

这是来自 Material-UI 的指南 link 用于构建多个 Select:

https://codesandbox.io/s/sr6pf

我尝试在我的表单中复制第一个示例(不使用反应钩子),但我仍然遗漏了什么?

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

我应该向我的组件添加什么才能使它正常工作?

谢谢,

出于某些原因,我设法找到了我自己问题的解决方案。正确的答案是创建一个自定义的 MultiSelect 组件,而不是重复使用 final-form-material-ui.

中的组件

注意:我尝试使用 final-form-material-ui 中的 <Select />,但是向组件添加 multiple prop 不会传递给 ,这很奇怪。

所以,我的自定义组件看起来像这样,几乎类似于 their github 添加了 multiple 属性的组件。

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;

希望这对以后的人有所帮助

我可以通过设置 fomat 来解决这个问题

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

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