0.4.17 • Published 3 years ago

schema-page v0.4.17

Weekly downloads
267
License
MIT
Repository
-
Last release
3 years ago

schema-page

Usage

yarn add schema-page

使用 SchemaPage 快速构建一个 CRUD 的页面

一、定义组件

import React from 'react';
import schema from '@/pages/ArticleSite/schema';
import {
  AddArticleSite,
  DeleteArticleSite,
  queryArticleSite,
  UpdateArticleSite,
} from '@/services/article';
import SchemaPage from 'schema-page';
import { API } from '@/services/API';
import { Button } from 'antd';

// 自定义表单组件
const CustomComponent = (props) => {
  return <input value={props.value || ''} onChange={(e) => props.onChange(e.target.value)} />;
};


const ArticleSite = () => {
  return (
    <SchemaPage<API.ArticleSite, API.SiteQuery, API.ArticleSiteAddParams>
      expressionScope={{        
        request: (params) => queryArticleSite({ ...params }),
        handleAdd: async (formValue) => AddArticleSite(formValue),
        handleUpdate: async ({ id, status, title }) => UpdateArticleSite({ id, status, title }),
        handleDelete: async (record) => DeleteArticleSite(record),
        handleDetail: async () => {},
        formProps: {
          schema: formSchema,
          // 注册自定义的表单组件
          components: {
             CustomComponent,
          },
        },
        // 自定义操作栏的按钮
        customOtherOperation: (record, { actionRef }) => (
          <Button key="customOtherOperation" type="link" onClick={() => actionRef.current?.reload()}>
              其他
          </Button>
        ),
      }}
      schema={schema}
    />
  );
};

export default ArticleSite;

二、定义表格 schema

目前暂时由前端静态维护,后期由后端根据数据表或者 ProtoBuf 自动生成

const schema = {
  type: 'object',
  properties: {
    NO_NAME_FIELD_$0: {
      name: 'NO_NAME_FIELD_$0',
      type: 'object',
      'x-component': 'schemaTable',
      'x-component-props': {        
        request: '{{request}}',
        handleAdd: '{{handleAdd}}',
        handleUpdate: '{{handleUpdate}}',
        handleDelete: '{{handleDelete}}',
        handleDetail: '{{handleDetail}}',
        // 自定义操作栏的按钮      
        operationProps: {
          operations: [              
            '{{customOtherOperation}}',
          ],
        },          
        // 表单参数
        formProps: '{{formProps}}',
        rowKey: 'id',
        columns: [
          {
            title: '编号',
            dataIndex: 'id',
            key: 'id',            
            sorter: true,
          },
          {
            title: '站点名称',
            dataIndex: 'title',
            key: 'title',
          },
          {
            title: '状态',
            dataIndex: 'status',
            key: 'status',
          },
        ],
      },
    },
  },
};

export default schema;

三、定义表单的 schema

可以使用 在线表单编辑器 来可视化创建表单schema

import { ISchema } from 'schema-page';

const formSchema: ISchema = {
  type: 'object',
  properties: {
    title: {      
      title: '站点名称',
      'x-component': 'input',
    },
    status: {     
      title: '状态',
      'x-component': 'switch',
    },    
    test: {
      title: 'test',
      // 写入自定义组件名即可
      'x-component': 'CustomComponent',
    },
  },
};

export default formSchema;
0.4.201-beta

3 years ago

0.4.200-beta

3 years ago

0.4.199-beta

3 years ago

0.4.198-beta

3 years ago

0.4.197-beta

3 years ago

0.4.196-beta

3 years ago

0.4.194-beta

3 years ago

0.4.193-beta

3 years ago

0.4.191-beta

3 years ago

0.4.192-beta

3 years ago

0.4.18-beta

3 years ago

0.4.19-beta

3 years ago

0.4.17

3 years ago

0.4.16

3 years ago

0.4.15

3 years ago

0.4.14

3 years ago

0.4.13

3 years ago

0.4.12

3 years ago

0.4.11

3 years ago

0.4.10

3 years ago

0.4.9

3 years ago

0.4.8

3 years ago

0.4.7

3 years ago

0.4.6

3 years ago

0.4.5

3 years ago

0.4.4

3 years ago

0.4.3

3 years ago

0.4.2

3 years ago

0.4.1

3 years ago

0.3.9

3 years ago

0.4.0

3 years ago

0.3.8

3 years ago

0.3.7

3 years ago

0.3.6

3 years ago

0.3.5

4 years ago

0.3.4

4 years ago

0.3.3

4 years ago

0.3.2

4 years ago

0.3.0

4 years ago

0.2.9

4 years ago

0.3.1

4 years ago

0.2.8

4 years ago

0.2.7

4 years ago

0.2.6

4 years ago

0.2.5

4 years ago

0.2.4

4 years ago

0.2.1

4 years ago

0.2.3

4 years ago

0.2.2

4 years ago

0.2.0

4 years ago

0.1.9

4 years ago

0.1.8

4 years ago

0.1.2

4 years ago

0.1.7

4 years ago

0.1.4

4 years ago

0.1.3

4 years ago

0.1.6

4 years ago

0.1.5

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.7

4 years ago

0.0.6

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago