Skip to content

Commit a29dcb6

Browse files
authored
Merge pull request #208 from yaob421123/master
fix(ProForm): 问题修复
2 parents 6dbd6a0 + 4160808 commit a29dcb6

File tree

7 files changed

+42
-17
lines changed

7 files changed

+42
-17
lines changed

packages/components/src/ProForm/README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ const Demo = () => {
190190
<ProForm
191191
readOnly={true}
192192
formType="pure"
193+
className="bbb"
193194
formDatas={ [
194195
{
195196
label: 'input',
@@ -844,7 +845,7 @@ const Demo = () => {
844845
}
845846

846847
const handleSave = async ()=>{
847-
const validateList = formList.map(itm=>itm.validateFieldsAndGetValue()) || []
848+
const validateList = await formList.map(itm => itm.validateFieldsAndGetValue()) || []
848849
const values = await asyncAwaitFormList(validateList)
849850
setState(values)
850851
// 调用接口
@@ -979,7 +980,7 @@ const Demo = () => {
979980
}
980981

981982
const handleSave = async ()=>{
982-
const validateList = formList.map(itm=>itm.validateFieldsAndGetValue()) || []
983+
const validateList = formList.map(itm=> itm.validateFieldsAndGetValue()) || []
983984
const values = await asyncAwaitFormList(validateList)
984985
setState(values)
985986
// 调用接口
@@ -996,6 +997,7 @@ const Demo = () => {
996997
extra={<span onClick={handleAddFormItems.bind(this,'delete',idx)}>删除</span>}
997998
>
998999
<ProForm
1000+
className="aaa"
9991001
ref={(e) =>(formRefList.current[idx] = e)}
10001002
// 表单类型
10011003
formType="pure"

packages/components/src/ProForm/formdom.tsx

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ function FormDom({
1717
saveButtonProps = {},
1818
resetButtonProps = {},
1919
formInstanceRef,
20+
className,
21+
style,
2022
}: ProFormProps & {
2123
formfields: Record<string, FormFieldsProps<{}>> | undefined;
2224
formInstanceRef: React.MutableRefObject<
@@ -36,13 +38,23 @@ function FormDom({
3638

3739
// 通过ref获取表单实例
3840
useEffect(() => {
39-
formInstanceRef.current = baseRef;
40-
}, [baseRef]);
41+
if (baseRef && baseRef.current) {
42+
formInstanceRef.current = baseRef;
43+
}
44+
}, [baseRef, formInstanceRef]);
45+
46+
const styles: React.CSSProperties = {
47+
background: '#fff',
48+
paddingBottom: 10,
49+
marginBottom: 14,
50+
...style,
51+
};
4152

4253
return (
4354
<Form
55+
className={className}
4456
ref={baseRef}
45-
style={{ background: '#fff', paddingBottom: 10, marginBottom: 14 }}
57+
style={styles}
4658
resetOnSubmit={false}
4759
onSubmit={({ initial, current }) => {
4860
// 如果传入了onSubmit走onSubmit,否则主动验证

packages/components/src/ProForm/index.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,20 @@ function ProForm(
4141

4242
// 通过ref导出实例方法
4343
useImperativeHandle(ref, () => {
44-
const { onSubmit, getError, getFieldValues } =
45-
formInstanceRef?.current?.current || {};
4644
// 表单验证(同时兼容老api submitvalidate和新api onSubmit )
47-
const submitvalidate = () => onSubmit?.() || null;
45+
const submitvalidate = () => {
46+
const { onSubmit } = formInstanceRef?.current?.current || {};
47+
onSubmit?.() || null;
48+
};
4849
// 验证并获取表单值
4950
const validateFieldsAndGetValue = () => {
5051
return new Promise(async function (resolve, reject) {
52+
const { getError, getFieldValues } =
53+
formInstanceRef?.current?.current || {};
5154
await submitvalidate();
5255
const errors = getError?.() || {};
5356
if (isObjectEmpty(errors)) {
54-
const value = getFieldValues?.() || {};
57+
const value = (await getFieldValues?.()) || {};
5558
resolve(value);
5659
} else {
5760
reject(errors);

packages/components/src/ProForm/readform.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,20 @@ import { Descriptions } from 'uiw';
22
import { ProFormProps } from './type';
33
import { getReadValue } from './utils/index';
44

5-
export default ({ title, formDatas, readOnlyProps }: ProFormProps) => {
5+
export default ({
6+
title,
7+
formDatas,
8+
readOnlyProps,
9+
className,
10+
style,
11+
}: ProFormProps) => {
612
return (
713
<Descriptions
814
bordered
915
title={title}
1016
{...readOnlyProps}
11-
style={{ width: '100%' }}
17+
className={className}
18+
style={{ width: '100%', ...style }}
1219
>
1320
{formDatas?.map(
1421
(

packages/components/src/ProForm/type.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import {
1111
ColProps,
1212
} from 'uiw';
1313
export interface ProFormProps {
14+
style?: React.CSSProperties;
15+
className?: string;
1416
formDatas?: FormItemsProps[];
1517
onSubmit?: (
1618
initial: Record<string, any>,

packages/components/src/ProForm/utils/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ export const fromValidate = (rules: FromValidateProps[] = []) => {
179179
*/
180180
export const isRequired = (rules: any[] = []): boolean => {
181181
if (rules.length === 0) return false;
182-
const requireds = rules.find((item) => item.required) || [];
182+
const requireds = rules.filter((item) => item.required || false);
183183
if (requireds && requireds.length > 0) {
184184
return true;
185185
}

packages/components/src/ProForm/widgets/index.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Fields, FormItemsProps } from '../type';
22
import {
33
Input,
4+
InputNumber,
45
Switch,
56
Textarea,
67
DateInput,
@@ -30,6 +31,7 @@ export function getFormFields(
3031
) {
3132
const widgetsList: Fields = {
3233
input: Input,
34+
inputNumber: InputNumber,
3335
radio: Radio,
3436
checkbox: CheckBox,
3537
switch: Switch,
@@ -62,12 +64,9 @@ export function getFormFields(
6264
if (!hide) {
6365
const name = key;
6466
const Widget = widgetsList[widget];
67+
const is = isRequired(otherProps?.rules || []);
6568
fields[name] = {
66-
label: isRequired(otherProps.rules) ? (
67-
<span className="w-proform-label">{label}</span>
68-
) : (
69-
label
70-
),
69+
label: is ? <span className="w-proform-label">{label}</span> : label,
7170
children: <Widget {...widgetProps} />,
7271
...otherProps,
7372
initialValue,

0 commit comments

Comments
 (0)