|
| 1 | +'use client'; |
| 2 | +/* eslint-disable react-hooks/exhaustive-deps */ |
| 3 | +/* eslint-disable react/hook-use-state */ |
| 4 | +/* eslint-disable no-plusplus */ |
| 5 | +import React, { useEffect, useRef, useState } from 'react'; |
| 6 | +import type { AllPaths } from 'skyroc-type-utils'; |
| 7 | + |
| 8 | +import type { InternalFormInstance } from './FieldContext'; |
| 9 | +import { useFieldContext } from './FieldContext'; |
| 10 | +import type { StoreValue } from './types/formStore'; |
| 11 | +import { keyOfName } from './utils/util'; |
| 12 | + |
| 13 | +export type ListRenderItem = { |
| 14 | + key: string; |
| 15 | + name: string; |
| 16 | +}; |
| 17 | + |
| 18 | +export type ListProps<Values = any> = { |
| 19 | + // array path |
| 20 | + children: ( |
| 21 | + fields: ListRenderItem[], |
| 22 | + ops: { |
| 23 | + insert: (index: number, item: any) => void; |
| 24 | + move: (from: number, to: number) => void; |
| 25 | + remove: (index: number) => void; |
| 26 | + replace: (index: number, val: any) => void; |
| 27 | + swap: (i: number, j: number) => void; |
| 28 | + } |
| 29 | + ) => React.ReactNode; |
| 30 | + initialValue?: StoreValue[]; |
| 31 | + name: AllPaths<Values>; |
| 32 | +}; |
| 33 | + |
| 34 | +function move<T>(arr: T[], from: number, to: number): T[] { |
| 35 | + const clone = arr.slice(); |
| 36 | + const item = clone.splice(from, 1)[0]; |
| 37 | + clone.splice(to, 0, item); |
| 38 | + return clone; |
| 39 | +} |
| 40 | + |
| 41 | +function List<Values = any>(props: ListProps<Values>) { |
| 42 | + const { children, initialValue, name } = props; |
| 43 | + |
| 44 | + const fieldContext = useFieldContext<Values>(); |
| 45 | + |
| 46 | + const keyRef = useRef({ id: 0, keys: [] as number[] }); |
| 47 | + const keyManager = keyRef.current; |
| 48 | + |
| 49 | + const { |
| 50 | + getFieldsValue, |
| 51 | + getFieldValue, |
| 52 | + getInternalHooks, |
| 53 | + validateTrigger: fieldValidateTrigger |
| 54 | + } = fieldContext as unknown as InternalFormInstance<Values>; |
| 55 | + |
| 56 | + const { dispatch, registerField } = getInternalHooks(); |
| 57 | + |
| 58 | + const [_, forceUpdate] = useState({}); |
| 59 | + |
| 60 | + const arr = (getFieldValue(name) as any[]) || initialValue || []; |
| 61 | + |
| 62 | + const fields = arr.map((___, i) => { |
| 63 | + let key = keyManager.keys[i]; |
| 64 | + if (key === undefined) { |
| 65 | + keyManager.keys[i] = keyManager.id++; |
| 66 | + key = keyManager.keys[i]; |
| 67 | + } |
| 68 | + return { |
| 69 | + key: String(key), // 稳定 key |
| 70 | + name: `${keyOfName(name)}.${i}` |
| 71 | + }; |
| 72 | + }); |
| 73 | + |
| 74 | + const unregisterRef = useRef<() => void>(null); |
| 75 | + |
| 76 | + if (!unregisterRef.current) { |
| 77 | + unregisterRef.current = registerField({ |
| 78 | + changeValue: (newValue, __, ___, mask) => { |
| 79 | + forceUpdate({}); |
| 80 | + }, |
| 81 | + initialValue, |
| 82 | + name, |
| 83 | + preserve: true |
| 84 | + }); |
| 85 | + } |
| 86 | + |
| 87 | + useEffect(() => { |
| 88 | + return () => { |
| 89 | + unregisterRef.current?.(); |
| 90 | + }; |
| 91 | + }, []); |
| 92 | + |
| 93 | + const ops = { |
| 94 | + insert: (index: number, item: any) => { |
| 95 | + dispatch({ args: { index, item }, name, op: 'insert', type: 'arrayOp' }); |
| 96 | + keyManager.keys.splice(index, 0, keyManager.id++); |
| 97 | + }, |
| 98 | + move: (from: number, to: number) => { |
| 99 | + dispatch({ args: { from, to }, name, op: 'move', type: 'arrayOp' }); |
| 100 | + keyManager.keys = move(keyManager.keys, from, to); |
| 101 | + }, |
| 102 | + remove: (index: number) => { |
| 103 | + dispatch({ args: { index }, name, op: 'remove', type: 'arrayOp' }); |
| 104 | + keyManager.keys.splice(index, 1); |
| 105 | + }, |
| 106 | + replace: (index: number, val: any) => { |
| 107 | + dispatch({ args: { index, item: val }, name, op: 'replace', type: 'arrayOp' }); |
| 108 | + }, |
| 109 | + swap: (i: number, j: number) => { |
| 110 | + dispatch({ args: { i, j }, name, op: 'swap', type: 'arrayOp' }); |
| 111 | + [keyManager.keys[i], keyManager.keys[j]] = [keyManager.keys[j], keyManager.keys[i]]; |
| 112 | + } |
| 113 | + }; |
| 114 | + |
| 115 | + return <>{children(fields, ops)}</>; |
| 116 | +} |
| 117 | + |
| 118 | +export default List; |
0 commit comments