Skip to content

Commit

Permalink
Remove sortable example
Browse files Browse the repository at this point in the history
  • Loading branch information
khiga8 committed Jan 24, 2025
1 parent 88ffc0d commit 8695829
Showing 1 changed file with 0 additions and 121 deletions.
121 changes: 0 additions & 121 deletions packages/react/src/ActionList/ActionList.examples.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -251,127 +251,6 @@ export function AsyncListWithSpinner(): JSX.Element {
)
}

type Option = {text: string; icon: React.ReactNode; selected: boolean}
export function MemexSortable(): JSX.Element {
const [options, setOptions] = React.useState<Option[]>([
{text: 'Status', icon: <IssueOpenedIcon />, selected: true},
{text: 'Stage', icon: <TableIcon />, selected: true},
{text: 'Assignee', icon: <PeopleIcon />, selected: true},
{text: 'Team', icon: <TypographyIcon />, selected: true},
{text: 'Estimate', icon: <NumberIcon />, selected: false},
{text: 'Due Date', icon: <CalendarIcon />, selected: false},
])

const toggle = (text: string) => {
setOptions(
options.map(option => {
if (option.text === text) option.selected = !option.selected
return option
}),
)
}

const reorder = ({optionToMove, moveAfterOption}: {optionToMove: Option; moveAfterOption: Option}) => {
setOptions(currentOptions => {
const newOptions = [...currentOptions]
// remove option to move
const currentPosition = newOptions.findIndex(o => o.text === optionToMove.text)
newOptions.splice(currentPosition, 1)
// add it after the provided element
const newPosition = newOptions.findIndex(o => o.text === moveAfterOption.text) + 1
newOptions.splice(newPosition, 0, optionToMove)
return newOptions
})
}

const visibleOptions = options.filter(option => option.selected)
const hiddenOptions = options.filter(option => !option.selected)

return (
// @ts-ignore react-dnd needs to be updated to support React 18
<DndProvider backend={HTML5Backend}>
<ActionList selectionVariant="multiple" role="menu">
<ActionList.Group>
<ActionList.GroupHeading>Visible fields (can be reordered)</ActionList.GroupHeading>
{visibleOptions.map(option => (
<SortableItem
key={option.text}
role="menuitemcheckbox"
option={option}
onSelect={() => toggle(option.text)}
reorder={reorder}
/>
))}
</ActionList.Group>
<ActionList.Group
selectionVariant={
/** selectionVariant override on Group: disable selection if there are no options */
hiddenOptions.length ? 'multiple' : false
}
>
<ActionList.GroupHeading>Hidden fields</ActionList.GroupHeading>
{hiddenOptions.map((option, index) => (
<ActionList.Item
key={index}
role="menuitemcheckbox"
selected={option.selected}
onSelect={() => toggle(option.text)}
>
<ActionList.LeadingVisual>{option.icon}</ActionList.LeadingVisual>
{option.text}
</ActionList.Item>
))}
{hiddenOptions.length === 0 && <ActionList.Item disabled>No hidden fields</ActionList.Item>}
</ActionList.Group>
</ActionList>
</DndProvider>
)
}
MemexSortable.storyName = 'Memex Sortable List'

type SortableItemProps = {
option: Option
role: ActionListItemProps['role']
onSelect: ActionListItemProps['onSelect']
reorder: ({optionToMove, moveAfterOption}: {optionToMove: Option; moveAfterOption: Option}) => void
}
const SortableItem: React.FC<React.PropsWithChildren<SortableItemProps>> = ({option, role, onSelect, reorder}) => {
const [{isDragging}, dragRef] = useDrag(() => ({
type: 'ITEM',
item: option,
collect: monitor => {
return {isDragging: monitor.isDragging()}
},
}))

const [{isOver}, dropRef] = useDrop(() => ({
accept: 'ITEM',
collect: monitor => {
return {isOver: monitor.isOver()}
},
drop: (optionDropped: Option) => {
reorder({optionToMove: optionDropped, moveAfterOption: option})
},
}))

return (
<ActionList.Item
role={role}
ref={element => dragRef(element) && dropRef(element)} // merge refs
selected={option.selected}
onSelect={onSelect}
sx={{
opacity: isDragging ? 0.5 : 1,
boxShadow: isOver ? theme => `0px 2px 0 0px ${theme.colors.accent.emphasis}` : undefined,
borderRadius: isOver ? 0 : 2,
}}
>
<ActionList.LeadingVisual>{option.icon}</ActionList.LeadingVisual>
{option.text}
</ActionList.Item>
)
}

export function AllCombinations(): JSX.Element {
return (
<>
Expand Down

0 comments on commit 8695829

Please sign in to comment.