简体   繁体   中英

How to set the initial value for Mutlti Select in FormItem in ANTD

I am trying to set (initialValue) defaultValue for a multi-select dropdown. Unfortunately, no items are set in the select textbox. I have made sure my text and value are string type below is the code. I have tried: https://github.com/ant-design/ant-design/issues/5226

 const [defaultsites, setDefaultSites] = useState([{id:'38', name:'BNE'},{id:'40', name:'Test'}]);

<Form.Item label='Sites' hasFeedback>
    {getFieldDecorator('sites', {
        intialValue: defaultsites,
        valuePropName: 'option',
        rules: [{
            required: true,
            message: 'Please select atleast one site'
        }],
    })(
        <Select
            mode="multiple"
            style={{ width: '100%' }}
            placeholder="Select atleast one site"
            defaultActiveFirstOption={true}
            onChange={handleSelectSiteChange}>

            {siteItems.map((names, index) => (
                <Option key={index} value={names.id}>
                    {names.name}
                </Option>
            ))}
        </Select>
    )}
 </Form.Item>

your defaultsites have to type following:

string | string[]
number | number[]
LabeledValue | LabeledValue[]
const [defaultsites, setDefaultSites] = useState([38,40]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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