Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ant Design reset select

I have 2 <Select>'s. The values in the second are dependant on the selection made on the first. When I change the selected item in the first, the available options on the second update. But if I already have a selection made on the second, that option remains selected even if it isn't supposed to be available based on a change to the first select.

How can I reset the second select to have nothing selected when a change is made to the first select?

First Select:

<FormItem {...formTailLayout}>
    <FormTitle>Operation</FormTitle>
    {getFieldDecorator('Operation', {
    rules: [
        {
        required: true
        }
    ]
    })(
    <Select
        showSearch
        placeholder="Select an option"
        onChange={this.handleOperationChange}
    >
        {operations.map(operation => (
        <Option value={operation.operation_id}>
            {operation.operation_name}
        </Option>
        ))}
    </Select>
    )}
</FormItem>

Second Select:

<FormItem {...formTailLayout}>
    <FormTitle>Metric</FormTitle>
    {getFieldDecorator('Metric', {
    rules: [
        {
        required: true
        }
    ]
    })(
    <Select
        showSearch
        placeholder="Select an operation first"
        onChange={this.handleMetricChange}
    >
        {matrics
        .filter(
            metric => metric.operation_fk === operation_fk
        )
        .map(metric => (
            <Option value={metric.metric_name}>
            {metric.metric_name}
            </Option>
        ))}
    </Select>
    )}
</FormItem>
like image 205
Kieran Quinn Avatar asked Apr 26 '19 08:04

Kieran Quinn


People also ask

How do you reset an ANTD select?

Clear the value of the Selection field by setting it to null programmatically (e.g. via Button). The value will be set to null but the Selection field will still display the most recent selected value.

How do I clear an ANTD after submitting?

How do you delete values after submitting a form? The reset() method resets the values of all elements in a form (same as clicking the Reset button). Tip: Use the submit() method to submit the form.


1 Answers

You need to take a look at Coordinated Controls example mentioned on ant-design page. You can simply use setFieldsValue in your first onChange method to set the value of second select field.

handleOperationChange = () => {
    this.props.form.setFieldsValue({
        Metric: undefined
    })
}

I have created a sandbox demo.

like image 76
Triyugi Narayan Mani Avatar answered Sep 22 '22 05:09

Triyugi Narayan Mani