|
| 1 | +'use client'; |
| 2 | +/* eslint-disable no-bitwise */ |
| 3 | + |
| 4 | +import { createContext, useContext, useEffect, useRef, useState } from 'react'; |
| 5 | +import { flushSync } from 'react-dom'; |
| 6 | +import type { AllPaths, PathToDeepType, ShapeFromPaths } from 'skyroc-type-utils'; |
| 7 | + |
| 8 | +import type { ChangeMask, SubscribeMaskOptions } from './form-core/event'; |
| 9 | +import { toMask } from './form-core/event'; |
| 10 | +import type { Action, Middleware } from './form-core/middleware'; |
| 11 | +import type { FieldEntity } from './form-core/types'; |
| 12 | +import type { ValidateMessages } from './form-core/validate'; |
| 13 | +import type { Rule } from './form-core/validation'; |
| 14 | +import type { FormState } from './types'; |
| 15 | +import type { Meta } from './types/shared-types'; |
| 16 | + |
| 17 | +export interface ValuesOptions<Values = any> { |
| 18 | + getFieldsValue: <K extends AllPaths<Values, number>[]>(...name: K) => ShapeFromPaths<Values, K>; |
| 19 | + getFieldValue: <T extends AllPaths<Values>>(name: T) => PathToDeepType<Values, T>; |
| 20 | + setFieldsValue: (values: Partial<Values>) => void; |
| 21 | + setFieldValue: <T extends AllPaths<Values>>(name: T, value: PathToDeepType<Values, T>) => void; |
| 22 | +} |
| 23 | + |
| 24 | +export interface StateOptions<Values = any> { |
| 25 | + getField: <T extends AllPaths<Values>>(name: T) => Meta<T, PathToDeepType<Values, T>>; |
| 26 | + getFieldError: (name: AllPaths<Values>) => string[]; |
| 27 | + getFieldsError: (...name: AllPaths<Values>[]) => Record<AllPaths<Values>, string[]>; |
| 28 | + getFieldsWarning: (...name: AllPaths<Values>[]) => Record<AllPaths<Values>, string[]>; |
| 29 | + getFieldWarning: (name: AllPaths<Values>) => string[]; |
| 30 | + getFormState: () => FormState; |
| 31 | + isFieldsTouched: (...name: AllPaths<Values>[]) => boolean; |
| 32 | + isFieldsValidating: (...name: AllPaths<Values>[]) => boolean; |
| 33 | + isFieldTouched: (name: AllPaths<Values>) => boolean; |
| 34 | + isFieldValidating: (name: AllPaths<Values>) => boolean; |
| 35 | + subscribeField: <T extends AllPaths<Values>>( |
| 36 | + name: T, |
| 37 | + cb: (value: PathToDeepType<Values, T>, key: T, all: Values, fired: ChangeMask) => void, |
| 38 | + opt?: { includeChildren?: boolean; mask?: ChangeMask } |
| 39 | + ) => () => void; |
| 40 | +} |
| 41 | + |
| 42 | +export interface OperationOptions<Values = any> { |
| 43 | + resetFields: (...names: AllPaths<Values>[]) => void; |
| 44 | + submit: () => void; |
| 45 | + use: (mw: Middleware) => void; |
| 46 | + validateField: (name: AllPaths<Values>) => Promise<boolean>; |
| 47 | + validateFields: (...names: AllPaths<Values>[]) => Promise<boolean>; |
| 48 | +} |
| 49 | + |
| 50 | +export interface RegisterCallbackOptions<Values = any> { |
| 51 | + onFieldsChange?: ( |
| 52 | + changedFields: Meta<AllPaths<Values>, PathToDeepType<Values, AllPaths<Values>>>[], |
| 53 | + allFields: Meta<AllPaths<Values>, PathToDeepType<Values, AllPaths<Values>>>[] |
| 54 | + ) => void; |
| 55 | + |
| 56 | + onFinish?: (values: Values) => void; |
| 57 | + |
| 58 | + onValuesChange?: (changedValues: Partial<Values>, values: Values) => void; |
| 59 | +} |
| 60 | + |
| 61 | +export interface InternalCallbacks<Values = any> { |
| 62 | + destroyForm: (clearOnDestroy?: boolean) => void; |
| 63 | + setCallbacks: (callbacks: RegisterCallbackOptions<Values>) => void; |
| 64 | + setInitialValues: (values: Partial<Values>) => void; |
| 65 | + setPreserve: (preserve: boolean) => void; |
| 66 | + setValidateMessages: (messages: ValidateMessages) => void; |
| 67 | +} |
| 68 | + |
| 69 | +export interface InternalFieldHooks<Values = any> { |
| 70 | + dispatch: (action: Action) => void; |
| 71 | + getInitialValue: (name: AllPaths<Values>) => PathToDeepType<Values, AllPaths<Values>>; |
| 72 | + registerField: (entity: FieldEntity) => () => void; |
| 73 | + setFieldRules: (name: AllPaths<Values>, rules?: Rule[]) => void; |
| 74 | +} |
| 75 | + |
| 76 | +export interface FormInstance<Values = any> |
| 77 | + extends ValuesOptions<Values>, |
| 78 | + StateOptions<Values>, |
| 79 | + OperationOptions<Values> {} |
| 80 | + |
| 81 | +export interface InternalFormHooks<Values = any> extends InternalCallbacks<Values>, InternalFieldHooks<Values> {} |
| 82 | + |
| 83 | +export interface InternalFormInstance<Values = any> extends FormInstance<Values> { |
| 84 | + /** 内部 API,不建议外部使用 */ |
| 85 | + getInternalHooks: () => InternalFormHooks<Values>; |
| 86 | +} |
| 87 | + |
| 88 | +export const FieldContext = createContext<FormInstance | null>(null); |
| 89 | + |
| 90 | +export const FieldContextProvider = FieldContext.Provider; |
| 91 | + |
| 92 | +export const useFieldContext = <Values = any>(): FormInstance<Values> => { |
| 93 | + const context = useContext(FieldContext); |
| 94 | + |
| 95 | + if (!context) { |
| 96 | + throw new Error('Can not find FormContext. Please make sure you wrap Field under Form.'); |
| 97 | + } |
| 98 | + |
| 99 | + return context; |
| 100 | +}; |
| 101 | + |
| 102 | +export const useFieldState = <Values = any>( |
| 103 | + name: AllPaths<Values>, |
| 104 | + mask: SubscribeMaskOptions = { |
| 105 | + errors: true, |
| 106 | + validated: true, |
| 107 | + validating: true, |
| 108 | + warnings: true |
| 109 | + } |
| 110 | +) => { |
| 111 | + const context = useFieldContext<Values>(); |
| 112 | + |
| 113 | + const state = useRef(context.getField(name)); |
| 114 | + |
| 115 | + // eslint-disable-next-line react/hook-use-state |
| 116 | + const [_, forceUpdate] = useState(0); |
| 117 | + |
| 118 | + if (!context) { |
| 119 | + throw new Error('Can not find FormContext. Please make sure you wrap Field under Form.'); |
| 120 | + } |
| 121 | + |
| 122 | + console.log('context', toMask(mask)); |
| 123 | + |
| 124 | + useEffect(() => { |
| 125 | + const unregister = context.subscribeField( |
| 126 | + name, |
| 127 | + () => { |
| 128 | + flushSync(() => { |
| 129 | + state.current = context.getField(name); |
| 130 | + forceUpdate(prev => prev + 1); |
| 131 | + }); |
| 132 | + }, |
| 133 | + { |
| 134 | + mask: toMask(mask) |
| 135 | + } |
| 136 | + ); |
| 137 | + |
| 138 | + return () => { |
| 139 | + unregister(); |
| 140 | + }; |
| 141 | + }, [context, name]); |
| 142 | + |
| 143 | + return state.current; |
| 144 | +}; |
| 145 | + |
| 146 | +export const useFieldErrors = <Values = any>(name: AllPaths<Values>) => { |
| 147 | + const state = useFieldState<Values>(name, { errors: true }); |
| 148 | + |
| 149 | + console.log('state', state); |
| 150 | + |
| 151 | + return state.errors; |
| 152 | +}; |
0 commit comments