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

Relations & Dynamic Zones reordering #15119

Merged
merged 234 commits into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
234 commits
Select commit Hold shift + click to select a range
4853558
add connect disconnect format
Marc-Roig Oct 24, 2022
2e7aa7c
use position attribute for positional arguments
Marc-Roig Oct 24, 2022
640aded
fractional orderer error management
Marc-Roig Oct 24, 2022
36ca8d7
max order default to 0 if no relations
Marc-Roig Oct 24, 2022
b8cf3a1
document relations orderer
Marc-Roig Oct 24, 2022
e8002a7
Merge branch 'feature/relations-main-view' into feature/relations-reo…
Marc-Roig Oct 24, 2022
0f330d1
Merge branch 'feature/relations-reordering' into relations-reordering…
Marc-Roig Oct 24, 2022
5106ed2
test relation orderer
Marc-Roig Oct 24, 2022
7a9f43a
log relation reorderer error
Marc-Roig Oct 25, 2022
bb6ceae
change column type to float
Marc-Roig Oct 25, 2022
5f5e3e6
all order columns to float
Marc-Roig Oct 26, 2022
aedff73
change joincolumn name to integer
Marc-Roig Oct 26, 2022
70595ae
access array last element without at
Marc-Roig Oct 26, 2022
6b6c394
feat(wip): add visual reordering of relations
joshuaellis Nov 4, 2022
eef9fdb
feat: add visual re-ordering with mouse
joshuaellis Nov 7, 2022
ab5dac4
refactor: repeatableComponents (make it simpler)
joshuaellis Nov 7, 2022
2b85ad0
chore: remove passing id to relationItem useDrag/useDrop
joshuaellis Nov 7, 2022
3e39b41
chore: remove debug line
joshuaellis Nov 7, 2022
102b842
chore: tidy ups
joshuaellis Nov 7, 2022
5978ae5
test: fix existing tests
joshuaellis Nov 7, 2022
7f7d4ef
test: update RelationItem and RelationList
joshuaellis Nov 7, 2022
6cc51a0
test: add dnd tests for relation input and data manager
joshuaellis Nov 7, 2022
ce20813
Merge branch 'main' into feature/relations-reordering
joshuaellis Nov 7, 2022
a60f265
chore: update snapshots post text fixes
joshuaellis Nov 7, 2022
09d8c28
feat: add keyboard navigation for relations reordering
joshuaellis Nov 7, 2022
420d6a4
Update packages/core/admin/admin/src/content-manager/components/Relat…
joshuaellis Nov 8, 2022
60b2104
chore: pr comments refactoring
joshuaellis Nov 8, 2022
88796e7
chore: merge
joshuaellis Nov 8, 2022
0c0efa6
chore: re-add preview
joshuaellis Nov 8, 2022
7294477
test: add accessibility tests for keyboard navigation
joshuaellis Nov 8, 2022
c2c3d25
Merge pull request #14800 from strapi/feat/relations-reordering/visua…
joshuaellis Nov 8, 2022
433b451
Merge branch 'main' into feature/relations-reordering
joshuaellis Nov 8, 2022
b42f795
chore: fix test from merge
joshuaellis Nov 8, 2022
99265cd
Merge branch 'feature/relations-reordering' into feat/relations-reord…
joshuaellis Nov 8, 2022
b26ecf3
fix: firefox stops parent drag event working with native button
joshuaellis Nov 10, 2022
67a489d
chore: update snapshots
joshuaellis Nov 10, 2022
6f4f7c9
Merge branch 'main' into feature/relations-reordering
Marc-Roig Nov 10, 2022
8b65fe7
Merge branch 'feature/relations-reordering' into relations-reordering…
Marc-Roig Nov 10, 2022
9bd5a9d
refactor: translations
joshuaellis Nov 10, 2022
a321b9f
fix: use relationsFromModifiedData not relations
joshuaellis Nov 10, 2022
8f8279d
chore: move accessibility tests since the data comes from the parent.
joshuaellis Nov 10, 2022
6e9424c
make relations sorter a pure function
Marc-Roig Nov 10, 2022
e151df5
add get method
Marc-Roig Nov 10, 2022
9d6efcc
chore: fix test with DS update
joshuaellis Nov 10, 2022
586ae57
Merge branch 'main' into feat/relations-reordering/keyboard-nav
joshuaellis Nov 10, 2022
8fcd376
chore: update icons deps too
joshuaellis Nov 10, 2022
18f7269
Merge branch 'main' into feature/relations-reordering
joshuaellis Nov 10, 2022
650877a
Merge branch 'feature/relations-reordering' into feat/relations-reord…
joshuaellis Nov 10, 2022
9481178
chore: update snapshots
joshuaellis Nov 11, 2022
ea5325b
compact max order and adjacent sql queries into one
Marc-Roig Nov 11, 2022
515bb5e
Merge pull request #14709 from strapi/relations-reordering/fractional…
Marc-Roig Nov 14, 2022
5a2effd
fix: design amends
joshuaellis Nov 14, 2022
c062b84
chore: update snapshots
joshuaellis Nov 14, 2022
d5081ae
chore: rename prop
joshuaellis Nov 14, 2022
b3a69e5
chore: replace wrapper with Stack
joshuaellis Nov 14, 2022
c13bf25
chore: fix tests
joshuaellis Nov 14, 2022
e26dbe6
fix: responsive styles
joshuaellis Nov 14, 2022
f4b0bd9
Merge branch 'main' into feature/relations-reordering
joshuaellis Nov 15, 2022
5fb4c10
chore: fix alignment of tags
joshuaellis Nov 15, 2022
4650013
chore: update snapshots
joshuaellis Nov 15, 2022
c9e9b1c
chore: update snapshots
joshuaellis Nov 15, 2022
fca4394
Merge pull request #14811 from strapi/feat/relations-reordering/keybo…
joshuaellis Nov 15, 2022
19367d0
Merge branch 'main' into feature/relations-reordering
joshuaellis Nov 15, 2022
e241d0c
feat: add relations reordering api requirements
joshuaellis Nov 15, 2022
1bde22f
chore: reverse connect array to ensure relations are connected before…
joshuaellis Nov 15, 2022
78a5022
chore: add code comment
joshuaellis Nov 15, 2022
505ad91
Merge pull request #14874 from strapi/feat/save-relations-reordering
joshuaellis Nov 15, 2022
737b577
Merge branch 'main' into feature/relations-reordering
Marc-Roig Nov 16, 2022
6df6c94
remove sorting from orderer
Marc-Roig Nov 16, 2022
980624f
add sorting
Marc-Roig Nov 17, 2022
c568197
Merge branch 'main' into feature/relations-reordering
Marc-Roig Nov 18, 2022
a6993fe
Merge branch 'feature/relations-reordering' into relations-reordering…
Marc-Roig Nov 18, 2022
457a6d1
Merge branch 'main' into feature/relations-reordering
joshuaellis Nov 18, 2022
f818488
chore: update snapshots
joshuaellis Nov 18, 2022
5fad917
add reordering in attachRelations
Marc-Roig Nov 20, 2022
114d67a
fix adjacentRelations query to look for joinColum name
Marc-Roig Nov 20, 2022
c3c8f40
remove adjacent relations sorting as it is not necessary
Marc-Roig Nov 20, 2022
75325fc
relations reordering api tests
Marc-Roig Nov 20, 2022
b0091aa
test for failing tests
Marc-Roig Nov 21, 2022
6a1100b
fix tests
Marc-Roig Nov 21, 2022
c95f2eb
remove describe only
Marc-Roig Nov 21, 2022
37d50e0
Merge branch 'main' into feature/relations-reordering
Marc-Roig Nov 22, 2022
e1810df
Merge branch 'feature/relations-reordering' into relations-reordering…
Marc-Roig Nov 22, 2022
37b1ab9
fix typo
Marc-Roig Nov 22, 2022
67beb86
test before after when creating relations
Marc-Roig Nov 22, 2022
e48a54d
remove repeated id in connect
Marc-Roig Nov 23, 2022
6f183c3
remove maxOrder from jsdoc
Marc-Roig Nov 23, 2022
6d49a36
rename to relations-orderer
Marc-Roig Nov 23, 2022
75c6f50
Merge pull request #14943 from strapi/relations-reordering/database-l…
Marc-Roig Nov 23, 2022
15460a6
Merge branch 'main' into feature/relations-reordering
Marc-Roig Nov 23, 2022
92de6e2
refactor: component architecture
joshuaellis Nov 23, 2022
b370841
test: add todo test suite for repeatable components
joshuaellis Nov 23, 2022
1b42e84
test: add Accordion tests
joshuaellis Nov 24, 2022
8a217a6
test: add Component tests
joshuaellis Nov 24, 2022
69546bd
test: add RepeatableComponent/index tests
joshuaellis Nov 24, 2022
ac78458
feat: add drag and drop abstractions and live text to repeatable comp…
joshuaellis Nov 24, 2022
9bb4dbf
test: add tests for useKeyboardDragAndDrop
joshuaellis Nov 24, 2022
93c1ba5
fix: add displayed value to drag layer
joshuaellis Nov 25, 2022
c73971f
allow connecting the same id multiple times
Marc-Roig Nov 25, 2022
eadfa98
fix: border styles
joshuaellis Nov 25, 2022
c1fd9f6
fix: active border issue
joshuaellis Nov 25, 2022
51ec518
chore: update snapshots
joshuaellis Nov 25, 2022
d417a59
fix: css specification
joshuaellis Nov 25, 2022
cfac329
Merge pull request #14994 from strapi/chore/refactor-d&d-repeatable-c…
joshuaellis Nov 25, 2022
3947f83
Merge branch 'main' into feature/relations-reordering
joshuaellis Nov 25, 2022
e82290b
Merge branch 'feature/relations-reordering' of https://github.com/str…
joshuaellis Nov 25, 2022
729ddd6
Merge pull request #15005 from strapi/relations-reordering/connect-sa…
Marc-Roig Dec 1, 2022
a148b81
Merge branch 'main' into feature/relations-reordering
Marc-Roig Dec 1, 2022
36ad92b
sort connect array
Marc-Roig Dec 1, 2022
05e09ee
test disordered relations with initial relations
Marc-Roig Dec 1, 2022
b0e37e8
sort connect array only when necessary
Marc-Roig Dec 5, 2022
2c854a0
change connect array error string to be more clear
Marc-Roig Dec 5, 2022
23bf8c5
Merge branch 'main' into feature/relations-reordering
joshuaellis Dec 5, 2022
f0b851a
feat(wip): add dnd to dynamic zones
joshuaellis Nov 25, 2022
0de7814
fix: dynamic zones not understanding which one is being dragged all t…
joshuaellis Nov 25, 2022
1ccb354
refactor tests to have more clear descriptions
Marc-Roig Dec 5, 2022
9b344ab
feat: add accessible live text to D&D implementation
joshuaellis Dec 5, 2022
9799b7c
chore: merge main
joshuaellis Dec 5, 2022
02a23a0
rephrase connect relation error
Marc-Roig Dec 5, 2022
968aec8
chore: pr amends
joshuaellis Dec 5, 2022
b041dcb
chore: tweak action placement
joshuaellis Dec 6, 2022
04a6cd4
fix: use __temp_key__ for when you've just added components and there…
joshuaellis Dec 6, 2022
15cd86c
fix: button background
joshuaellis Dec 6, 2022
32a95de
test: fix reducer tests
joshuaellis Dec 6, 2022
f1a47fb
Merge pull request #15079 from strapi/feat/dynamic-zone-dragndrop
joshuaellis Dec 6, 2022
ac0ec44
RelationInputDataManager: Fix crash when relation is inside a component
gu-stav Dec 7, 2022
56ef872
Merge pull request #15110 from strapi/fix/relation-input-data-manager…
gu-stav Dec 7, 2022
cb9b8e0
chore: add documentation for useDragAndDrop hook
joshuaellis Dec 7, 2022
30dc764
chore: add link to firefox bug
joshuaellis Dec 8, 2022
223d119
Merge pull request #15113 from strapi/chore/document-reordering
joshuaellis Dec 8, 2022
c5ec5bd
fix: remove overflow hidden to allow react-select menu to break out o…
joshuaellis Dec 8, 2022
fee3724
Update packages/core/admin/admin/src/content-manager/components/Relat…
joshuaellis Dec 8, 2022
1bd99c8
chore: fix lint
joshuaellis Dec 8, 2022
2383242
chore: update snapshots
joshuaellis Dec 8, 2022
75d8cac
fix: reset `gridTemplateColumns` otherwise component breaks out of grid
joshuaellis Dec 8, 2022
b7c609c
chore: update snapshots
joshuaellis Dec 8, 2022
5de5a3b
Merge pull request #15123 from strapi/fix/overflows-in-repeatable-com…
joshuaellis Dec 8, 2022
0e0174e
Merge pull request #15126 from strapi/fix/growing-relation-input
joshuaellis Dec 8, 2022
f1f4634
fix: add specific preview for RelationItem to DragLayer
joshuaellis Dec 8, 2022
17dc00d
fix: issue with scrolling and removing items
joshuaellis Dec 8, 2022
4eb5db8
test: edit them
joshuaellis Dec 8, 2022
e3b74f5
fix: tighten key on relation input & reset empty drag preview when in…
joshuaellis Dec 9, 2022
b01fc82
validate sort array
Marc-Roig Dec 9, 2022
230bf81
Merge pull request #15130 from strapi/fix/relation-drag-display
joshuaellis Dec 9, 2022
c51cde2
Merge branch 'main' into feature/relations-reordering
joshuaellis Dec 9, 2022
65e41f1
fix: indicies were wrong way round
joshuaellis Dec 12, 2022
dfb04d3
test: fix test
joshuaellis Dec 12, 2022
632a815
Merge pull request #15152 from strapi/fix/re-ordering-indicies
joshuaellis Dec 12, 2022
32bb3a0
throw validation error when passing wrong relations
Marc-Roig Dec 12, 2022
36e5ac0
fix: fix previews by not providing ref to the leftover element
joshuaellis Dec 12, 2022
2c1a864
chore: update docs
joshuaellis Dec 12, 2022
2639f84
add strict configuration
Marc-Roig Dec 12, 2022
e9f1361
Merge pull request #15157 from strapi/fix/dnd-preview
joshuaellis Dec 13, 2022
865b5d1
Merge branch 'main' into feature/relations-reordering
joshuaellis Dec 13, 2022
1dc0f93
add content manager relations reordering tests
Marc-Roig Dec 14, 2022
c0c3365
Update yarn.lock
Convly Dec 14, 2022
2d6650c
fix: pass an id to manually manage "isDragging"
joshuaellis Dec 15, 2022
ca52f1e
update yarn lock
Marc-Roig Dec 16, 2022
12c676d
fix: add dropSensitivity prop
joshuaellis Dec 16, 2022
1ad8af4
Merge pull request #15192 from strapi/fix/relation-reordering-scrolling
joshuaellis Dec 16, 2022
5895713
change strictConnect to strict
Marc-Roig Dec 20, 2022
7135f60
fix: expect normal throw
Marc-Roig Dec 20, 2022
7f37735
Merge branch 'main' into feature/relations-reordering
gu-stav Dec 21, 2022
499cff8
Chore: Update snapshot tests
gu-stav Dec 21, 2022
df2313e
improve read-only state
ronronscelestes Dec 22, 2022
470c1c9
fix(content-manager): reordering dynamicZones and repeatable components
jhoward1994 Dec 22, 2022
6d49a3c
updated tests
ronronscelestes Dec 23, 2022
0d2b5c7
chore(content-manager): prop types and fix existing tests
jhoward1994 Dec 23, 2022
9e43428
UI feedback fix
ronronscelestes Dec 27, 2022
ef9c911
Merge branch 'main' into feature/relations-reordering
Marc-Roig Jan 2, 2023
2528ae5
Merge branch 'feature/relations-reordering' into relations-reordering…
Marc-Roig Jan 2, 2023
26fba9f
test for circular references
Marc-Roig Jan 3, 2023
c5ebf3e
Merge branch 'main' into feature/relations-reordering
joshuaellis Jan 3, 2023
c028712
test(content-manager): RelationInputDataManager select to use source …
jhoward1994 Jan 3, 2023
70fe7d2
chore: fix snapshot
joshuaellis Jan 3, 2023
621a161
remove constraints list
Marc-Roig Jan 3, 2023
030c109
better circular reference error handling
Marc-Roig Jan 3, 2023
9f6e79e
chore(content-manager)
jhoward1994 Jan 3, 2023
99bd18d
Merge pull request #15260 from strapi/fix/RelationInput-read-only
joshuaellis Jan 4, 2023
8bf254f
move computeRelation outside for each
Marc-Roig Jan 5, 2023
d2bb3e2
fix(content-manager): EditViewDataManagerProvider load relations usin…
jhoward1994 Jan 5, 2023
48a8cba
chore(content-manager): revert unnecessary changes
jhoward1994 Jan 5, 2023
a05bdd8
change firstSeen to mappedRelationsIndex
Marc-Roig Jan 5, 2023
74e3870
test(content-manager): EditViewDataManagerProvider/reducer
jhoward1994 Jan 5, 2023
b2ca1f0
fix(content-manager): use __temp_key__ indexing with deeply nested co…
jhoward1994 Jan 6, 2023
7c6ff32
test(content-manager): cover nested content cases
jhoward1994 Jan 6, 2023
b6d570a
chore(content-manager): refactor
jhoward1994 Jan 6, 2023
148d6f0
chore(content-manager): code cleanup
jhoward1994 Jan 6, 2023
71ce491
fix(content-manager): compute path to initialData in RelationInputDat…
jhoward1994 Jan 9, 2023
143ddc8
chore(content-manager)
jhoward1994 Jan 9, 2023
b81cfb0
do not allow to connect same relation more than once
Marc-Roig Jan 10, 2023
5a185f1
delete duplicate relation in connect array
Marc-Roig Jan 10, 2023
610ce2a
simplify marking relation as computed
Marc-Roig Jan 10, 2023
168fe77
fix(content-manager): initial data indexing
jhoward1994 Jan 10, 2023
8d6a247
fix: tidy-up name solving function and handle refetching and removing
joshuaellis Jan 10, 2023
99f1b3a
Merge branch 'main' into feature/relations-reordering
joshuaellis Jan 10, 2023
a9d4816
fix: cleanData can now check the original location of the relation fo…
joshuaellis Jan 10, 2023
18216ca
tests: fix em
joshuaellis Jan 10, 2023
985e193
Update packages/core/admin/admin/src/content-manager/components/EditV…
joshuaellis Jan 11, 2023
4dc6e5e
Merge pull request #15396 from strapi/fix/dynamic-zone-reorderings
joshuaellis Jan 11, 2023
5ef73b2
test(content-manager): update snapshot
jhoward1994 Jan 11, 2023
8677580
test(content-manager): utils/paths getInitialDataPathUsingTempKeys
jhoward1994 Jan 11, 2023
031640e
Merge pull request #15265 from strapi/fix/reorder-dz-and-repeatbles-r…
joshuaellis Jan 11, 2023
51cd3dd
rename arr to computedRelations
Marc-Roig Jan 11, 2023
cf01fb8
make strict connect explicit when inserting relation
Marc-Roig Jan 11, 2023
312cc2b
Merge pull request #15158 from strapi/relations-reordering/non-strict…
Marc-Roig Jan 11, 2023
d59bcb3
docs: add doc for use callback ref
joshuaellis Jan 11, 2023
6d3f6dc
chore: use strict false for relations
joshuaellis Jan 11, 2023
85cb3be
Merge pull request #15414 from strapi/chore/add-strict-and-docs
joshuaellis Jan 12, 2023
9ec14bc
chore: revert strict additon
joshuaellis Jan 12, 2023
55e6274
fix: use box to contain ellipse
joshuaellis Jan 16, 2023
990ffe4
fix: use all-scroll on IconButton
joshuaellis Jan 16, 2023
194e9da
chore: update snapshot
joshuaellis Jan 16, 2023
09d05be
Merge pull request #15459 from strapi/fix/relations-with-long-names-s…
joshuaellis Jan 16, 2023
0f3b1c0
Merge branch 'main' into feature/relations-reordering
joshuaellis Jan 16, 2023
00cf8c8
Merge branch 'feature/relations-reordering' of https://github.com/str…
joshuaellis Jan 16, 2023
ac1374f
Merge pull request #15460 from strapi/fix/relation-cursors
joshuaellis Jan 16, 2023
92f381d
fix: add shadow
joshuaellis Jan 16, 2023
afb678a
feat: add active to keyboard hook to stop it working when not active
joshuaellis Jan 16, 2023
e7ec0e9
chore: fix tests
joshuaellis Jan 16, 2023
66b25a9
fix: reverse data from BE to show relations in same order
joshuaellis Jan 16, 2023
4a424b8
Merge pull request #15461 from strapi/fix/relations-reordering-keyboa…
joshuaellis Jan 16, 2023
88dfb19
fix: use grids but not min-content, just 100%
joshuaellis Jan 16, 2023
273869f
fix: the indicator should not be a dropdown according to their spec
joshuaellis Jan 16, 2023
769e5a7
Merge pull request #15464 from strapi/fix/list-view-relation-multiple
joshuaellis Jan 16, 2023
fbadfe4
fix: change shadow and add fade to avoid harshness
joshuaellis Jan 16, 2023
f6537ca
chore: update snapshot
joshuaellis Jan 16, 2023
0e5ed50
chore: fix tests
joshuaellis Jan 16, 2023
47e9074
Merge pull request #15465 from strapi/fix/react-select-input-grids
joshuaellis Jan 16, 2023
d4fee96
chore: update snapshots
joshuaellis Jan 16, 2023
b1f9c61
chore: update snapshots
joshuaellis Jan 16, 2023
a989fd7
Merge pull request #15466 from strapi/fix/react-select-menu-closing
joshuaellis Jan 16, 2023
3cba5b7
Merge pull request #15467 from strapi/fix/relations-list-shadow
joshuaellis Jan 16, 2023
fb358ef
Merge branch 'main' into feature/relations-reordering
joshuaellis Jan 16, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 44 additions & 0 deletions docs/docs/core/content-manager/hooks/use-callback-ref.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
title: useCallbackRef
description: API reference for the useCallbackRef hook in Strapi's Content Manager
tags:
- content-manager
- hooks
- refs
- callbacks
- effects
---

