🆕 优化表单组件, 添加一键导入json文件功能,可以基于json一键生成页面

This commit is contained in:
xujiang 2020-11-12 20:31:28 +08:00
parent 4523394462
commit 255d24f80f
6 changed files with 103 additions and 60 deletions

View File

@ -110,7 +110,7 @@ const Form: IFormSchema = {
titColor: 'rgba(60,60,60,1)', titColor: 'rgba(60,60,60,1)',
titWeight: '400', titWeight: '400',
bgColor: 'rgba(255,255,255,1)', bgColor: 'rgba(255,255,255,1)',
btnColor: 'rgba(129,173,173,1)', btnColor: 'rgba(20,54,226,100)',
btnTextColor: 'rgba(255,255,255,1)', btnTextColor: 'rgba(255,255,255,1)',
api: '', api: '',
formControls: [ formControls: [

View File

@ -188,6 +188,13 @@ const Container = (props: {
}; };
}, [dispatch]); }, [dispatch]);
const importTpl = data => {
dispatch({
type: 'editorModal/importTplData',
payload: data,
});
};
useEffect(() => { useEffect(() => {
if (window.innerWidth < 1024) { if (window.innerWidth < 1024) {
props.history.push('/mobileTip'); props.history.push('/mobileTip');
@ -440,6 +447,7 @@ const Container = (props: {
pointData={context.theme === 'h5' ? pointData : cpointData} pointData={context.theme === 'h5' ? pointData : cpointData}
clearData={clearData} clearData={clearData}
location={props.location} location={props.location}
importTpl={importTpl}
/> />
<div className={styles.container}> <div className={styles.container}>
<div <div

View File

@ -89,55 +89,70 @@ const SourceBox = memo((props: SourceBoxProps) => {
const dragStop: ItemCallback = useMemo(() => { const dragStop: ItemCallback = useMemo(() => {
return (layout, oldItem, newItem, placeholder, e, element) => { return (layout, oldItem, newItem, placeholder, e, element) => {
if (context.theme === 'h5') {
const curPointData = pointData.filter(item => item.id === newItem.i)[0]; const curPointData = pointData.filter(item => item.id === newItem.i)[0];
dispatch({ dispatch({
type: 'editorModal/modPointData', type: 'editorModal/modPointData',
payload: { ...curPointData, point: newItem, status: 'inToCanvas' }, payload: { ...curPointData, point: newItem, status: 'inToCanvas' },
}); });
} else { // if (context.theme === 'h5') {
const curPointData = cpointData.filter(item => item.id === newItem.i)[0]; // const curPointData = pointData.filter(item => item.id === newItem.i)[0];
dispatch({ // dispatch({
type: 'editorPcModal/modPointData', // type: 'editorModal/modPointData',
payload: { ...curPointData, point: newItem, status: 'inToCanvas' }, // payload: { ...curPointData, point: newItem, status: 'inToCanvas' },
}); // });
} // } else {
// const curPointData = cpointData.filter(item => item.id === newItem.i)[0];
// dispatch({
// type: 'editorPcModal/modPointData',
// payload: { ...curPointData, point: newItem, status: 'inToCanvas' },
// });
// }
}; };
}, [context.theme, cpointData, dispatch, pointData]); }, [context.theme, cpointData, dispatch, pointData]);
const onDragStart: ItemCallback = useMemo(() => { const onDragStart: ItemCallback = useMemo(() => {
return (layout, oldItem, newItem, placeholder, e, element) => { return (layout, oldItem, newItem, placeholder, e, element) => {
if (context.theme === 'h5') {
const curPointData = pointData.filter(item => item.id === newItem.i)[0]; const curPointData = pointData.filter(item => item.id === newItem.i)[0];
dispatch({ dispatch({
type: 'editorModal/modPointData', type: 'editorModal/modPointData',
payload: { ...curPointData, status: 'inToCanvas' }, payload: { ...curPointData, status: 'inToCanvas' },
}); });
} else { // if (context.theme === 'h5') {
const curPointData = cpointData.filter(item => item.id === newItem.i)[0]; // const curPointData = pointData.filter(item => item.id === newItem.i)[0];
dispatch({ // dispatch({
type: 'editorPcModal/modPointData', // type: 'editorModal/modPointData',
payload: { ...curPointData, status: 'inToCanvas' }, // payload: { ...curPointData, status: 'inToCanvas' },
}); // });
} // } else {
// const curPointData = cpointData.filter(item => item.id === newItem.i)[0];
// dispatch({
// type: 'editorPcModal/modPointData',
// payload: { ...curPointData, status: 'inToCanvas' },
// });
// }
}; };
}, [context.theme, cpointData, dispatch, pointData]); }, [context.theme, cpointData, dispatch, pointData]);
const onResizeStop: ItemCallback = useMemo(() => { const onResizeStop: ItemCallback = useMemo(() => {
return (layout, oldItem, newItem, placeholder, e, element) => { return (layout, oldItem, newItem, placeholder, e, element) => {
if (context.theme === 'h5') {
const curPointData = pointData.filter(item => item.id === newItem.i)[0]; const curPointData = pointData.filter(item => item.id === newItem.i)[0];
dispatch({ dispatch({
type: 'editorModal/modPointData', type: 'editorModal/modPointData',
payload: { ...curPointData, point: newItem, status: 'inToCanvas' }, payload: { ...curPointData, point: newItem, status: 'inToCanvas' },
}); });
} else { // if (context.theme === 'h5') {
const curPointData = cpointData.filter(item => item.id === newItem.i)[0]; // const curPointData = pointData.filter(item => item.id === newItem.i)[0];
dispatch({ // dispatch({
type: 'editorPcModal/modPointData', // type: 'editorModal/modPointData',
payload: { ...curPointData, point: newItem, status: 'inToCanvas' }, // payload: { ...curPointData, point: newItem, status: 'inToCanvas' },
}); // });
} // } else {
// const curPointData = cpointData.filter(item => item.id === newItem.i)[0];
// dispatch({
// type: 'editorPcModal/modPointData',
// payload: { ...curPointData, point: newItem, status: 'inToCanvas' },
// });
// }
}; };
}, [context.theme, cpointData, dispatch, pointData]); }, [context.theme, cpointData, dispatch, pointData]);
const initSelect: any = (data: any = []) => { const initSelect: any = (data: any = []) => {
@ -249,17 +264,6 @@ const SourceBox = memo((props: SourceBoxProps) => {
data-grid={value.point} data-grid={value.point}
> >
<DynamicEngine {...value.item} isTpl={false} /> <DynamicEngine {...value.item} isTpl={false} />
{/* <div
className={styles.tooltip}
style={{ display: value.isMenu ? 'block' : 'none' }}
>
<div className="tooltipRow1">
<a></a>
</div>
<div className="tooltipRow2">
<a></a>
</div>
</div> */}
</div> </div>
))} ))}
</GridLayout> </GridLayout>

View File

@ -35,7 +35,7 @@
text-align: center; text-align: center;
} }
.btnArea { .btnArea {
width: 390px; width: 250px;
margin-left: 20px; margin-left: 20px;
text-align: right; text-align: right;
} }
@ -53,8 +53,5 @@
.controlArea { .controlArea {
width: auto; width: auto;
} }
.btnArea {
width: 30%;
}
} }
} }

View File

@ -1,5 +1,5 @@
import React, { useRef, memo, useContext, useState, useEffect } from 'react'; import React, { useRef, memo, useMemo, useContext, useState, useEffect } from 'react';
import { Button, Input, Modal, Select } from 'antd'; import { Button, Input, Modal, Select, Upload } from 'antd';
import { import {
ArrowLeftOutlined, ArrowLeftOutlined,
MobileOutlined, MobileOutlined,
@ -11,6 +11,7 @@ import {
FileAddOutlined, FileAddOutlined,
CodeOutlined, CodeOutlined,
SketchOutlined, SketchOutlined,
UploadOutlined,
} from '@ant-design/icons'; } from '@ant-design/icons';
import { history } from 'umi'; import { history } from 'umi';
import QRCode from 'qrcode.react'; import QRCode from 'qrcode.react';
@ -31,10 +32,11 @@ interface HeaderComponentProps {
clearData: any; clearData: any;
undohandler: any; undohandler: any;
redohandler: any; redohandler: any;
importTpl: any;
} }
const HeaderComponent = memo((props: HeaderComponentProps) => { const HeaderComponent = memo((props: HeaderComponentProps) => {
const { pointData, location, clearData, undohandler, redohandler } = props; const { pointData, location, clearData, undohandler, redohandler, importTpl } = props;
const [showModalIframe, setShowModalIframe] = useState(false); const [showModalIframe, setShowModalIframe] = useState(false);
const iptRef = useRef<Input>(null); const iptRef = useRef<Input>(null);
@ -185,6 +187,24 @@ const HeaderComponent = memo((props: HeaderComponentProps) => {
setShowModalIframe(false); setShowModalIframe(false);
}; };
}, []); }, []);
const uploadprops = useMemo(
() => ({
name: 'file',
showUploadList: false,
beforeUpload(file, fileList) {
// 解析并提取excel数据
let reader = new FileReader();
reader.onload = function(e: Event) {
let data = (e as any).target.result;
importTpl && importTpl(JSON.parse(data));
};
reader.readAsText(file);
},
}),
[],
);
const { setTheme } = useContext(dooringContext); const { setTheme } = useContext(dooringContext);
return ( return (
<div className={styles.header}> <div className={styles.header}>
@ -206,6 +226,11 @@ const HeaderComponent = memo((props: HeaderComponentProps) => {
> >
</Button> </Button>
<Upload {...uploadprops}>
<Button type="link" style={{ marginRight: '8px' }}>
<UploadOutlined />
</Button>
</Upload>
<Button <Button
type="link" type="link"
style={{ marginRight: '9px' }} style={{ marginRight: '9px' }}
@ -277,7 +302,8 @@ const HeaderComponent = memo((props: HeaderComponentProps) => {
</Button> </Button>
</div> </div>
<div className={styles.btnArea}> <div className={styles.btnArea}>
<Select {/* 隐藏pc端切换, 保证代码纯粹 */}
{/* <Select
defaultValue="h5" defaultValue="h5"
style={{ width: 100, marginRight: 20 }} style={{ width: 100, marginRight: 20 }}
onChange={e => { onChange={e => {
@ -286,7 +312,7 @@ const HeaderComponent = memo((props: HeaderComponentProps) => {
> >
<Select.Option value="h5">h5模式</Select.Option> <Select.Option value="h5">h5模式</Select.Option>
<Select.Option value="pc">pc模式</Select.Option> <Select.Option value="pc">pc模式</Select.Option>
</Select> </Select> */}
<Button type="primary" ghost onClick={toOnlineCoding} style={{ marginRight: '12px' }}> <Button type="primary" ghost onClick={toOnlineCoding} style={{ marginRight: '12px' }}>
<CodeOutlined /> <CodeOutlined />
线 线

View File

@ -44,6 +44,14 @@ export default {
curPoint: payload, curPoint: payload,
}; };
}, },
importTplData(state, { payload }) {
overSave('userData', payload);
return {
...state,
pointData: payload,
curPoint: null,
};
},
copyPointData(state, { payload }) { copyPointData(state, { payload }) {
const { id } = payload; const { id } = payload;
const pointData = []; const pointData = [];