fix(client): improve code

This commit is contained in:
chenos 2022-02-09 23:36:21 +08:00
parent faf0977a0b
commit 6608901596
5 changed files with 57 additions and 42 deletions

View File

@ -1,13 +1,10 @@
import React from 'react'; import React from 'react';
import { useDesignable } from '../..';
import { SortableItem } from '../../common'; import { SortableItem } from '../../common';
export const BlockItem: React.FC<any> = (props) => { export const BlockItem: React.FC<any> = (props) => {
const { DesignableBar } = useDesignable();
return ( return (
<SortableItem> <SortableItem className={'nb-block-item'} style={{ position: 'relative' }}>
{props.children} {props.children}
<DesignableBar />
</SortableItem> </SortableItem>
); );
}; };

View File

@ -9,6 +9,7 @@ import {
SchemaComponent, SchemaComponent,
SchemaComponentProvider, SchemaComponentProvider,
SchemaInitializer, SchemaInitializer,
SchemaInitializerItemOptions,
useDesignable useDesignable
} from '@nocobase/client'; } from '@nocobase/client';
import { Switch } from 'antd'; import { Switch } from 'antd';
@ -67,10 +68,11 @@ const schema: ISchema = {
const useFormItemInitializerFields = () => { const useFormItemInitializerFields = () => {
return [ return [
{ {
key: 'name', type: 'item',
title: 'Name', title: 'Name',
component: InitializeFormItem, component: InitializeFormItem,
schema: { schema: {
name: 'name',
type: 'string', type: 'string',
title: 'Name', title: 'Name',
'x-component': 'Input', 'x-component': 'Input',
@ -79,10 +81,11 @@ const useFormItemInitializerFields = () => {
}, },
}, },
{ {
key: 'title', type: 'item',
title: 'Title', title: 'Title',
component: InitializeFormItem, component: InitializeFormItem,
schema: { schema: {
name: 'title',
type: 'string', type: 'string',
title: 'Title', title: 'Title',
'x-component': 'Input', 'x-component': 'Input',
@ -90,7 +93,7 @@ const useFormItemInitializerFields = () => {
'x-collection-field': 'posts.title', 'x-collection-field': 'posts.title',
}, },
}, },
]; ] as SchemaInitializerItemOptions[];
}; };
const gridRowColWrap = (schema: ISchema) => { const gridRowColWrap = (schema: ISchema) => {
@ -116,6 +119,7 @@ const AddGridFormItem = observer((props: any) => {
insertPosition={'beforeEnd'} insertPosition={'beforeEnd'}
items={[ items={[
{ {
type: 'itemGroup',
title: 'Display fields', title: 'Display fields',
children: useFormItemInitializerFields(), children: useFormItemInitializerFields(),
}, },
@ -123,6 +127,7 @@ const AddGridFormItem = observer((props: any) => {
type: 'divider', type: 'divider',
}, },
{ {
type: 'item',
title: 'Add text', title: 'Add text',
component: InitializeTextFormItem, component: InitializeTextFormItem,
}, },
@ -161,9 +166,11 @@ const useCurrentFieldSchema = (path: string) => {
}; };
}; };
const InitializeFormItem = (props) => { const itemWrap = SchemaInitializer.itemWrap;
const { title, schema, insert } = props;
const { exists, remove } = useCurrentFieldSchema(schema['x-collection-field']); const InitializeFormItem = itemWrap((props) => {
const { item, insert } = props;
const { schema, exists, remove } = useCurrentFieldSchema(item.schema['x-collection-field']);
return ( return (
<SchemaInitializer.Item <SchemaInitializer.Item
onClick={() => { onClick={() => {
@ -171,22 +178,22 @@ const InitializeFormItem = (props) => {
return remove(); return remove();
} }
insert({ insert({
...schema, ...item.schema,
}); });
}} }}
> >
<div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between' }}> <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between' }}>
{title} <Switch size={'small'} checked={exists} /> {item.title} <Switch size={'small'} checked={exists} />
</div> </div>
</SchemaInitializer.Item> </SchemaInitializer.Item>
); );
}; });
const InitializeTextFormItem = (props) => { const InitializeTextFormItem = itemWrap((props) => {
const { title, insert } = props; const { insert } = props;
return ( return (
<SchemaInitializer.Item <SchemaInitializer.Item
onClick={(info) => { onClick={() => {
insert({ insert({
type: 'void', type: 'void',
'x-component': 'Markdown.Void', 'x-component': 'Markdown.Void',
@ -194,11 +201,9 @@ const InitializeTextFormItem = (props) => {
// 'x-editable': false, // 'x-editable': false,
}); });
}} }}
> />
{title}
</SchemaInitializer.Item>
); );
}; });
export default function App() { export default function App() {
return ( return (

View File

@ -51,15 +51,16 @@ const AddGridBlockItem = observer((props: any) => {
insertPosition={'beforeEnd'} insertPosition={'beforeEnd'}
items={[ items={[
{ {
type: 'itemGroup',
title: 'Data blocks', title: 'Data blocks',
children: [ children: [
{ {
key: 'table', type: 'item',
title: 'Table', title: 'Table',
component: TableBlockInitializerItem, component: TableBlockInitializerItem,
}, },
{ {
key: 'form', type: 'item',
title: 'Form', title: 'Form',
component: FormBlockInitializerItem, component: FormBlockInitializerItem,
}, },
@ -72,12 +73,14 @@ const AddGridBlockItem = observer((props: any) => {
); );
}); });
const TableBlockInitializerItem = (props) => { const itemWrap = SchemaInitializer.itemWrap;
const TableBlockInitializerItem = itemWrap((props) => {
const { insert } = props; const { insert } = props;
return ( return (
<SchemaInitializer.Item <SchemaInitializer.Item
icon={<TableOutlined />} icon={<TableOutlined />}
onClick={(info) => { onClick={() => {
insert({ insert({
type: 'void', type: 'void',
'x-component': 'VoidTable', 'x-component': 'VoidTable',
@ -90,11 +93,13 @@ const TableBlockInitializerItem = (props) => {
title: 'select a data source', title: 'select a data source',
children: [ children: [
{ {
key: 'users', type: 'item',
name: 'users',
title: 'Users', title: 'Users',
}, },
{ {
key: 'posts', type: 'item',
name: 'posts',
title: 'Posts', title: 'Posts',
}, },
], ],
@ -104,14 +109,14 @@ const TableBlockInitializerItem = (props) => {
Table Table
</SchemaInitializer.Item> </SchemaInitializer.Item>
); );
}; });
const FormBlockInitializerItem = (props) => { const FormBlockInitializerItem = itemWrap((props) => {
const { insert } = props; const { insert } = props;
return ( return (
<SchemaInitializer.Item <SchemaInitializer.Item
icon={<FormOutlined />} icon={<FormOutlined />}
onClick={(info) => { onClick={() => {
insert({ insert({
type: 'void', type: 'void',
'x-component': 'Form', 'x-component': 'Form',
@ -124,11 +129,13 @@ const FormBlockInitializerItem = (props) => {
title: 'select a data source', title: 'select a data source',
children: [ children: [
{ {
key: 'users', type: 'item',
name: 'users',
title: 'Users', title: 'Users',
}, },
{ {
key: 'posts', type: 'item',
name: 'posts',
title: 'Posts', title: 'Posts',
}, },
], ],
@ -138,7 +145,7 @@ const FormBlockInitializerItem = (props) => {
Form Form
</SchemaInitializer.Item> </SchemaInitializer.Item>
); );
}; });
export default function App() { export default function App() {
return ( return (

View File

@ -5,30 +5,33 @@ import React, { createContext, useContext } from 'react';
export const DraggableContext = createContext(null); export const DraggableContext = createContext(null);
export const Sortable = (props: any) => { export const Sortable = (props: any) => {
const { id, data, style, children, ...others } = props;
const draggable = useDraggable({ const draggable = useDraggable({
id: props.id, id,
data: props.data, data,
}); });
const { isOver, setNodeRef } = useDroppable({ const { isOver, setNodeRef } = useDroppable({
id: props.id, id,
data: props.data, data,
}); });
const droppableStyle = { const droppableStyle = { ...style };
color: isOver ? 'green' : undefined,
}; if (isOver) {
droppableStyle['color'] = 'green';
}
return ( return (
<DraggableContext.Provider value={draggable}> <DraggableContext.Provider value={draggable}>
<div ref={setNodeRef} style={droppableStyle}> <div {...others} ref={setNodeRef} style={droppableStyle}>
{props.children} {children}
</div> </div>
</DraggableContext.Provider> </DraggableContext.Provider>
); );
}; };
export const SortableItem = observer((props) => { export const SortableItem: React.FC<any> = observer((props) => {
const field = useField(); const field = useField();
const fieldSchema = useFieldSchema(); const fieldSchema = useFieldSchema();
const onInsertAdjacent = ({ dn, orginDraggedParentSchema }) => { const onInsertAdjacent = ({ dn, orginDraggedParentSchema }) => {
@ -38,6 +41,7 @@ export const SortableItem = observer((props) => {
}; };
return ( return (
<Sortable <Sortable
{...props}
id={field.address.toString()} id={field.address.toString()}
data={{ insertAdjacent: 'afterEnd', onInsertAdjacent, schema: fieldSchema }} data={{ insertAdjacent: 'afterEnd', onInsertAdjacent, schema: fieldSchema }}
> >

View File

@ -21,6 +21,7 @@ const useFormItemInitializerFields = () => {
schema: { schema: {
type: 'string', type: 'string',
title: 'Name', title: 'Name',
name: 'name',
'x-component': 'Input', 'x-component': 'Input',
'x-decorator': 'FormItem', 'x-decorator': 'FormItem',
'x-collection-field': 'posts.name', 'x-collection-field': 'posts.name',
@ -33,6 +34,7 @@ const useFormItemInitializerFields = () => {
schema: { schema: {
type: 'string', type: 'string',
title: 'Title', title: 'Title',
name: 'title',
'x-component': 'Input', 'x-component': 'Input',
'x-decorator': 'FormItem', 'x-decorator': 'FormItem',
'x-collection-field': 'posts.title', 'x-collection-field': 'posts.title',