Skip to content

Commit

Permalink
Merge pull request #606 from lumapps/fix/tab-state
Browse files Browse the repository at this point in the history
fix(tab): fix tab state update
  • Loading branch information
gcornut authored Jan 21, 2021
2 parents e25841a + 0e4e1fa commit 1bf146e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed `Thumbnail` focus state style.
- Fixed `Thumbnail` fallback placement.
- Fixed `Thumbnail` fill height style.
- Fixed `TabProvider` and `ProgressTrackerProvider` state on unmout/remount.

## [1.0.2][] - 2021-01-18

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const DEFAULT_PROPS: Partial<ProgressTrackerProviderProps> = {
* @return React element.
*/
export const ProgressTrackerProvider: React.FC<ProgressTrackerProviderProps> = (props) => {
const { children, onChange, activeStepIndex, ...propState } = props;
const { children, onChange, ...propState } = props;
const [state, dispatch] = useReducer(reducer, INIT_STATE);

// On prop state change => dispatch update.
Expand All @@ -42,18 +42,18 @@ export const ProgressTrackerProvider: React.FC<ProgressTrackerProviderProps> = (
type: 'update',
payload: {
...propState,
activeTabIndex: activeStepIndex,
activeTabIndex: propState.activeStepIndex,
},
});
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[dispatch, ...Object.values(propState), activeStepIndex],
[dispatch, ...Object.values(propState)],
);

// On active tab index state change => send update to the onChange.
useEffect(
() => {
if (state === INIT_STATE || !onChange || activeStepIndex === state.activeTabIndex) {
if (state === INIT_STATE || !onChange || propState.activeStepIndex === state.activeTabIndex) {
return;
}
onChange(state.activeTabIndex);
Expand Down
2 changes: 1 addition & 1 deletion packages/lumx-react/src/components/tabs/TabProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const TabProvider: React.FC<TabProviderProps> = (props) => {
onChange(state.activeTabIndex);
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[onChange, propState.activeTabIndex],
[onChange, state.activeTabIndex],
);

return <TabProviderContext.Provider value={[state, dispatch]}>{children}</TabProviderContext.Provider>;
Expand Down
21 changes: 20 additions & 1 deletion packages/lumx-react/src/components/tabs/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export const INIT_STATE: State = {
export type Action =
| { type: 'update'; payload: Partial<State> }
| { type: 'setActiveTabIndex'; payload: number }
| { type: 'register'; payload: { type: TabType; id: string } };
| { type: 'register'; payload: { type: TabType; id: string } }
| { type: 'unregister'; payload: { type: TabType; id: string } };

export const reducer = (state: State, action: Action): State => {
switch (action.type) {
Expand All @@ -36,6 +37,20 @@ export const reducer = (state: State, action: Action): State => {
// Append tab/tabPanel id in state.
return { ...state, ids: { ...state.ids, [type]: [...state.ids[type], id] } };
}
case 'unregister': {
const { type, id } = action.payload;
const index = state.ids[type].indexOf(id);
if (index === -1) return state;
// Remove tab & tab panel at index.
const tabIds = [...state.ids.tab];
tabIds.splice(index, 1);
const tabPanelIds = [...state.ids.tabPanel];
tabPanelIds.splice(index, 1);
return {
...state,
ids: { tab: tabIds, tabPanel: tabPanelIds },
};
}
default:
return state;
}
Expand Down Expand Up @@ -68,6 +83,10 @@ export const useTabProviderContext = (type: TabType, originalId?: string): undef
() => {
// On mount: register tab or tab panel id.
dispatch({ type: 'register', payload: { type, id } });
return () => {
// On unmount: unregister tab or tab panel id.
dispatch({ type: 'unregister', payload: { type, id } });
};
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[],
Expand Down

0 comments on commit 1bf146e

Please sign in to comment.