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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Controllable collection model (select) #2675

Open
NicholasBoll opened this issue Apr 3, 2024 · 0 comments
Open

Controllable collection model (select) #2675

NicholasBoll opened this issue Apr 3, 2024 · 0 comments
Labels

Comments

@NicholasBoll
Copy link
Member

馃殌 Feature Proposal

Add controllable selectedIds and unselectedIds to the useSelectionListModel. A common ask is for control over a <Select> component's selected state. A "controllable" model is one where the model handles internal state changes based on use input, but allows state to be overridden by incoming config, hence allowing control externally.

We can add the following to the defaultConfig in the useSelectionListModel:

    selectedIds: undefined as undefined | string[],
    unselectedIds: undefined as undefined | string[]

Then update the state to:

const state = {...cursor.state, selectedIds: config.selectedIds || selectedIds, unselectedIds: config.unselectedIds || unselectedIds};

This will allow an override of the model.state.selectedIds from externally:

const [selectedIds, setSelectedIds] = React.useState<string[]>([]);
const model = useSelectModel({
  items: options,
  selectedIds,
  onSelect({ id }) {
    setSelectedIds([id])
  }
})

// some button callback or server fetch callback or something
setSelectedIds(['Foo'])

Motivation

Right now, a developer needs to use a key to completely throw away a component in order to update the current selected state.

Example

https://stackblitz.com/edit/controllable-select-model-6bwrh3?file=src%2FuseControlledSelectModel.ts,src%2FApp.tsx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: 馃啎 New
Development

No branches or pull requests

1 participant