Skip to content

Commit

Permalink
从form中删掉defaultValue,各字段自己管理
Browse files Browse the repository at this point in the history
  • Loading branch information
codebdy committed Dec 20, 2023
1 parent ac9a5ae commit 3a77804
Show file tree
Hide file tree
Showing 16 changed files with 69 additions and 77 deletions.
2 changes: 1 addition & 1 deletion packages/fieldy/fieldy-react/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rxdrag/react-fieldy",
"version": "0.6.6",
"version": "0.6.7",
"type": "module",
"module": "index.ts",
"files": [
Expand Down
4 changes: 2 additions & 2 deletions packages/fieldy/fieldy-react/src/components/Field/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<XField fieldMeta={fieldMeta} initialValue={initialValue} value={value} defaultValue={defaultValue}>
<XField fieldMeta={fieldMeta} initialValue={initialValue} value={value}>
{children}
</XField>
)
Expand Down
13 changes: 6 additions & 7 deletions packages/fieldy/fieldy-react/src/components/VirtualForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown>,
children?: React.ReactNode
}) => {
const { name, initialValue, defaultValue, value, children, onValueChange, expContext } = props
const { name, initialValue, value, children, onValueChange, expContext } = props
const [form, setForm] = useState<IForm>()
const fieldy = useFieldy()
useEffect(() => {
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
10 changes: 5 additions & 5 deletions packages/fieldy/fieldy-react/src/components/XField/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand All @@ -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 (
<FieldContext.Provider value={field}>
Expand Down
2 changes: 1 addition & 1 deletion packages/fieldy/fieldy/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rxdrag/fieldy",
"version": "0.6.3",
"version": "0.6.4",
"author": "Water.Li",
"type": "module",
"module": "index.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/fieldy/fieldy/src/actions/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
40 changes: 21 additions & 19 deletions packages/fieldy/fieldy/src/classes/FieldyEngineImpl.ts
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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()
Expand All @@ -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)
// }
}


Expand Down
12 changes: 6 additions & 6 deletions packages/fieldy/fieldy/src/classes/FormImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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()
Expand Down
13 changes: 7 additions & 6 deletions packages/fieldy/fieldy/src/interfaces/fieldy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ export type FormState = {
fieldSchemas: IFieldSchema[];
//初始值
initialValue?: FormValue | undefined;
//默认值
defaultValue?: FormValue | undefined;
//默认值,默认值各字段自己管理
//defaultValue?: FormValue | undefined;
//当前值
value?: FormValue | undefined;
}
Expand Down Expand Up @@ -109,12 +109,12 @@ export interface IValidationSubscriber {
export interface IFormNode<T> 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<ValidateResult>
Expand Down Expand Up @@ -155,6 +155,7 @@ export interface IField<ValidateRules extends IValidateSchema = IValidateSchema>
basePath?: string
path: string
inputValue(value: unknown): void
getDefaultValue(): unknown
getFieldSchema(): IFieldSchema<ValidateRules>
getSubFieldSchemas(): IFieldSchema<ValidateRules>[] | undefined
getSubFields(): IField<ValidateRules>[] | undefined
Expand All @@ -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
Expand All @@ -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
Expand Down
15 changes: 0 additions & 15 deletions packages/fieldy/fieldy/src/reducers/forms/form/defaultValue.ts

This file was deleted.

6 changes: 3 additions & 3 deletions packages/fieldy/fieldy/src/reducers/forms/form/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions packages/fieldy/fieldy/src/reducers/forms/form/index.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -23,7 +22,7 @@ export function formReduce(state: FormState, action: IAction<unknown>): 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),
}
}
Expand Down
10 changes: 5 additions & 5 deletions packages/fieldy/fieldy/src/reducers/forms/form/value.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -12,10 +12,10 @@ export function valueReduer(state: FormValue | undefined, action: IAction<unknow
case SET_FORM_VALUE: {
return (setFormValuePayload)?.value === undefined ? state : (setFormValuePayload).value
}
case SET_FORM_DEFAULT_VALUE: {
//合格并value
return formHelper.mergeDefaultValueToValue((action.payload as SetFormValuePayload).value);
}
// case SET_FORM_DEFAULT_VALUE: {
// //合格并value
// return formHelper.mergeDefaultValueToValue((action.payload as SetFormValuePayload).value);
// }
case REMOVE_FORM_FIELD:
//删除相关value
return formHelper.removeValueByPath(setFieldValuePayload.path);
Expand Down

0 comments on commit 3a77804

Please sign in to comment.