From 3e5bc113ab1cf07e131495067e80db25094f96f5 Mon Sep 17 00:00:00 2001 From: sansiro Date: Fri, 3 Mar 2023 15:31:11 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=8D=95=E9=80=89=E9=80=89=E9=A1=B9?= =?UTF-8?q?=E5=80=BC=E5=8C=85=E5=90=AB=E9=80=97=E5=8F=B7=E6=97=B6=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E9=80=89=E9=A1=B9=E9=94=99=E8=AF=AF=E9=97=AE=E9=A2=98?= =?UTF-8?q?=20(#6258)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: 单选选项值包含逗号时获取选项错误问题 * 加个单测 * 单测问题 * 在WrapControl 中使渲染器默认 multiple 生效 * 修改 --- docs/zh-CN/components/form/transfer.md | 1 + .../amis-core/src/renderers/wrapControl.tsx | 7 +++- packages/amis-core/src/store/formItem.ts | 3 +- .../__tests__/renderers/Form/select.test.tsx | 42 +++++++++++++++++++ packages/amis/src/renderers/Form/Transfer.tsx | 4 ++ 5 files changed, 55 insertions(+), 2 deletions(-) diff --git a/docs/zh-CN/components/form/transfer.md b/docs/zh-CN/components/form/transfer.md index 69d85f1b6..db02b45b4 100644 --- a/docs/zh-CN/components/form/transfer.md +++ b/docs/zh-CN/components/form/transfer.md @@ -18,6 +18,7 @@ icon: "label": "默认", "type": "transfer", "name": "transfer", + "value": "zhugeliang,libai", "options": [ { "label": "诸葛亮", diff --git a/packages/amis-core/src/renderers/wrapControl.tsx b/packages/amis-core/src/renderers/wrapControl.tsx index 8ab7170b9..48088da1a 100644 --- a/packages/amis-core/src/renderers/wrapControl.tsx +++ b/packages/amis-core/src/renderers/wrapControl.tsx @@ -31,6 +31,7 @@ import {withRootStore} from '../WithRootStore'; import {FormBaseControl, FormItemWrap} from './Item'; import {Api} from '../types'; import {TableStore} from '../store/table'; +import pick from 'lodash/pick'; export interface ControlOutterProps extends RendererProps { formStore?: IFormStore; @@ -181,6 +182,11 @@ export function wrapControl< // @issue 打算干掉这个 formItem?.addSubFormItem(model); model.config({ + // 理论上需要将渲染器的 defaultProps 全部生效,此处保险起见先只处理 multiple + ...pick( + {...ComposedComponent.defaultProps, ...this.props.$schema}, + ['multiple'] + ), id, type, required, @@ -189,7 +195,6 @@ export function wrapControl< isValueSchemaExp: isExpression(value), rules: validations, messages: validationErrors, - multiple, delimiter, valueField, labelField, diff --git a/packages/amis-core/src/store/formItem.ts b/packages/amis-core/src/store/formItem.ts index 38c362b50..07c17f5b4 100644 --- a/packages/amis-core/src/store/formItem.ts +++ b/packages/amis-core/src/store/formItem.ts @@ -164,7 +164,8 @@ export const FormItemStore = StoreNode.named('FormItemStore') ? nodeValueArray : Array.isArray(value) ? value - : typeof value === 'string' + : // 单选时不应该分割 + typeof value === 'string' && self.multiple ? value.split(self.delimiter || ',') : [value]; const selected = valueArray.map(item => diff --git a/packages/amis/__tests__/renderers/Form/select.test.tsx b/packages/amis/__tests__/renderers/Form/select.test.tsx index 610978c5b..72a2ed4ee 100644 --- a/packages/amis/__tests__/renderers/Form/select.test.tsx +++ b/packages/amis/__tests__/renderers/Form/select.test.tsx @@ -817,3 +817,45 @@ test('Renderer:select associated mode with virtual', async () => { expect(container).toMatchSnapshot(''); }); + +test('Renderer:select value contains delimiter when single', async () => { + const {container} = render( + amisRender({ + type: 'page', + body: { + type: 'form', + body: [ + { + label: '单选不分割', + type: 'select', + name: 'select', + value: 'a,b', + options: [ + { + label: 'ALabel', + value: 'a,b' + }, + { + label: 'BLabel', + value: 'b' + }, + { + label: 'CLabel', + value: 'c' + }, + { + label: 'DLabel', + value: 'd' + } + ] + } + ] + } + }) + ); + + expect( + container.querySelector('.cxd-Select-valueWrap .cxd-Select-value')! + .innerHTML + ).toEqual('ALabel'); +}); diff --git a/packages/amis/src/renderers/Form/Transfer.tsx b/packages/amis/src/renderers/Form/Transfer.tsx index e750a9652..6c09a2223 100644 --- a/packages/amis/src/renderers/Form/Transfer.tsx +++ b/packages/amis/src/renderers/Form/Transfer.tsx @@ -175,6 +175,10 @@ type OptionsControlWithSpinnerProps = OptionsControlProps & SpinnerExtraProps; export class BaseTransferRenderer< T extends OptionsControlWithSpinnerProps = BaseTransferProps > extends React.Component { + static defaultProps = { + multiple: true + }; + tranferRef?: any; reload() {