Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import React from 'react';
- import Select from 'react-select';
- import ReactTable from 'react-table';
- import 'react-table/react-table.css';
- const data = [
- {author: 'Terry Pratchett', title: 'Snuf'},
- {author: 'Terry Pratchett', title: 'Carpe Jugulum'},
- {author: 'Neil Gaiman', title: 'Good Omens'},
- {author: 'Orson Scott Card', title: `Enders Game`}
- ];
- const selectOptions = [
- {value: '', label: 'Show all'},
- {value: 'Terry Pratchett', label: 'Terry Pratchett'},
- {value: 'Orson Scott Card', label: 'Orson Scott Card'},
- {value: 'Neil Gaiman', label: 'Neil Gaiman'}
- ];
- const multiSelectOptions = [
- {value: 'Terry Pratchett', label: 'Terry Pratchett'},
- {value: 'Orson Scott Card', label: 'Orson Scott Card'},
- {value: 'Neil Gaiman', label: 'Neil Gaiman'}
- ];
- function App() {
- const columns = [
- {
- Header: 'Author',
- accessor: 'author',
- Filter: ({filter, onChange}) => (
- // FOR SINGLE SELECT
- //
- // <Select
- // options={selectOptions}
- // menuPortalTarget={document.body}
- // onChange={opt => onChange(opt.value)}
- // />
- // FOR MULTI SELECT
- //
- <Select
- options={multiSelectOptions}
- onChange={opt => {
- onChange(opt);
- }}
- menuPortalTarget={document.body}
- isMulti
- />
- ),
- filterAll: true,
- filterMethod: (filter, rows, column) => {
- // FOR SINGLE SELECT
- //
- //return rows.filter(element => element.author === filter.value);
- // FOR MULTI SELECT
- //
- if(!filter.value || filter.value.length === 0) return rows;
- const authors = filter.value.map(opt => opt.value);
- return rows.filter(element => authors.indexOf(element.author) !== -1);
- }
- },{
- Header: 'Title',
- accessor: 'title',
- filterable: false
- }
- ];
- return (
- <section>
- <ReactTable
- columns={columns}
- data={data}
- filterable
- />
- </section>
- );
- }
- export default App;
Advertisement
Add Comment
Please, Sign In to add comment