diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index d8297754e20..9e1146f8fb7 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -736,7 +736,7 @@ export function createForm( FormPath.parse(state.path) ) let val = getValue() - return (index !== undefined ? newPath.concat(index) : newPath).existIn( + return (isValid(index) ? newPath.concat(index) : newPath).existIn( val, newPath ) diff --git a/packages/core/src/shared/model.ts b/packages/core/src/shared/model.ts index c9d5ff9718e..86ce4c81559 100644 --- a/packages/core/src/shared/model.ts +++ b/packages/core/src/shared/model.ts @@ -6,7 +6,8 @@ import { globalThisPolyfill, Subscribable, FormPath, - FormPathPattern + FormPathPattern, + isValid } from '@uform/shared' import produce, { Draft, setAutoFreeze } from 'immer' import { @@ -133,7 +134,7 @@ export const createStateModel = ( ) if (isFn(this.controller.dirtyCheck)) { const result = this.controller.dirtyCheck(this.dirtys) - if (result !== undefined) { + if (isValid(result)) { Object.assign(this.dirtys, result) } } @@ -179,7 +180,7 @@ export const createStateModel = ( ) if (isFn(this.controller.dirtyCheck)) { const result = this.controller.dirtyCheck(this.dirtys) - if (result !== undefined) { + if (isValid(result)) { Object.assign(this.dirtys, result) } } diff --git a/packages/core/src/state/field.ts b/packages/core/src/state/field.ts index 3c838a6c633..4ebfe7dbe49 100644 --- a/packages/core/src/state/field.ts +++ b/packages/core/src/state/field.ts @@ -76,7 +76,7 @@ export const FieldState = createStateModel( readRequired(rules: any[]) { for (let i = 0; i < rules.length; i++) { - if (rules[i].required !== undefined) { + if (isValid(rules[i].required)) { return rules[i].required } } @@ -90,8 +90,8 @@ export const FieldState = createStateModel( } else { rules = rules.reduce((buf: any[], item: any) => { const keys = Object.keys(item || {}) - if (item.required !== undefined) { - if (item.message !== undefined) { + if (isValid(item.required)) { + if (isValid(item.message)) { if (keys.length > 2) { return buf.concat({ ...item, diff --git a/packages/react-schema-renderer/src/shared/connect.ts b/packages/react-schema-renderer/src/shared/connect.ts index 5c7451141dd..cb3f91dd8bd 100644 --- a/packages/react-schema-renderer/src/shared/connect.ts +++ b/packages/react-schema-renderer/src/shared/connect.ts @@ -1,5 +1,5 @@ import React from 'react' -import { isArr, each, isFn } from '@uform/shared' +import { isArr, each, isFn,isValid } from '@uform/shared' import { ISchema, IConnectOptions, @@ -81,7 +81,7 @@ export const connect = (options?: IConnectOptions) => { onBlur: () => mutators.blur(), onFocus: () => mutators.focus() } - if (editable !== undefined) { + if (isValid(editable)) { if (isFn(editable)) { if (!editable(name)) { componentProps.disabled = true @@ -105,7 +105,7 @@ export const connect = (options?: IConnectOptions) => { if (isFn(options.getProps)) { const newProps = options.getProps(componentProps, fieldProps) - if (newProps !== undefined) { + if (isValid(newProps)) { componentProps = newProps as any } } @@ -114,7 +114,7 @@ export const connect = (options?: IConnectOptions) => { componentProps.dataSource = createEnum((props as ISchema).enum) } - if (componentProps.editable !== undefined) { + if (isValid(componentProps.editable)) { delete componentProps.editable } diff --git a/packages/react/src/shared.ts b/packages/react/src/shared.ts index a2829bd9dbb..eb76b0c5ac9 100644 --- a/packages/react/src/shared.ts +++ b/packages/react/src/shared.ts @@ -1,4 +1,4 @@ -import { isFn, FormPath, Subscribable } from '@uform/shared' +import { isFn, FormPath, Subscribable, isValid } from '@uform/shared' import { IFormEffect, IFormActions, @@ -93,11 +93,11 @@ export const getValueFromEvent = (event: any) => { if ( !isReactNative && event.nativeEvent && - event.nativeEvent.text !== undefined + isValid(event.nativeEvent.text) ) { return event.nativeEvent.text } - if (isReactNative && event.nativeEvent !== undefined) { + if (isReactNative && isValid(event.nativeEvent)) { return event.nativeEvent.text } diff --git a/packages/validator/src/rules.ts b/packages/validator/src/rules.ts index 4130f873b65..8053510aee0 100644 --- a/packages/validator/src/rules.ts +++ b/packages/validator/src/rules.ts @@ -1,6 +1,7 @@ import { getMessage } from './message' import { isEmpty, + isValid, stringLength, isStr, isArr, @@ -12,7 +13,7 @@ import { ValidateDescription } from './types' const isValidateEmpty = (value: any) => { if (isArr(value)) { for (let i = 0; i < value.length; i++) { - if (value[i] !== undefined) return false + if (isValid(value[i])) return false } return true } else { diff --git a/packages/validator/src/validator.ts b/packages/validator/src/validator.ts index 9d0ee4f3d71..ff7446ed605 100644 --- a/packages/validator/src/validator.ts +++ b/packages/validator/src/validator.ts @@ -18,6 +18,7 @@ import { isStr, isArr, isObj, + isValid, each, FormPath, FormPathPattern @@ -101,8 +102,9 @@ class FormValidator { errors: string[] warnings: string[] }> { - const first = - options.first !== undefined ? !!options.first : !!this.validateFirst + const first = isValid(options.first) + ? !!options.first + : !!this.validateFirst const errors: string[] = [] const warnings = [] try { @@ -113,7 +115,7 @@ class FormValidator { ) for (let l = 0; l < keys.length; l++) { let key = keys[l] - if (ruleObj.hasOwnProperty(key) && ruleObj[key] !== undefined) { + if (ruleObj.hasOwnProperty(key) && isValid(ruleObj[key])) { const rule = ValidatorRules[key] if (rule) { const payload = await rule(value, ruleObj)