diff --git a/packages/fieldy/fieldy-react/package.json b/packages/fieldy/fieldy-react/package.json index 007422877..1ed8fc390 100644 --- a/packages/fieldy/fieldy-react/package.json +++ b/packages/fieldy/fieldy-react/package.json @@ -1,6 +1,6 @@ { "name": "@rxdrag/react-fieldy", - "version": "0.6.6", + "version": "0.6.7", "type": "module", "module": "index.ts", "files": [ diff --git a/packages/fieldy/fieldy-react/src/components/Field/index.tsx b/packages/fieldy/fieldy-react/src/components/Field/index.tsx index ff3993db7..8c5aca9c8 100644 --- a/packages/fieldy/fieldy-react/src/components/Field/index.tsx +++ b/packages/fieldy/fieldy-react/src/components/Field/index.tsx @@ -18,10 +18,10 @@ export const Field = ( } ) => { const { name, value, initialValue, defaultValue, children, type, rules } = props - const fieldMeta = useCreateFieldSchema(name, type, rules) + const fieldMeta = useCreateFieldSchema(name, type, defaultValue, rules) return ( - + {children} ) diff --git a/packages/fieldy/fieldy-react/src/components/VirtualForm.tsx b/packages/fieldy/fieldy-react/src/components/VirtualForm.tsx index aaabdd46f..c43853965 100644 --- a/packages/fieldy/fieldy-react/src/components/VirtualForm.tsx +++ b/packages/fieldy/fieldy-react/src/components/VirtualForm.tsx @@ -6,14 +6,13 @@ import { useFieldy } from "../hooks" export const VirtualForm = (props: { name?: string, initialValue?: FormValue | undefined, - defaultValue?: FormValue | undefined, value?: FormValue | undefined, onValueChange?: (value?: FormValue | undefined) => void, //表达式中用到的变量 expContext?: Record, children?: React.ReactNode }) => { - const { name, initialValue, defaultValue, value, children, onValueChange, expContext } = props + const { name, initialValue, value, children, onValueChange, expContext } = props const [form, setForm] = useState() const fieldy = useFieldy() useEffect(() => { @@ -36,11 +35,11 @@ export const VirtualForm = (props: { } }, [fieldy, form, initialValue]) - useEffect(() => { - if (fieldy && form) { - form.setDefaultValue(defaultValue) - } - }, [defaultValue, fieldy, form]) + // useEffect(() => { + // if (fieldy && form) { + // form.setDefaultValue(defaultValue) + // } + // }, [defaultValue, fieldy, form]) useEffect(() => { if (fieldy && form && value !== undefined) { diff --git a/packages/fieldy/fieldy-react/src/components/XField/hooks/useCreateFieldSchema.ts b/packages/fieldy/fieldy-react/src/components/XField/hooks/useCreateFieldSchema.ts index 052b6e249..457feee5b 100644 --- a/packages/fieldy/fieldy-react/src/components/XField/hooks/useCreateFieldSchema.ts +++ b/packages/fieldy/fieldy-react/src/components/XField/hooks/useCreateFieldSchema.ts @@ -4,7 +4,12 @@ import { useFieldPath } from "../../../hooks/useFieldPath" import { FieldType } from "@rxdrag/fieldy" import { IYupValidateSchema } from "@rxdrag/fieldy-yup-validation" -export function useCreateFieldSchema(name: string | number, type?: FieldType, rules?: IYupValidateSchema) { +export function useCreateFieldSchema( + name: string | number, + type: FieldType | undefined, + defaultValue: unknown | undefined, + rules: IYupValidateSchema | undefined, +) { const parentPath = useFieldPath() || "" const fieldPath = useMemo(() => { return parentPath ? parentPath + "." + name : name.toString() @@ -17,9 +22,10 @@ export function useCreateFieldSchema(name: string | number, type?: FieldType, ru path: fieldPath, type: type, name, + defaultValue, validateRules: rules, } - }, [fieldPath, name, rules, type]) + }, [defaultValue, fieldPath, name, rules, type]) return fieldSchema } \ No newline at end of file diff --git a/packages/fieldy/fieldy-react/src/components/XField/hooks/useFieldSchemas.ts b/packages/fieldy/fieldy-react/src/components/XField/hooks/useFieldSchemas.ts index 6287e8706..525d6a61f 100644 --- a/packages/fieldy/fieldy-react/src/components/XField/hooks/useFieldSchemas.ts +++ b/packages/fieldy/fieldy-react/src/components/XField/hooks/useFieldSchemas.ts @@ -11,7 +11,7 @@ const getPath = (parentPath: string, name: string) => { } function parseSchemas(fieldMeta: IFieldMeta, parentPath: string) { - const arr = (fieldMeta.name)?.split(".") || [] + const arr = (fieldMeta.name as string)?.split(".") || [] let currentPath = parentPath const schemas: IFieldSchema[] = [] for (let i = 0; i < arr.length; i++) { diff --git a/packages/fieldy/fieldy-react/src/components/XField/hooks/useRegisterField.ts b/packages/fieldy/fieldy-react/src/components/XField/hooks/useRegisterField.ts index f2df722ae..1722f34c7 100644 --- a/packages/fieldy/fieldy-react/src/components/XField/hooks/useRegisterField.ts +++ b/packages/fieldy/fieldy-react/src/components/XField/hooks/useRegisterField.ts @@ -20,7 +20,7 @@ export function useRegisterField(fieldMeta: IFieldMeta) { setField(field) } return () => { - for(const fieldSchema of fieldSchemas){ + for (const fieldSchema of fieldSchemas) { form.unregisterField(fieldSchema.path) } } diff --git a/packages/fieldy/fieldy-react/src/components/XField/index.tsx b/packages/fieldy/fieldy-react/src/components/XField/index.tsx index f50805e13..26b95778b 100644 --- a/packages/fieldy/fieldy-react/src/components/XField/index.tsx +++ b/packages/fieldy/fieldy-react/src/components/XField/index.tsx @@ -8,9 +8,9 @@ export const XField = memo((props: { children?: React.ReactNode, initialValue?: unknown, value?: unknown, - defaultValue?: unknown, }) => { - const { fieldMeta, initialValue, value, defaultValue, children } = props + const { fieldMeta, initialValue, value, children } = props + //defaultValue要在字段注册时附加,方式非受控组件的问题 const field = useRegisterField(fieldMeta) useEffect(() => { @@ -21,9 +21,9 @@ export const XField = memo((props: { value !== undefined && field?.setValue(value) }, [field, value]) - useEffect(() => { - defaultValue !== undefined && field?.setDefaultValue(defaultValue) - }, [field, defaultValue]) + // useEffect(() => { + // defaultValue !== undefined && field?.setDefaultValue(defaultValue) + // }, [field, defaultValue]) return ( diff --git a/packages/fieldy/fieldy/package.json b/packages/fieldy/fieldy/package.json index 7bf1d6fb9..5f084d50e 100644 --- a/packages/fieldy/fieldy/package.json +++ b/packages/fieldy/fieldy/package.json @@ -1,6 +1,6 @@ { "name": "@rxdrag/fieldy", - "version": "0.6.3", + "version": "0.6.4", "author": "Water.Li", "type": "module", "module": "index.ts", diff --git a/packages/fieldy/fieldy/src/actions/registry.ts b/packages/fieldy/fieldy/src/actions/registry.ts index ef9efd9c3..1e6445344 100644 --- a/packages/fieldy/fieldy/src/actions/registry.ts +++ b/packages/fieldy/fieldy/src/actions/registry.ts @@ -6,7 +6,7 @@ export const ADD_FORM_FIELDS = "fieldy/ADD_FORM_FIELDS" export const REMOVE_FORM_FIELD = "fieldy/REMOVE_FORM_FIELD" //export const SET_FORM_FLAT_VALUE = "fieldy/SET_FORM_FLAT_VALUE" export const SET_FORM_INITIAL_VALUE = "fieldy/SET_FORM_INITIAL_VALUE" -export const SET_FORM_DEFAULT_VALUE = "fieldy/SET_FORM_DEFAULT_VALUE" +//export const SET_FORM_DEFAULT_VALUE = "fieldy/SET_FORM_DEFAULT_VALUE" export const SET_FORM_VALUE = "fieldy/SET_FORM_VALUE" export const SET_FORM_INITIALIZED_FLAG = "fieldy/SET_FORM_INITIALIZED_FLAG" export const SET_FORM_FIELDS_FEEDBACKS = "fieldy/SET_FORM_FIELDS_FEEDBACKS" diff --git a/packages/fieldy/fieldy/src/classes/FieldyEngineImpl.ts b/packages/fieldy/fieldy/src/classes/FieldyEngineImpl.ts index b2f76a225..808d48e03 100644 --- a/packages/fieldy/fieldy/src/classes/FieldyEngineImpl.ts +++ b/packages/fieldy/fieldy/src/classes/FieldyEngineImpl.ts @@ -1,12 +1,12 @@ /* eslint-disable @typescript-eslint/no-unused-vars */ import { configureStore, Store } from "@reduxjs/toolkit"; import { invariant } from "@rxdrag/shared"; -import { ADD_FORM_FIELDS, CREATE_FORM, FormActionPayload, REMOVE_FORM, REMOVE_FORM_FIELD, SetFieldValuePayload, SET_FIELD_INITIAL_VALUE, SET_FIELD_VALUE, SET_FORM_INITIAL_VALUE, SET_FORM_VALUE, SetFieldStatePayload, SET_FIELD_STATE, SET_FORM_INITIALIZED_FLAG, SET_FORM_DEFAULT_VALUE, SET_FIELD_DEFAULT_VALUE, INPUT_FIELD_VALUE, SET_FORM_FIELDS_FEEDBACKS, SetFormFeedbacksPayload, IFieldFeedback, RESET_FORM, CLEAR_FIELD_ERRORS } from "../actions"; +import { ADD_FORM_FIELDS, CREATE_FORM, FormActionPayload, REMOVE_FORM, REMOVE_FORM_FIELD, SetFieldValuePayload, SET_FIELD_INITIAL_VALUE, SET_FIELD_VALUE, SET_FORM_INITIAL_VALUE, SET_FORM_VALUE, SetFieldStatePayload, SET_FIELD_STATE, SET_FORM_INITIALIZED_FLAG, SET_FIELD_DEFAULT_VALUE, INPUT_FIELD_VALUE, SET_FORM_FIELDS_FEEDBACKS, SetFormFeedbacksPayload, IFieldFeedback, RESET_FORM, CLEAR_FIELD_ERRORS } from "../actions"; import { FieldChangeListener, FieldState, FieldValueChangeListener, FormChangeListener, FormState, FormValue, FormValueChangeListener, IAction, IFieldSchema, IFieldyEngine, IForm, IFormProps, Unsubscribe } from "../interfaces/fieldy"; import { reduce, State } from "../reducers"; import { FormImpl } from "./FormImpl"; import { FormHelper } from "../reducers/forms/form/helpers"; -import { IValidationError, IValidator } from "../interfaces"; +import { IValidator } from "../interfaces"; let idSeed = 0 @@ -104,15 +104,15 @@ export class FieldyEngineImpl implements IFieldyEngine { } - setFormDefaultValue(name: string, value: FormValue | undefined): void { - this.store.dispatch({ - type: SET_FORM_DEFAULT_VALUE, - payload: { - formName: name, - value: value, - } - }) - } + // setFormDefaultValue(name: string, value: FormValue | undefined): void { + // this.store.dispatch({ + // type: SET_FORM_DEFAULT_VALUE, + // payload: { + // formName: name, + // value: value, + // } + // }) + // } setFormValue(name: string, value: FormValue | undefined): void { this.store.dispatch({ @@ -269,9 +269,9 @@ export class FieldyEngineImpl implements IFieldyEngine { getFormInitialValue(formName: string): FormValue | undefined { return this.store.getState().forms[formName]?.initialValue } - getFormDefaultValue(formName: string): FormValue | undefined { - return this.store.getState().forms[formName]?.defaultValue - } + // getFormDefaultValue(formName: string): FormValue | undefined { + // return this.store.getState().forms[formName]?.defaultValue + // } getFieldState(formName: string, fieldPath: string): FieldState | undefined { const state = this.store.getState() @@ -286,12 +286,14 @@ export class FieldyEngineImpl implements IFieldyEngine { } } + //defautValue不存全局 getFieldDefaultValue(formName: string, fieldPath: string): unknown { - const formState = this.getFormState(formName) - if (formState) { - const formHelper = new FormHelper(formState) - return formHelper.getDefaultValueByPath(fieldPath) - } + return this.getFieldState(formName, fieldPath)?.meta.defaultValue + // const formState = this.getFormState(formName) + // if (formState) { + // const formHelper = new FormHelper(formState) + // return formHelper.getDefaultValueByPath(fieldPath) + // } } diff --git a/packages/fieldy/fieldy/src/classes/FormImpl.ts b/packages/fieldy/fieldy/src/classes/FormImpl.ts index 7e5dd2803..7f97a6bb2 100644 --- a/packages/fieldy/fieldy/src/classes/FormImpl.ts +++ b/packages/fieldy/fieldy/src/classes/FormImpl.ts @@ -43,9 +43,9 @@ export class FormImpl implements IForm { throw new Error("Method not implemented."); } - getDefaultValue(): FormValue | undefined { - return this.fieldy.getFormDefaultValue(this.name) - } + // getDefaultValue(): FormValue | undefined { + // return this.fieldy.getFormDefaultValue(this.name) + // } getInitialValue() { return this.fieldy.getFormInitialValue(this.name) } @@ -104,9 +104,9 @@ export class FormImpl implements IForm { setInitialValue(value: FormValue | undefined): void { this.fieldy.setFormInitialValue(this.name, value) } - setDefaultValue(value: FormValue | undefined): void { - this.fieldy.setFormDefaultValue(this.name, value) - } + // setDefaultValue(value: FormValue | undefined): void { + // this.fieldy.setFormDefaultValue(this.name, value) + // } async validate() { if (this.fieldy.validator) { this.validationSubscriber.emitStart() diff --git a/packages/fieldy/fieldy/src/interfaces/fieldy.ts b/packages/fieldy/fieldy/src/interfaces/fieldy.ts index bd720147f..bc947d8e2 100644 --- a/packages/fieldy/fieldy/src/interfaces/fieldy.ts +++ b/packages/fieldy/fieldy/src/interfaces/fieldy.ts @@ -77,8 +77,8 @@ export type FormState = { fieldSchemas: IFieldSchema[]; //初始值 initialValue?: FormValue | undefined; - //默认值 - defaultValue?: FormValue | undefined; + //默认值,默认值各字段自己管理 + //defaultValue?: FormValue | undefined; //当前值 value?: FormValue | undefined; } @@ -109,12 +109,12 @@ export interface IValidationSubscriber { export interface IFormNode extends IValidationSubscriber { fieldy: IFieldyEngine getModified(): boolean - getDefaultValue(): T + //getDefaultValue(): T getInitialValue(): T getValue(): T setValue(value: T): void setInitialValue(value: T): void - setDefaultValue(value: T): void + //setDefaultValue(value: T): void // mount(): void // unmount(): void validate(): Promise @@ -155,6 +155,7 @@ export interface IField basePath?: string path: string inputValue(value: unknown): void + getDefaultValue(): unknown getFieldSchema(): IFieldSchema getSubFieldSchemas(): IFieldSchema[] | undefined getSubFields(): IField[] | undefined @@ -175,7 +176,7 @@ export interface IFieldyEngine { //setFormFieldMetas(name: string, fieldMetas: IFieldSchema[]): void //不触发change事件 setFormInitialValue(name: string, value: FormValue | undefined): void - setFormDefaultValue(name: string, value: FormValue | undefined): void + //setFormDefaultValue(name: string, value: FormValue | undefined): void setFormValue(name: string, value: FormValue | undefined): void //setFormFlatValue(name: string, flatValues: FormValue): void addFields(name: string, ...fieldSchemas: IFieldSchema[]): void @@ -199,7 +200,7 @@ export interface IFieldyEngine { getFieldDefaultValue(formName: string, fieldPath: string): unknown getFormValue(formName: string): FormValue | undefined getFormInitialValue(formName: string): FormValue | undefined - getFormDefaultValue(formName: string): FormValue | undefined + // getFormDefaultValue(formName: string): FormValue | undefined subscribeToFormChange(name: string, listener: FormChangeListener): Unsubscribe subscribeToFormValuesChange(name: string, listener: FormValueChangeListener): Unsubscribe subscribeToFieldChange(formName: string, path: string, listener: FieldChangeListener): Unsubscribe diff --git a/packages/fieldy/fieldy/src/reducers/forms/form/defaultValue.ts b/packages/fieldy/fieldy/src/reducers/forms/form/defaultValue.ts deleted file mode 100644 index 3a51b076f..000000000 --- a/packages/fieldy/fieldy/src/reducers/forms/form/defaultValue.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { SET_FIELD_DEFAULT_VALUE, SET_FORM_DEFAULT_VALUE, SetFieldValuePayload, SetFormValuePayload } from "../../../actions"; -import { FormState, FormValue, IAction } from "../../../interfaces/fieldy"; -import { FormHelper } from "./helpers"; - -export function defaultValueReduer(state: FormValue | undefined, action: IAction, formState: FormState) { - const setFieldValuePayload = action.payload as SetFieldValuePayload - const formHelper = new FormHelper(formState) - switch (action.type) { - case SET_FORM_DEFAULT_VALUE: - return (action.payload as SetFormValuePayload).value - case SET_FIELD_DEFAULT_VALUE: - return formHelper.doSetValueByPath(state, setFieldValuePayload.path, setFieldValuePayload.value); - } - return state -} \ No newline at end of file diff --git a/packages/fieldy/fieldy/src/reducers/forms/form/helpers.ts b/packages/fieldy/fieldy/src/reducers/forms/form/helpers.ts index cf17f8c93..9e639333b 100644 --- a/packages/fieldy/fieldy/src/reducers/forms/form/helpers.ts +++ b/packages/fieldy/fieldy/src/reducers/forms/form/helpers.ts @@ -24,9 +24,9 @@ export class FormHelper { return this.doGetValueByPath(this.formState.value, path); } - getDefaultValueByPath(path: string): unknown { - return this.doGetValueByPath(this.formState.defaultValue, path); - } + // getDefaultValueByPath(path: string): unknown { + // return this.doGetValueByPath(this.formState.defaultValue, path); + // } getInitialValueByPath(path: string): unknown { return this.doGetValueByPath(this.formState.initialValue, path); diff --git a/packages/fieldy/fieldy/src/reducers/forms/form/index.ts b/packages/fieldy/fieldy/src/reducers/forms/form/index.ts index f02c6552d..99dc7b1d9 100644 --- a/packages/fieldy/fieldy/src/reducers/forms/form/index.ts +++ b/packages/fieldy/fieldy/src/reducers/forms/form/index.ts @@ -1,6 +1,5 @@ import { FormState, FormValue, IAction } from "../../../interfaces/fieldy"; -import { defaultValueReduer } from "./defaultValue"; import { fieldsReduer } from "./fields"; import { fieldSchemasReduer } from "./fieldSchemas"; import { initializedReduer } from "./initialized"; @@ -23,7 +22,7 @@ export function formReduce(state: FormState, action: IAction): FormStat fields: fieldsReduer(state.fields, action), fieldSchemas: fieldSchemasReduer(state.fieldSchemas, action), initialValue: initialValueReduer(state.initialValue as FormValue | undefined, action, state), - defaultValue: defaultValueReduer(state.defaultValue as FormValue | undefined, action, state), + //defaultValue: defaultValueReduer(state.defaultValue as FormValue | undefined, action, state), value: valueReduer(state.value, action, state), } } diff --git a/packages/fieldy/fieldy/src/reducers/forms/form/value.ts b/packages/fieldy/fieldy/src/reducers/forms/form/value.ts index ac54da9c7..66b6476f9 100644 --- a/packages/fieldy/fieldy/src/reducers/forms/form/value.ts +++ b/packages/fieldy/fieldy/src/reducers/forms/form/value.ts @@ -1,4 +1,4 @@ -import { SetFormValuePayload, SET_FORM_INITIAL_VALUE, SET_FORM_VALUE, SET_FORM_DEFAULT_VALUE, REMOVE_FORM_FIELD, SetFieldValuePayload, SET_FIELD_VALUE, SET_FIELD_INITIAL_VALUE, SET_FIELD_DEFAULT_VALUE, INPUT_FIELD_VALUE } from "../../../actions"; +import { SetFormValuePayload, SET_FORM_INITIAL_VALUE, SET_FORM_VALUE, REMOVE_FORM_FIELD, SetFieldValuePayload, SET_FIELD_VALUE, SET_FIELD_INITIAL_VALUE, SET_FIELD_DEFAULT_VALUE, INPUT_FIELD_VALUE } from "../../../actions"; import { FormState, FormValue, IAction } from "../../../interfaces/fieldy"; import { FormHelper } from "./helpers"; @@ -12,10 +12,10 @@ export function valueReduer(state: FormValue | undefined, action: IAction