Depracated - this is now part of Enterwell/UI monorepo.
The Select control for React. Wrapper above Material UI Autocomplete that supports pagination on menu scroll.
| yarn build
| Build the sample app |
| yarn start
| Start the sample app server|
| yarn test
| Run tests |
import React from "react";
import Select from "select";
const options = [
{ value: "chocolate", label: "Chocolate" },
{ value: "strawberry", label: "Strawberry" },
{ value: "vanilla", label: "Vanilla" },
{ value: "blueberry", label: "Blueberry" },
];
class App extends React.Component {
state = {
loading: false,
items: [],
selectedItems: [
{ value: "vanilla", label: "Vanilla" },
{ value: "strawberry", label: "Strawberry" },
],
};
handleSelection = (selectedItems) => {
this.setState({ selectedItems });
};
handlePageRequested = async (filter, page, pageSize) => {
try {
this.setState({ loading: true });
const newItems = await // GET ITEMS
this.setState({
items: [
...(page > 0 ? items : []),
...newItems.slice(page * pageSize, (page + 1) * pageSize),
],
});
} finally {
this.setState({ loading: false });
}
};
render() {
const { loading, items, selectedItems } = this.state;
return (
<Select
multiple
items={items}
placeholder="Placeholder text"
outlined
loading={loading}
selected={selectedItems}
onSelection={this.handleSelection}
pageSize={15}
onPage={this.handlePageRequested}
/>
);
}
}
import React, { useState } from "react";
import Select from "select";
const options = [
{ value: "chocolate", label: "Chocolate" },
{ value: "strawberry", label: "Strawberry" },
{ value: "vanilla", label: "Vanilla" },
{ value: "blueberry", label: "Blueberry" },
];
const App = () => {
const [loading, setLoading] = useState(false);
const [items, setItems] = useState([]);
const [selectedItems, setSelectedItems] = useState([
{ value: "vanilla", label: "Vanilla" },
{ value: "strawberry", label: "Strawberry" },
]);
const handlePageRequested = async (filter, page, pageSize) => {
try {
setLoading(true);
const newItems = await // GET ITEMS
setItems([
...(page > 0 ? items : []),
...newItems.slice(page * pageSize, (page + 1) * pageSize),
]);
} finally {
setLoading(false);
}
};
return (
<Select
multiple
items={items}
placeholder="Placeholder text"
outlined
loading={loading}
selected={selectedItems}
onSelection={setSelectedItems}
pageSize={15}
onPage={handlePageRequested}
/>
);
};
export default App;
Common props you may want to specify include:
multiple
- allow the user to select multiple valuesitems
- specify the options the user can select fromplaceholder
- change the text displayed when no option is selectedoutlined
- specify if the select component is supposed to look outlinedloading
- specify if the component should be in a loading stateselected
- control the current value selectedonSelection
- subscribe to change eventonPage
- subscribe to requesting new page of options eventpageSize
- specify the number of options the component will request when
Option items passed to the component need to be of type:
{ value: string, label: string }
onPage property points to the callback function that will be called whenever component requests new page
handlePageRequested: (filter: (string | null), page: number, pageSize: number) => Promise<void>
where:
- filter: (string | null)
- a string that represents user input at the moment of requesting a new page. Value can be and will be null the first time component requests new data and as long as user did not type anything. After typing and emptying the input, value is an empty string.
- page: number:
- index of the new page to paginate the data for.
- pageSize: number:
- size of the page