Skip to content

SelectNext: support searching/filtering options #1254

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

Closed
acelaya opened this issue Sep 22, 2023 · 1 comment
Closed

SelectNext: support searching/filtering options #1254

acelaya opened this issue Sep 22, 2023 · 1 comment

Comments

@acelaya
Copy link
Contributor

acelaya commented Sep 22, 2023

Add some way to do text search inside the SelectNext options, as sketched here hypothesis/client#5775 (comment)

Based on how the SelectNext component was architected, one way to support filtering items would be introducing a new <Select.Filterable /> wrapper for <Select.Option /> items, which allows children to be filtered via context.

function App() {
  const [selected, setSelected] = useState<(typeof items)[number]>();

  return (
    <Select {...}>
      <Select.Option value={...}>Main</Select.Option>

      <Select.Filterable onFilter={(query: string, value: T) => boolean} placeholder={...}>
        {items.map(item => (
          <Select.Option value={item} key={item.id}>
            {({ disabled, isSelected }) => <>{item.name} ({item.id})</>}
          </Select.Option>
        ))}
      </Select.Filterable>
    </Select>
  )
}

Then we can expose the onFilter callback via context, down to the Option entries, which can decide if they should render or not, based on that.

We could even allow multiple Filterable blocks, or options outside any Filterable, that cannot be filtered. Ideally the search box should be position sticky, making sure it's visible while scrolling between the options it can filter.

The Filterable component could be something in these lines:

const FilterableContext = createContext<{ shouldRender: (value: T) => boolean } | null>(null);

function Filterable<T>({ onFilter, placeholder, children }) {
  const [query, setQuery] = useState('');
  const shouldRender = useCallback(
    (value: T) => onFilter(query, value), 
    [onFilter, query],
  );

  return (
    <FilterableContext.Provider value={{ shouldRender }}>
      <input type="search" placeholder={placeholder} value={query} onInput={e => setQuery(e.target.value)} />
      {children}
    </FilterableContext.Provider>
  );
}
@acelaya
Copy link
Contributor Author

acelaya commented Feb 22, 2024

Closing in favor of #1315

@acelaya acelaya closed this as completed Feb 22, 2024
@acelaya acelaya reopened this Feb 22, 2024
@acelaya acelaya closed this as not planned Won't fix, can't repro, duplicate, stale Feb 22, 2024
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

2 participants