fix: options value normalize 修改 (#5280)

* fix: options value boolean 不 normalize

* 修改, 单测
This commit is contained in:
sansiro 2022-09-01 12:59:01 +08:00 committed by GitHub
parent 7d481e37f0
commit 1583e64df6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 59 additions and 39 deletions

View File

@ -26,7 +26,8 @@ import {
getTreeDepth,
flattenTree,
keyToPath,
getVariable
getVariable,
isObject
} from '../utils/helper';
import {reaction} from 'mobx';
import {
@ -460,11 +461,7 @@ export function registerOptionsControl(config: OptionsConfig) {
)
.then(() => this.normalizeValue());
}
} else if (
!isEqual(props.value, prevProps.value) &&
!this.whetherValueNormal() &&
props.formInited
) {
} else if (!isEqual(props.value, prevProps.value) && props.formInited) {
this.normalizeValue();
}
@ -566,34 +563,6 @@ export function registerOptionsControl(config: OptionsConfig) {
}
}
// 判断当前值是否符合预期的格式
whetherValueNormal() {
const {value, joinValues, extractValue, multiple} = this.props;
if (value === undefined) {
return true;
}
if (joinValues !== false || (!multiple && extractValue === true)) {
return typeof value === 'string' || typeof value === 'number';
}
if (multiple) {
if (!Array.isArray(value)) return false;
if (
extractValue === true &&
!value.every(
val => typeof val === 'string' || typeof val === 'number'
)
) {
return false;
}
}
return true;
}
// 当前值,跟设置预期的值格式不一致时自动转换。
normalizeValue() {
const {
@ -614,8 +583,8 @@ export function registerOptionsControl(config: OptionsConfig) {
}
if (joinValues !== false) {
if (!value || typeof value === 'string' || typeof value === 'number')
return;
// 只处理多选且值为 array 的情况,因为理应为分隔符隔开的字符串
if (!(multiple && Array.isArray(value))) return;
const selectedOptions = formItem.getSelectedOptions(value);

View File

@ -65,7 +65,6 @@ test('Renderer:radios', async () => {
expect(container).toMatchSnapshot();
});
test('Renderer:radios source & autoFill', async () => {
const {getByText, container} = render(
amisRender(
@ -104,7 +103,7 @@ test('Renderer:radios source & autoFill', async () => {
valueField: 'id',
optionClassName: 'class-a',
autoFill: {
fillFromRadios: "${fill}"
fillFromRadios: '${fill}'
}
},
{
@ -135,9 +134,61 @@ test('Renderer:radios source & autoFill', async () => {
fireEvent.click(getByText(/C/));
await waitFor(() => {
expect(
(container.querySelector('.cxd-TplField.autoFillClass span') as Element).innerHTML
(container.querySelector('.cxd-TplField.autoFillClass span') as Element)
.innerHTML
).toBe('13');
});
expect(container).toMatchSnapshot();
});
test('Renderer:radios with boolean value', async () => {
const onSubmit = jest.fn();
const {getByText, container} = render(
amisRender(
{
type: 'form',
title: 'The form',
body: [
{
name: 'radios',
type: 'radios',
label: 'radios',
options: [
{
label: 'Option True',
value: true
},
{
label: 'Option False',
value: false
}
]
}
],
submitText: 'Submit'
},
{onSubmit},
makeEnv()
)
);
await wait(200);
fireEvent.click(getByText(/Option True/));
await wait(200);
fireEvent.click(getByText(/Submit/));
await waitFor(() => {
expect(onSubmit).toBeCalled();
});
expect(onSubmit.mock.calls[0][0]).toMatchObject({
radios: true
});
fireEvent.click(getByText(/Option False/));
await wait(200);
fireEvent.click(getByText(/Submit/));
await waitFor(() => {
expect(onSubmit).toBeCalledTimes(2);
});
expect(onSubmit.mock.calls[1][0].radios).toEqual(false);
});