| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- import { FormItem, Input, ArrayTable, Editable, Form, NumberPicker, Radio } from '@formily/antd';
- import { createForm } from '@formily/core';
- import { createSchemaField } from '@formily/react';
- import { Button } from 'antd';
- import { useEffect } from 'react';
- import RemoveData from './RemoveData';
- interface Props {
- onChange: (data: any) => void;
- data: Partial<DataSourceItem>[];
- table: {
- id: string;
- table: string;
- };
- }
- const EditTable = (props: Props) => {
- const SchemaField = createSchemaField({
- components: {
- FormItem,
- Editable,
- Input,
- ArrayTable,
- NumberPicker,
- Radio,
- RemoveData,
- },
- });
- const form = createForm({
- initialValues: {
- array: props.data,
- },
- });
- const schema = {
- type: 'object',
- properties: {
- array: {
- type: 'array',
- 'x-decorator': 'FormItem',
- 'x-component': 'ArrayTable',
- 'x-component-props': {
- pagination: { pageSize: 10 },
- scroll: { x: '100%' },
- },
- items: {
- type: 'object',
- properties: {
- column1: {
- type: 'void',
- 'x-component': 'ArrayTable.Column',
- 'x-component-props': { title: '列名' },
- properties: {
- name: {
- type: 'string',
- 'x-decorator': 'FormItem',
- 'x-component': 'Input',
- 'x-component-props': {
- placeholder: '请输入名称',
- },
- name: 'name',
- 'x-validator': [
- {
- max: 64,
- message: '最多可输入64个字符',
- },
- {
- required: true,
- message: '请输入名称',
- },
- ],
- required: true,
- },
- },
- },
- column2: {
- type: 'void',
- 'x-component': 'ArrayTable.Column',
- 'x-component-props': { title: '类型' },
- properties: {
- type: {
- type: 'string',
- 'x-decorator': 'FormItem',
- 'x-component': 'Input',
- 'x-component-props': {
- placeholder: '请输入类型',
- },
- name: 'typeId',
- 'x-validator': [
- {
- required: true,
- message: '请输入类型',
- },
- {
- max: 64,
- message: '最多可输入64个字符',
- },
- ],
- required: true,
- },
- },
- },
- column3: {
- type: 'void',
- 'x-component': 'ArrayTable.Column',
- 'x-component-props': { title: '长度' },
- properties: {
- length: {
- type: 'string',
- 'x-decorator': 'FormItem',
- 'x-component': 'NumberPicker',
- 'x-component-props': {
- placeholder: '请输入长度',
- },
- 'x-validator': [
- {
- required: true,
- message: '请输入长度',
- },
- {
- maximum: 99999,
- minimum: 1,
- },
- ],
- required: true,
- },
- },
- },
- column4: {
- type: 'void',
- 'x-component': 'ArrayTable.Column',
- 'x-component-props': { title: '精度' },
- properties: {
- scale: {
- type: 'string',
- 'x-decorator': 'FormItem',
- 'x-component': 'NumberPicker',
- 'x-component-props': {
- placeholder: '请输入精度',
- },
- 'x-validator': [
- {
- required: true,
- message: '请输入精度',
- },
- {
- maximum: 99999,
- minimum: 0,
- },
- ],
- required: true,
- },
- },
- },
- column5: {
- type: 'void',
- 'x-component': 'ArrayTable.Column',
- 'x-component-props': { width: 120, title: '不能为空' },
- properties: {
- notnull: {
- type: 'boolean',
- default: false,
- 'x-decorator': 'FormItem',
- 'x-component': 'Radio.Group',
- 'x-component-props': {
- placeholder: '请选择是否不能为空',
- optionType: 'button',
- buttonStyle: 'solid',
- },
- 'x-validator': [
- {
- required: true,
- message: '请选择是否不能为空',
- },
- ],
- required: true,
- enum: [
- {
- label: '是',
- value: true,
- },
- {
- label: '否',
- value: false,
- },
- ],
- },
- },
- },
- column6: {
- type: 'void',
- 'x-component': 'ArrayTable.Column',
- 'x-component-props': { title: '说明' },
- properties: {
- description: {
- 'x-component': 'Input',
- 'x-decorator': 'FormItem',
- 'x-component-props': {
- placeholder: '请输入说明',
- },
- },
- },
- },
- column7: {
- type: 'void',
- 'x-component': 'ArrayTable.Column',
- 'x-component-props': {
- title: '操作',
- dataIndex: 'operations',
- width: 50,
- },
- properties: {
- item: {
- type: 'void',
- 'x-component': 'FormItem',
- properties: {
- remove: {
- type: 'number',
- 'x-component': 'RemoveData',
- 'x-component-props': {
- type: props.table,
- },
- },
- },
- },
- },
- },
- },
- },
- properties: {
- add: {
- type: 'void',
- 'x-component': 'ArrayTable.Addition',
- title: '新增行',
- },
- },
- },
- },
- };
- useEffect(() => {
- form.setValues({ array: props?.data || [] });
- }, [props.data]);
- return (
- <div>
- <div style={{ display: 'flex', justifyContent: 'flex-end' }}>
- <Button
- type="primary"
- style={{ marginBottom: 20 }}
- onClick={async () => {
- const data: any = await form.submit();
- props.onChange(data);
- }}
- >
- 保存
- </Button>
- </div>
- <Form form={form}>
- <SchemaField schema={schema} />
- </Form>
- </div>
- );
- };
- export default EditTable;
|