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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request]: Support Controlled Selection in useRowSelect #156

Open
anuraghazra opened this issue Oct 10, 2024 · 0 comments
Open

[Feature Request]: Support Controlled Selection in useRowSelect #156

anuraghazra opened this issue Oct 10, 2024 · 0 comments

Comments

@anuraghazra
Copy link

Currently useRowSelect's state prop behaves as an uncontrolled state only.

We had a requirement where we wanted to support controlled selection for the Table selections.

Example:

const [selectedRows, setSelectedRows] = React.useState([]);

<WrapperTable 
  selectedIds={selectedRows}
  onSelectionChange={({ selectedIds }) => {
    setSelectedRows(selectedIds)
  }}
/>

Pseudo Implementation code:

const WrapperTable = ({ data, onSelectionChange, selectedIds, defaultSelectedIds }) => {
  // controllable controlled/uncontrolled state
  const [selectedRows, setSelectedRows] = useControllableState({
    defaultValue: defaultSelectedIds,
    value: selectedIds,
    onChange: (value) => {
      onSelectionChange?.({
        selectedIds: value,
      });
    },
  });
  
  const onSelectChange: MiddlewareFunction = (action, state): void => {
    // Logic for updating state
    setSelectedRows(() => selectedIds);
  };

  const rowSelectConfig = useRowSelect(
    data,
    {
      onChange: onSelectChange,
      state: { ids: selectedRows }
    },
    {
      rowSelect: 'multiple'
    },
  );  
  
  return <ReactTable data={data} select={rowSelectConfig} />
}

Is there any way to achieve this?

I'm also willing to raise a PR for adding this feature in useRowSelect

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant