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() {