#AddSubModelButton
指定された FlowModel にサブモデル(subModel)を追加するために使用します。非同期ロード、グループ、サブメニュー、カスタムモデル継承ルールなど、さまざまな設定方法に対応しています。
#Props
interface AddSubModelButtonProps {
model: FlowModel;
subModelKey: string;
subModelType?: 'object' | 'array';
items?: SubModelItemsType;
subModelBaseClass?: string | ModelConstructor;
subModelBaseClasses?: Array<string | ModelConstructor>;
afterSubModelInit?: (subModel: FlowModel) => Promise<void>;
afterSubModelAdd?: (subModel: FlowModel) => Promise<void>;
afterSubModelRemove?: (subModel: FlowModel) => Promise<void>;
children?: React.ReactNode;
keepDropdownOpen?: boolean;
}| パラメータ | 型 | 説明 |
|---|---|---|
model | FlowModel | 必須。サブモデルを追加する対象モデルです。 |
subModelKey | string | 必須。model.subModels 内のサブモデルのキー名です。 |
subModelType | 'object' | 'array' | サブモデルのデータ構造タイプです。デフォルトは 'array' です。 |
items | SubModelItem[] | (ctx) => SubModelItem[] | Promise<...> | メニュー項目の定義です。静的または非同期生成に対応しています。 |
subModelBaseClass | string | ModelConstructor | 基底クラスを指定し、そのクラスを継承するすべてのモデルをメニュー項目として一覧表示します。 |
subModelBaseClasses | (string | ModelConstructor)[] | 複数の基底クラスを指定し、グループ別に継承モデルを自動一覧表示します。 |
afterSubModelInit | (subModel) => Promise<void> | サブモデル初期化後のコールバックです。 |
afterSubModelAdd | (subModel) => Promise<void> | サブモデル追加後のコールバックです。 |
afterSubModelRemove | (subModel) => Promise<void> | サブモデル削除後のコールバックです。 |
children | React.ReactNode | ボタンの内容です。テキストやアイコンにカスタマイズできます。 |
keepDropdownOpen | boolean | 追加後もドロップダウンメニューを開いたままにするかどうか。デフォルトでは自動的に閉じます。 |
#SubModelItem 型定義
interface SubModelItem {
key?: string;
label?: string;
type?: 'group' | 'divider';
disabled?: boolean;
hide?: boolean | ((ctx: FlowModelContext) => boolean | Promise<boolean>);
icon?: React.ReactNode;
children?: SubModelItemsType;
useModel?: string;
createModelOptions?: {
props?: Record<string, any>;
stepParams?: Record<string, any>;
};
toggleable?: boolean | ((model: FlowModel) => boolean);
}| フィールド | 型 | 説明 |
|---|---|---|
key | string | 一意識別子です。 |
label | string | 表示テキストです。 |
type | 'group' | 'divider' | グループまたは区切り線です。省略時は通常の項目またはサブメニューになります。 |
disabled | boolean | 現在の項目を無効にするかどうかです。 |
hide | boolean | (ctx) => boolean | Promise<boolean> | 動的な非表示(true を返すと非表示になります)。 |
icon | React.ReactNode | アイコンの内容です。 |
children | SubModelItemsType | サブメニュー項目です。ネストされたグループやサブメニューに使用します。 |
useModel | string | 使用する Model のタイプ(登録名)を指定します。 |
createModelOptions | object | モデル初期化時のパラメータです。 |
toggleable | boolean | (model: FlowModel) => boolean | トグル形態です。追加済みなら削除、未追加なら追加します(1 つのみ許可)。 |
#例
#<AddSubModelButton /> を使用した subModels の追加
import { Application, Plugin, BlockModel } from '@nocobase/client-v2';
import { AddSubModelButton, FlowModel, FlowModelRenderer } from '@nocobase/flow-engine';
import { Button, Space } from 'antd';
class HelloBlockModel extends FlowModel {
render() {
return (
<Space direction="vertical" style={{ width: '100%' }}>
{this.mapSubModels('items', (item) => {
return <FlowModelRenderer key={item.uid} model={item} showFlowSettings={{ showBorder: true }} />;
})}
<AddSubModelButton
model={this}
subModelKey="items"
items={[
{
key: 'sub1',
label: 'Sub1 Block',
useModel: 'Sub1BlockModel',
},
{
key: 'sub2',
label: 'Sub2 Block',
children: [
{
key: 'sub2-1',
label: 'Sub2-1 Block',
useModel: 'Sub2BlockModel',
},
{
key: 'sub2-2',
label: 'Sub2-2 Block',
useModel: 'Sub2BlockModel',
},
],
},
]}
>
<Button>Add block</Button>
</AddSubModelButton>
</Space>
);
}
}
class Sub1BlockModel extends BlockModel {
renderComponent() {
return (
<div>
<h2>Sub1 Block</h2>
<p>This is a sub block rendered by Sub1BlockModel.</p>
</div>
);
}
}
class Sub2BlockModel extends BlockModel {
renderComponent() {
return (
<div>
<h2>Sub2 Block</h2>
<p>This is a sub block rendered by Sub2BlockModel.</p>
</div>
);
}
}
class PluginHelloModel extends Plugin {
async load() {
await this.flowEngine.flowSettings.forceEnable();
this.flowEngine.registerModels({ HelloBlockModel, Sub1BlockModel, Sub2BlockModel });
const model = this.flowEngine.createModel({
uid: 'my-model',
use: 'HelloBlockModel',
});
this.router.add('root', {
path: '/',
element: <FlowModelRenderer model={model} />,
});
}
}
const app = new Application({
router: { type: 'memory', initialEntries: ['/'] },
plugins: [PluginHelloModel],
});
export default app.getRootComponent();
<AddSubModelButton />を使用して subModels を追加します。ボタンは FlowModel 内に配置する必要があります。model.mapSubModels()を使用して subModels をイテレートします。mapSubModelsメソッドは欠落やソートの問題を解決します。<FlowModelRenderer />を使用して subModels をレンダリングします。
#異なる形態の AddSubModelButton
import { PlusOutlined } from '@ant-design/icons';
import { Application, Plugin, BlockModel } from '@nocobase/client-v2';
import { AddSubModelButton, FlowModel, FlowModelRenderer } from '@nocobase/flow-engine';
import { Button, Card, Space } from 'antd';
import type { ReactNode } from 'react';
function AddBlock(props: { model: FlowModel; children?: ReactNode }) {
const { model, children } = props;
return (
<AddSubModelButton
model={model}
subModelKey="items"
items={[
{
key: 'sub1',
label: 'Sub1 Block',
createModelOptions: {
use: 'Sub1BlockModel',
},
},
]}
>
{children}
</AddSubModelButton>
);
}
class HelloBlockModel extends FlowModel {
render() {
return (
<Card>
<Space direction="vertical" style={{ width: '100%' }}>
{this.mapSubModels('items', (item) => {
return <FlowModelRenderer key={item.uid} model={item} showFlowSettings={{ showBorder: true }} />;
})}
<Space>
<AddBlock model={this}>
<Button>Add block</Button>
</AddBlock>
<AddBlock model={this}>
<a>
<PlusOutlined /> Add block
</a>
</AddBlock>
</Space>
</Space>
</Card>
);
}
}
class Sub1BlockModel extends BlockModel {
renderComponent() {
return (
<div>
<h2>Sub1 Block</h2>
<p>This is a sub block rendered by Sub1BlockModel.</p>
</div>
);
}
}
class PluginHelloModel extends Plugin {
async load() {
this.flowEngine.flowSettings.forceEnable();
this.flowEngine.registerModels({ HelloBlockModel, Sub1BlockModel });
const model = this.flowEngine.createModel({
uid: 'my-model',
use: 'HelloBlockModel',
});
this.router.add('root', {
path: '/',
element: (
<FlowModelRenderer
model={model}
showFlowSettings={{ showBorder: true }}
hideRemoveInSettings
extraToolbarItems={[
{
key: 'add-block',
component: () => {
return (
<AddBlock model={model}>
<PlusOutlined />
</AddBlock>
);
},
sort: 1,
},
]}
/>
),
});
}
}
const app = new Application({
router: { type: 'memory', initialEntries: ['/'] },
plugins: [PluginHelloModel],
});
export default app.getRootComponent();
- ボタンコンポーネント
<Button>Add block</Button>として使用でき、自由に配置できます。 - アイコン
<PlusOutlined />としても使用できます。 - 右上の Flow Settings の位置に配置することもできます。
#トグル形態のサポート
import { Application, Plugin, BlockModel } from '@nocobase/client-v2';
import { AddSubModelButton, FlowModel, FlowModelRenderer } from '@nocobase/flow-engine';
import { Button, Space } from 'antd';
class HelloBlockModel extends FlowModel {
render() {
return (
<Space direction="vertical" style={{ width: '100%' }}>
{this.mapSubModels('items', (item) => {
return <FlowModelRenderer key={item.uid} model={item} showFlowSettings={{ showBorder: true }} />;
})}
<AddSubModelButton
model={this}
subModelKey="items"
items={[
{
key: 'sub1',
label: 'Sub1 Block',
toggleable: true,
useModel: 'Sub1BlockModel',
},
{
key: 'sub2',
label: 'Sub2 Block',
children: [
{
key: 'sub2-1',
label: 'Sub2-1 Block',
toggleable: Sub2BlockModel.customToggleable('foo'),
useModel: 'Sub2BlockModel',
createModelOptions: {
props: {
name: 'foo',
},
},
},
{
key: 'sub2-2',
label: 'Sub2-2 Block',
toggleable: Sub2BlockModel.customToggleable('bar'),
useModel: 'Sub2BlockModel',
createModelOptions: {
props: {
name: 'bar',
},
},
},
],
},
]}
>
<Button>Add block</Button>
</AddSubModelButton>
</Space>
);
}
}
class Sub1BlockModel extends BlockModel {
renderComponent() {
return (
<div>
<h2>Sub1 Block</h2>
<p>This is a sub block rendered by Sub1BlockModel.</p>
</div>
);
}
}
class Sub2BlockModel extends BlockModel {
static customToggleable(name: string) {
return (model: Sub2BlockModel) => {
return model.props.name === name;
};
}
renderComponent() {
return (
<div>
<h2>Sub2 Block - {this.props.name}</h2>
<p>This is a sub block rendered by Sub2BlockModel.</p>
</div>
);
}
}
class PluginHelloModel extends Plugin {
async load() {
this.flowEngine.flowSettings.forceEnable();
this.flowEngine.registerModels({ HelloBlockModel, Sub1BlockModel, Sub2BlockModel });
const model = this.flowEngine.createModel({
uid: 'my-model',
use: 'HelloBlockModel',
});
this.router.add('root', {
path: '/',
element: <FlowModelRenderer model={model} />,
});
}
}
const app = new Application({
router: { type: 'memory', initialEntries: ['/'] },
plugins: [PluginHelloModel],
});
export default app.getRootComponent();
- シンプルなケースでは
toggleable: trueを設定するだけで十分です。デフォルトではクラス名に基づいて検索し、同じクラスのインスタンスは 1 つだけ許可されます。 - カスタム検索ルール:
toggleable: (model: FlowModel) => boolean。
#非同期 items
import { Application, Plugin, BlockModel } from '@nocobase/client-v2';
import { AddSubModelButton, FlowModel, FlowModelRenderer } from '@nocobase/flow-engine';
import { Button, Space } from 'antd';
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
class HelloBlockModel extends FlowModel {
render() {
return (
<Space direction="vertical" style={{ width: '100%' }}>
{this.mapSubModels('items', (item) => {
return <FlowModelRenderer key={item.uid} model={item} showFlowSettings={{ showBorder: true }} />;
})}
<AddSubModelButton
model={this}
subModelKey="items"
items={async (ctx) => {
return await ctx.getTestModels();
}}
>
<Button>Add block</Button>
</AddSubModelButton>
</Space>
);
}
}
class Sub1BlockModel extends BlockModel {
renderComponent() {
return (
<div>
<h2>Sub1 Block</h2>
<p>This is a sub block rendered by Sub1BlockModel.</p>
</div>
);
}
}
class Sub2BlockModel extends BlockModel {
renderComponent() {
return (
<div>
<h2>Sub2 Block</h2>
<p>This is a sub block rendered by Sub2BlockModel.</p>
</div>
);
}
}
class PluginHelloModel extends Plugin {
async load() {
this.flowEngine.flowSettings.forceEnable();
this.flowEngine.context.defineMethod('getTestModels', async () => {
await sleep(1000);
return [
{
key: 'sub1',
label: 'Sub1 Block',
createModelOptions: {
use: 'Sub1BlockModel',
},
},
{
key: 'sub2',
label: 'Sub2 Block',
children: async () => {
await sleep(1000);
return [
{
key: 'sub2-1',
label: 'Sub2-1 Block',
createModelOptions: {
use: 'Sub2BlockModel',
},
},
{
key: 'sub2-2',
label: 'Sub2-2 Block',
createModelOptions: {
use: 'Sub2BlockModel',
},
},
];
},
},
{
key: 'async-group',
label: 'Async Group',
type: 'group' as const,
children: async () => {
await sleep(800);
return [
{
key: 'g-sub1',
label: 'G-Sub1 Block',
createModelOptions: { use: 'Sub1BlockModel' },
},
{
key: 'g-nested-group',
label: 'Nested Group',
type: 'group' as const,
children: async () => {
await sleep(500);
return [
{
key: 'g-sub2-1',
label: 'G-Sub2-1 Block',
createModelOptions: { use: 'Sub2BlockModel' },
},
{
key: 'g-sub2-2',
label: 'G-Sub2-2 Block',
createModelOptions: { use: 'Sub2BlockModel' },
},
];
},
},
];
},
},
];
});
this.flowEngine.registerModels({ HelloBlockModel, Sub1BlockModel, Sub2BlockModel });
const model = this.flowEngine.createModel({
uid: 'my-model',
use: 'HelloBlockModel',
});
this.router.add('root', {
path: '/',
element: <FlowModelRenderer model={model} />,
});
}
}
const app = new Application({
router: { type: 'memory', initialEntries: ['/'] },
plugins: [PluginHelloModel],
});
export default app.getRootComponent();
コンテキストから動的な items を取得できます。例えば:
- リモートの
ctx.api.request()を使用できます。 ctx.dataSourceManagerが提供する API から必要なデータを取得することもできます。- カスタムのコンテキストプロパティやメソッドも使用できます。
itemsとchildrenの両方が async 呼び出しに対応しています。
#メニュー項目の動的非表示(hide)
import { Application, Plugin, BlockModel } from '@nocobase/client-v2';
import { AddSubModelButton, FlowModel, FlowModelRenderer } from '@nocobase/flow-engine';
import { Button, Space, Switch, Typography } from 'antd';
import { useState } from 'react';
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
class ContainerModel extends FlowModel {
render() {
return <View model={this} />;
}
}
function View({ model }: { readonly model: FlowModel }) {
const [showAdvanced, setShowAdvanced] = useState(false);
return (
<Space direction="vertical" style={{ width: '100%' }}>
<Space>
<Typography.Text>showAdvanced</Typography.Text>
<Switch checked={showAdvanced} onChange={setShowAdvanced} />
</Space>
{model.mapSubModels('items', (item) => {
return <FlowModelRenderer key={item.uid} model={item} showFlowSettings={{ showBorder: true }} />;
})}
<AddSubModelButton
model={model}
subModelKey="items"
items={[
{
key: 'basic',
label: 'Basic Block',
createModelOptions: { use: 'BasicBlockModel' },
},
{
key: 'advanced-group',
label: 'Advanced (hide)',
type: 'group' as const,
children: [
{
key: 'advanced-async',
label: 'Advanced Block (async hide)',
hide: async () => {
await sleep(200);
return !showAdvanced;
},
createModelOptions: { use: 'AdvancedBlockModel' },
},
{
key: 'advanced-alias',
label: 'Advanced Block (sync hide)',
hide: () => !showAdvanced,
createModelOptions: { use: 'AdvancedBlockModel' },
},
],
},
]}
>
<Button>Add block</Button>
</AddSubModelButton>
</Space>
);
}
class BasicBlockModel extends BlockModel {
renderComponent() {
return (
<div>
<h3>Basic Block</h3>
<div style={{ color: '#666' }}>通常の subModel です。</div>
</div>
);
}
}
class AdvancedBlockModel extends BlockModel {
renderComponent() {
return (
<div>
<h3>Advanced Block</h3>
<div style={{ color: '#666' }}>showAdvanced=true の場合のみ AddSubModelButton メニューに表示されます。</div>
</div>
);
}
}
class PluginDemo extends Plugin {
async load() {
this.flowEngine.flowSettings.forceEnable();
this.flowEngine.registerModels({ ContainerModel, BasicBlockModel, AdvancedBlockModel });
const model = this.flowEngine.createModel({
uid: 'container',
use: 'ContainerModel',
});
this.router.add('root', {
path: '/',
element: <FlowModelRenderer model={model} showFlowSettings={{ showBorder: true, showBackground: true }} />,
});
}
}
const app = new Application({
router: { type: 'memory', initialEntries: ['/'] },
plugins: [PluginDemo],
});
export default app.getRootComponent();
hideはbooleanま たは関数(async 対応)をサポートします。trueを返すと非表示になります。- group と children に対して再帰的に適用されます。
#グループ、サブメニュー、区切り線の使用
import { Application, Plugin, BlockModel } from '@nocobase/client-v2';
import { AddSubModelButton, FlowModel, FlowModelRenderer } from '@nocobase/flow-engine';
import { Button, Space } from 'antd';
class HelloBlockModel extends FlowModel {
render() {
return (
<Space direction="vertical" style={{ width: '100%' }}>
{this.mapSubModels('items', (item) => {
return <FlowModelRenderer key={item.uid} model={item} showFlowSettings={{ showBorder: true }} />;
})}
<AddSubModelButton
model={this}
subModelKey="items"
items={[
{
key: 'group1',
label: 'Group1',
type: 'group',
children: [
{
key: 'group1-sub1',
label: 'Block1',
createModelOptions: {
use: 'Sub1BlockModel',
},
},
],
},
{
type: 'divider',
},
{
key: 'submenu1',
label: 'Sub Menu',
children: [
{
key: 'submenu1-sub1',
label: 'Block1',
createModelOptions: {
use: 'Sub1BlockModel',
},
},
],
},
{
type: 'divider',
},
{
key: 'sub1',
label: 'Block1',
createModelOptions: {
use: 'Sub1BlockModel',
},
},
]}
>
<Button>Add block</Button>
</AddSubModelButton>
</Space>
);
}
}
class Sub1BlockModel extends BlockModel {
renderComponent() {
return (
<div>
<h2>Sub1 Block</h2>
<p>This is a sub block rendered by Sub1BlockModel.</p>
</div>
);
}
}
class PluginHelloModel extends Plugin {
async load() {
this.flowEngine.flowSettings.forceEnable();
this.flowEngine.registerModels({ HelloBlockModel, Sub1BlockModel });
const model = this.flowEngine.createModel({
uid: 'my-model',
use: 'HelloBlockModel',
});
this.router.add('root', {
path: '/',
element: <FlowModelRenderer model={model} />,
});
}
}
const app = new Application({
router: { type: 'memory', initialEntries: ['/'] },
plugins: [PluginHelloModel],
});
export default app.getRootComponent();
type: dividerの場合は区切り線になります。type: groupでchildrenがある場合はメニューグループになります。childrenがあるがtypeがない場合はサブメニューになります。
#継承クラスによる items の自動生成
import { Application, Plugin, BlockModel } from '@nocobase/client-v2';
import { AddSubModelButton, FlowModel, FlowModelRenderer } from '@nocobase/flow-engine';
import { Button, Space } from 'antd';
class HelloBlockModel extends FlowModel {
get title() {
return 'HelloBlockModel';
}
render() {
return (
<Space direction="vertical" style={{ width: '100%' }}>
{this.mapSubModels('items', (item) => {
return <FlowModelRenderer key={item.uid} model={item} showFlowSettings={{ showBorder: true }} />;
})}
<AddSubModelButton model={this} subModelKey="items" subModelBaseClass="BlockModel">
<Button>Add block</Button>
</AddSubModelButton>
</Space>
);
}
}
class Sub1BlockModel extends BlockModel {
renderComponent() {
return (
<div>
<h2>Sub1 Block</h2>
<p>This is a sub block rendered by Sub1BlockModel.</p>
</div>
);
}
}
class Sub2BlockModel extends BlockModel {
renderComponent() {
return (
<div>
<h2>Sub2 Block</h2>
<p>This is a sub block rendered by Sub2BlockModel.</p>
</div>
);
}
}
class Sub3BlockModel extends BlockModel {
renderComponent() {
return (
<div>
<h2>Sub3 Block</h2>
<p>This is a sub block rendered by Sub3BlockModel.</p>
</div>
);
}
}
class Sub4BlockModel extends BlockModel {
renderComponent() {
return (
<div>
<h2>Sub4 Block</h2>
<p>This is a sub block rendered by Sub4BlockModel.</p>
</div>
);
}
}
Sub2BlockModel.define({
label: 'Sub2 Block',
hide: (ctx) => ctx.model.title !== 'HelloBlockModel',
});
Sub3BlockModel.define({
hide: (ctx) => ctx.model.title === 'HelloBlockModel',
});
Sub4BlockModel.define({
label: 'Sub4 Block',
hide: true,
});
class PluginHelloModel extends Plugin {
async load() {
this.flowEngine.flowSettings.forceEnable();
this.flowEngine.registerModels({
BlockModel,
HelloBlockModel,
Sub1BlockModel,
Sub2BlockModel,
Sub3BlockModel,
});
const model = this.flowEngine.createModel({
uid: 'my-model',
use: 'HelloBlockModel',
});
this.router.add('root', {
path: '/',
element: <FlowModelRenderer model={model} />,
});
}
}
const app = new Application({
router: { type: 'memory', initialEntries: ['/'] },
plugins: [PluginHelloModel],
});
export default app.getRootComponent();
subModelBaseClassを継承するすべての FlowModel が一覧表示されます。Model.define()で関連するメタデータを定義できます。hide: trueとマークされたものは自動的に非表示になります。
#継承クラスによるグループ化
import { Application, Plugin, BlockModel } from '@nocobase/client-v2';
import { AddSubModelButton, FlowModel, FlowModelRenderer } from '@nocobase/flow-engine';
import { Button, Space } from 'antd';
class HelloBlockModel extends FlowModel {
render() {
return (
<Space direction="vertical" style={{ width: '100%' }}>
{this.mapSubModels('items', (item) => {
return <FlowModelRenderer key={item.uid} model={item} showFlowSettings={{ showBorder: true }} />;
})}
<AddSubModelButton
model={this}
subModelKey="items"
subModelBaseClasses={['BaseBlockModel', 'BlockModel']}
>
<Button>Add block</Button>
</AddSubModelButton>
</Space>
);
}
}
class Sub1BlockModel extends BlockModel {
renderComponent() {
return (
<div>
<h2>Sub1 Block</h2>
<p>This is a sub block rendered by Sub1BlockModel.</p>
</div>
);
}
}
class BaseBlockModel extends BlockModel {}
BaseBlockModel.define({
label: 'Group1',
});
class Sub2BlockModel extends BaseBlockModel {
renderComponent() {
return (
<div>
<h2>Sub2 Block</h2>
<p>This is a sub block rendered by Sub2BlockModel.</p>
</div>
);
}
}
class Sub3BlockModel extends BaseBlockModel {
renderComponent() {
return (
<div>
<h2>Sub3 Block</h2>
<p>This is a sub block rendered by Sub2BlockModel.</p>
</div>
);
}
}
Sub2BlockModel.define({
label: 'Sub2 Block',
});
class PluginHelloModel extends Plugin {
async load() {
this.flowEngine.flowSettings.forceEnable();
this.flowEngine.registerModels({
BlockModel,
HelloBlockModel,
BaseBlockModel,
Sub1BlockModel,
Sub2BlockModel,
Sub3BlockModel,
});
const model = this.flowEngine.createModel({
uid: 'my-model',
use: 'HelloBlockModel',
});
this.router.add('root', {
path: '/',
element: <FlowModelRenderer model={model} />,
});
}
}
const app = new Application({
router: { type: 'memory', initialEntries: ['/'] },
plugins: [PluginHelloModel],
});
export default app.getRootComponent();
subModelBaseClassesを継承するすべての FlowModel が一覧表示されます。subModelBaseClassesに基づいて自動的にグループ化され、重複が排除されます。
#継承クラス + menuType=submenu による 2 階層メニュー
import { Application, Plugin } from '@nocobase/client-v2';
import { AddSubModelButton, FlowEngineProvider, FlowModel, FlowModelRenderer } from '@nocobase/flow-engine';
import { Button, Space } from 'antd';
class DemoRootModel extends FlowModel {
render() {
return (
<Space direction="vertical" style={{ width: '100%' }}>
{this.mapSubModels('items', (item) => (
<FlowModelRenderer key={item.uid} model={item} showFlowSettings={{ showBorder: true }} />
))}
<AddSubModelButton model={this} subModelKey="items" subModelBaseClasses={[GroupBase, SubmenuBase]}>
<Button>サブモデルを追加</Button>
</AddSubModelButton>
</Space>
);
}
}
class GroupBase extends FlowModel {}
GroupBase.define({
label: 'グループ(フラット表示)',
sort: 200,
children: () => [{ key: 'group-leaf', label: 'グループ内の子項目', createModelOptions: { use: 'Leaf' } }],
});
class SubmenuBase extends FlowModel {}
SubmenuBase.define({
label: 'サブメニュー',
menuType: 'submenu',
sort: 110,
children: () => [{ key: 'submenu-leaf', label: 'サブメニュ ーの子項目', createModelOptions: { use: 'Leaf' } }],
});
class Leaf extends FlowModel {
render() {
return (
<div
style={{
padding: 12,
border: '1px dashed #d9d9d9',
background: '#fafafa',
borderRadius: 6,
}}
>
<div style={{ fontWeight: 600, marginBottom: 4 }}>Leaf Block</div>
<div style={{ color: '#555' }}>UID: {this.uid.slice(0, 6)}</div>
</div>
);
}
}
class PluginSubmenuDemo extends Plugin {
async load() {
this.flowEngine.flowSettings.forceEnable();
this.flowEngine.registerModels({ DemoRootModel, GroupBase, SubmenuBase, Leaf });
const model = this.flowEngine.createModel({ uid: 'demo-root', use: 'DemoRootModel' });
this.router.add('root', {
path: '/',
element: (
<FlowEngineProvider engine={this.flowEngine}>
<FlowModelRenderer model={model} />
</FlowEngineProvider>
),
});
}
}
const app = new Application({
router: { type: 'memory', initialEntries: ['/'] },
plugins: [PluginSubmenuDemo],
});
export default app.getRootComponent();
- 基底クラスに
Model.define({ menuType: 'submenu' })で表示形態を指定します。 - 1 階層の項目として表示され、展開すると 2 階層メニューになります。他のグループと
meta.sortで混合ソートが可能です。

