This repository was archived by the owner on Jun 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathListProperty.tsx
More file actions
117 lines (111 loc) · 3.3 KB
/
ListProperty.tsx
File metadata and controls
117 lines (111 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import { ArrayHelpers, FieldArray, useField } from 'formik';
import { ReactElement } from 'react';
import { DragDropContext, Draggable, Droppable } from 'react-beautiful-dnd';
import DeleteItemIcon from '../../../icons/ui/DeleteItemIcon';
import DragListIcon from '../../../icons/ui/DragItemIcon';
import CollapsibleList from '../../containers/CollapsibleList';
import { InspectorFieldProps } from './InspectorProperty';
export type ListPropertyProps = InspectorFieldProps & {
titleExpanded?: ReactElement;
values?: any;
description?: string;
expanded?: boolean;
emptyText?: string;
};
export type ListItemProps = {
index: number;
name: string;
arrayHelper: ArrayHelpers;
};
const ListItem = ({ index, name, arrayHelper }: ListItemProps) => {
return (
<Draggable key={index} draggableId={`${index}`} index={index}>
{(provided, snapshot) => (
<div
className="w-full mb-2 p-1 px-3 text-sm
bg-white border border-circle-gray-300 rounded-md2 flex flex-row"
ref={provided.innerRef}
{...provided.draggableProps}
>
<button
className="flex-1 cursor-pointer text-left text-circle-black leading-6"
type="button"
>
{name}
</button>
<div className="ml-auto mr-3" {...provided.dragHandleProps}>
<DragListIcon className="w-4 h-6 py-1" color="#AAAAAA" />
</div>
<button
onClick={() => {
arrayHelper.remove(index);
}}
type="button"
className="my-auto"
>
<DeleteItemIcon className="w-3 h-3" color="#AAAAAA" />
</button>
</div>
)}
</Draggable>
);
};
const ListProperty = ({
label,
values,
description,
emptyText,
...props
}: InspectorFieldProps & ListPropertyProps) => {
const [field] = useField(props);
return (
<CollapsibleList
title={label}
titleExpanded={props.titleExpanded}
expanded={props.expanded}
>
{values?.length > 0 ? (
<FieldArray
{...field}
name={props.name}
render={(arrayHelper) => (
<DragDropContext
onDragEnd={(result) => {
if (result.destination) {
arrayHelper.move(
result.source.index,
result.destination.index,
);
}
}}
>
<Droppable droppableId="droppable">
{(provided, snapshot) => (
<div
{...provided.droppableProps}
ref={provided.innerRef}
className="p-2 pr-0"
>
{Object.keys(values[0]).map((cmd: any, index: number) => (
<ListItem
name={cmd}
index={index}
arrayHelper={arrayHelper}
/>
))}
{provided.placeholder}
</div>
)}
</Droppable>
</DragDropContext>
)}
/>
) : (
<p className="ml-2 font-medium text-sm text-circle-gray-500">
{emptyText}
</p>
)}
</CollapsibleList>
);
};
export default ListProperty;