Enable multi-selection on table views (#112)
* Enable multi-selection on table views * Enable multi-selection
This commit is contained in:
@ -4,6 +4,9 @@ import styled from '@emotion/styled';
|
||||
type OwnProps = {
|
||||
name: string;
|
||||
id: string;
|
||||
checked?: boolean;
|
||||
indeterminate?: boolean;
|
||||
onChange?: (event: React.ChangeEvent<HTMLInputElement>) => void;
|
||||
};
|
||||
|
||||
const StyledContainer = styled.span`
|
||||
@ -28,14 +31,25 @@ const StyledContainer = styled.span`
|
||||
}
|
||||
`;
|
||||
|
||||
function Checkbox({ name, id }: OwnProps) {
|
||||
function Checkbox({ name, id, checked, onChange, indeterminate }: OwnProps) {
|
||||
const ref = React.useRef<HTMLInputElement>(null);
|
||||
React.useEffect(() => {
|
||||
if (ref.current === null) return;
|
||||
if (typeof indeterminate === 'boolean') {
|
||||
ref.current.indeterminate = !checked && indeterminate;
|
||||
}
|
||||
}, [ref, indeterminate, checked]);
|
||||
|
||||
return (
|
||||
<StyledContainer>
|
||||
<input
|
||||
ref={ref}
|
||||
type="checkbox"
|
||||
data-testid="input-checkbox"
|
||||
id={id}
|
||||
name={name}
|
||||
checked={checked}
|
||||
onChange={onChange}
|
||||
></input>
|
||||
</StyledContainer>
|
||||
);
|
||||
|
||||
18
front/src/components/table/SelectAllCheckbox.tsx
Normal file
18
front/src/components/table/SelectAllCheckbox.tsx
Normal file
@ -0,0 +1,18 @@
|
||||
import Checkbox from '../form/Checkbox';
|
||||
|
||||
export const SelectAllCheckbox = ({
|
||||
indeterminate,
|
||||
onChange,
|
||||
}: {
|
||||
indeterminate?: boolean;
|
||||
onChange?: any;
|
||||
} & React.HTMLProps<HTMLInputElement>) => {
|
||||
return (
|
||||
<Checkbox
|
||||
name="select-all-checkbox"
|
||||
id="select-all-checkbox"
|
||||
indeterminate={indeterminate}
|
||||
onChange={onChange}
|
||||
/>
|
||||
);
|
||||
};
|
||||
@ -2,6 +2,7 @@ import * as React from 'react';
|
||||
|
||||
import {
|
||||
ColumnDef,
|
||||
RowSelectionState,
|
||||
flexRender,
|
||||
getCoreRowModel,
|
||||
useReactTable,
|
||||
@ -34,6 +35,7 @@ type OwnProps<TData, SortField, FilterProperties> = {
|
||||
filter: FilterType<FilterProperties> | null,
|
||||
searchValue: string,
|
||||
) => void;
|
||||
onRowSelectionChange?: (rowSelection: RowSelectionState) => void;
|
||||
};
|
||||
|
||||
const StyledTable = styled.table`
|
||||
@ -98,11 +100,23 @@ function Table<TData extends { id: string }, SortField, FilterProperies>({
|
||||
onSortsUpdate,
|
||||
onFiltersUpdate,
|
||||
onFilterSearch,
|
||||
onRowSelectionChange,
|
||||
}: OwnProps<TData, SortField, FilterProperies>) {
|
||||
const [rowSelection, setRowSelection] = React.useState({});
|
||||
|
||||
React.useEffect(() => {
|
||||
onRowSelectionChange && onRowSelectionChange(rowSelection);
|
||||
}, [rowSelection, onRowSelectionChange]);
|
||||
|
||||
const table = useReactTable<TData>({
|
||||
data,
|
||||
columns,
|
||||
state: {
|
||||
rowSelection,
|
||||
},
|
||||
getCoreRowModel: getCoreRowModel(),
|
||||
enableRowSelection: true, //enable row selection for all rows
|
||||
onRowSelectionChange: setRowSelection,
|
||||
});
|
||||
|
||||
return (
|
||||
|
||||
Reference in New Issue
Block a user