Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions .storybook/stories/Features/Select/click-only-selects.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import * as React from 'react';
import { storiesOf } from '@storybook/react';

import { createTheme as createMaterialTheme } from '@mui/material/styles';
import { ThemeProvider as MaterialThemeProvider } from '@mui/material/styles';
import MaterialCheckbox from '@mui/material/Checkbox';

import {
Table,
Header,
HeaderRow,
Body,
Row,
HeaderCell,
Cell,
} from '@table-library/react-table-library/table';
import { useTheme } from '@table-library/react-table-library/theme';

import {
HeaderCellSelect,
CellSelect,
SelectClickTypes,
SelectTypes,
useRowSelect,
} from '@table-library/react-table-library/select';

import { nodes } from '../../data';

const Component = () => {
const data = { nodes };

const select = useRowSelect(
data,
{
onChange: onSelectChange,
},
{ clickOnlySelects: true },
);

function onSelectChange(action, state) {
console.log(action, state);
}

return (
<Table data={data} select={select}>
{(tableList) => (
<>
<Header>
<HeaderRow>
<HeaderCell>Task</HeaderCell>
<HeaderCell>Deadline</HeaderCell>
<HeaderCell>Type</HeaderCell>
<HeaderCell>Complete</HeaderCell>
<HeaderCell>Tasks</HeaderCell>
</HeaderRow>
</Header>

<Body>
{tableList.map((item) => (
<Row item={item} key={item.id}>
<Cell>{item.name}</Cell>
<Cell>
{item.deadline.toLocaleDateString('en-US', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
})}
</Cell>
<Cell>{item.type}</Cell>
<Cell>{item.isComplete.toString()}</Cell>
<Cell>{item.nodes?.length}</Cell>
</Row>
))}
</Body>
</>
)}
</Table>
);
};

export default Component;
4 changes: 4 additions & 0 deletions .storybook/stories/Features/Select/index.story.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ import DefaultSingleSelectCode from './default-single-select?raw';
import DefaultMultiSelectComponent from './default-multi-select';
import DefaultMultiSelectCode from './default-multi-select?raw';

import ClickOnlySelectsComponent from './click-only-selects';
import ClickOnlySelectsCode from './click-only-selects?raw';

import IsCarryForwardComponent from './is-carry-forward';
import IsCarryForwardCode from './is-carry-forward?raw';

Expand All @@ -55,6 +58,7 @@ createStories(
createStory('all multi select', AllMultiSelectComponent, AllMultiSelectCode),
createStory('default single select', DefaultSingleSelectComponent, DefaultSingleSelectCode),
createStory('default multi select', DefaultMultiSelectComponent, DefaultMultiSelectCode),
createStory('click only selects', ClickOnlySelectsComponent, ClickOnlySelectsCode),
createStory('is carry forward', IsCarryForwardComponent, IsCarryForwardCode),
createStory('is partial to all', IsPartialToAllComponent, IsPartialToAllCode),
createStory('custom checkbox (Material UI)', IsPartialToAllComponent, IsPartialToAllCode),
Expand Down
16 changes: 10 additions & 6 deletions src/select/useRowSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ const getRowProps = <T extends TableNode>(
select.fns.onToggleByIdShift(node.id, select.options, applyModifiers(features));
} else if (isMultiSelectType) {
select.fns.onToggleById(node.id);
} /* isSingleSelectType */ else {
} /* isSingleSelectType */ else if (select.options.clickOnlySelects) {
select.fns.onAddByIdExclusively(node.id);
} else {
select.fns.onToggleByIdExclusively(node.id);
}
};
Expand All @@ -102,6 +104,7 @@ const DEFAULT_OPTIONS = {
buttonSelect: SelectTypes.MultiSelect,
isCarryForward: true,
isPartialToAll: false,
clickOnlySelects: false,
};

const useRowSelect = <T extends TableNode>(
Expand All @@ -110,19 +113,20 @@ const useRowSelect = <T extends TableNode>(
options?: SelectOptions,
context?: any,
): Select<T> => {
const controlledState: State = primary?.state
? { ...DEFAULT_STATE, ...primary.state }
: { ...DEFAULT_STATE };
const controlledState: State = {
...DEFAULT_STATE,
...(primary?.state ?? {}),
};

const onChange = primary?.onChange ? primary.onChange : () => {};
const onChange = primary?.onChange ?? (() => {});

const [state, fns] = useIdReducer<T>(data, controlledState, onChange, context);

useSyncRefState('select', context, state);

const mergedOptions = {
...DEFAULT_OPTIONS,
...(options ? options : {}),
...(options ?? {}),
};

return {
Expand Down
9 changes: 2 additions & 7 deletions src/types/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,10 @@ export type SelectOptions = {
buttonSelect?: SelectTypes;
isCarryForward?: boolean;
isPartialToAll?: boolean;
clickOnlySelects?: boolean;
};

export type SelectOptionsSound = {
clickType: SelectClickTypes;
rowSelect: SelectTypes;
buttonSelect: SelectTypes;
isCarryForward: boolean;
isPartialToAll: boolean;
};
export type SelectOptionsSound = Required<SelectOptions>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀


export type CellSelectProps<T extends TableNode> = {
item: T;
Expand Down