Skip to content

Commit

Permalink
Merge pull request #208 from yaob421123/master
Browse files Browse the repository at this point in the history
fix(ProForm): 问题修复
  • Loading branch information
ChenlingasMx authored Apr 8, 2023
2 parents 6dbd6a0 + 4160808 commit a29dcb6
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 17 deletions.
6 changes: 4 additions & 2 deletions packages/components/src/ProForm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ const Demo = () => {
<ProForm
readOnly={true}
formType="pure"
className="bbb"
formDatas={ [
{
label: 'input',
Expand Down Expand Up @@ -844,7 +845,7 @@ const Demo = () => {
}

const handleSave = async ()=>{
const validateList = formList.map(itm=>itm.validateFieldsAndGetValue()) || []
const validateList = await formList.map(itm => itm.validateFieldsAndGetValue()) || []
const values = await asyncAwaitFormList(validateList)
setState(values)
// 调用接口
Expand Down Expand Up @@ -979,7 +980,7 @@ const Demo = () => {
}

const handleSave = async ()=>{
const validateList = formList.map(itm=>itm.validateFieldsAndGetValue()) || []
const validateList = formList.map(itm=> itm.validateFieldsAndGetValue()) || []
const values = await asyncAwaitFormList(validateList)
setState(values)
// 调用接口
Expand All @@ -996,6 +997,7 @@ const Demo = () => {
extra={<span onClick={handleAddFormItems.bind(this,'delete',idx)}>删除</span>}
>
<ProForm
className="aaa"
ref={(e) =>(formRefList.current[idx] = e)}
// 表单类型
formType="pure"
Expand Down
18 changes: 15 additions & 3 deletions packages/components/src/ProForm/formdom.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ function FormDom({
saveButtonProps = {},
resetButtonProps = {},
formInstanceRef,
className,
style,
}: ProFormProps & {
formfields: Record<string, FormFieldsProps<{}>> | undefined;
formInstanceRef: React.MutableRefObject<
Expand All @@ -36,13 +38,23 @@ function FormDom({

// 通过ref获取表单实例
useEffect(() => {
formInstanceRef.current = baseRef;
}, [baseRef]);
if (baseRef && baseRef.current) {
formInstanceRef.current = baseRef;
}
}, [baseRef, formInstanceRef]);

const styles: React.CSSProperties = {
background: '#fff',
paddingBottom: 10,
marginBottom: 14,
...style,
};

return (
<Form
className={className}
ref={baseRef}
style={{ background: '#fff', paddingBottom: 10, marginBottom: 14 }}
style={styles}
resetOnSubmit={false}
onSubmit={({ initial, current }) => {
// 如果传入了onSubmit走onSubmit,否则主动验证
Expand Down
11 changes: 7 additions & 4 deletions packages/components/src/ProForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,20 @@ function ProForm(

// 通过ref导出实例方法
useImperativeHandle(ref, () => {
const { onSubmit, getError, getFieldValues } =
formInstanceRef?.current?.current || {};
// 表单验证(同时兼容老api submitvalidate和新api onSubmit )
const submitvalidate = () => onSubmit?.() || null;
const submitvalidate = () => {
const { onSubmit } = formInstanceRef?.current?.current || {};
onSubmit?.() || null;
};
// 验证并获取表单值
const validateFieldsAndGetValue = () => {
return new Promise(async function (resolve, reject) {
const { getError, getFieldValues } =
formInstanceRef?.current?.current || {};
await submitvalidate();
const errors = getError?.() || {};
if (isObjectEmpty(errors)) {
const value = getFieldValues?.() || {};
const value = (await getFieldValues?.()) || {};
resolve(value);
} else {
reject(errors);
Expand Down
11 changes: 9 additions & 2 deletions packages/components/src/ProForm/readform.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@ import { Descriptions } from 'uiw';
import { ProFormProps } from './type';
import { getReadValue } from './utils/index';

export default ({ title, formDatas, readOnlyProps }: ProFormProps) => {
export default ({
title,
formDatas,
readOnlyProps,
className,
style,
}: ProFormProps) => {
return (
<Descriptions
bordered
title={title}
{...readOnlyProps}
style={{ width: '100%' }}
className={className}
style={{ width: '100%', ...style }}
>
{formDatas?.map(
(
Expand Down
2 changes: 2 additions & 0 deletions packages/components/src/ProForm/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
ColProps,
} from 'uiw';
export interface ProFormProps {
style?: React.CSSProperties;
className?: string;
formDatas?: FormItemsProps[];
onSubmit?: (
initial: Record<string, any>,
Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/ProForm/utils/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ export const fromValidate = (rules: FromValidateProps[] = []) => {
*/
export const isRequired = (rules: any[] = []): boolean => {
if (rules.length === 0) return false;
const requireds = rules.find((item) => item.required) || [];
const requireds = rules.filter((item) => item.required || false);
if (requireds && requireds.length > 0) {
return true;
}
Expand Down
9 changes: 4 additions & 5 deletions packages/components/src/ProForm/widgets/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Fields, FormItemsProps } from '../type';
import {
Input,
InputNumber,
Switch,
Textarea,
DateInput,
Expand Down Expand Up @@ -30,6 +31,7 @@ export function getFormFields(
) {
const widgetsList: Fields = {
input: Input,
inputNumber: InputNumber,
radio: Radio,
checkbox: CheckBox,
switch: Switch,
Expand Down Expand Up @@ -62,12 +64,9 @@ export function getFormFields(
if (!hide) {
const name = key;
const Widget = widgetsList[widget];
const is = isRequired(otherProps?.rules || []);
fields[name] = {
label: isRequired(otherProps.rules) ? (
<span className="w-proform-label">{label}</span>
) : (
label
),
label: is ? <span className="w-proform-label">{label}</span> : label,
children: <Widget {...widgetProps} />,
...otherProps,
initialValue,
Expand Down

0 comments on commit a29dcb6

Please sign in to comment.