Skip to content

Commit a7c2126

Browse files
committed
refactor(Form): 重构表单组件 uiwjs#845
1 parent b1b0d1b commit a7c2126

File tree

7 files changed

+98
-26
lines changed

7 files changed

+98
-26
lines changed

packages/react-form/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
"dependencies": {
4747
"@uiw/react-grid": "^4.21.2",
4848
"@uiw/react-input": "^4.21.2",
49-
"@uiw/utils": "^4.21.2"
49+
"@uiw/utils": "^4.21.2",
50+
"@uiw/react-select": "^4.21.2",
51+
"@uiw/react-textarea": "^4.21.2"
5052
}
5153
}

packages/react-form/src/Form.tsx

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import React, { useState, useImperativeHandle, useMemo } from 'react';
22
import { IProps } from '@uiw/utils';
33
import FormItem, { FormItemProps } from './FormItem';
4-
import './style/form.less';
4+
// import './style/form.less';
5+
import { FormWarp, Fieldset } from './style';
56

67
export interface FormProps<T> extends IProps, Omit<React.FormHTMLAttributes<HTMLFormElement>, 'onChange' | 'onSubmit'> {
78
prefixCls?: string;
@@ -59,7 +60,10 @@ export type FormElementProps = {
5960
onChange?: (env: React.BaseSyntheticEvent<HTMLInputElement>, list?: string[]) => void;
6061
};
6162

62-
export type FormRefType = Record<'onSubmit' | 'resetForm' | 'getFieldValues' | 'setFields' | 'getError' | 'setFieldValue', Function>;
63+
export type FormRefType = Record<
64+
'onSubmit' | 'resetForm' | 'getFieldValues' | 'setFields' | 'getError' | 'setFieldValue',
65+
Function
66+
>;
6367

6468
function newFormState<T>(
6569
fields: FormProps<T>['fields'],
@@ -303,14 +307,14 @@ function Form<T>(
303307
}
304308

305309
return (
306-
<form
310+
<FormWarp
307311
{...{
308312
...others,
309313
className: [prefixCls, className].filter(Boolean).join(' ').trim(),
310314
onSubmit: handleSubmit,
311315
}}
312316
>
313-
<fieldset {...{ disabled: data.submitting }}>
317+
<Fieldset {...{ disabled: data.submitting }}>
314318
{typeof children === 'function'
315319
? children({
316320
fields: formUnits,
@@ -319,8 +323,8 @@ function Form<T>(
319323
canSubmit: canSubmit,
320324
})
321325
: children}
322-
</fieldset>
323-
</form>
326+
</Fieldset>
327+
</FormWarp>
324328
);
325329
}
326330

packages/react-form/src/FormItem.tsx

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import React from 'react';
22
import { Col, Row } from '@uiw/react-grid';
33
import { IProps, HTMLInputProps } from '@uiw/utils';
44
import { FormFieldsProps } from './Form';
5-
import './style/form-item.less';
5+
// import './style/form-item.less';
6+
import { ParentDiv, LabelStyle, HelpStyle, RowStyle } from './style/item';
67

78
export interface FormItemProps<T> extends IProps, HTMLInputProps {
89
inline?: boolean;
@@ -43,37 +44,39 @@ export default class FormItem<T> extends React.PureComponent<FormItemProps<T>> {
4344
const labelCls = ['w-form-label', labelClassName].filter(Boolean).join(' ').trim();
4445
if (inline) {
4546
return (
46-
<div className={cls} style={style} {...otherProps}>
47-
<Row>
48-
<Col fixed className={labelCls}>
47+
<ParentDiv hasError={hasError} className={cls} style={style} {...otherProps}>
48+
<RowStyle as={Row}>
49+
<LabelStyle as={Col} fixed className={labelCls}>
4950
{required && <label style={{ color: 'red' }}>*</label>}
50-
<label style={labelStyle} htmlFor={labelFor}>
51+
<LabelStyle style={labelStyle} htmlFor={labelFor}>
5152
{label}
52-
</label>
53-
</Col>
53+
</LabelStyle>
54+
</LabelStyle>
5455
<Col className="w-form-row">{this.props.children}</Col>
55-
</Row>
56+
</RowStyle>
5657
{help && (
57-
<Row>
58-
<Col className="w-form-help">{help}</Col>
59-
</Row>
58+
<RowStyle as={Row}>
59+
<HelpStyle as={Col} className="w-form-help">
60+
{help}
61+
</HelpStyle>
62+
</RowStyle>
6063
)}
61-
</div>
64+
</ParentDiv>
6265
);
6366
}
6467
return (
65-
<div className={cls} style={style} {...otherProps}>
68+
<ParentDiv hasError={hasError} className={cls} style={style} {...otherProps}>
6669
{label && (
6770
<React.Fragment>
6871
{required && <label style={{ color: 'red' }}>*</label>}
69-
<label className={labelCls} style={labelStyle} htmlFor={labelFor}>
72+
<LabelStyle className={labelCls} style={labelStyle} htmlFor={labelFor}>
7073
{label}
71-
</label>
74+
</LabelStyle>
7275
</React.Fragment>
7376
)}
7477
<Col className="w-form-row">{this.props.children}</Col>
75-
{help && <div className="w-form-help">{help}</div>}
76-
</div>
78+
{help && <HelpStyle className="w-form-help">{help}</HelpStyle>}
79+
</ParentDiv>
7780
);
7881
}
7982
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import styled from 'styled-components';
2+
import { ThemeVariantValueOptions } from '@uiw/utils';
3+
4+
export interface FormStyleProps extends ThemeVariantValueOptions {}
5+
6+
export const FormWarp = styled.form<FormStyleProps>``;
7+
8+
export const Fieldset = styled.fieldset`
9+
margin: 0;
10+
padding: 0;
11+
border-width: 0;
12+
`;

packages/react-form/src/style/item.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import styled, { css } from 'styled-components';
2+
import { HTMLInputProps, ThemeVariantValueOptions } from '@uiw/utils';
3+
import { InputBase } from '@uiw/react-input';
4+
import { SelectWarp } from '@uiw/react-select';
5+
import { TextareaWarp } from '@uiw/react-textarea';
6+
7+
interface ParentDivProps extends HTMLInputProps, ThemeVariantValueOptions {
8+
hasError?: boolean;
9+
}
10+
11+
export const LabelStyle = styled.label`
12+
line-height: 32px;
13+
min-height: 32px;
14+
font-weight: 600;
15+
font-size: 14px;
16+
padding-right: 5px;
17+
label {
18+
display: inline-block;
19+
}
20+
`;
21+
22+
export const HelpStyle = styled.div`
23+
color: #c2c2c2;
24+
font-size: 12px;
25+
padding-top: 3px;
26+
`;
27+
export const RowStyle = styled.div`
28+
align-items: center;
29+
display: flex;
30+
`;
31+
32+
export const ParentDiv = styled.div<ParentDivProps>`
33+
margin-bottom: 10px;
34+
${(props) =>
35+
props.hasError &&
36+
css`
37+
${InputBase},${SelectWarp},${TextareaWarp} {
38+
box-shadow: 0 0 0 1px #dc3545, 0 0 0 3px rgba(220, 53, 69, 0.17), inset 0 1px 1px rgba(16, 22, 26, 0.2);
39+
&:hover {
40+
box-shadow: 0 0 0 1px #dc3545, 0 0 0 3px rgba(220, 53, 69, 0.17), inset 0 1px 1px rgba(16, 22, 26, 0.2);
41+
}
42+
&:focus,
43+
&:hover {
44+
box-shadow: 0 0 0 1px #dc3545, 0 0 0 3px rgba(220, 53, 69, 0.17), inset 0 1px 1px rgba(16, 22, 26, 0.2);
45+
}
46+
}
47+
${LabelStyle}, ${HelpStyle} {
48+
color: #dc3545;
49+
}
50+
`}
51+
`;

packages/react-select/src/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Group from './Group';
55
// import './style/index.less';
66

77
import { SelectWarp } from './style';
8-
8+
export * from './style';
99
export interface SelectProps extends IProps, Omit<React.SelectHTMLAttributes<HTMLSelectElement>, 'size'> {
1010
size?: 'large' | 'default' | 'small';
1111
}

packages/react-textarea/src/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from 'react';
22
import { IProps, HTMLTextProps } from '@uiw/utils';
33
// import './style/index.less';
44
import { TextareaWarp } from './style/index';
5-
5+
export * from './style';
66
export interface TextareaProps extends IProps, HTMLTextProps {}
77

88
export default React.forwardRef<HTMLTextAreaElement, TextareaProps>((props, ref) => {

0 commit comments

Comments
 (0)