Skip to content

Commit

Permalink
Merge pull request #1504 from alibaba/fix-drawer-list
Browse files Browse the repository at this point in the history
feat: 重构 drawerList
  • Loading branch information
lhbxs committed Mar 23, 2024
2 parents 001ae85 + cffd069 commit 18e277a
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 60 deletions.
1 change: 0 additions & 1 deletion packages/form-render/src/render-core/FieldItem/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ export default (props: any) => {
const initialValue = schema.default ?? schema.defaultValue;
const classRest = { 'fr-hide-label': label === 'fr-hide-label', 'fr-inline-field': inlineSelf, 'fr-field-visibility': !visible, [schema.className] : !! schema.className };

debugger;
const formItem = (
<Form.Item
className={classnames('fr-field', classRest)}
Expand Down
4 changes: 2 additions & 2 deletions packages/form-render/src/render-core/FieldList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ export default (props: any) => {
const { schema, rootPath } = props;
const { items, ...listSchema } = schema || {};

const store = useContext(FRContext);
const store:any = useContext(FRContext);
const { schema: formSchema } = store.getState();

const configCtx = useContext(ConfigContext);
const configCtx: any = useContext(ConfigContext);
const mustacheDisabled = configCtx?.globalConfig?.mustacheDisabled;
const dependencies = schema?.dependencies;

Expand Down
2 changes: 1 addition & 1 deletion packages/form-render/src/render-core/FieldList/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default (props: any) => {
const [_, setListData] = useState([]);
const { configContext } = props;

const store = useContext(FRContext);
const store: any = useContext(FRContext);

const formCtx: any = useStore(store, (state: any) => state.context);
const upperCtx: any = useContext(UpperContext);
Expand Down
2 changes: 0 additions & 2 deletions packages/form-render/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,6 @@ export function isCheckBoxType(schema: any, readOnly: boolean) {
}
}



export const translation = (configCtx: any) => (key: string) => {
const locale = configCtx?.locale.FormRender;
return locale[key];
Expand Down
37 changes: 7 additions & 30 deletions packages/form-render/src/widgets/listDrawer/drawerForm.tsx
Original file line number Diff line number Diff line change
@@ -1,57 +1,34 @@
import React, { useContext } from 'react';
import { Button, Drawer, Space, ConfigProvider } from 'antd';
import FRender, { useForm } from '../../index';
import { translation } from '../utils';

const DrawerForm = (props: any) => {
const { schema, widgets, onClose, data, configContext } = props;
const { children, onConfirm, onClose } = props;

const form: any = useForm();
const configCtx = useContext(ConfigProvider.ConfigContext);
const t = translation(configCtx);

const handleFinish = (data: any) => {
onClose(data);
};

const handleClose = () => {
onClose();
};

let drawerProps: any = {
open: true
};
let drawerProps: any = { open: true };
if ((window as any).antdVersion === 'v4') {
drawerProps = {
visible: true
};
drawerProps = { visible: true };
}

return (
<Drawer
{...drawerProps}
width={600}
title={t('operate')}
onClose={handleClose}
onClose={onClose}
extra={
<Space>
<Button onClick={handleClose}>{t('cancel')}</Button>
<Button type="primary" onClick={form.submit}>
<Button onClick={onClose}>{t('cancel')}</Button>
<Button type='primary' onClick={onConfirm}>
{t('confirm')}
</Button>
</Space>
}
>
<FRender
schema={schema.items}
initialValues={data}
form={form}
labelCol={{ span: 8 }}
widgets={widgets}
onFinish={handleFinish}
locale={configContext?.locale}
maxWidth='auto'
/>
{children}
</Drawer>
);
}
Expand Down
43 changes: 20 additions & 23 deletions packages/form-render/src/widgets/listDrawer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@



import React, { useState, useRef, useContext } from 'react';
import { Space, Table, Form, Button, Popconfirm, ConfigProvider, Tooltip, Divider } from 'antd';
import React, { useState, useRef } from 'react';
import { Space, Table, Form, Button, Popconfirm, Tooltip, Divider } from 'antd';
import { ArrowDownOutlined, ArrowUpOutlined, PlusOutlined, InfoCircleOutlined, CloseOutlined, CopyOutlined } from '@ant-design/icons';
import type { FormListFieldData, FormListOperation, TableColumnsType } from 'antd';
import FormDrawer from './drawerForm';
import FButton from '../components/FButton';
import { translation } from '../utils';
import './index.less';

interface Props {
Expand Down Expand Up @@ -61,6 +60,7 @@ const TableList: React.FC<Props> = (props: any) => {
hideAdd,
hideEdit,

operation,
addItem,
copyItem,
moveItem,
Expand All @@ -69,17 +69,11 @@ const TableList: React.FC<Props> = (props: any) => {
} = props;

const { colHeaderText, ...otherActionColumnProps } = actionColumnProps;


const configCtx = useContext(ConfigProvider.ConfigContext);
const t = translation(configCtx);

const paginationConfig = {
size: 'small',
hideOnSinglePage: true,
...pagination,
};

const columnSchema = schema?.items?.properties || {};

const [visible, setVisible] = useState(false);
Expand All @@ -92,23 +86,23 @@ const TableList: React.FC<Props> = (props: any) => {
};

const handleAdd = () => {
indexRef.current = -1;
setVisible(true);
addItem();
};

const handleDrawerClose = (data: any) => {
setVisible(false);
setItemData(null);
if (!data) {
return;
}

if (indexRef.current === -1) {
addItem(data);
const handleRepeal = () => {
if (!indexRef.current && indexRef.current !== 0) {
operation.remove(fields.length-1);
} else {
form.setFieldValue([...rootPath, indexRef.current], data);
form.setFieldValue([...rootPath, indexRef.current], itemData);
}
indexRef.current === null;
hanldeConfirm();
};

const hanldeConfirm = () => {
setItemData(null);
setVisible(false);
indexRef.current = null;
};

const columns: TableColumnsType<FormListFieldData> = Object.keys(columnSchema).map(dataIndex => {
Expand Down Expand Up @@ -228,8 +222,11 @@ const TableList: React.FC<Props> = (props: any) => {
data={itemData}
widgets={widgets}
configContext={configContext}
onClose={handleDrawerClose}
/>
onClose={handleRepeal}
onConfirm={hanldeConfirm}
>
{renderCore({ schema: schema.items, parentPath: [fields.length - 1], rootPath: [...rootPath, fields.length - 1] })}
</FormDrawer>
)}
</div>
);
Expand Down
17 changes: 17 additions & 0 deletions packages/form-render/src/widgets/utils/hooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { message } from 'antd';
import { useReducer, useRef, useEffect, useState } from 'react';

export function usePrevious(value: any) {
const ref = useRef();
useEffect(() => {
ref.current = value;
}, [value]);
return ref.current;
}

// 类似于class component的setState
export const useSet = (initState: any) => {
return useReducer((state: any, newState: any) => {
return { ...state, ...newState };
}, initState);
};
2 changes: 1 addition & 1 deletion packages/form-render/src/withProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export default function withProvider<T>(Element: React.ComponentType<T>, default

const langPack: any = {
...antdLocale,
"FormRender": locales[locale],
'FormRender': locales[locale],
...configProvider?.locale
};

Expand Down

0 comments on commit 18e277a

Please sign in to comment.