Disabled Items in React AutoComplete

24 Sep 20269 minutes to read

The React AutoComplete provides options for individual items to be either in an enabled or disabled state for specific scenarios. The disabled state of each list item is controlled by the fields.disabled data field, which must map to a column containing boolean values (true to disable, false to keep the item enabled). Once an item is disabled, it cannot be selected as a value of the component. To configure the disabled item column, use the fields.disabled property.

In the following sample, list items are disabled based on a boolean field using the fields.disabled property.

[Class Component]

import { AutoCompleteComponent } from '@syncfusion/ej2-react-dropdowns';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
export default class App extends React.Component {
    // define the data with category
    statusData = [
        { "Status": "Open", "State": false },
        { "Status": "Waiting for Customer", "State": false },
        { "Status": "On Hold", "State": true },
        { "Status": "Follow-up", "State": false },
        { "Status": "Closed", "State": true },
        { "Status": "Solved", "State": false },
        { "Status": "Feature Request", "State": false }
    ];
    // map the groupBy field with Category column
    fields = { value: 'Status', disabled: 'State' };
    render() {
        return (
        // specifies the tag for render the AutoComplete component
        <AutoCompleteComponent id="atcelement" fields={this.fields} dataSource={this.statusData} placeholder="Select Status"/>);
    }
}
ReactDOM.render(<App />, document.getElementById('sample'));
import { AutoCompleteComponent } from '@syncfusion/ej2-react-dropdowns';
import * as React from 'react';
import * as ReactDOM from 'react-dom';

export default class App extends React.Component<{}, {}> {
    // define the data with category
    private statusData: { [key: string]: Object }[] = [
        { "Status": "Open", "State": false },
        { "Status": "Waiting for Customer", "State": false },
        { "Status": "On Hold", "State": true },
        { "Status": "Follow-up", "State": false },
        { "Status": "Closed", "State": true },
        { "Status": "Solved", "State": false },
        { "Status": "Feature Request", "State": false }
    ];

    // map the groupBy field with Category column
    private fields: object = { value: 'Status', disabled: 'State' };

    public render() {
        return (
             // specifies the tag for render the AutoComplete component
            <AutoCompleteComponent id="atcelement" fields={this.fields} dataSource={this.statusData} placeholder="Select Status" />
        );
    }
}
ReactDOM.render(<App />, document.getElementById('sample'));

[Functional Component]

import { AutoCompleteComponent } from '@syncfusion/ej2-react-dropdowns';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
function App() {
    // define the data with category
    let statusData = [
        { "Status": "Open", "State": false },
        { "Status": "Waiting for Customer", "State": false },
        { "Status": "On Hold", "State": true },
        { "Status": "Follow-up", "State": false },
        { "Status": "Closed", "State": true },
        { "Status": "Solved", "State": false },
        { "Status": "Feature Request", "State": false }
    ];
    // map the groupBy field with Category column
    const fields = { value: 'Status', disabled: 'State' };
    return (
    // specifies the tag for render the AutoComplete component
    <AutoCompleteComponent id="atcelement" fields={fields} dataSource={statusData} placeholder="Select Status"/>);
}
ReactDOM.render(<App />, document.getElementById('sample'));
import { AutoCompleteComponent } from '@syncfusion/ej2-react-dropdowns';
import * as React from 'react';
import * as ReactDOM from 'react-dom';

function App() {
    // define the data with category
    let statusData: { [key: string]: Object }[] = [
        { "Status": "Open", "State": false },
        { "Status": "Waiting for Customer", "State": false },
        { "Status": "On Hold", "State": true },
        { "Status": "Follow-up", "State": false },
        { "Status": "Closed", "State": true },
        { "Status": "Solved", "State": false },
        { "Status": "Feature Request", "State": false }
    ];

    // map the groupBy field with Category column
    const fields: object = { value: 'Status', disabled: 'State' };
    return (
    // specifies the tag for render the AutoComplete component
        <AutoCompleteComponent id="atcelement" fields={fields} dataSource={statusData} placeholder="Select Status" />
    );
}
ReactDOM.render(<App />, document.getElementById('sample'));

Disabling the overall component

To disable the overall React AutoComplete component, set the enabled property to false (or the disabled property to true). Note that this enabled/disabled property controls the whole component, whereas fields.disabled controls only individual list items.

Disabled React AutoComplete Component

Disabling an item at runtime

The disableItem method handles dynamic changes in the disable state of a specific item. Only one item can be disabled per call. To disable multiple items, call this method iteratively over the items list or array. The disabled state will be updated in the dataSource when the item is disabled using this method. If a selected item is disabled dynamically, its selection is automatically cleared.

Method signature: disableItem(item: HTMLLIElement | string | number | boolean | object): void

The item argument accepts any of the following:

Argument type Description
HTMLLIElement The HTML li element of the item to be disabled.
string The string value of the item to be disabled.
number The number value of the item to be disabled.
boolean The boolean value of the item to be disabled.
object The object value of the item to be disabled.
number (index) The index of the item to be disabled.