Skip to content

Cannot update EuiSelectable programmatically using new option objects #2071

@jen-huang

Description

@jen-huang

The code below explains the problem. This is not a priority bug since there is a workaround (last code block):

// Options state using React `useState` hook
const [indicesOptions, setIndicesOptions] = useState<Option[]>([
  {label: 'foo', checked: 'on'}, 
  {label: 'foo', checked: 'on'}, 
]);

// Rendered selectable list
<EuiSelectable
  allowExclusions={false}
  options={indicesOptions}
  onChange={options => {
    const newSelectedIndices: string[] = [];
    options.forEach(({ label, checked }) => {
      if (checked === 'on') {
        newSelectedIndices.push(label);
      }
    });
    setIndicesOptions(options);
  }}
  searchable
  height={300}
>
  {(list, search) => (
    <EuiPanel paddingSize="s" hasShadow={false}>
      {search}
      {list}
    </EuiPanel>
  )}
</EuiSelectable>

// DOESN'T WORK
// Deselecting all options programmatically via building new `indicesOptions` (new objects)
<EuiLink
  onClick={() => {
    setIndicesOptions(indicesOptions.map(option => ({
      ...option,
      checked: undefined,
    })));
  }}
>
  De-select all
</EuiLink>

// WORKS!
// Deselecting all options programmatically via modifying each `indicesOption` object
<EuiLink
  onClick={() => {
    indicesOptions.forEach(option => {
      option.checked = undefined;
    });
  }}
>
  De-select all
</EuiLink>

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions