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

ui: fix drag and drop in FlatList while in strict mode #2413

Merged
merged 31 commits into from
Oct 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
ad3a66b
add react-dnd
Forfold May 23, 2022
ec60676
add new components for flatlist items and draggable items
Forfold May 23, 2022
c31e434
update styling of draggable list items
Forfold May 23, 2022
d087b83
get drag and drop working with react-dnd
Forfold May 25, 2022
30e4eca
get working when dragging up or down
Forfold May 25, 2022
dea58e9
fix issue with tabbing over new draggable lis
Forfold May 26, 2022
627936f
add react-dnd backends
Forfold May 26, 2022
77512e4
add touch backend lib
Forfold May 27, 2022
40f3cbb
remove react-dnd, add dnd-kit
Forfold Jun 1, 2022
cf2bacb
implement drag and drop using dnd-kit
Forfold Jun 1, 2022
079ab6c
wip use theme color when dragging
Forfold Jun 2, 2022
fac7722
use default bg color
Forfold Jun 2, 2022
b568925
remove unused vars
Forfold Jun 2, 2022
3d0fc49
remove use of drag overlay
Forfold Jun 3, 2022
d33610b
merge master into react-dnd
Forfold Jun 6, 2022
318b325
fix zIndex when dragging list item over other items
Forfold Jun 6, 2022
783ceec
add screen reader announcements
Forfold Jun 6, 2022
33fcd4e
update rotation dnd test
Forfold Jun 6, 2022
b69e4bd
remove unused dep
Forfold Jun 6, 2022
6af0a74
Merge branch 'master' of github.com:target/goalert into react-dnd
Forfold Jun 29, 2022
1c49f10
use drag handles
Forfold Jun 30, 2022
4e0f7f0
Merge branch 'master' into react-dnd
Forfold Jul 26, 2022
3180b20
Merge branch 'master' into react-dnd
Forfold Oct 5, 2022
fce51c5
remove unused vars
Forfold Oct 5, 2022
38473c4
flip order of toggle
Forfold Oct 5, 2022
5b3c8c9
ux improvements
Forfold Oct 5, 2022
e5680dd
fix drag handles
Forfold Oct 7, 2022
890e82b
disable toggle while dragging
Forfold Oct 7, 2022
ffcb1e8
update tests
Forfold Oct 7, 2022
e7a4269
Update web/src/app/lists/FlatList.tsx
Forfold Oct 12, 2022
408af12
add import
Forfold Oct 12, 2022
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
4 changes: 2 additions & 2 deletions web/src/app/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { GOALERT_VERSION, pathPrefix } from './env'

import React, { StrictMode } from 'react'
import { createRoot } from 'react-dom/client'
import { Provider as ReduxProvider } from 'react-redux'
import { ApolloProvider } from '@apollo/client'
import { StyledEngineProvider } from '@mui/material/styles'

import { GOALERT_VERSION, pathPrefix } from './env'
import { ThemeProvider } from './theme/themeConfig'
import { GraphQLClient } from './apollo'
import './styles'
Expand Down
108 changes: 108 additions & 0 deletions web/src/app/lists/DraggableListItem.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import React, { MutableRefObject } from 'react'
import { FlatListItem as FlatListItemType } from './FlatList'
import FlatListItem from './FlatListItem'
import { Announcements, UniqueIdentifier } from '@dnd-kit/core'
import { CSS } from '@dnd-kit/utilities'
import { useSortable } from '@dnd-kit/sortable'
import { useTheme } from '@mui/material'
import DragHandleIcon from '@mui/icons-material/DragHandle'

export function getAnnouncements(
items: string[],
isFirstAnnouncement: MutableRefObject<boolean>,
): Announcements {
const getPosition = (id: UniqueIdentifier): number =>
items.indexOf(id.toString()) + 1

return {
onDragStart({ active: { id } }) {
return `Picked up sortable item ${String(
id,
)}. Sortable item ${id} is in position ${getPosition(id)} of ${
items.length
}`
},
onDragOver({ active, over }) {
// onDragOver is called right after onDragStart, cancel first run here
// in favor of the pickup announcement
if (isFirstAnnouncement.current) {
isFirstAnnouncement.current = false
return
}

if (over) {
return `Sortable item ${
active.id
} was moved into position ${getPosition(over.id)} of ${items.length}`
}
},
onDragEnd({ active, over }) {
if (over) {
return `Sortable item ${
active.id
} was dropped at position ${getPosition(over.id)} of ${items.length}`
}
},
onDragCancel({ active: { id } }) {
return `Sorting was cancelled. Sortable item ${id} was dropped and returned to position ${getPosition(
id,
)} of ${items.length}.`
},
}
}

interface DraggableListItemProps {
id: string
index: number
item: FlatListItemType
draggable: boolean
}

export function DraggableListItem({
id,
index,
item,
draggable,
}: DraggableListItemProps): JSX.Element {
const theme = useTheme()
const {
attributes,
isDragging,
listeners,
setNodeRef,
transform,
transition,
} = useSortable({
id,
})

const style = {
transform: CSS.Transform.toString(transform),
transition,
backgroundColor: isDragging ? theme.palette.background.default : 'inherit',
zIndex: isDragging ? 9001 : 1,
}

const draggableItem = {
...item,
icon: (
<DragHandleIcon
{...listeners}
id={'drag-' + item?.id ?? index}
tabIndex={0}
focusable
sx={{ cursor: 'pointer', marginLeft: '8px' }}
/>
),
}

return (
<div ref={setNodeRef} style={style} {...attributes}>
<FlatListItem
index={index}
item={draggable ? draggableItem : item}
showOptions={!draggable}
/>
</div>
)
}
Loading