123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563 |
- import { Form, Button, Switch, Input, Radio, Space, Row } from 'antd';
- import React, { useMemo, useState, useEffect } from 'react';
- import { DeleteOutlined } from '@ant-design/icons';
- function ItemAttribute(props) {
- const { item, onChange, onRelClick } = props;
- if (!item) return null;
- const renderForm = () => {
- let FormContent;
- const formProps = {
- btns: (
- <Form.Item>
- <Button type="primary" htmlType="submit" style={{ marginRight: 20 }}>
- 保存
- </Button>
- </Form.Item>
- ),
- item,
- onFinish: (values) => {
- console.log(values);
- onChange?.(values);
- },
- };
- switch (item.componentName) {
- case 'InnerContactField':
- FormContent = <InnerContactField {...formProps} />;
- break;
- case 'DepartmentField':
- FormContent = <DepartmentField {...formProps} />;
- break;
- case 'ProjectField':
- FormContent = <ProjectField {...formProps} />;
- break;
- case 'ManufacturerField':
- FormContent = <ManufacturerField {...formProps} />;
- break;
- case 'TextField':
- FormContent = <TextField {...formProps} />;
- break;
- case 'TextareaField':
- FormContent = <TextareaField {...formProps} />;
- break;
- case 'DDSelectField':
- FormContent = <DDSelectField onRelClick={onRelClick} {...formProps} />;
- break;
- case 'DDMultiSelectField':
- FormContent = <DDMultiSelectField {...formProps} />;
- break;
- case 'NumberField':
- FormContent = <NumberField {...formProps} />;
- break;
- case 'TextNote':
- FormContent = <TextNote {...formProps} />;
- break;
- case 'DDAttachment':
- FormContent = <DDAttachment {...formProps} />;
- break;
- case 'DDDateField':
- FormContent = <DDDateField {...formProps} />;
- break;
- case 'DIYTable':
- FormContent = <TableName {...formProps} />;
- }
- return FormContent;
- };
- return (
- <div
- style={{
- // position: 'absolute',
- // top: 0,
- // right: 0,
- width: 500,
- height: 636,
- overflowY: 'auto',
- }}
- >
- {renderForm()}
- </div>
- );
- }
- export default ItemAttribute;
- function InnerContactField(props) {
- const { item, btns, onFinish } = props;
- const [form] = Form.useForm();
- const onSwitchChange = (checked) => {
- form.setFieldValue('choice', checked ? 1 : 0);
- };
- return (
- <Form
- form={form}
- labelCol={{ span: 4 }}
- wrapperCol={{ span: 20 }}
- autoComplete="off"
- initialValues={item.props}
- onFinish={onFinish}
- >
- <Form.Item label="标题" name="label">
- <Input />
- </Form.Item>
- <Form.Item label="提示文字" name="placeholder">
- <Input />
- </Form.Item>
- <Form.Item label="选项" name="choice">
- <Radio.Group>
- <Space direction="vertical">
- <Radio value={'0'}>只能选择一人</Radio>
- <Radio value={'1'}>可同时选择多人</Radio>
- </Space>
- </Radio.Group>
- </Form.Item>
- <Form.Item label="必填" name="required" valuePropName="checked">
- <Switch />
- </Form.Item>
- {btns}
- </Form>
- );
- }
- function DepartmentField(props) {
- const { item, btns, onFinish } = props;
- const [form] = Form.useForm();
- return (
- <Form
- form={form}
- labelCol={{ span: 4 }}
- wrapperCol={{ span: 20 }}
- autoComplete="off"
- initialValues={item.props}
- onFinish={onFinish}
- >
- <Form.Item label="标题" name="label">
- <Input />
- </Form.Item>
- <Form.Item label="提示文字" name="placeholder">
- <Input />
- </Form.Item>
- <Form.Item label="选项" name="choice">
- <Radio.Group>
- <Space direction="vertical">
- <Radio value={'0'}>只能选择一人</Radio>
- <Radio value={'1'}>可同时选择多人</Radio>
- </Space>
- </Radio.Group>
- </Form.Item>
- <Form.Item label="必填" name="required" valuePropName="checked">
- <Switch />
- </Form.Item>
- {btns}
- </Form>
- );
- }
- function ProjectField(props) {
- const { item, btns, onFinish } = props;
- const [form] = Form.useForm();
- return (
- <Form
- form={form}
- labelCol={{ span: 4 }}
- wrapperCol={{ span: 20 }}
- autoComplete="off"
- initialValues={item.props}
- onFinish={onFinish}
- >
- <Form.Item label="标题" name="label">
- <Input />
- </Form.Item>
- <Form.Item label="提示文字" name="placeholder">
- <Input />
- </Form.Item>
- <Form.Item label="必填" name="required" valuePropName="checked">
- <Switch />
- </Form.Item>
- {btns}
- </Form>
- );
- }
- function ManufacturerField(props) {
- const { item, btns, onFinish } = props;
- const [form] = Form.useForm();
- return (
- <Form
- form={form}
- labelCol={{ span: 4 }}
- wrapperCol={{ span: 20 }}
- autoComplete="off"
- initialValues={item.props}
- onFinish={onFinish}
- >
- <Form.Item label="标题" name="label">
- <Input />
- </Form.Item>
- <Form.Item label="提示文字" name="placeholder">
- <Input />
- </Form.Item>
- <Form.Item label="必填" name="required" valuePropName="checked">
- <Switch />
- </Form.Item>
- {btns}
- </Form>
- );
- }
- function TextField(props) {
- const { item, btns, onFinish } = props;
- const [form] = Form.useForm();
- const onReset = () => {
- form.resetFields();
- };
- return (
- <Form
- form={form}
- labelCol={{ span: 4 }}
- wrapperCol={{ span: 20 }}
- onFinish={onFinish}
- autoComplete="off"
- initialValues={item.props}
- >
- <Form.Item label="标题" name="label">
- <Input />
- </Form.Item>
- <Form.Item label="提示文字" name="placeholder">
- <Input />
- </Form.Item>
- <Form.Item label="必填" valuePropName="checked" name="required">
- <Switch />
- </Form.Item>
- {btns}
- </Form>
- );
- }
- function DDAttachment(props) {
- const { item, btns, onFinish } = props;
- const [form] = Form.useForm();
- return (
- <Form
- form={form}
- labelCol={{ span: 4 }}
- wrapperCol={{ span: 20 }}
- onFinish={onFinish}
- autoComplete="off"
- initialValues={item.props}
- >
- <Form.Item label="标题" name="label">
- <Input />
- </Form.Item>
- <Form.Item label="提示文字" name="placeholder">
- <Input />
- </Form.Item>
- <Form.Item label="必填" valuePropName="checked" name="required">
- <Switch />
- </Form.Item>
- {btns}
- </Form>
- );
- }
- function TextNote(props) {
- const { item, btns, onFinish } = props;
- const [form] = Form.useForm();
- return (
- <Form
- form={form}
- labelCol={{ span: 4 }}
- wrapperCol={{ span: 20 }}
- onFinish={onFinish}
- autoComplete="off"
- initialValues={item.props}
- >
- <Form.Item label="说明文字" name="placeholder">
- <Input />
- </Form.Item>
- {btns}
- </Form>
- );
- }
- function DDDateField(props) {
- const { item, btns, onFinish } = props;
- const [form] = Form.useForm();
- return (
- <Form
- form={form}
- labelCol={{ span: 4 }}
- wrapperCol={{ span: 20 }}
- onFinish={onFinish}
- autoComplete="off"
- initialValues={item.props}
- >
- <Form.Item label="标题" name="label">
- <Input />
- </Form.Item>
- <Form.Item label="提示文字" name="placeholder">
- <Input />
- </Form.Item>
- {btns}
- </Form>
- );
- }
- function TextareaField(props) {
- const { item, btns, onFinish } = props;
- const [form] = Form.useForm();
- return (
- <Form
- form={form}
- labelCol={{ span: 4 }}
- wrapperCol={{ span: 20 }}
- autoComplete="off"
- initialValues={item.props}
- onFinish={onFinish}
- >
- <Form.Item label="标题" name="label">
- <Input />
- </Form.Item>
- <Form.Item label="提示文字" name="placeholder">
- <Input />
- </Form.Item>
- <Form.Item label="必填" valuePropName="checked" name="required">
- <Switch />
- </Form.Item>
- {btns}
- </Form>
- );
- }
- function DDSelectField(props) {
- const { item, btns, onFinish, onRelClick } = props;
- const [form] = Form.useForm();
- const handleFinish = (value) => {
- let tempValue = { ...value };
- let arr = [];
- tempValue.options.map((item) => {
- arr.push(item.value);
- });
- if (arr) {
- arr = arr.filter((item) => item);
- arr = [...new Set(arr)];
- tempValue.options = arr;
- }
- onFinish?.(tempValue);
- };
- return (
- <Form
- form={form}
- labelCol={{ span: 4 }}
- wrapperCol={{ span: 20 }}
- autoComplete="off"
- initialValues={item.props}
- onFinish={handleFinish}
- >
- <Form.Item label="标题" name="label">
- <Input />
- </Form.Item>
- <Form.Item label="提示文字" name="placeholder">
- <Input />
- </Form.Item>
- <Form.Item label="选项" name="options" wrapperCol={{ span: 24 }}>
- <SelectItem onRelClick={onRelClick} />
- </Form.Item>
- <Form.Item label="必填" valuePropName="checked" name="required">
- <Switch />
- </Form.Item>
- {btns}
- </Form>
- );
- }
- function DDMultiSelectField(props) {
- const { item, btns, onFinish } = props;
- const [form] = Form.useForm();
- const handleFinish = (value) => {
- let tempValue = { ...value };
- let arr = [];
- tempValue.options.map((item) => {
- arr.push(item.value);
- });
- if (arr) {
- arr = arr.filter((item) => item);
- arr = [...new Set(arr)];
- tempValue.options = arr;
- }
- onFinish?.(tempValue);
- };
- return (
- <Form
- form={form}
- labelCol={{ span: 4 }}
- wrapperCol={{ span: 20 }}
- autoComplete="off"
- initialValues={item.props}
- onFinish={handleFinish}
- >
- <Form.Item label="标题" name="label">
- <Input />
- </Form.Item>
- <Form.Item label="提示文字" name="placeholder">
- <Input />
- </Form.Item>
- <Form.Item label="选项" name="options" wrapperCol={{ span: 24 }}>
- <SelectItem />
- </Form.Item>
- <Form.Item label="必填" valuePropName="checked" name="required">
- <Switch />
- </Form.Item>
- {btns}
- </Form>
- );
- }
- function SelectItem(props) {
- const { value, onChange, onRelClick } = props;
- const [localValue, setLocalValue] = useState([]);
- const pushItem = (item) => {
- let tempValue = [
- ...localValue,
- {
- id: item,
- value: item,
- },
- ];
- setLocalValue(tempValue);
- onChange(tempValue);
- };
- const handleDelete = (index) => {
- let tempValue = [...localValue];
- tempValue.splice(index, 1);
- setLocalValue(tempValue);
- onChange(tempValue);
- };
- const handleInputOnChange = (targetValue, index) => {
- let tempValue = [...localValue];
- tempValue[index].value = targetValue;
- setLocalValue(tempValue);
- onChange(tempValue);
- };
- useEffect(() => {
- let tempValue = value.map((item) => ({ id: item, value: item }));
- setLocalValue(tempValue);
- onChange(tempValue);
- }, []);
- return (
- <div
- style={{
- minHeight: 40,
- display: 'flex',
- flexDirection: 'column',
- }}
- >
- <Space>
- <div
- style={{
- fontSize: 4,
- color: '#40a9ff',
- cursor: 'pointer',
- lineHeight: '32px',
- }}
- onClick={() => {
- pushItem('');
- }}
- >
- 添加选项
- </div>
- {onRelClick && (
- <div
- style={{
- fontSize: 4,
- color: '#40a9ff',
- cursor: 'pointer',
- lineHeight: '32px',
- }}
- onClick={onRelClick}
- >
- 选项关联
- </div>
- )}
- </Space>
- <div style={{ minHeight: 20 }}>
- {localValue.map((item, index) => (
- <div
- style={{
- display: 'flex',
- justifyContent: 'center',
- alignItems: 'center',
- marginBottom: 5,
- }}
- key={item.value}
- >
- <Input
- style={{ marginRight: 10 }}
- value={item.value}
- onChange={(e) => handleInputOnChange(e.target.value, index)}
- />
- <DeleteOutlined onClick={() => handleDelete(index)} />
- </div>
- ))}
- </div>
- </div>
- );
- }
- function NumberField(props) {
- const { item, btns, onFinish } = props;
- const [form] = Form.useForm();
- return (
- <Form
- form={form}
- labelCol={{ span: 4 }}
- wrapperCol={{ span: 20 }}
- autoComplete="off"
- initialValues={item.props}
- onFinish={onFinish}
- >
- <Form.Item label="标题" name="label">
- <Input />
- </Form.Item>
- <Form.Item label="提示文字" name="placeholder">
- <Input />
- </Form.Item>
- <Form.Item label="单位" name="unit">
- <Input />
- </Form.Item>
- <Form.Item label="必填" valuePropName="checked" name="required">
- <Switch />
- </Form.Item>
- {btns}
- </Form>
- );
- }
- function TableName(props) {
- const { item, btns, onFinish } = props;
- const [form] = Form.useForm();
- return (
- <Form
- form={form}
- labelCol={{ span: 4 }}
- wrapperCol={{ span: 20 }}
- autoComplete="off"
- initialValues={item.props}
- onFinish={onFinish}
- >
- <Form.Item label="表格名称" name="label">
- <Input />
- </Form.Item>
- {btns}
- </Form>
- );
- }
|