Skip to content

Commit 1b91b83

Browse files
authored
feat(form): 增加 taro components 属性的支持,抽象类型 (#3182)
1 parent aa06cc7 commit 1b91b83

File tree

16 files changed

+169
-226
lines changed

16 files changed

+169
-226
lines changed

src/packages/form/form.taro.tsx

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,11 @@
1-
import React, { ReactNode } from 'react'
1+
import React from 'react'
22
import classNames from 'classnames'
3-
import { Form as TForm, FormProps as TFormProps } from '@tarojs/components'
3+
import { Form as TForm } from '@tarojs/components'
44
import { Context } from './context'
55
import { SECRET, useForm } from './useform.taro'
66
import { ComponentDefaults } from '@/utils/typings'
77
import Cell from '@/packages/cell/index.taro'
8-
import { FormInstance } from '@/packages/form/types'
9-
10-
export interface FormProps extends TFormProps {
11-
footer: ReactNode
12-
initialValues: any
13-
name: string
14-
form: any
15-
disabled: boolean
16-
divider: boolean
17-
validateTrigger: string | string[] | false
18-
labelPosition: 'top' | 'left' | 'right'
19-
starPosition: 'left' | 'right'
20-
onFinish: (values: any) => void
21-
onFinishFailed: (values: any, errorFields: any) => void
22-
}
8+
import { TaroFormProps, FormInstance } from '@/types'
239

2410
const defaultProps = {
2511
...ComponentDefaults,
@@ -28,17 +14,19 @@ const defaultProps = {
2814
disabled: false,
2915
divider: false,
3016
validateTrigger: 'onChange',
17+
onReset: () => {},
18+
onSubmit: () => {},
3119
onFinish: (values) => {},
3220
onFinishFailed: (values, errorFields) => {},
33-
} as FormProps
21+
} as TaroFormProps
3422

3523
const PositionInfo: any = {
3624
top: 'form-layout-top',
3725
left: 'form-layout-left',
3826
right: 'form-layout-right',
3927
}
4028

41-
export const Form = React.forwardRef<FormInstance, Partial<FormProps>>(
29+
export const Form = React.forwardRef<FormInstance, Partial<TaroFormProps>>(
4230
(props, ref) => {
4331
const classPrefix = 'nut-form'
4432
const {
@@ -54,6 +42,8 @@ export const Form = React.forwardRef<FormInstance, Partial<FormProps>>(
5442
validateTrigger,
5543
labelPosition,
5644
starPosition,
45+
onReset,
46+
onSubmit,
5747
form,
5848
...rest
5949
} = {
@@ -97,11 +87,13 @@ export const Form = React.forwardRef<FormInstance, Partial<FormProps>>(
9787
e.preventDefault()
9888
e.stopPropagation()
9989
submit()
90+
onSubmit?.()
10091
}}
10192
onReset={(e) => {
10293
e.preventDefault()
10394
e.stopPropagation()
10495
resetFields()
96+
onReset?.()
10597
}}
10698
>
10799
<Cell.Group divider={divider}>

src/packages/form/form.tsx

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,10 @@
1-
import React, { ReactNode } from 'react'
1+
import React from 'react'
22
import classNames from 'classnames'
33
import { Context } from './context'
44
import { SECRET, useForm } from './useform'
5-
import { BasicComponent, ComponentDefaults } from '@/utils/typings'
5+
import { ComponentDefaults } from '@/utils/typings'
66
import Cell from '@/packages/cell'
7-
import { FormInstance } from '@/packages/form/types'
8-
9-
export interface FormProps extends BasicComponent {
10-
footer: ReactNode
11-
initialValues: any
12-
name: string
13-
form: any
14-
disabled: boolean
15-
divider: boolean
16-
validateTrigger: string | string[] | false
17-
labelPosition: 'top' | 'left' | 'right'
18-
starPosition: 'left' | 'right'
19-
onFinish: (values: any) => void
20-
onFinishFailed: (values: any, errorFields: any) => void
21-
}
7+
import { WebFormProps, FormInstance } from '@/types'
228

239
const defaultProps = {
2410
...ComponentDefaults,
@@ -27,17 +13,19 @@ const defaultProps = {
2713
disabled: false,
2814
divider: false,
2915
validateTrigger: 'onChange',
16+
onReset: () => {},
17+
onSubmit: () => {},
3018
onFinish: (values) => {},
3119
onFinishFailed: (values, errorFields) => {},
32-
} as FormProps
20+
} as WebFormProps
3321

3422
const PositionInfo: any = {
3523
top: 'form-layout-top',
3624
left: 'form-layout-left',
3725
right: 'form-layout-right',
3826
}
3927

40-
export const Form = React.forwardRef<FormInstance, Partial<FormProps>>(
28+
export const Form = React.forwardRef<FormInstance, Partial<WebFormProps>>(
4129
(props, ref) => {
4230
const classPrefix = 'nut-form'
4331
const {
@@ -50,6 +38,8 @@ export const Form = React.forwardRef<FormInstance, Partial<FormProps>>(
5038
disabled,
5139
onFinish,
5240
onFinishFailed,
41+
onSubmit,
42+
onReset,
5343
validateTrigger,
5444
labelPosition,
5545
starPosition,
@@ -94,11 +84,13 @@ export const Form = React.forwardRef<FormInstance, Partial<FormProps>>(
9484
e.preventDefault()
9585
e.stopPropagation()
9686
submit()
87+
onSubmit?.()
9788
}}
9889
onReset={(e) => {
9990
e.preventDefault()
10091
e.stopPropagation()
10192
resetFields()
93+
onReset?.()
10294
}}
10395
>
10496
<Cell.Group divider={divider}>

src/packages/form/index.taro.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
import React from 'react'
2-
import { Form, FormProps } from './form.taro'
2+
import { Form } from './form.taro'
33
import { FormItem } from '../formitem/formitem.taro'
4-
import { FormInstance } from './types'
54
import { useForm, useWatch } from '@/packages/form/useform.taro'
5+
import { FormInstance, TaroFormProps } from '@/types'
66

7-
export type { FormProps } from './form.taro'
87
export type {
98
FormItemRuleWithoutValidator,
109
FormInstance,
1110
FormFieldEntity,
1211
NamePath,
1312
Store,
14-
} from './types'
13+
TaroFormProps as FormProps,
14+
} from '@/types'
1515

1616
type CompoundedComponent = React.ForwardRefExoticComponent<
17-
Partial<FormProps> &
17+
Partial<TaroFormProps> &
1818
Omit<React.HTMLAttributes<HTMLDivElement>, 'onChange'> &
1919
React.RefAttributes<FormInstance>
2020
> & {

src/packages/form/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
import React from 'react'
2-
import { Form, FormProps } from './form'
2+
import { Form } from './form'
33
import { FormItem } from '../formitem/formitem'
4-
import { FormInstance } from './types'
54
import { useForm, useWatch } from '@/packages/form/useform'
5+
import { FormInstance, WebFormProps } from '@/types'
66

7-
export type { FormProps } from './form'
87
export type {
98
FormItemRuleWithoutValidator,
109
FormInstance,
1110
FormFieldEntity,
1211
NamePath,
1312
Store,
14-
} from './types'
13+
WebFormProps as FormProps,
14+
} from '@/types'
1515

1616
type CompoundedComponent = React.ForwardRefExoticComponent<
17-
Partial<FormProps> &
17+
Partial<WebFormProps> &
1818
Omit<React.HTMLAttributes<HTMLDivElement>, 'onChange'> &
1919
React.RefAttributes<FormInstance>
2020
> & {

src/packages/form/types.ts

Lines changed: 0 additions & 42 deletions
This file was deleted.

src/packages/form/useform.taro.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
FormInstance,
88
NamePath,
99
Store,
10-
} from './types'
10+
} from '@/types'
1111

1212
export const SECRET = 'NUT_FORM_INTERNAL'
1313
type UpdateItem = { entity: FormFieldEntity; condition: any }

src/packages/form/useform.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
FormInstance,
88
NamePath,
99
Store,
10-
} from './types'
10+
} from '@/types'
1111

1212
export const SECRET = 'NUT_FORM_INTERNAL'
1313
type UpdateItem = { entity: FormFieldEntity; condition: any }

src/packages/formitem/formitem.taro.tsx

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,12 @@
1-
import React, { ReactNode } from 'react'
1+
import React from 'react'
22
import { Text, View } from '@tarojs/components'
3-
import { BaseFormField } from './types'
43
import { Context } from '../form/context'
54
import Cell from '@/packages/cell/index.taro'
6-
import { BasicComponent, ComponentDefaults } from '@/utils/typings'
5+
import { ComponentDefaults } from '@/utils/typings'
76
import { isForwardRefComponent } from '@/utils/is-forward-ref-component'
87
import { toArray } from '@/utils/to-array'
98
import { SECRET } from '@/packages/form/useform.taro'
10-
11-
type TextAlign =
12-
| 'start'
13-
| 'end'
14-
| 'left'
15-
| 'right'
16-
| 'center'
17-
| 'justify'
18-
| 'match-parent'
19-
20-
type ShouldUpdate = (prevValue: any, curValue: any) => boolean
21-
22-
export interface FormItemProps
23-
extends Omit<BasicComponent, 'children'>,
24-
BaseFormField {
25-
required: boolean
26-
initialValue: any
27-
trigger: string
28-
valuePropName: string
29-
getValueFromEvent: (...args: any) => any
30-
onClick: (
31-
event: React.MouseEvent,
32-
componentRef: React.MutableRefObject<any>
33-
) => void
34-
errorMessageAlign: TextAlign
35-
validateTrigger: string | string[]
36-
shouldUpdate: boolean
37-
noStyle: boolean
38-
children: ReactNode | ((obj: any) => React.ReactNode)
39-
align?: 'flex-start' | 'center' | 'flex-end'
40-
}
9+
import { TaroFormItemProps } from '@/types'
4110

4211
const defaultProps = {
4312
...ComponentDefaults,
@@ -48,10 +17,10 @@ const defaultProps = {
4817
errorMessageAlign: 'left',
4918
shouldUpdate: false,
5019
noStyle: false,
51-
} as FormItemProps
20+
} as TaroFormItemProps
5221

5322
export class FormItem extends React.Component<
54-
Partial<FormItemProps>,
23+
Partial<TaroFormItemProps>,
5524
{ resetCount: number }
5625
> {
5726
static defaultProps = defaultProps
@@ -66,7 +35,7 @@ export class FormItem extends React.Component<
6635

6736
private eventOff: any
6837

69-
constructor(props: FormItemProps) {
38+
constructor(props: TaroFormItemProps) {
7039
super(props)
7140
this.componentRef = React.createRef()
7241
this.state = {

src/packages/formitem/formitem.tsx

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import React, { ReactNode } from 'react'
2-
import { BaseFormField } from './types'
1+
import React from 'react'
32
import { Context } from '../form/context'
43
import Cell from '@/packages/cell'
5-
import { BasicComponent, ComponentDefaults } from '@/utils/typings'
4+
import { ComponentDefaults } from '@/utils/typings'
65
import { isForwardRefComponent } from '@/utils/is-forward-ref-component'
76
import { toArray } from '@/utils/to-array'
87
import { SECRET } from '@/packages/form/useform'
8+
import { WebFormItemProps } from '@/types'
99

1010
type TextAlign =
1111
| 'start'
@@ -16,28 +16,6 @@ type TextAlign =
1616
| 'justify'
1717
| 'match-parent'
1818

19-
type ShouldUpdate = (prevValue: any, curValue: any) => boolean
20-
21-
export interface FormItemProps
22-
extends Omit<BasicComponent, 'children'>,
23-
BaseFormField {
24-
required: boolean
25-
initialValue: any
26-
trigger: string
27-
valuePropName: string
28-
getValueFromEvent: (...args: any) => any
29-
onClick: (
30-
event: React.MouseEvent,
31-
componentRef: React.MutableRefObject<any>
32-
) => void
33-
errorMessageAlign: TextAlign
34-
validateTrigger: string | string[]
35-
shouldUpdate: boolean
36-
noStyle: boolean
37-
children: ReactNode | ((obj: any) => React.ReactNode)
38-
align?: 'flex-start' | 'center' | 'flex-end'
39-
}
40-
4119
const defaultProps = {
4220
...ComponentDefaults,
4321
required: false,
@@ -47,10 +25,10 @@ const defaultProps = {
4725
errorMessageAlign: 'left',
4826
shouldUpdate: false,
4927
noStyle: false,
50-
} as FormItemProps
28+
} as WebFormItemProps
5129

5230
export class FormItem extends React.Component<
53-
Partial<FormItemProps>,
31+
Partial<WebFormItemProps>,
5432
{ resetCount: number }
5533
> {
5634
static defaultProps = defaultProps
@@ -65,7 +43,7 @@ export class FormItem extends React.Component<
6543

6644
private eventOff: any
6745

68-
constructor(props: FormItemProps) {
46+
constructor(props: WebFormItemProps) {
6947
super(props)
7048
this.componentRef = React.createRef()
7149
this.state = {

0 commit comments

Comments
 (0)