Skip to content

Commit

Permalink
fix(useList): mexState -> controlledState
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaevAlexandr committed Jun 20, 2024
1 parent d5d74ca commit 1b0b182
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/components/TreeSelect/TreeSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export const TreeSelect = React.forwardRef(function TreeSelect<T>(
});

const list = useList({
mixState: {
controlledState: {
selectedById: selected,
},
items,
Expand Down
4 changes: 2 additions & 2 deletions src/components/useList/__stories__/docs/use-list.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ The main hook to use what provide you normalized representation of list items (`
| defaultExpandedState | Default state for nodes with children items if `withExpandedState` is true | `expanded`, `closed` | `expanded` |
| withExpandedState | Is nodes with children's needed to be controlled | `boolean` | `true` |
| initialState | Initial state values | `Partial<ListState>` | |
| mixState | Way to override state by some controlled values. | `Partial<ListState>` | |
| controlledState | Way to override state by some controlled values. | `Partial<ListState>` | |

#### Result (UseList):

Expand Down Expand Up @@ -129,7 +129,7 @@ const [selectedById] = React.useState<Record<ListItemId, boolean>>({});

const list = useList({
// outer controlled state
mixState: {
controlledState: {
selectedById,
},
});
Expand Down
12 changes: 5 additions & 7 deletions src/components/useList/hooks/useList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {useListState} from './useListState';
import type {UseListStateProps} from './useListState';

interface UseListProps<T> extends UseListParsedStateProps<T>, UseListStateProps {
mixState?: Partial<InitialListParsedState>;
controlledState?: Partial<InitialListParsedState>;
}

/**
Expand All @@ -22,7 +22,7 @@ export const useList = <T>({
defaultExpandedState = 'expanded',
withExpandedState = true,
initialState: initialValues,
mixState,
controlledState,
}: UseListProps<T>): UseList<T> => {
const {itemsById, groupsState, itemsState, initialState} = useListParsedState({
items,
Expand Down Expand Up @@ -60,17 +60,15 @@ export const useList = <T>({
});

const realState = React.useMemo(() => {
if (mixState) {
if (controlledState) {
return {
...innerState,
expandedById: {...innerState.expandedById, ...mixState?.expandedById},
selectedById: {...innerState.selectedById, ...mixState?.selectedById},
disabledById: {...innerState.disabledById, ...mixState?.disabledById},
...controlledState,
};
}

return innerState;
}, [mixState, innerState]);
}, [controlledState, innerState]);

return {
state: realState,
Expand Down

0 comments on commit 1b0b182

Please sign in to comment.