Skip to content

Commit 145ea65

Browse files
committed
feat: add the disabled attribute, setFieldsValue should not trigger validation
1 parent 737de11 commit 145ea65

File tree

8 files changed

+146
-78
lines changed

8 files changed

+146
-78
lines changed

src/packages/form/form.taro.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export interface FormProps extends BasicComponent {
1212
initialValues: any
1313
name: string
1414
form: any
15+
disabled: boolean
1516
divider: boolean
1617
labelPosition: 'top' | 'left' | 'right'
1718
starPosition: 'left' | 'right'
@@ -23,6 +24,7 @@ const defaultProps = {
2324
...ComponentDefaults,
2425
labelPosition: 'right',
2526
starPosition: 'left',
27+
disabled: false,
2628
divider: false,
2729
onFinish: (values) => {},
2830
onFinishFailed: (values, errorFields) => {},
@@ -44,6 +46,7 @@ export const Form = React.forwardRef<FormInstance, Partial<FormProps>>(
4446
children,
4547
initialValues,
4648
divider,
49+
disabled,
4750
onFinish,
4851
onFinishFailed,
4952
labelPosition,
@@ -97,7 +100,7 @@ export const Form = React.forwardRef<FormInstance, Partial<FormProps>>(
97100
}}
98101
>
99102
<Cell.Group divider={divider}>
100-
<Context.Provider value={{ formInstance, labelPosition }}>
103+
<Context.Provider value={{ formInstance, labelPosition, disabled }}>
101104
{children}
102105
</Context.Provider>
103106
{footer ? (

src/packages/form/form.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export interface FormProps extends BasicComponent {
1111
initialValues: any
1212
name: string
1313
form: any
14+
disabled: boolean
1415
divider: boolean
1516
labelPosition: 'top' | 'left' | 'right'
1617
starPosition: 'left' | 'right'
@@ -22,6 +23,7 @@ const defaultProps = {
2223
...ComponentDefaults,
2324
labelPosition: 'right',
2425
starPosition: 'left',
26+
disabled: false,
2527
divider: false,
2628
onFinish: (values) => {},
2729
onFinishFailed: (values, errorFields) => {},
@@ -43,6 +45,7 @@ export const Form = React.forwardRef<FormInstance, Partial<FormProps>>(
4345
children,
4446
initialValues,
4547
divider,
48+
disabled,
4649
onFinish,
4750
onFinishFailed,
4851
labelPosition,
@@ -96,7 +99,7 @@ export const Form = React.forwardRef<FormInstance, Partial<FormProps>>(
9699
}}
97100
>
98101
<Cell.Group divider={divider}>
99-
<Context.Provider value={{ formInstance, labelPosition }}>
102+
<Context.Provider value={{ formInstance, labelPosition, disabled }}>
100103
{children}
101104
</Context.Provider>
102105
{footer ? (

src/packages/form/useform.taro.ts

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { useRef } from 'react'
22
import Schema from 'async-validator'
3+
import { merge } from '@/utils/merge'
34
import {
4-
Store,
55
Callbacks,
6-
FormInstance,
76
FormFieldEntity,
7+
FormInstance,
88
NamePath,
9+
Store,
910
} from './types'
1011

1112
export const SECRET = 'NUT_FORM_INTERNAL'
@@ -74,28 +75,32 @@ class FormStore {
7475
return fieldsValue
7576
}
7677

78+
updateStore(nextStore: Store) {
79+
this.store = nextStore
80+
}
81+
7782
/**
7883
* 设置 form 的初始值,之后在 reset 的时候使用
7984
* @param values
8085
* @param init
8186
*/
8287

83-
setInitialValues = (values: Store, init: boolean) => {
88+
setInitialValues = (initialValues: Store, init: boolean) => {
89+
this.initialValues = initialValues || {}
8490
if (init) {
85-
this.initialValues = values
86-
this.store = values
91+
const nextStore = merge(initialValues, this.store)
92+
this.updateStore(nextStore)
93+
console.log(this.store === this.initialValues)
8794
}
8895
}
8996

9097
/**
9198
* 存储组件数据
9299
* @param newStore { [name]: newValue }
93100
*/
94-
setFieldsValue = (newStore: any, needValidate = true) => {
95-
this.store = {
96-
...this.store,
97-
...newStore,
98-
}
101+
setFieldsValue = (newStore: any) => {
102+
const nextStore = merge(this.store, newStore)
103+
this.updateStore(nextStore)
99104
this.fieldEntities.forEach((entity: FormFieldEntity) => {
100105
const { name } = entity.props
101106
Object.keys(newStore).forEach((key) => {
@@ -113,7 +118,6 @@ class FormStore {
113118
item.entity.onStoreChange('update')
114119
}
115120
})
116-
needValidate && this.validateFields()
117121
}
118122

119123
setCallback = (callback: Callbacks) => {
@@ -142,7 +146,7 @@ class FormStore {
142146
// validator.messages()
143147
try {
144148
await validator.validate({ [name]: this.store?.[name] })
145-
} catch ({ errors }: any) {
149+
} catch ({ errors }) {
146150
if (errors) {
147151
errs.push(...(errors as any[]))
148152
this.errors[name] = errors
@@ -186,7 +190,9 @@ class FormStore {
186190

187191
resetFields = () => {
188192
this.errors.length = 0
189-
this.store = this.initialValues
193+
const nextStore = merge({}, this.initialValues)
194+
console.log('xxx', nextStore, this.initialValues)
195+
this.updateStore(nextStore)
190196
this.fieldEntities.forEach((entity: FormFieldEntity) => {
191197
entity.onStoreChange('reset')
192198
})
@@ -244,6 +250,6 @@ export const useForm = (form?: FormInstance): [FormInstance] => {
244250
const formStore = new FormStore()
245251
formRef.current = formStore.getForm() as FormInstance
246252
}
253+
return [formRef.current as FormInstance]
247254
}
248-
return [formRef.current]
249255
}

src/packages/form/useform.ts

Lines changed: 59 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { useRef } from 'react'
22
import Schema from 'async-validator'
3+
import { merge } from '@/utils/merge'
34
import {
4-
Store,
55
Callbacks,
6-
FormInstance,
76
FormFieldEntity,
7+
FormInstance,
88
NamePath,
9+
Store,
910
} from './types'
1011

1112
export const SECRET = 'NUT_FORM_INTERNAL'
@@ -74,28 +75,31 @@ class FormStore {
7475
return fieldsValue
7576
}
7677

78+
updateStore(nextStore: Store) {
79+
this.store = nextStore
80+
}
81+
7782
/**
7883
* 设置 form 的初始值,之后在 reset 的时候使用
7984
* @param values
8085
* @param init
8186
*/
8287

83-
setInitialValues = (values: Store, init: boolean) => {
88+
setInitialValues = (initialValues: Store, init: boolean) => {
89+
this.initialValues = initialValues || {}
8490
if (init) {
85-
this.initialValues = values
86-
this.store = values
91+
const nextStore = merge(initialValues, this.store)
92+
this.updateStore(nextStore)
8793
}
8894
}
8995

9096
/**
9197
* 存储组件数据
9298
* @param newStore { [name]: newValue }
9399
*/
94-
setFieldsValue = (newStore: any, needValidate = true) => {
95-
this.store = {
96-
...this.store,
97-
...newStore,
98-
}
100+
setFieldsValue = (newStore: any) => {
101+
const nextStore = merge(this.store, newStore)
102+
this.updateStore(nextStore)
99103
this.fieldEntities.forEach((entity: FormFieldEntity) => {
100104
const { name } = entity.props
101105
Object.keys(newStore).forEach((key) => {
@@ -113,7 +117,6 @@ class FormStore {
113117
item.entity.onStoreChange('update')
114118
}
115119
})
116-
needValidate && this.validateFields()
117120
}
118121

119122
setCallback = (callback: Callbacks) => {
@@ -123,48 +126,55 @@ class FormStore {
123126
}
124127
}
125128

129+
validateEntities = async (entity: FormFieldEntity, errs: any[]) => {
130+
const { name, rules = [] } = entity.props
131+
const descriptor: any = {}
132+
if (rules.length) {
133+
// 多条校验规则
134+
if (rules.length > 1) {
135+
descriptor[name] = []
136+
rules.forEach((v: any) => {
137+
descriptor[name].push(v)
138+
})
139+
} else {
140+
descriptor[name] = rules[0]
141+
}
142+
}
143+
const validator = new Schema(descriptor)
144+
// 此处合并无值message 没有意义?
145+
// validator.messages()
146+
try {
147+
await validator.validate({ [name]: this.store?.[name] })
148+
} catch ({ errors }) {
149+
if (errors) {
150+
errs.push(...(errors as any[]))
151+
this.errors[name] = errors
152+
}
153+
} finally {
154+
if (!errs || errs.length === 0) {
155+
this.errors[name] = []
156+
}
157+
}
158+
159+
entity.onStoreChange('validate')
160+
}
161+
126162
validateFields = async (nameList?: NamePath[]) => {
127-
let filterEntitys = []
128-
const errs = []
163+
let filterEntities = []
129164
this.errors.length = 0
130165
if (!nameList || nameList.length === 0) {
131-
filterEntitys = this.fieldEntities
166+
filterEntities = this.fieldEntities
132167
} else {
133-
filterEntitys = this.fieldEntities.filter(({ props: { name } }) =>
168+
filterEntities = this.fieldEntities.filter(({ props: { name } }) =>
134169
nameList.includes(name)
135170
)
136171
}
137-
for (const entity of filterEntitys) {
138-
const { name, rules = [] } = entity.props
139-
const descriptor: any = {}
140-
if (rules.length) {
141-
// 多条校验规则
142-
if (rules.length > 1) {
143-
descriptor[name] = []
144-
rules.forEach((v: any) => {
145-
descriptor[name].push(v)
146-
})
147-
} else {
148-
descriptor[name] = rules[0]
149-
}
150-
}
151-
const validator = new Schema(descriptor)
152-
// 此处合并无值message 没有意义?
153-
// validator.messages()
154-
try {
155-
await validator.validate({ [name]: this.store?.[name] })
156-
} catch ({ errors }: any) {
157-
if (errors) {
158-
errs.push(...(errors as any[]))
159-
this.errors[name] = errors
160-
}
161-
} finally {
162-
if (!errs || errs.length === 0) {
163-
this.errors[name] = []
164-
}
165-
}
166-
entity.onStoreChange('validate')
167-
}
172+
const errs: any[] = []
173+
await Promise.all(
174+
filterEntities.map(async (entity) => {
175+
await this.validateEntities(entity, errs)
176+
})
177+
)
168178
return errs
169179
}
170180

@@ -179,7 +189,8 @@ class FormStore {
179189

180190
resetFields = () => {
181191
this.errors.length = 0
182-
this.store = this.initialValues
192+
const nextStore = merge({}, this.initialValues)
193+
this.updateStore(nextStore)
183194
this.fieldEntities.forEach((entity: FormFieldEntity) => {
184195
entity.onStoreChange('reset')
185196
})
@@ -237,6 +248,6 @@ export const useForm = (form?: FormInstance): [FormInstance] => {
237248
const formStore = new FormStore()
238249
formRef.current = formStore.getForm() as FormInstance
239250
}
251+
return [formRef.current as FormInstance]
240252
}
241-
return [formRef.current]
242253
}

src/packages/formitem/formitem.scss

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
.nut-form-item {
22
display: flex;
33

4+
&-disabled {
5+
opacity: 0.4;
6+
pointer-events: none;
7+
}
8+
49
&-label {
510
display: flex;
611
flex-direction: row;

0 commit comments

Comments
 (0)