-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into withTableSettings/deprecate-render-controls
- Loading branch information
Showing
39 changed files
with
1,181 additions
and
310 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import React from 'react'; | ||
|
||
import {useUniqId} from '../../hooks'; | ||
import {ListItemView, getItemRenderState, useList, useListKeydown} from '../useList'; | ||
import type {ListItemId} from '../useList'; | ||
import {block} from '../utils/cn'; | ||
|
||
import {TreeListContainer} from './components/TreeListContainer/TreeListContainer'; | ||
import type {TreeListProps, TreeListRenderContainerProps} from './types'; | ||
|
||
const b = block('tree-list'); | ||
|
||
export const TreeList = <T,>({ | ||
id, | ||
size = 'm', | ||
items, | ||
className, | ||
expandedById, | ||
disabledById, | ||
activeItemId, | ||
selectedById, | ||
getId, | ||
renderItem: propsRenderItem, | ||
renderContainer = TreeListContainer, | ||
onItemClick, | ||
multiple, | ||
setActiveItemId, | ||
containerRef: propsContainerRef, | ||
mapItemDataToProps, | ||
}: TreeListProps<T>) => { | ||
const uniqId = useUniqId(); | ||
const treeListId = id ?? uniqId; | ||
const containerRefLocal = React.useRef<HTMLDivElement>(null); | ||
const containerRef = propsContainerRef ?? containerRefLocal; | ||
|
||
const listParsedState = useList({ | ||
items, | ||
getId, | ||
expandedById, | ||
disabledById, | ||
activeItemId, | ||
selectedById, | ||
}); | ||
|
||
const handleItemClick = React.useCallback( | ||
(listItemId: ListItemId) => { | ||
onItemClick?.({ | ||
id: listItemId, | ||
data: listParsedState.itemsById[listItemId], | ||
disabled: disabledById | ||
? Boolean(disabledById[listItemId]) | ||
: Boolean(listParsedState.initialState.disabledById[listItemId]), | ||
isLastItem: | ||
listParsedState.visibleFlattenIds[ | ||
listParsedState.visibleFlattenIds.length - 1 | ||
] === listItemId, | ||
groupState: listParsedState.groupsState[listItemId], | ||
itemState: listParsedState.itemsState[listItemId], | ||
}); | ||
}, | ||
[ | ||
disabledById, | ||
listParsedState.groupsState, | ||
listParsedState.initialState.disabledById, | ||
listParsedState.itemsById, | ||
listParsedState.itemsState, | ||
listParsedState.visibleFlattenIds, | ||
onItemClick, | ||
], | ||
); | ||
|
||
useListKeydown({ | ||
containerRef, | ||
onItemClick: handleItemClick, | ||
...listParsedState, | ||
activeItemId, | ||
disabledById, | ||
setActiveItemId, | ||
}); | ||
|
||
const renderItem: TreeListRenderContainerProps<T>['renderItem'] = ( | ||
itemId, | ||
index, | ||
renderContextProps, | ||
) => { | ||
const renderState = getItemRenderState({ | ||
id: itemId, | ||
size, | ||
mapItemDataToProps, | ||
onItemClick: handleItemClick, | ||
...listParsedState, | ||
expandedById, | ||
disabledById, | ||
activeItemId, | ||
selectedById, | ||
}); | ||
|
||
// redefining the view logic for groups and multiple selection of list items | ||
renderState.props.hasSelectionIcon = Boolean(multiple) && !renderState.context.groupState; | ||
|
||
if (propsRenderItem) { | ||
return propsRenderItem({ | ||
data: renderState.data, | ||
props: renderState.props, | ||
itemState: renderState.context, | ||
index, | ||
renderContext: renderContextProps, | ||
}); | ||
} | ||
|
||
return <ListItemView {...renderState.props} {...renderContextProps} />; | ||
}; | ||
|
||
// not JSX decl here is from weird `react-beautiful-dnd` render bug | ||
return renderContainer({ | ||
id: `list-${treeListId}`, | ||
size, | ||
containerRef, | ||
className: b(null, className), | ||
...listParsedState, | ||
expandedById, | ||
disabledById, | ||
activeItemId, | ||
selectedById, | ||
renderItem, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import type {Meta, StoryObj} from '@storybook/react'; | ||
|
||
import {TreeList} from '../TreeList'; | ||
|
||
import {DefaultStory} from './stories/DefaultStory'; | ||
import {InfinityScrollStory} from './stories/InfinityScrollStory'; | ||
import {WithDndListStory} from './stories/WithDndListStory'; | ||
import {WithFiltrationAndControlsStory} from './stories/WithFiltrationAndControlsStory'; | ||
import {WithGroupSelectionAndCustomIconStory} from './stories/WithGroupSelectionAndCustomIconStory'; | ||
import {WithItemLinksAndActionsStory} from './stories/WithItemLinksAndActionsStory'; | ||
|
||
export default { | ||
title: 'Unstable/TreeList', | ||
component: TreeList, | ||
} as Meta; | ||
|
||
type DefaultStoryObj = StoryObj<typeof DefaultStory>; | ||
|
||
export const Default: DefaultStoryObj = { | ||
render: DefaultStory, | ||
}; | ||
|
||
type InfinityScrollStoryObj = StoryObj<typeof InfinityScrollStory>; | ||
|
||
export const InfinityScroll: InfinityScrollStoryObj = { | ||
render: InfinityScrollStory, | ||
}; | ||
|
||
type WithDndListStoryObj = StoryObj<typeof WithDndListStory>; | ||
|
||
export const WithDndList: WithDndListStoryObj = { | ||
parameters: { | ||
// Strict mode ruins sortable list due to this react-beautiful-dnd issue | ||
// https://github.com/atlassian/react-beautiful-dnd/issues/2350 | ||
disableStrictMode: true, | ||
}, | ||
render: WithDndListStory, | ||
}; | ||
|
||
type WithFiltrationAndControlsStoryObj = StoryObj<typeof WithFiltrationAndControlsStory>; | ||
|
||
export const WithFiltrationAndControls: WithFiltrationAndControlsStoryObj = { | ||
render: WithFiltrationAndControlsStory, | ||
}; | ||
|
||
type WithGroupSelectionAndCustomIconStoryObj = StoryObj< | ||
typeof WithGroupSelectionAndCustomIconStory | ||
>; | ||
|
||
export const WithGroupSelectionAndCustomIcon: WithGroupSelectionAndCustomIconStoryObj = { | ||
render: WithGroupSelectionAndCustomIconStory, | ||
}; | ||
|
||
type WithItemLinksAndActionsStoryObj = StoryObj<typeof WithItemLinksAndActionsStory>; | ||
|
||
export const WithItemLinksAndActions: WithItemLinksAndActionsStoryObj = { | ||
render: WithItemLinksAndActionsStory, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/components/TreeList/__stories__/stories/DefaultStory.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import React from 'react'; | ||
|
||
import {Flex} from '../../../layout'; | ||
import {createRandomizedData} from '../../../useList/__stories__/utils/makeData'; | ||
import {TreeList} from '../../TreeList'; | ||
import type {TreeListProps} from '../../types'; | ||
|
||
function identity<T>(value: T): T { | ||
return value; | ||
} | ||
|
||
export interface DefaultStoryProps | ||
extends Omit<TreeListProps<{title: string}>, 'items' | 'mapItemDataToProps'> { | ||
itemsCount?: number; | ||
} | ||
|
||
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> | ||
); | ||
}; |
Oops, something went wrong.