jack zhang bf1a19426a
refactor: new schema initializer and schema settings (#2802)
* fix: form

* refactor: schema-initializer

* fix: bug

* refactor: schema initializer

* refactor: rename

* fix: delete SchemaInitializerProvider

* refactor: props `insert` to hooks `useSchemaInitializerV2`

* fix: bug

* refactor: delete `SchemaInitializer.Button`

* refactor: delete old SchemaInitializer

* fix: bug

* fix: workflow

* fix: docs

* fix: bug

* fix: bug

* feat: style

* fix: remove v2

* fix: visible

* fix: bug

* fix: item hook

* feat: item hook

* fix: add search DataBlockInitializer

* fix: build bug

* fix: style bug

* fix: style bug

* fix: test bug

* fix: test bug

* fix: rerender bug

* fix: remove menu select

* fix: bug

* chore: add aria-label for SchemaInitializerButton

* refactor: rename name to camel case

* fix: menu height bug

* fix: build errors

* fix: build errors

* fix: bug

* fix: bug

* fix: performance

* test: add test for header

* fix: sidebar is not refresh (T-2422)

* feat(e2e): support to add group page and link page

* chore: make sure the page is configurable when using page.goto

* test: add tests for menu initializer

* fix: imporve  code

* chore: fix build error

* chore: optimize locator of menu item

* refactor: rename testid for select

* test: make tests passing

* fix: make tests passing

* chore: upgrade vitest to v0.34.6

* chore: increase timeout of e2e

* feat: core

* fix: revert schema initializer demos

* test: menu, page tabs, page grid, table column

* fix: schema button interface

* feat: refactor: page tab settings

* feat: page settings

* fix: dumirc

* fix: export CSSVariableProvider

* feat: lazy render

* fix: form-item

* fix: general schema desinger

* feat: filter form item settings

* refactor: form-v2 schema settings

* refactor: form-v1 schema settings

* refactor: action schema settings

* fix: action bug

* fix: form-item bug

* fix: types error

* docs: schema settings  doc

* docs: schema settings

* feat: schema setting  item add name

* fix: visible lazy render bug

* fix: revert form item filter

* fix: test bug

* fix: test JSON.parse bug

* fix: test bug

* fix: improve styling

* fix: styling

* fix: cleanup

* fix: token.borderRadiusSM

* fix: bug

* test: add tests

* fix: style bug

* fix: add chart performance

* feat: add SchemaDesignerContext

* fix: bug

* fix: test bug

* style: create record action style improve

* fix: make test passing

* chore: mack tests passing

* chore: make tests passing

* test: fix tests

* style: style revert

* fix: bug

* fix: data selector

* fix: fix tests

* fix: fix tests

* fix: delete PluginManagerContext

* refactor: improve router and add SchemaComponentProvider & CSSVariableProvider to MainComponent

* fix: add dn and field builtin to SchemaSettingWrapper

* feat: update docs

* refactor: application providers

* fix: test bug

* fix: fix tests

* chore: make test passing

* feat: update docs

* chore: rename collection name

* feat: update docs

* chore: skip weird test

* fix: blockInitializers media to otherBlocks

* fix: cancel to skip test

* fix: bug

* test: add test

* refactor: migrate to small files

* test: add tests for form block settings

* chore: format

* fix: add chart scroll bug

* refactor: action designer improve

* refactor: formitem designer schemaSetting

* feat: schemaSettingsManager and schemaInitializerManager addItem and removeItem

* test: add tests for color field in creating block

* test: add tests for email field in creating block

* test: make tests passing

* perf: reduce fields number

* fix: sub menu bug

* test: add tests basic in editing form

* test: add tests basic in details form

* fix: improve code

* test: make tests passing

* test(plugin-mock-collections): add color for enum options

* refactor: improve code

* fix: bug

* fix: bug

* refactor: convert parameters to destructured object

* test: add tests choices

* test: add tests media

* test: add tests for datetime in creating form

* feat(plugin-mock-collection): generate faker time

* test: add tests for datetime in editing form

* test: add tests for datetime in details form

* fix: bug

* feat: improve code

* test: add tests for relation fields

* fix: rename SchemaSettings

* fix: type bug

* refactor: useDesinger()

* fix: bug

* fix: bug

* fix: build tip

* fix: designableState

* fix: bug

* fix: designable

* fix: designable

* test: add tests for relation fields

* test: add tests for relation fields

* test: add tests for relation fields

* feat: client api doc

* test: add tests for relation fields

* test: avoid errors

* test: make tests passing

* fix: bug

* test: make tests passing

* test: add tests for advanced fields

* test: increase e2e timeout-minutes to 60

* fix: bug

* fix: improve code

* feat: add schema initailizer component  demos

* test: make tests passing

* fix: schema settings demos

* feat: shallowMerge & deepMerge

* test: reduce number of tests

* test: make tests passing

* feat: updates

* fix: add Initializer Internal

* demos:  useSchemaSettingsRender

* test: make tests passing

* test: make tests passing

* fix: improve docs

* fix: bug

* chore: upgrade dumi theme

* test: make tests passing

* test: add tests for linkage rules

* test: add test for form data templates

* test: add tests for default value

* test: reduce number of tests

* fix: dn.deepMerge

* fix: bug

* fix: bug

* fix: toolbar

* fix: docs ssr

* test: add tests for system fields

* test: add tests for actions

* fix:  bug

* test: add tests for lazy loading of variables

* test: make testing more stable

* fix: update docs

* fix: bug

---------

Co-authored-by: Rain <958414905@qq.com>
Co-authored-by: chenos <chenlinxh@gmail.com>
Co-authored-by: katherinehhh <katherine_15995@163.com>
2023-12-04 14:56:46 +08:00

9.5 KiB
Raw Blame History

group
group
title order
Client 1

SchemaSettings

API

new SchemaSettings(options)

创建一个 Schema 配置实例。

参数类型

interface SchemaSettingOptions<T = {}>{
  name: string;
  Component?: ComponentType<T>;
  componentProps?: T;
  style?: React.CSSProperties;
  items: SchemaSettingsItemType[];
}

参数详细信息

  • name

用于标识 Schema 配置的名称。

会用在 schema 中的 x-settings 配置值以及读取 schema 的值会传给 useSchemaSettingRender() 的第一个参数。

const mySettings = new SchemaSettings({
   //  定义 name
  name: 'MySettings',
})
  • Component、componentProps & style

Component 默认是一个 Icon如果需要定制化可以为一个 React 组件。

import { SettingOutlined } from '@ant-design/icons';
const mySettings = new SchemaSettings({
  Component: () => <SettingOutlined style={{ cursor: 'pointer' }} />
})

如果你使用的是一个公共的组件,不同的 Settings 有定制化的诉求,那么你可以使用 componentProps 和 style。

import { Button } from '@ant-design/icons';
const mySettings = new SchemaSettings({
  Component: Button,
  componentProps: {
    type: 'primary'
  }
})

当然也可以这样是使用。

const mySettings = new SchemaSettings({
  Component: () => <Button type="primary" />
})
  • items

items 的类型为 SchemaSettingsItemType[],具体如下:

export type SchemaSettingsItemType<T = {}> = {
  name: string;
  type?: string;
  sort?: number;
  Component?: string | ComponentType<T>;
  componentProps?: T;
  useComponentProps?: () => T;
  useVisible?: () => boolean;
  children?: SchemaSettingsItemType[];
  [index]: any;
};

最简单的配置如下:

const mySettings = new SchemaSettings({
  items: [
    {
      name: 'edit',
      Component: MyComponent,
    },
  ],
})

type 内置类型:为了更简单的使用,我们提供了一些内置的类型,可以直接使用。

const mySettings = new SchemaSettings({
  items: [
    {
      name: 'edit',
      type: 'modal', // 弹窗
    },
  ],
})

其底层对应的是 SchemaSettingsModalItem 组件。具体内置类型和对应的组件如下:

type Component 效果
item SchemaSettingsItem 文本
itemGroup SchemaSettingsItemGroup 分组,同 Menu 组件的 type: 'group'
subMenu SchemaSettingsSubMenu 子菜单,同 Menu 组件的子菜单
divider SchemaSettingsDivider 分割线,同 Menu 组件的 type: 'divider'
remove SchemaSettingsRemove 删除,用于删除一个区块
select SchemaSettingsSelectItem 下拉选择
cascader SchemaSettingsCascaderItem 级联选择
switch SchemaSettingsSwitchItem 开关
popup SchemaSettingsPopupItem 弹出层
actionModal SchemaSettingsActionModalItem 操作弹窗
modal SchemaSettingsModalItem 弹窗

componentPropsuseComponentProps:用于定制化内置组件的 props两者的区别是后面的可以使用一些 hooks。

const mySettings = new SchemaSettings({
  items: [
    {
      name: 'allowAddNew',
      type: 'switch',
      useComponentProps() {
        // 可以在这里使用 hooks
        const { t } = useTranslation();
        const field = useField<Field>();
        return {
          title: t('Allow add new data'),
          checked: fieldSchema['x-add-new'] as boolean,
        }
      }
    },
    {
      name: 'remove',
      type: 'remove',
      componentProps: {
        removeParentsIfNoChildren: true
      },
    },
  ],
})

useVisible 则是控制组件是否显示,可以使用 hooks。

const mySettings = new SchemaSettings({
  items: [
    {
      name: 'upload',
      type: 'switch',
      useVisible() {
        // 这里使用了 hooks
        const { form } = useFormBlockContext();
        return !!form?.readPretty;
      }
    },
  ],
})

schemaSetting 实例方法

schemaSetting 实例用于对 items 进行增删改查。

schemaSetting.get()

用于获取一个 item并且可以通过 . 获取子 item。

  • 类型
interface SchemaSettings {
  get(name: string): SchemaSettingsItemType | undefined;
}
  • 示例
const mySchemaSettings = new SchemaSettings({
  name: 'MySchemaSettings',
  items: [
    {
      name: 'demo',
      type: 'itemGroup',
      children: [
        {
          name: 'child1',
          type: 'modal',
        },
      ],
    },
  ],
});

// 获取 item
const demo = mySchemaSettings.get('demo');

// 获取子 item
const child1 = mySchemaSettings.get('demo.child1');

schemaSetting.remove()

用于删除一个 item。

  • 类型
interface SchemaSettings {
  remove(name: string): void;
}
  • 示例
mySchemaSettings.remove('demo2');

mySchemaSettings.remove('demo.child2');

schemaSetting.add()

用于新增一个 item。

  • 类型
interface SchemaSettings {
  add(name: string, item: Omit<SchemaSettingsItemType, 'name'>): void;
}
  • 示例
mySchemaSettings.add('demo2', { type: 'switch' });

mySchemaSettings.add('demo.child2', { type: 'actionModel' });

app.schemaSettingsManager 管理器

app.schemaSettingsManager.getAll()

获取所有的 schemaSetting 实例。

  • 类型
interface SchemaSettingsManager {
  getAll(): SchemaSettings[];
}
  • 示例
import { Plugin } from '@nocobase/plugin'
class MyPlugin extends Plugin {
  load() {
    const allSchemaSettings = this.app.schemaSettingsManager.getAll();
  }
}

app.schemaSettingsManager.get()

用于获取一个 schemaSetting 实例,

  • 类型
interface SchemaSettingsManager {
  get(name: string): SchemaSettings | undefined;
}
  • 参数说明

name 为 new SchemaSettings(options) 中的 name。

  • 示例
import { Plugin } from '@nocobase/plugin'
class MyPlugin extends Plugin {
  load() {
    const mySchemaSettings = this.app.schemaSettingsManager.get('MySchemaSettings');
  }
}

app.schemaSettingsManager.remove()

用于删除一个 schemaSetting 实例。

  • 类型
interface SchemaSettingsManager {
  remove(name: string): void;
}
  • 参数说明

name 为 new SchemaSettings(options) 中的 name。

  • 示例
import { Plugin } from '@nocobase/plugin'
class MyPlugin extends Plugin {
  load() {
    this.app.schemaSettingsManager.remove('MySchemaSettings');
  }
}

app.schemaSettingsManager.add()

新增一个 schemaSetting 实例。

  • 类型
interface SchemaSettingsManager {
  add(schemaSetting: SchemaSettings): void;
}
  • 示例
import { Plugin } from '@nocobase/plugin'

const mySchemaSettings = new SchemaSettings({
  name: 'MySchemaSettings',
  items: [],
});

class MyPlugin extends Plugin {
  load() {
    this.app.schemaSettingsManager.add(mySchemaSettings);
  }
}

app.schemaSettingsManager.has()

判断是否存在一个 schemaSetting 实例。

  • 类型
interface SchemaSettingsManager {
  has(name: string): boolean;
}
  • 示例
import { Plugin } from '@nocobase/plugin'

class MyPlugin extends Plugin {
  load() {
    const hasMySchemaSettings = this.app.schemaSettingsManager.has('MySchemaSettings');
  }
}

Hooks

useSchemaSettingsRender()

用于渲染 schemaSetting 实例。

  • 类型
interface SchemaSettingOptions<T = {}>{
  name: string;
  Component?: ComponentType<T>;
  componentProps?: T;
  style?: React.CSSProperties;
  items: SchemaSettingsItemType[];
}

interface SchemaSettingsRenderResult {
  render: (options?: SchemaSettingOptions) => React.ReactElement;
  exists: boolean;
}

function useSchemaSettingsRender(
  name: string,
  options?: SchemaSettingsRenderOptions,
): SchemaSettingsRenderResult;
  • 示例
const MyDesigner = (props) => {
  const { modalTip } = props;

  const { render, exists } = useSchemaSettingsRender(fieldSchema['x-settings'], fieldSchema['x-settings-props']);

  return <div>{render()}</div>
}

useSchemaSettings()

获取 schemaSetting 上下文数据。

上下文数据包含了 schemaSetting 实例化时的 options 以及调用 useSchemaSettingsRender() 时传入的 options

  • 类型
interface UseSchemaSettingsResult<T> extends SchemaSettingOptions {
  dn?: Designable;
  field?: GeneralField;
  fieldSchema?: Schema;
}

function useSchemaSettings(): UseSchemaSettingsResult;
  • 示例
const { dn } = useSchemaSettings();

useSchemaSettingsItem()

用于获取一个 item 的数据。

  • 类型
export type SchemaSettingsItemType<T = {}> = {
  name: string;
  type?: string;
  sort?: number;
  Component?: string | ComponentType<T>;
  componentProps?: T;
  useComponentProps?: () => T;
  useVisible?: () => boolean;
  children?: SchemaSettingsItemType[];
  [index]: any;
};

function useSchemaSettingsItem(): SchemaSettingsItemType;
  • 示例
const { name } = useSchemaSettingsItem();

Components

TODO: 文档待补充

Examples

基础用法

自定义按钮

内置类型

NocoBase 提供了几个内置的组件,可以直接使用。

Schema

动态显示、隐藏

Designer 上下文