A custom hook that converts a callback to a ref to avoid triggering re-renders when passed as a
prop or avoid re-executing effects when passed as a dependency. Helpful for working with `modifiedData`
or `initialData` in the content-manager.

Stolen from [`@radix-ui/react-use-callback-ref`](https://www.npmjs.com/package/@radix-ui/react-use-callback-ref).

## Usage

```jsx
import { useCallbackRef } from 'path/to/hooks';

const MyComponent = ({ callbackFromSomewhere }) => {
const mySafeCallback = useCallbackRef(callbackFromSomewhere);

useEffect(() => {
const handleKeyDown = (event) => {
mySafeCallback(event);
};

document.addEventListener('keydown', handleKeyDown);

return () => document.removeEventListener('keydown', handleKeyDown);
}, [mySafeCallback]);

return <div>{children}</div>;
};
```

## Typescript

```ts
function useCallbackRef<T extends (...args: any[]) => any>(callback: T | undefined): T;
```
220 changes: 220 additions & 0 deletions docs/docs/core/content-manager/hooks/use-drag-and-drop.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
---
title: useDragAndDrop
slug: /content-manager/hooks/use-drag-and-drop
description: API reference for the useDragAndDrop hook in Strapi's Content Manager
tags:
- content-manager
- hooks
- drag-and-drop
---

An abstraction around `react-dnd`'s `useDrag` and `useDrop` hooks. It provides a simple API to handle drag and drop
events maintaining the same behaviour across the application e.g. when we consider the item to be above a new drop zone.

This hook also wraps an internal hook `useKeyboardDragAndDrop` which implements keyboard accessibile drag and drop by
returning an onKeyDown handler to be passed to the component's drag icon button.

## Usage

:::note
The following examples assume that you have already set up the `DndProvider` with `HTML5Backend` in your application and
that you are somewhat familiar with `@strapi/design-system` components.
:::

### Basic usage

Below is a basic example usage where we're not interested in rendering custom previews in the DragLayer. However, we do replace
the current item with a placeholder.

```jsx
import { Box, Flex, IconButton } from '@strapi/design-system';
import { Drag } from '@strapi/icons';

import { useDragAndDrop } from 'path/to/hooks';
import { composeRefs } from 'path/to/utils';

import { Placeholder } from './Placeholder';

const MyComponent = ({ onMoveItem }) => {
const [{ handlerId, isDragging, handleKeyDown }, myRef, dropRef, dragRef] = useDragAndDrop(true, {
type: 'my-type',
index,
onMoveItem,
});

const composedRefs = composeRefs(myRef, dragRef);

return (
<Box ref={dropRef} cursor={'all-scroll'}>
{isDragging ? (
<Placeholder />
) : (
<Flex ref={composedRefs} data-handler-id={handlerId}>
<IconButton
forwardedAs="div"
role="button"
tabIndex={0}
aria-label="Drag"
noBorder
onKeyDown={handleKeyDown}
>
<Drag />
</IconButton>
{'My item'}
</Flex>
)}
</Box>
);
};
```

### Using custom previews

The only really difference between the previous example and this one is
that we're using the `getEmptyImage` function from `react-dnd-html5-backend`.

```jsx
import { getEmptyImage } from 'react-dnd-html5-backend';
import { Box, Flex, IconButton } from '@strapi/design-system';
import { Drag } from '@strapi/icons';

import { useDragAndDrop } from 'path/to/hooks';
import { composeRefs } from 'path/to/utils';

import { Placeholder } from './Placeholder';

const MyComponent = ({ onMoveItem }) => {
const [{ handlerId, isDragging, handleKeyDown }, myRef, dropRef, dragRef, dragPreviewRef] =
useDragAndDrop(true, {
type: 'my-type',
index,
onMoveItem,
});

// highlight-start
useEffect(() => {
dragPreviewRef(getEmptyImage());
}, [dragPreviewRef]);
// highlight-end

const composedRefs = composeRefs(myRef, dragRef);

return (
<Box ref={dropRef} cursor={'all-scroll'}>
{isDragging ? (
<Placeholder />
) : (
<Flex ref={composedRefs} data-handler-id={handlerId}>
<IconButton
forwardedAs="div"
role="button"
tabIndex={0}
aria-label="Drag"
noBorder
onKeyDown={handleKeyDown}
>
<Drag />
</IconButton>
{'My item'}
</Flex>
)}
</Box>
);
};
```

## Typescript

```ts
import { Identifier } from 'dnd-core';
import { ConnectDropTarget, ConnectDragSource, ConnectDragPreview } from 'react-dnd';

interface UseDragAndDropOptions {
index: number;
onMoveItem: (newIndex: number, currentIndex: number) => void;
/**
* @default "regular"
* Defines whether the change in index should be immediately over another
* dropzone or half way over it (regular).
*/
dropSensitivity?: 'immediate' | 'regular';
item?: object;
/**
* @default 'STRAPI_DND'
*/
type?: string;
onCancel?: (index: number) => void;
onDropItem?: (index: number) => void;
onEnd?: () => void;
onGrabItem?: (index: number) => void;
onStart?: () => void;
}

type UseDragAndDropReturn = [
props: {
handlerId: Identifier;
isDragging: boolean;
handleKeyDown: (event: KeyboardEvent<HTMLButtonElement>) => void;
},
objectRef: React.RefObject<HTMLElement>,
dropRef: ConnectDropTarget,
dragRef: ConnectDragSource,
dragPreviewRef: ConnectDragPreview
];

type UseDragAndDrop = (active: boolean, options: UseDragAndDropOptions) => UseDragAndDropReturn;
```

## Accessibility

Its advised to implement a [live text region](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Live_Regions) in the
parent component holding your individual dnd children. This should be done to inform the user of the current state of the drag and drop.
To implement this, you need to pass the `onDropItem`, `onGrabItem` and `onCancel` callbacks to the `useDragAndDrop` hook which are fired
only with the purpose of updating the live region, hence why they're optional. You would also update the live region as part of your
`onMoveItem` callback. There are generic messages that can be used in the `intl` provider, an example of using this may look like:

```js
setLiveText(
formatMessage(
{
id: getTrad('dnd.drop-item'),
defaultMessage: `{item}, dropped. Final position in list: {position}.`,
},
{
item: 'my item',
position: 1,
}
)
);
```

## Further Reading

- [react-dnd docs](https://react-dnd.github.io/react-dnd/docs/overview)
- [useDrag API](https://react-dnd.github.io/react-dnd/docs/api/use-drag)
- [useDrop API](https://react-dnd.github.io/react-dnd/docs/api/use-drop)
- [useDragLayer API](https://react-dnd.github.io/react-dnd/docs/api/use-drag-layer)

## Troubleshooting

### Firefox quirks

You might notice in the [basic usage](#basic-usage) section this piece of code:

```jsx
<IconButton
forwardedAs="div"
role="button"
tabIndex={0}
aria-label="Drag"
noBorder
onKeyDown={handleKeyDown}
>
<Drag />
</IconButton>
```

In `firefox` the drag handler will not work if you click and drag when the element is a `button`, this is known [bug in the browser](bugzilla.mozilla.org/show_bug.cgi?id=568313).
Therefore the workaround is to use the `forwardedAs` prop to render a `div` instead of a `button`
and add the `role` and `tabIndex` props to make this accessible. The actual `IconButton` component
adds an accessible lable from the `aria-label` prop. So we don't have to concern ourselves with that.
16 changes: 16 additions & 0 deletions docs/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@ const sidebars = {
id: 'core/content-manager/intro',
},
items: [
{
type: 'category',
label: 'Hooks',
items: [
{
type: 'doc',
label: 'useCallbackRef',
id: 'core/content-manager/hooks/use-callback-ref',
},
{
type: 'doc',
label: 'useDragAndDrop',
id: 'core/content-manager/hooks/use-drag-and-drop',
},
],
},
{
type: 'doc',
label: 'Relations',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,33 @@
},
"enumeration": {
"type": "enumeration",
"enum": ["A", "B", "C", "D", "E"]
"enum": [
"A",
"B",
"C",
"D",
"E"
]
},
"single_media": {
"type": "media",
"multiple": false,
"required": false,
"allowedTypes": ["images", "files", "videos"]
"allowedTypes": [
"images",
"files",
"videos"
]
},
"multiple_media": {
"type": "media",
"multiple": true,
"required": false,
"allowedTypes": ["images", "files", "videos"]
"allowedTypes": [
"images",
"files",
"videos"
]
},
"json": {
"type": "json"
Expand All @@ -86,7 +100,10 @@
},
"dynamiczone": {
"type": "dynamiczone",
"components": ["basic.simple"]
"components": [
"basic.simple",
"blog.test-como"
]
},
"one_way_tag": {
"type": "relation",
Expand Down Expand Up @@ -139,6 +156,13 @@
"type": "customField",
"regex": "^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$",
"customField": "plugin::color-picker.color"
},
"cats": {
"type": "dynamiczone",
"components": [
"basic.relation",
"basic.simple"
]
}
}
}
14 changes: 14 additions & 0 deletions examples/getstarted/src/components/basic/relation.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"collectionName": "components_basic_relations",
"info": {
"displayName": "Relation"
},
"options": {},
"attributes": {
"categories": {
"type": "relation",
"relation": "oneToMany",
"target": "api::category.category"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,7 @@ const ComponentInitializer = ({ error, isReadOnly, onClick }) => {
</Box>
{error?.id && (
<Typography textColor="danger600" variant="pi">
{formatMessage(
{
id: error.id,
defaultMessage: error.id,
},
{ ...error.values }
)}
{formatMessage(error, { ...error.values })}
</Typography>
)}
</>
Expand Down