-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1453 from merico-dev/1452-drag-to-re-arrange-arra…
…y-items 1452 drag to re arrange array items
- Loading branch information
Showing
11 changed files
with
384 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
dashboard/src/components/plugins/editor-components/field-array-tabs/tab-list.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { DragDropProvider } from '@dnd-kit/react'; | ||
import { useSortable } from '@dnd-kit/react/sortable'; | ||
import { Center, Tabs, Tooltip } from '@mantine/core'; | ||
import { IconGripHorizontal, IconPlus } from '@tabler/icons-react'; | ||
import { useBoolean } from 'ahooks'; | ||
import { ReactNode } from 'react'; | ||
import { ArrayPath, FieldValues, UseFieldArrayReturn } from 'react-hook-form'; | ||
import { ControlledField } from './types'; | ||
|
||
type DraggableTabProps = { | ||
index: number; | ||
value: string; | ||
children: ReactNode; | ||
}; | ||
|
||
const DraggableTab = ({ value, index, children }: DraggableTabProps) => { | ||
const [hovering, { setTrue, setFalse }] = useBoolean(false); | ||
const { ref, handleRef } = useSortable({ | ||
id: value, | ||
index, | ||
}); | ||
|
||
return ( | ||
<Tabs.Tab | ||
ref={ref} | ||
value={value} | ||
icon={<IconGripHorizontal size={14} color={hovering ? 'rgb(34, 139, 230)' : 'transparent'} />} | ||
onMouseEnter={setTrue} | ||
onMouseLeave={setFalse} | ||
> | ||
{children} | ||
</Tabs.Tab> | ||
); | ||
}; | ||
|
||
type Props<T extends FieldValues, FieldItem> = { | ||
fieldArray: UseFieldArrayReturn<T, ArrayPath<T>, 'id'>; | ||
addButtonText: string; | ||
add: () => void; | ||
renderTabName: (field: FieldItem, index: number) => ReactNode; | ||
controlledFields: ControlledField<T>[]; | ||
}; | ||
|
||
export const TabList = <T extends FieldValues, FieldItem>({ | ||
fieldArray, | ||
add, | ||
addButtonText, | ||
renderTabName, | ||
controlledFields, | ||
}: Props<T, FieldItem>) => { | ||
const onDragEnd = (event: any) => { | ||
const { source, target } = event.operation; | ||
const fromIndex = controlledFields.findIndex((f) => f.id === source.id); | ||
const toIndex = target.index; | ||
fieldArray.move(fromIndex, toIndex); | ||
}; | ||
|
||
return ( | ||
<Tabs.List> | ||
<DragDropProvider onDragEnd={onDragEnd}> | ||
{controlledFields.map((field, index) => ( | ||
<DraggableTab key={field.id} value={field.id} index={index}> | ||
{renderTabName(field, index)} | ||
</DraggableTab> | ||
))} | ||
</DragDropProvider> | ||
|
||
<Tabs.Tab onClick={add} value="add"> | ||
<Tooltip label={addButtonText}> | ||
<Center> | ||
<IconPlus size={18} color="#228be6" /> | ||
</Center> | ||
</Tooltip> | ||
</Tabs.Tab> | ||
</Tabs.List> | ||
); | ||
}; |
5 changes: 5 additions & 0 deletions
5
dashboard/src/components/plugins/editor-components/field-array-tabs/types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { ReactNode } from 'react'; | ||
import { ArrayPath, FieldArrayWithId, FieldValues, Path, PathValue } from 'react-hook-form'; | ||
|
||
export type ControlledField<T extends FieldValues> = FieldArrayWithId<T, ArrayPath<T>, 'id'> & PathValue<T, Path<T>>; | ||
export type FieldArrayTabsChildren<FieldItem> = ({ field, index }: { field: FieldItem; index: number }) => ReactNode; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "@devtable/root", | ||
"version": "13.19.0", | ||
"version": "13.20.0", | ||
"private": true, | ||
"workspaces": [ | ||
"api", | ||
|
Oops, something went wrong.