Skip to content

Commit 1859cb5

Browse files
authored
Add DnD SSR tests and NextJS example (#3640)
1 parent 9e19dee commit 1859cb5

File tree

5 files changed

+91
-1
lines changed

5 files changed

+91
-1
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { Item, ListView, Text, useListData } from "@adobe/react-spectrum";
2+
import { useDragAndDrop } from "@react-spectrum/dnd";
3+
import Folder from "@spectrum-icons/illustrations/Folder";
4+
5+
export default function ReorderableListView() {
6+
let list = useListData({
7+
initialItems: [
8+
{ id: "1", type: "file", name: "Adobe Photoshop" },
9+
{ id: "2", type: "file", name: "Adobe XD" },
10+
{ id: "3", type: "folder", name: "Documents", childNodes: [] },
11+
{ id: "4", type: "file", name: "Adobe InDesign" },
12+
{ id: "5", type: "folder", name: "Utilities", childNodes: [] },
13+
{ id: "6", type: "file", name: "Adobe AfterEffects" },
14+
],
15+
});
16+
17+
// Append a generated key to the item type so they can only be reordered within this list and not dragged elsewhere.
18+
let { dragAndDropHooks } = useDragAndDrop({
19+
getItems(keys) {
20+
return [...keys].map((key) => {
21+
let item = list.getItem(key);
22+
// Setup the drag types and associated info for each dragged item.
23+
return {
24+
"custom-app-type-reorder": JSON.stringify(item),
25+
"text/plain": item.name,
26+
};
27+
});
28+
},
29+
acceptedDragTypes: ["custom-app-type-reorder"],
30+
onReorder: async (e) => {
31+
let { keys, target, dropOperation } = e;
32+
33+
if (target.dropPosition === "before") {
34+
list.moveBefore(target.key, [...keys]);
35+
} else if (target.dropPosition === "after") {
36+
list.moveAfter(target.key, [...keys]);
37+
}
38+
},
39+
getAllowedDropOperations: () => ["move"],
40+
});
41+
42+
return (
43+
<ListView
44+
aria-label="Reorderable ListView"
45+
selectionMode="multiple"
46+
width="size-3600"
47+
height="size-3600"
48+
items={list.items}
49+
dragAndDropHooks={dragAndDropHooks}
50+
>
51+
{(item) => (
52+
<Item textValue={item.name}>
53+
{item.type === "folder" && <Folder />}
54+
<Text>{item.name}</Text>
55+
</Item>
56+
)}
57+
</ListView>
58+
);
59+
}

examples/rsp-next-ts/next.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const withTM = require("next-transpile-modules")([
1313
"@react-spectrum/datepicker",
1414
"@react-spectrum/dialog",
1515
"@react-spectrum/divider",
16+
"@react-spectrum/dnd",
1617
"@react-spectrum/form",
1718
"@react-spectrum/icon",
1819
"@react-spectrum/illustratedmessage",

examples/rsp-next-ts/pages/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ import {
7979
ColorSlider,
8080
ColorWheel,
8181
} from "@react-spectrum/color";
82+
import ReorderableListView from "../components/ReorderableListView";
8283

8384
export default function Home() {
8485
let [isDialogOpen, setIsDialogOpen] = useState(false);
@@ -134,6 +135,7 @@ export default function Home() {
134135
<Item>Adobe Illustrator</Item>
135136
<Item>Adobe Lightroom</Item>
136137
</ListView>
138+
<ReorderableListView />
137139
<MenuTrigger>
138140
<ActionButton>Menu</ActionButton>
139141
<Menu onAction={(key) => alert(key)}>

examples/rsp-next-ts/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"resolveJsonModule": true,
1414
"isolatedModules": true,
1515
"jsx": "preserve",
16-
"incremental": true
16+
"incremental": true,
17+
"downlevelIteration": true
1718
},
1819
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
1920
"exclude": ["node_modules"]
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2020 Adobe. All rights reserved.
3+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License. You may obtain a copy
5+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under
8+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9+
* OF ANY KIND, either express or implied. See the License for the specific language
10+
* governing permissions and limitations under the License.
11+
*/
12+
13+
import {testSSR} from '@react-spectrum/test-utils';
14+
15+
describe('useDrag and useDrop SSR', function () {
16+
it('should render without errors', async function () {
17+
await testSSR(__filename, `
18+
import {Draggable, Droppable} from './examples';
19+
import React from 'react';
20+
21+
<>
22+
<Draggable />
23+
<Droppable />
24+
</>
25+
`);
26+
});
27+
});

0 commit comments

Comments
 (0)