Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(TreeList): added missed initial state for elements #1524

Merged
merged 1 commit into from
Apr 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/components/TreeList/TreeList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ export const TreeList = <T,>({
size = 'm',
items,
className,
expandedById,
disabledById,
expandedById: propsExpandedById,
disabledById: propsDisabledById,
selectedById: propsSelectedById,
activeItemId,
selectedById,
defaultGroupsExpanded = true,
getItemId,
renderItem: propsRenderItem,
Expand All @@ -38,12 +38,17 @@ export const TreeList = <T,>({
const listParsedState = useList({
items,
getItemId,
expandedById,
disabledById,
// used not all of all properties but it may be needed in future
expandedById: propsExpandedById,
disabledById: propsDisabledById,
selectedById: propsSelectedById,
activeItemId,
selectedById,
});

const expandedById = propsExpandedById || listParsedState.initialState.expandedById;
const disabledById = propsDisabledById || listParsedState.initialState.disabledById;
const selectedById = propsSelectedById || listParsedState.initialState.selectedById;

const handleItemClick = React.useMemo(() => {
if (onItemClick) {
return (listItemId: ListItemId) => {
Expand Down
36 changes: 36 additions & 0 deletions src/components/TreeList/__stories__/TreeList.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {TreeList} from '../TreeList';

import {DefaultStory} from './stories/DefaultStory';
import {InfinityScrollStory} from './stories/InfinityScrollStory';
import {WithDisabledElementsStory} from './stories/WithDisabledElementsStory';
import {WithDndListStory} from './stories/WithDndListStory';
import {WithFiltrationAndControlsStory} from './stories/WithFiltrationAndControlsStory';
import {WithGroupSelectionAndCustomIconStory} from './stories/WithGroupSelectionAndCustomIconStory';
Expand Down Expand Up @@ -213,3 +214,38 @@ export const WithItemLinksAndActions: WithItemLinksAndActionsStoryObj = {
},
},
};

type WithDisabledElementsStoryObj = StoryObj<typeof WithDisabledElementsStory>;

export const WithDisabledElements: WithDisabledElementsStoryObj = {
render: WithDisabledElementsStory,
parameters: {
a11y: {
element: '#storybook-root',
config: {
rules: [
{
id: 'color-contrast',
enabled: false,
},
{
id: 'aria-input-field-name',
enabled: false,
},
{
id: 'aria-required-children',
enabled: false,
},
{
id: 'aria-required-parent',
enabled: false,
},
{
id: 'nested-interactive',
enabled: false,
},
],
},
},
},
};
29 changes: 27 additions & 2 deletions src/components/TreeList/__stories__/stories/DefaultStory.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import React from 'react';

import {Text} from '../../../Text';
import {Flex} from '../../../layout';
import {ListItemView} from '../../../useList';
import {createRandomizedData} from '../../../useList/__stories__/utils/makeData';
import {TreeList} from '../../TreeList';
import type {TreeListProps} from '../../types';
Expand All @@ -18,8 +20,31 @@ export const DefaultStory = ({itemsCount = 5, ...props}: DefaultStoryProps) => {
const items = React.useMemo(() => createRandomizedData({num: itemsCount}), [itemsCount]);

return (
<Flex width="500">
<TreeList {...props} items={items} mapItemDataToProps={identity} />
<Flex gap="5">
<Flex direction={'column'} gap="3">
<Text color="secondary">Default TreeList</Text>
<TreeList {...props} items={items} mapItemDataToProps={identity} />
</Flex>
<Flex direction={'column'} gap="3">
<Text color="secondary">
To remove default group view, override corresponding (expanded) prop in
renderItem method
</Text>

<TreeList
{...props}
items={items}
mapItemDataToProps={identity}
renderItem={({props, context: {groupState}, renderContainerProps}) => {
// if item group
if (groupState) {
props.expanded = undefined;
}

return <ListItemView {...props} {...renderContainerProps} />;
}}
/>
</Flex>
</Flex>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import React from 'react';

import {Button} from '../../../Button';
import {Flex} from '../../../layout';
import {useListState} from '../../../useList';
import type {ListItemType} from '../../../useList';
import {TreeList} from '../../TreeList';
import type {TreeListProps} from '../../types';

export interface WithDisabledElementsStoryProps
extends Omit<TreeListProps<{text: string}>, 'items' | 'mapItemDataToProps'> {}

const items: ListItemType<{text: string}>[] = [
{
text: 'one',
disabled: true,
},
{
text: 'two',
},
{
text: 'free',
},
{
text: 'four',
},
{
text: 'five',
},
];

export const WithDisabledElementsStory = ({...props}: WithDisabledElementsStoryProps) => {
const {disabledById: _disabledById, setDisabled: _setDisabled, ...listState} = useListState();
const containerRef = React.useRef<HTMLDivElement>(null);

return (
<Flex width="500" gap="5" direction="column" alignItems="flex-start">
<Flex alignItems="center" gap="1">
<Button
onClick={() => {
containerRef.current?.focus();
}}
>
focus elements
</Button>{' '}
to control from keyboard
</Flex>
<TreeList
{...props}
containerRef={containerRef}
items={items}
{...listState}
mapItemDataToProps={({text}) => ({title: text})}
onItemClick={({data, id, selected}) => {
listState.setSelected({[id]: !selected});
alert(`Clicked by item with id :"${id}" and data: ${JSON.stringify(data)}`);
}}
/>
</Flex>
);
};
Loading