Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CheckboxGroup 在使用Form的getFieldProps 会引起 Cannot read property 'indexOf' of undefined #1631

Closed
GitNiko opened this issue May 11, 2016 · 2 comments

Comments

@GitNiko
Copy link

GitNiko commented May 11, 2016

版本: "1.0.0-beta.3"
如下例子

// in render()
const useTypeProps = getFieldProps('useType', {
            rules: [
                {  type: 'array',
                   required: true,
                   validator: this.checkUseType
                },
            ]
        });
return (
<Form form={this.props.form}>
<FormItem
             id="useType"
             label="用途:"
             labelCol={{ span: 6 }}
             wrapperCol={{ span: 18 }}
             required
             >
             <CheckboxGroup options={[{value:1, label: '农机'},
               {value:2, label: '农资'}, {value:3, label: '土地租金'}, {value:9, label: '其他'}]}
               {...useTypeProps}
              />
</FormItem>
</Form>
)

看了源码实现

constructor(props) {
    super(props);
    let value;
     // 在getFieldProps后 这里value会undefined
    // 可以加一个check
    //   if (props.value === undefined || props.value === null) {
    //       value = '';
    //   } else {
    //       value = props.value;
    //   }
    if ('value' in props) {
      value = props.value;
    } else if ('defaultValue' in props) {
      value = props.defaultValue;
    }
    this.state = { value };
  }
render() {
    const options = this.getOptions();
    return (
      <div className="ant-checkbox-group">
        {
          options.map(option =>
            <Checkbox disabled={'disabled' in option ? option.disabled : this.props.disabled}
              // 这里会抛出 'indexOf' of undefined
              checked={this.state.value.indexOf(option.value) !== -1}
              onChange={() => this.toggleOption(option)}
              className="ant-checkbox-group-item" key={option.value}>
              {option.label}
            </Checkbox>
          )
        }
      </div>
    );
  }
@afc163 afc163 closed this as completed in d57632c May 11, 2016
@afc163
Copy link
Member

afc163 commented May 11, 2016

感谢反馈,可以先用 Form 的 initialValue: [] 来临时修复。

@lock
Copy link

lock bot commented May 5, 2018

This thread has been automatically locked because it has not had recent activity. Please open a new issue for related bugs and link to relevant comments in this thread.

@lock lock bot locked as resolved and limited conversation to collaborators May 5, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants