Skip to content

Commit

Permalink
refactor(accordion): ♻️ add helper for reducer
Browse files Browse the repository at this point in the history
  • Loading branch information
navin-moorthy committed Aug 25, 2020
1 parent d04eae9 commit 04c5d16
Show file tree
Hide file tree
Showing 3 changed files with 366 additions and 313 deletions.
30 changes: 15 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,24 @@
"dependencies": {
"@chakra-ui/hooks": "1.0.0-next.7",
"@chakra-ui/utils": "1.0.0-next.7",
"reakit": "1.2.2",
"reakit-system": "0.14.2",
"reakit-utils": "0.14.2"
"reakit": "1.2.3",
"reakit-system": "0.14.3",
"reakit-utils": "0.14.3"
},
"devDependencies": {
"@babel/core": "7.11.1",
"@babel/core": "7.11.4",
"@commitlint/cli": "9.1.2",
"@commitlint/config-conventional": "9.1.2",
"@storybook/addon-a11y": "6.0.14",
"@storybook/addon-actions": "6.0.14",
"@storybook/addon-essentials": "6.0.14",
"@storybook/addon-links": "6.0.14",
"@storybook/addon-storysource": "6.0.14",
"@storybook/react": "6.0.14",
"@types/react": "16.9.46",
"@storybook/addon-a11y": "6.0.17",
"@storybook/addon-actions": "6.0.17",
"@storybook/addon-essentials": "6.0.17",
"@storybook/addon-links": "6.0.17",
"@storybook/addon-storysource": "6.0.17",
"@storybook/react": "6.0.17",
"@types/react": "16.9.47",
"@types/react-dom": "16.9.8",
"@typescript-eslint/eslint-plugin": "3.9.1",
"@typescript-eslint/parser": "3.9.1",
"@typescript-eslint/eslint-plugin": "3.10.0",
"@typescript-eslint/parser": "3.10.0",
"babel-eslint": "10.1.0",
"babel-loader": "8.1.0",
"eslint": "7.7.0",
Expand All @@ -58,11 +58,11 @@
"gacp": "2.10.0",
"husky": "4.2.5",
"lint-staged": "10.2.11",
"prettier": "2.0.5",
"prettier": "2.1.0",
"react": "16.13.1",
"react-dom": "16.13.1",
"react-is": "16.13.1",
"sort-package-json": "1.44.0",
"typescript": "3.9.7"
"typescript": "4.0.2"
}
}
24 changes: 14 additions & 10 deletions src/accordion/AccordionState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,7 @@ function reducer(

case "registerButton": {
const { button } = action;
const item = items.find(r => r.ref.current?.contains(button.ref.current));
const nextItems = items.filter(
r => !r.ref.current?.contains(button.ref.current),
);
const nextItem = { ...item, button } as Item;
const { nextItem, nextItems } = getNextItem("button", button, items);

return {
...state,
Expand All @@ -136,11 +132,7 @@ function reducer(

case "registerPanel": {
const { panel } = action;
const item = items.find(r => r.ref.current?.contains(panel.ref.current));
const nextItems = items.filter(
r => !r.ref.current?.contains(panel.ref.current),
);
const nextItem = { ...item, panel } as Item;
const { nextItem, nextItems } = getNextItem("panel", panel, items);

return {
...state,
Expand All @@ -162,3 +154,15 @@ function reducer(
throw new Error();
}
}

function getNextItem(type: string, currentItem: Button | Panel, items: Item[]) {
const item = items.find(r =>
r.ref.current?.contains(currentItem.ref.current),
);
const nextItems = items.filter(
r => !r.ref.current?.contains(currentItem.ref.current),
);
const nextItem = { ...item, [type]: currentItem } as Item;

return { nextItem, nextItems };
}
Loading

0 comments on commit 04c5d16

Please sign in to comment.