Skip to content

Commit

Permalink
fix: 修复label类型导致hooks内存溢出
Browse files Browse the repository at this point in the history
  • Loading branch information
kongjing committed Mar 27, 2023
1 parent 89de933 commit 3d4bac2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
14 changes: 14 additions & 0 deletions packages/vantui/src/form-item/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export function FormItem(props: FormItemProps) {
layout = 'horizontal',
children,
label,
labelName,
required = false,
rules = {},
trigger = 'onChange',
Expand All @@ -44,6 +45,17 @@ export function FormItem(props: FormItemProps) {
const [, forceUpdate_] = useState({})
const _name = Array.isArray(name) ? name.join('.') : name

const _label = useMemo(() => {
let l = ''
if (typeof label === 'string') {
l = label
} else if (labelName) {
l = labelName
}

return l
}, [label, labelName])

const onStoreChange = useMemo(() => {
const onStoreChange = {
changeValue() {
Expand All @@ -60,6 +72,7 @@ export function FormItem(props: FormItemProps) {
rules,
required,
mutiLevel,
label: _label,
})
}, [
_name,
Expand All @@ -69,6 +82,7 @@ export function FormItem(props: FormItemProps) {
required,
rules,
unRegisterValidate,
_label,
])

useEffect(function () {
Expand Down
20 changes: 11 additions & 9 deletions packages/vantui/src/form/core/formstore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const formInstanceApi = [
const isReg = (value: any) => value instanceof RegExp
class FormStore {
static instance: FormStore
public requiredMessageCallback?: (fieldName: string) => string
public requiredMessageCallback?: (label: string) => string
public FormUpdate: () => any
public model: Record<string, any>
public control: Record<string, any>
Expand Down Expand Up @@ -54,18 +54,19 @@ class FormStore {
}, {})
}

registerRequiredMessageCallback(callback: (fieldName: string) => string) {
registerRequiredMessageCallback(callback: (label: string) => string) {
this.requiredMessageCallback = callback
}

static createValidate(validateModal: any) {
const { value, required, rules } = validateModal
const { value, required, rules, label } = validateModal

return {
value,
rules: Array.isArray(rules) ? rules : [rules],
required: required || false,
status: 'pendding',
label,
}
}

Expand Down Expand Up @@ -143,10 +144,7 @@ class FormStore {
!this.model[name] || this.model[name].value === undefined

const validate = FormStore.createValidate(model)
this.model[name] = {
...validate,
name,
}
this.model[name] = validate
this.control[name] = control

if (shouldUpdate) {
Expand Down Expand Up @@ -366,10 +364,14 @@ class FormStore {
status = 'reject'
if (this.requiredMessageCallback) {
this.model[name].message = this.requiredMessageCallback(
this.model[name].name,
this.model[name].label,
)
} else {
this.model[name].message = '字段不能为空'
if (this.model[name].label) {
this.model[name].message = this.model[name].label + '不能为空'
} else {
this.model[name].message = '此处不能为空'
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions packages/vantui/types/form.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ export interface FormItemProps extends ViewProps {
* @description 表单label
*/
label: ReactNode
/**
* @description 表单label字符串, 当label不为字符串的时候,传入labelName
*/
labelName?: string
/**
* @description 垂直 | 水平
* @default horizontal
Expand Down

0 comments on commit 3d4bac2

Please sign in to comment.