Skip to content

Commit

Permalink
Merge pull request #8663 from zhangtao07/master
Browse files Browse the repository at this point in the history
优化状态容器配置 & listSelect自定义label value适配
  • Loading branch information
zhangtao07 committed Nov 8, 2023
2 parents 0c41e8b + 91a8e81 commit 3e1e8a6
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 37 deletions.
4 changes: 2 additions & 2 deletions packages/amis-editor-core/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ export interface RendererInfo extends RendererScaffoldInfo {
sharedContext?: Record<string, any>;
dialogTitle?: string; //弹窗标题用于弹窗大纲的展示
dialogType?: string; //区分确认对话框类型
subEditorVariable?: Array<{label: string; children: any}>; // 传递给子编辑器的组件自定义变量,如listSelect的选项名称和值
getSubEditorVariable: (schema?: any) => Array<{label: string; children: any}>; // 传递给子编辑器的组件自定义变量,如listSelect的选项名称和值
}

export type BasicRendererInfo = Omit<
Expand Down Expand Up @@ -1051,7 +1051,7 @@ export abstract class BasePlugin implements PluginInterface {
isListComponent: plugin.isListComponent,
rendererName: plugin.rendererName,
memberImmutable: plugin.memberImmutable,
subEditorVariable: plugin.subEditorVariable
getSubEditorVariable: plugin.getSubEditorVariable
};
}
}
Expand Down
5 changes: 4 additions & 1 deletion packages/amis-editor-core/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1224,7 +1224,10 @@ export async function resolveVariablesFromScope(node: any, manager: any) {
// 子编辑器内读取的host节点自定义变量,非数据域方式,如listSelect的选项值
let hostNodeVaraibles = [];
if (manager?.store?.isSubEditor) {
hostNodeVaraibles = manager.config?.hostNode?.info?.subEditorVariable || [];
hostNodeVaraibles =
manager.config?.hostNode?.info?.getSubEditorVariable?.(
manager.config?.hostNode.schema
) || [];
}

const variables: VariableItem[] =
Expand Down
48 changes: 22 additions & 26 deletions packages/amis-editor/src/plugin/Form/ListSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,21 +109,26 @@ export class ListControlPlugin extends BasePlugin {
}
];

subEditorVariable: Array<{label: string; children: any}> = [
{
label: '当前选项',
children: [
{
label: '选项名称',
value: 'label'
},
{
label: '选项值',
value: 'value'
}
]
}
];
getSubEditorVariable(schema: any): Array<{label: string; children: any}> {
let labelField = schema?.labelField || 'label';
let valueField = schema?.valueField || 'value';

return [
{
label: '当前选项',
children: [
{
label: '选项名称',
value: labelField
},
{
label: '选项值',
value: valueField
}
]
}
];
}

