2022-02-08 12:17:06 +08:00
|
|
|
import { observer, Schema, useFieldSchema } from '@formily/react';
|
|
|
|
import {
|
|
|
|
ArrayTable,
|
|
|
|
Input,
|
|
|
|
SchemaComponent,
|
|
|
|
SchemaComponentProvider,
|
|
|
|
SchemaInitializer,
|
|
|
|
useDesignable
|
|
|
|
} from '@nocobase/client';
|
|
|
|
import { Switch } from 'antd';
|
2022-02-07 21:52:51 +08:00
|
|
|
import React from 'react';
|
|
|
|
|
2022-02-07 23:30:24 +08:00
|
|
|
const useTableColumnInitializerFields = () => {
|
|
|
|
const fields = [
|
|
|
|
{
|
2022-02-08 12:17:06 +08:00
|
|
|
key: 'name',
|
|
|
|
title: 'Name',
|
|
|
|
schema: {
|
|
|
|
type: 'string',
|
|
|
|
name: 'name',
|
|
|
|
'x-collection-field': 'posts.name',
|
|
|
|
'x-component': 'Input',
|
|
|
|
},
|
2022-02-07 23:30:24 +08:00
|
|
|
component: ColumnInitializer,
|
|
|
|
},
|
|
|
|
{
|
2022-02-08 12:17:06 +08:00
|
|
|
key: 'title',
|
|
|
|
title: 'Title',
|
|
|
|
schema: {
|
|
|
|
type: 'string',
|
|
|
|
name: 'title',
|
|
|
|
'x-collection-field': 'posts.title',
|
|
|
|
'x-component': 'Input',
|
|
|
|
},
|
|
|
|
component: ColumnInitializer,
|
2022-02-07 23:30:24 +08:00
|
|
|
},
|
|
|
|
];
|
|
|
|
return fields;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const InitializerButton = observer((props: any) => {
|
|
|
|
return (
|
|
|
|
<SchemaInitializer.Button
|
|
|
|
wrap={(schema) => schema}
|
|
|
|
insertPosition={'beforeEnd'}
|
|
|
|
items={[
|
|
|
|
{
|
|
|
|
title: 'Display fields',
|
|
|
|
children: useTableColumnInitializerFields(),
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
>
|
|
|
|
Configure columns
|
|
|
|
</SchemaInitializer.Button>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2022-02-08 12:17:06 +08:00
|
|
|
const useCurrentColumnSchema = (path: string) => {
|
|
|
|
const fieldSchema = useFieldSchema();
|
|
|
|
const { remove } = useDesignable();
|
|
|
|
const findFieldSchema = (schema: Schema) => {
|
|
|
|
return schema.reduceProperties((buf, s) => {
|
|
|
|
if (s['x-collection-field'] === path) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
const child = findFieldSchema(s);
|
|
|
|
if (child) {
|
|
|
|
return child;
|
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
const schema = findFieldSchema(fieldSchema);
|
|
|
|
return {
|
|
|
|
schema,
|
|
|
|
exists: !!schema,
|
|
|
|
remove() {
|
|
|
|
schema && remove(schema.parent);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-02-07 23:30:24 +08:00
|
|
|
export const ColumnInitializer = (props) => {
|
2022-02-08 12:17:06 +08:00
|
|
|
const { title, schema, insert } = props;
|
|
|
|
const { exists, remove } = useCurrentColumnSchema(schema['x-collection-field']);
|
2022-02-07 23:30:24 +08:00
|
|
|
return (
|
|
|
|
<SchemaInitializer.Item
|
2022-02-08 12:17:06 +08:00
|
|
|
onClick={() => {
|
|
|
|
if (exists) {
|
|
|
|
return remove();
|
|
|
|
}
|
2022-02-07 23:30:24 +08:00
|
|
|
insert({
|
|
|
|
type: 'void',
|
2022-02-08 12:17:06 +08:00
|
|
|
title: schema.name,
|
2022-02-07 23:30:24 +08:00
|
|
|
'x-component': 'ArrayTable.Column',
|
|
|
|
properties: {
|
2022-02-08 12:17:06 +08:00
|
|
|
[schema.name]: {
|
2022-02-07 23:30:24 +08:00
|
|
|
'x-read-pretty': true,
|
2022-02-08 12:17:06 +08:00
|
|
|
...schema,
|
2022-02-07 23:30:24 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
>
|
2022-02-08 12:17:06 +08:00
|
|
|
<div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between' }}>
|
|
|
|
{title} <Switch size={'small'} checked={exists} />
|
|
|
|
</div>
|
2022-02-07 23:30:24 +08:00
|
|
|
</SchemaInitializer.Item>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-02-07 21:52:51 +08:00
|
|
|
const schema = {
|
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
input: {
|
|
|
|
type: 'array',
|
|
|
|
default: [
|
|
|
|
{ id: 1, name: 'Name1' },
|
|
|
|
{ id: 2, name: 'Name2' },
|
|
|
|
{ id: 3, name: 'Name3' },
|
|
|
|
],
|
|
|
|
'x-component': 'ArrayTable',
|
2022-02-08 12:17:06 +08:00
|
|
|
'x-column-initializer': 'InitializerButton',
|
2022-02-07 21:52:51 +08:00
|
|
|
'x-component-props': {
|
|
|
|
rowKey: 'id',
|
|
|
|
},
|
|
|
|
properties: {
|
|
|
|
column1: {
|
|
|
|
type: 'void',
|
|
|
|
title: 'Name',
|
|
|
|
'x-component': 'ArrayTable.Column',
|
|
|
|
properties: {
|
|
|
|
name: {
|
|
|
|
type: 'string',
|
|
|
|
'x-component': 'Input',
|
2022-02-08 12:17:06 +08:00
|
|
|
'x-collection-field': 'posts.name',
|
2022-02-07 21:52:51 +08:00
|
|
|
'x-read-pretty': true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
export default function App() {
|
|
|
|
return (
|
2022-02-08 12:17:06 +08:00
|
|
|
<SchemaComponentProvider components={{ InitializerButton, Input, ArrayTable }}>
|
2022-02-07 21:52:51 +08:00
|
|
|
<SchemaComponent schema={schema} />
|
|
|
|
</SchemaComponentProvider>
|
|
|
|
);
|
|
|
|
}
|