Skip to content

Commit

Permalink
fix: 修改Checkbox.Group的value属性内容为string和number的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
xinup committed Jan 26, 2022
1 parent d1545f8 commit 1796843
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 11 deletions.
6 changes: 3 additions & 3 deletions site/test-coverage.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions src/checkbox/CheckboxGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ export interface CheckboxGroupProps extends TdCheckboxGroupProps, StyledProps {
// 将 checkBox 的 value 转换为 string|number
const getCheckboxValue = (v: CheckboxOption): string | number => {
switch (typeof v) {
case 'number' || 'string':
return v as string | number;
case 'number':
return v as number;
case 'string':
return v as string;
case 'object': {
const vs = v as CheckboxOptionObj;
return vs.value;
Expand Down
42 changes: 38 additions & 4 deletions src/checkbox/__tests__/__snapshots__/checkbox.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,9 @@ exports[`CheckboxGroup max 1`] = `
class="t-checkbox-group"
>
<label
class="t-checkbox t-is-checked"
class="t-checkbox"
>
<input
checked=""
class="t-checkbox__former"
readonly=""
type="checkbox"
Expand Down Expand Up @@ -113,11 +112,10 @@ exports[`CheckboxGroup max 1`] = `
</span>
</label>
<label
class="t-checkbox t-is-disabled"
class="t-checkbox"
>
<input
class="t-checkbox__former"
disabled=""
readonly=""
type="checkbox"
value="bj"
Expand Down Expand Up @@ -243,6 +241,42 @@ exports[`CheckboxGroup option 1`] = `
北京
</span>
</label>
<label
class="t-checkbox"
>
<input
class="t-checkbox__former"
readonly=""
type="checkbox"
value="1"
/>
<span
class="t-checkbox__input"
/>
<span
class="t-checkbox__label"
>
1
</span>
</label>
<label
class="t-checkbox"
>
<input
class="t-checkbox__former"
readonly=""
type="checkbox"
value="重庆"
/>
<span
class="t-checkbox__input"
/>
<span
class="t-checkbox__label"
>
重庆
</span>
</label>
<label
class="t-checkbox t-is-indeterminate"
>
Expand Down
12 changes: 10 additions & 2 deletions src/checkbox/__tests__/checkbox.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ describe('CheckboxGroup', () => {
{ value: '上海', label: '上海' },
{ value: '广州', label: '广州', disabled: true },
{ value: '北京', label: '北京', name: '北京' },
1,
'重庆',
{ label: '全选', checkAll: true },
]}
></Checkbox.Group>,
Expand All @@ -102,11 +104,16 @@ describe('CheckboxGroup', () => {
expect(asFragment()).toMatchSnapshot();
});

test('value', () => {
test('value is string', () => {
const { container } = render(<Checkbox.Group options={['北京', '广州']} value={['北京']}></Checkbox.Group>);
expect(container.firstChild.firstChild).toHaveClass('t-is-checked');
});

test('value is number', () => {
const { container } = render(<Checkbox.Group options={[1, 2]} value={[1]}></Checkbox.Group>);
expect(container.firstChild.firstChild).toHaveClass('t-is-checked');
});

test('defaultValue', () => {
const { container } = render(
<Checkbox.Group defaultValue={['gz']}>
Expand Down Expand Up @@ -138,13 +145,14 @@ describe('CheckboxGroup', () => {

test('max', () => {
const { container, asFragment } = render(
<Checkbox.Group max={2} defaultValue={['gz', 'sz']}>
<Checkbox.Group max={2} defaultValue={['sz']}>
<Checkbox value="gz">广州</Checkbox>
<Checkbox value="sz">深圳</Checkbox>
<Checkbox value="bj">北京</Checkbox>
</Checkbox.Group>,
);
expect(asFragment()).toMatchSnapshot();
fireEvent.click(container.firstChild.firstChild);
expect(container.firstChild.lastChild).toHaveClass('t-is-disabled');
});
});

0 comments on commit 1796843

Please sign in to comment.