panelBodyCreator = (context: BaseEventContext) => {
return formItemControl(
Expand Down Expand Up @@ -201,7 +206,7 @@ export class ListControlPlugin extends BasePlugin {
body: [
{
type: 'tpl',
tpl: `\${${this.getDisplayField(value)}}`,
tpl: `\${${this.getDisplayField(data)}}`,
wrapperComponent: '',
inline: true
}
Expand Down Expand Up @@ -275,16 +280,7 @@ export class ListControlPlugin extends BasePlugin {
}

getDisplayField(data: any) {
if (
data.source ||
(data.map &&
Array.isArray(data.map) &&
data.map[0] &&
Object.keys(data.map[0]).length > 1)
) {
return data.labelField ?? 'label';
}
return 'label';
return data?.labelField ?? 'label';
}

editDetail(id: string, field: string) {
Expand Down
15 changes: 14 additions & 1 deletion packages/amis-editor/src/plugin/SwitchContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ export class SwitchContainerPlugin extends LayoutBasePlugin {
name: 'items',
label: '状态列表',
addTip: '新增组件状态',
minLength: 1,
items: [
{
type: 'input-text',
Expand Down Expand Up @@ -356,6 +357,10 @@ export class SwitchContainerPlugin extends LayoutBasePlugin {
title: '外观',
className: 'p-none',
body: getSchemaTpl('collapseGroup', [
getSchemaTpl('theme:base', {
collapsed: false,
extra: []
}),
{
title: '布局',
body: [
Expand Down Expand Up @@ -460,7 +465,15 @@ export class SwitchContainerPlugin extends LayoutBasePlugin {
getSchemaTpl('layout:stickyPosition')
]
},
...getSchemaTpl('theme:common', {exclude: ['layout']})
{
title: '自定义样式',
body: [
{
type: 'theme-cssCode',
label: false
}
]
}
])
},
{
Expand Down
10 changes: 7 additions & 3 deletions packages/amis-editor/src/renderer/ListItemControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {findDOMNode} from 'react-dom';
import cx from 'classnames';
import get from 'lodash/get';
import Sortable from 'sortablejs';
import {FormItem, Button, Icon, render as amisRender} from 'amis';
import {FormItem, Button, Icon, render as amisRender, toast} from 'amis';
import {autobind} from 'amis-editor-core';
import type {Option} from 'amis';
import {createObject, FormControlProps} from 'amis-core';
Expand All @@ -30,7 +30,6 @@ export type SourceType = 'custom' | 'api' | 'apicenter' | 'variable';

export interface OptionControlState {
items: Array<PlainObject>;
api: SchemaApi;
labelField: string;
valueField: string;
}
Expand All @@ -50,7 +49,6 @@ export default class ListItemControl extends React.Component<

this.state = {
items: this.transformOptions(props),
api: props.data.source,
labelField: props.data.labelField || 'title',
valueField: props.data.valueField
};
Expand Down Expand Up @@ -173,6 +171,12 @@ export default class ListItemControl extends React.Component<
*/
handleDelete(index: number) {
const items = this.state.items.concat();
const minLength = this.props.minLength;

if (minLength > 0 && items.length <= minLength) {
toast.warning(`列表项数目不能少于${minLength}`);
return;
}

items.splice(index, 1);
this.setState({items}, () => this.onChange());
Expand Down
38 changes: 35 additions & 3 deletions packages/amis/src/renderers/Each.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import React from 'react';
import {Renderer, RendererProps, buildStyle, isPureVariable} from 'amis-core';
import {
CustomStyle,
Renderer,
RendererProps,
buildStyle,
isPureVariable,
setThemeClassName
} from 'amis-core';
import {Schema} from 'amis-core';
import {resolveVariable, resolveVariableAndFilter} from 'amis-core';
import {createObject, getPropValue, isObject} from 'amis-core';
Expand Down Expand Up @@ -96,7 +103,11 @@ export default class Each extends React.Component<EachProps> {
indexKeyName,
placeholder,
classnames: cx,
translate: __
translate: __,
env,
id,
wrapperCustomStyle,
themeCss
} = this.props;

const value = getPropValue(this.props, props =>
Expand Down Expand Up @@ -124,7 +135,14 @@ export default class Each extends React.Component<EachProps> {
}

return (
<div className={cx('Each', className)} style={buildStyle(style, data)}>
<div
className={cx(
'Each',
className,
setThemeClassName('baseControlClassName', id, themeCss)
)}
style={buildStyle(style, data)}
>
{Array.isArray(arr) && arr.length && items ? (
arr.map((item: any, index: number) => (
<EachItem
Expand All @@ -144,6 +162,20 @@ export default class Each extends React.Component<EachProps> {
{render('placeholder', __(placeholder))}
</div>
)}

<CustomStyle
config={{
wrapperCustomStyle,
id,
themeCss,
classNames: [
{
key: 'baseControlClassName'
}
]
}}
env={env}
/>
</div>
);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/amis/src/renderers/SwitchContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export default class SwitchContainer extends React.Component<

componentDidUpdate(preProps: SwitchContainerProps) {
const items = this.props.items || [];
if (this.state.activeIndex >= 0 && !items[this.state.activeIndex]) {
if (this.state.activeIndex > 0 && !items[this.state.activeIndex]) {
this.setState({
activeIndex: 0
});
Expand Down

0 comments on commit 3e1e8a6

Please sign in to comment.