Skip to content

Commit

Permalink
feat: setError支持配置action.ignore (#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
mengshang918 committed Jan 13, 2022
1 parent 6647fd0 commit b84aa5e
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 42 deletions.
9 changes: 3 additions & 6 deletions packages/drip-form-theme-antd/src/global.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { CSSProperties } from 'react'
import { CSSProperties, Dispatch as DispatchR } from 'react'
import type { GetKey } from '@jdfed/hooks'
export declare type Dispatch = (arg0: {
key?: string
type: string
[propName: string]: any
}) => void
import type { Action } from '@jdfed/utils'
export declare type Dispatch = DispatchR<Action>

export declare type CommonProps = Partial<{
// 是否禁用表单
Expand Down
3 changes: 2 additions & 1 deletion packages/drip-form/src/DripForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: jiangxiaowei
* @Date: 2020-05-14 16:54:32
* @Last Modified by: jiangxiaowei
* @Last Modified time: 2022-01-09 14:39:05
* @Last Modified time: 2022-01-13 16:21:24
*/
import React, {
forwardRef,
Expand Down Expand Up @@ -147,6 +147,7 @@ const DripForm = forwardRef<DripFormRefType, DripFormRenderProps>(
visibleFieldKey: [],
changeKey: '',
arrayKey: {},
ignoreErrKey: [],
}),
[
initTypePath,
Expand Down
28 changes: 23 additions & 5 deletions packages/drip-form/src/reducers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @Author: jiangxiaowei
* @Date: 2020-05-14 15:43:02
* @Last Modified by: jiangxiaowei
* @Last Modified time: 2021-12-28 14:32:01
* @Last Modified time: 2022-01-13 16:37:39
*/
import { createContext, Dispatch } from 'react'
import {
Expand All @@ -14,7 +14,7 @@ import {
} from '@jdfed/utils'
import addField from './addField'
import deleteField from './deleteField'
import type { Action, State } from '@jdfed/utils'
import type { Action, State, SetErrType } from '@jdfed/utils'

export const FormDataContext = createContext('')

Expand Down Expand Up @@ -93,8 +93,13 @@ const formDataReducer = (state: State, action: Action): void => {
if (args.errors && typeCheck(args.errors) === 'Object') {
const ignoreErr: Record<string, string> = {}
// ignore 用来将type:change的异步校验的结果保存下来。防止被validae给清空
if (args.ignore && Array.isArray(args.ignore)) {
args.ignore.map((item) => {
if (
(args.ignore && Array.isArray(args.ignore)) ||
state.ignoreErrKey.length > 0
) {
Array.from(
new Set([...(args.ignore || []), ...state.ignoreErrKey])
).map((item) => {
if (state.errors[item]) {
ignoreErr[item] = state.errors[item]
}
Expand All @@ -106,13 +111,26 @@ const formDataReducer = (state: State, action: Action): void => {
}
} else {
for (const i in args) {
const params = args as SetErrType
if (params?.action?.ignore) {
state.ignoreErrKey = Array.from(
new Set([...state.ignoreErrKey, ...params.action.ignore])
)
}

state.errors[i] = args[i as keyof typeof args] as string
}
}
break
}
case 'deleteError': {
delete state.errors[action.key]
if (Array.isArray(action.key)) {
action.key.map((item) => {
delete state.errors[item]
})
} else {
delete state.errors[action.key]
}
break
}
case 'setChecking': {
Expand Down
13 changes: 4 additions & 9 deletions packages/hooks/src/useField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import { produce } from 'immer'
* @Last Modified by: jiangxiaowei
* @Last Modified time: yyyy-05-dd 15:10:43
*/

import moment, { Moment } from 'moment'
import useEventCallback from './useEventCallback'
import { useDebounceFn } from 'ahooks'
import { isEmpty, typeCheck } from '@jdfed/utils'
import type { GetKey } from '@jdfed/utils'
import type { GetKey, Action } from '@jdfed/utils'
import type { Dispatch } from 'react'
type FormatMomentDataProps = {
value: string | [Moment, Moment]
format: string
Expand Down Expand Up @@ -220,11 +220,6 @@ const myComponent = ({fieldKey,onChange})=>{
* @param {object} options 可选 表单字段特殊处理配置。注意:options中只能有一个字段的值是true。否则不会对特殊数据进行格式化
* @param {func} dispatch 操作context
*/
type Dispatch = (arg0: {
key?: string
type: string
[propName: string]: any
}) => void
type CustomFunc = ({
val,
dispatch,
Expand All @@ -235,7 +230,7 @@ type CustomFunc = ({
fieldData,
}: {
val: any
dispatch: Dispatch
dispatch: Dispatch<Action>
getKey: GetKey
fieldKey: string
prevFieldData: any
Expand Down Expand Up @@ -272,7 +267,7 @@ const useField = (
prevFieldData?: any
fieldData?: any
},
dispatch: Dispatch
dispatch: Dispatch<Action>
): UseFieldR => {
// onChange 回调 debounce
const { run } = useDebounceFn(
Expand Down
11 changes: 3 additions & 8 deletions packages/hooks/src/useQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
* @Last Modified time: 2021-11-05 10:45:25
*/
import { useCallback } from 'react'
import type { GetKey } from '@jdfed/utils'
import type { Dispatch } from 'react'
import type { GetKey, Action } from '@jdfed/utils'
// import useCusDispatch from './useCusDispatch'

/**
Expand All @@ -26,15 +27,9 @@ type Arg0 = {
getKey: GetKey
}

type Dispatch = (arg0: {
key?: string
type: string
[propName: string]: any
}) => void

const useQuery = (
{ options, queryFunc, requestCache = true, fieldKey, getKey }: Arg0,
dispatch: Dispatch
dispatch: Dispatch<Action>
): ((...args: any[]) => void) => {
return useCallback(
async (...args) => {
Expand Down
6 changes: 3 additions & 3 deletions packages/hooks/src/useValidate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { useDebounceFn } from 'ahooks'
import type Ajv from 'ajv/dist/2019'
import type { ErrorObject } from 'ajv/dist/2019'

import React from 'react'

import type { Dispatch } from 'react'
import type { Action } from '@jdfed/utils'
type Errors = ErrorObject[]
export type ErrorsMap = Record<string, string>
type ValidateReturn = {
Expand All @@ -36,7 +36,7 @@ type Params = {
[propName: string]: any
}
ajv: any
dispatch: React.Dispatch<any>
dispatch: Dispatch<Action>
visibleFieldKey: string[]
onValidate: {
[propName: string]: any
Expand Down
27 changes: 17 additions & 10 deletions packages/utils/src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export type State = {
changeKey: string
// 数组容器对应的react key映射
arrayKey: Record<string, Array<string>>
// 异步校验需要保留的错误key(用于防止异步校验的错误信息被ajv覆盖)
ignoreErrKey: Array<string>
}

type ReloadAction = {
Expand Down Expand Up @@ -78,19 +80,24 @@ type AddFieldAction = {
shouldDelete: boolean
}

type SetErrorAction =
| ({
type: 'setError'
} & Record<string, string>)
| {
type: 'setError'
ignore?: Array<string>
errors?: Record<string, string>
}
export type SetErrType = {
type: 'setError'
action?: {
ignore?: Array<string>
}
} & Record<string, string>

export type SetErrsType = {
type: 'setError'
ignore?: Array<string>
errors?: Record<string, string>
}

type SetErrorAction = SetErrType | SetErrsType

type DeleteErrorAction = {
type: 'deleteError'
key: string
key: Array<string> | string
}

type SetCheckingAction = {
Expand Down

0 comments on commit b84aa5e

Please sign in to comment.