Skip to content

Commit 74969b0

Browse files
authored
fix: tagRender should work on max tag (#1044)
* fix: tagRender should work on max tag * chore: update @type/jest * chore: fix lint and tsc errors
1 parent d905f98 commit 74969b0

16 files changed

+67
-36
lines changed

docs/examples/filterSort.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const Test = () => (
2222
filterSort={sorterByLabel}
2323
optionFilterProp="label"
2424
options={incidencesStateResource}
25-
></Select>
25+
/>
2626
<h3> without filter sort </h3>
2727
<Select
2828
showSearch

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@
5555
},
5656
"devDependencies": {
5757
"@rc-component/father-plugin": "^1.0.2",
58-
"@testing-library/jest-dom": "^5.16.5",
59-
"@testing-library/react": "^13.0.0",
60-
"@types/jest": "^26.0.24",
58+
"@testing-library/jest-dom": "^6.4.5",
59+
"@testing-library/react": "^15.0.6",
60+
"@types/jest": "^29.5.12",
6161
"@types/react": "^18.2.45",
6262
"@types/react-dom": "^18.2.18",
6363
"babel-jest": "^29.6.1",

src/BaseSelect.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export type CustomTagProps = {
6767
disabled: boolean;
6868
onClose: (event?: React.MouseEvent<HTMLElement, MouseEvent>) => void;
6969
closable: boolean;
70+
isMaxTag: boolean;
7071
};
7172

7273
export interface BaseSelectRef {

src/Selector/MultipleSelector.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,16 +128,17 @@ const SelectSelector: React.FC<SelectorProps> = (props) => {
128128
value: RawValueType,
129129
content: React.ReactNode,
130130
itemDisabled: boolean,
131-
closable: boolean,
132-
onClose: React.MouseEventHandler,
131+
closable?: boolean,
132+
onClose?: React.MouseEventHandler,
133+
isMaxTag?: boolean,
133134
) => {
134135
const onMouseDown = (e: React.MouseEvent) => {
135136
onPreventMouseDown(e);
136137
onToggleOpen(!open);
137138
};
138139
return (
139140
<span onMouseDown={onMouseDown}>
140-
{tagRender({ label: content, value, disabled: itemDisabled, closable, onClose })}
141+
{tagRender({ label: content, value, disabled: itemDisabled, closable, onClose, isMaxTag: !!isMaxTag })}
141142
</span>
142143
);
143144
};
@@ -174,8 +175,9 @@ const SelectSelector: React.FC<SelectorProps> = (props) => {
174175
typeof maxTagPlaceholder === 'function'
175176
? maxTagPlaceholder(omittedValues)
176177
: maxTagPlaceholder;
177-
178-
return defaultRenderSelector({ title: content }, content, false);
178+
return typeof tagRender === 'function'
179+
? customizeRenderSelector(undefined, content, false, false, undefined, true)
180+
: defaultRenderSelector({ title: content }, content, false);
179181
};
180182

181183
// >>> Input Node

tests/BaseSelect.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { OptionListProps, RefOptionListProps } from '@/OptionList';
2-
import { act, fireEvent, render } from '@testing-library/react';
3-
import { forwardRef } from 'react';
2+
import { fireEvent, render } from '@testing-library/react';
3+
import { forwardRef, act } from 'react';
44
import BaseSelect from '../src/BaseSelect';
55

66
const OptionList = forwardRef<RefOptionListProps, OptionListProps>(() => (

tests/Combobox.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/* eslint-disable max-classes-per-file */
22

33
import '@testing-library/jest-dom';
4-
import { act, fireEvent, render } from '@testing-library/react';
4+
import { fireEvent, render } from '@testing-library/react';
55
import KeyCode from 'rc-util/lib/KeyCode';
66
import { resetWarned } from 'rc-util/lib/warning';
7-
import React from 'react';
7+
import React, { act } from 'react';
88
import type { SelectProps } from '../src';
99
import Select, { Option } from '../src';
1010
import allowClearTest from './shared/allowClearTest';

tests/Field.test.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { act } from 'react-dom/test-utils';
2-
import * as React from 'react';
1+
import React, { act } from 'react';
32
import Select from '../src';
43
import type { SelectProps } from '../src';
54
import { injectRunAllTimers } from './utils/common';

tests/Multiple.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Select, { Option, OptGroup } from '../src';
44
import focusTest from './shared/focusTest';
55
import blurTest from './shared/blurTest';
66
import hoverTest from './shared/hoverTest';
7-
import renderTest from './shared/renderTest';
7+
import maxTagRenderTest from './shared/maxTagRenderTest';
88
import removeSelectedTest from './shared/removeSelectedTest';
99
import dynamicChildrenTest from './shared/dynamicChildrenTest';
1010
import inputFilterTest from './shared/inputFilterTest';
@@ -27,7 +27,7 @@ describe('Select.Multiple', () => {
2727
focusTest('multiple');
2828
blurTest('multiple');
2929
hoverTest('multiple');
30-
renderTest('multiple');
30+
maxTagRenderTest('multiple');
3131
removeSelectedTest('multiple');
3232
dynamicChildrenTest('multiple');
3333
inputFilterTest('multiple');

tests/OptionList.test.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import KeyCode from 'rc-util/lib/KeyCode';
2-
import React from 'react';
3-
import { act } from 'react-dom/test-utils';
2+
import React, { act } from 'react';
43
import { BaseSelectContext } from '../src/hooks/useBaseProps';
54
import type { RefOptionListProps } from '../src/OptionList';
65
import OptionList from '../src/OptionList';

tests/Select.test.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ import KeyCode from 'rc-util/lib/KeyCode';
44
import { spyElementPrototypes } from 'rc-util/lib/test/domHook';
55
import { resetWarned } from 'rc-util/lib/warning';
66
import type { ScrollConfig } from 'rc-virtual-list/lib/List';
7-
import React from 'react';
8-
import { act } from 'react-dom/test-utils';
7+
import React, { act } from 'react';
98
import type { SelectProps } from '../src';
109
import Select, { OptGroup, Option, useBaseProps } from '../src';
1110
import type { BaseSelectRef } from '../src/BaseSelect';

tests/Tags.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import hoverTest from './shared/hoverTest';
1111
import inputFilterTest from './shared/inputFilterTest';
1212
import openControlledTest from './shared/openControlledTest';
1313
import removeSelectedTest from './shared/removeSelectedTest';
14-
import renderTest from './shared/renderTest';
14+
import maxTagRenderTest from './shared/maxTagRenderTest';
1515
import throwOptionValue from './shared/throwOptionValue';
1616
import { injectRunAllTimers, findSelection, expectOpen, toggleOpen, keyDown } from './utils/common';
1717

@@ -22,7 +22,7 @@ describe('Select.Tags', () => {
2222
focusTest('tags', {});
2323
blurTest('tags');
2424
hoverTest('tags');
25-
renderTest('tags');
25+
maxTagRenderTest('tags');
2626
removeSelectedTest('tags');
2727
throwOptionValue('tags');
2828
dynamicChildrenTest('tags', {});

tests/__snapshots__/Multiple.test.tsx.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`Select.Multiple render not display maxTagPlaceholder if maxTagCount not reach 1`] = `
3+
exports[`Select.Multiple max tag render not display maxTagPlaceholder if maxTagCount not reach 1`] = `
44
<div
55
class="rc-select rc-select-multiple rc-select-show-search"
66
>
@@ -50,7 +50,7 @@ exports[`Select.Multiple render not display maxTagPlaceholder if maxTagCount not
5050
</div>
5151
`;
5252

53-
exports[`Select.Multiple render truncates tags by maxTagCount and show maxTagPlaceholder 1`] = `
53+
exports[`Select.Multiple max tag render truncates tags by maxTagCount and show maxTagPlaceholder 1`] = `
5454
<div
5555
class="rc-select rc-select-multiple rc-select-show-search"
5656
>
@@ -167,7 +167,7 @@ exports[`Select.Multiple render truncates tags by maxTagCount and show maxTagPla
167167
</div>
168168
`;
169169

170-
exports[`Select.Multiple render truncates tags by maxTagCount and show maxTagPlaceholder function 1`] = `
170+
exports[`Select.Multiple max tag render truncates tags by maxTagCount and show maxTagPlaceholder function 1`] = `
171171
<div
172172
class="rc-select rc-select-multiple rc-select-show-search"
173173
>
@@ -285,7 +285,7 @@ exports[`Select.Multiple render truncates tags by maxTagCount and show maxTagPla
285285
</div>
286286
`;
287287

288-
exports[`Select.Multiple render truncates values by maxTagTextLength 1`] = `
288+
exports[`Select.Multiple max tag render truncates values by maxTagTextLength 1`] = `
289289
<div
290290
class="rc-select rc-select-multiple rc-select-show-search"
291291
>

tests/__snapshots__/Tags.test.tsx.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ exports[`Select.Tags OptGroup renders correctly 1`] = `
228228
</div>
229229
`;
230230

231-
exports[`Select.Tags render not display maxTagPlaceholder if maxTagCount not reach 1`] = `
231+
exports[`Select.Tags max tag render not display maxTagPlaceholder if maxTagCount not reach 1`] = `
232232
<div
233233
class="rc-select rc-select-multiple rc-select-show-search"
234234
>
@@ -275,7 +275,7 @@ exports[`Select.Tags render not display maxTagPlaceholder if maxTagCount not rea
275275
</div>
276276
`;
277277

278-
exports[`Select.Tags render truncates tags by maxTagCount and show maxTagPlaceholder 1`] = `
278+
exports[`Select.Tags max tag render truncates tags by maxTagCount and show maxTagPlaceholder 1`] = `
279279
<div
280280
class="rc-select rc-select-multiple rc-select-show-search"
281281
>
@@ -389,7 +389,7 @@ exports[`Select.Tags render truncates tags by maxTagCount and show maxTagPlaceho
389389
</div>
390390
`;
391391

392-
exports[`Select.Tags render truncates tags by maxTagCount and show maxTagPlaceholder function 1`] = `
392+
exports[`Select.Tags max tag render truncates tags by maxTagCount and show maxTagPlaceholder function 1`] = `
393393
<div
394394
class="rc-select rc-select-multiple rc-select-show-search"
395395
>
@@ -504,7 +504,7 @@ exports[`Select.Tags render truncates tags by maxTagCount and show maxTagPlaceho
504504
</div>
505505
`;
506506

507-
exports[`Select.Tags render truncates values by maxTagTextLength 1`] = `
507+
exports[`Select.Tags max tag render truncates values by maxTagTextLength 1`] = `
508508
<div
509509
class="rc-select rc-select-multiple rc-select-show-search"
510510
>

tests/focus.test.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import React, { useState } from 'react';
2-
import { act } from 'react-dom/test-utils';
1+
import React, { useState, act } from 'react';
32
import Select from '../src';
43
import { fireEvent, render } from '@testing-library/react';
54

tests/shared/renderTest.tsx renamed to tests/shared/maxTagRenderTest.tsx

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { findSelection } from '../utils/common';
55
import { render } from '@testing-library/react';
66

77
export default function maxTagTextLengthTest(mode: any) {
8-
describe('render', () => {
8+
describe('max tag render', () => {
99
it('truncates values by maxTagTextLength', () => {
1010
const { container } = render(
1111
<Select mode={mode} value={['one', 'two']} maxTagTextLength={2}>
@@ -90,5 +90,37 @@ export default function maxTagTextLengthTest(mode: any) {
9090

9191
expect(container.firstChild).toMatchSnapshot();
9292
});
93+
94+
it('tagRender should work on maxTagPlaceholder', () => {
95+
const tagRender = jest.fn();
96+
const maxTagPlaceholder = (omittedValues) => (
97+
`${omittedValues.length} values omitted`
98+
);
99+
render(
100+
<Select
101+
mode={mode}
102+
value={['one', 'two', 'three']}
103+
maxTagCount={2}
104+
tagRender={tagRender}
105+
maxTagPlaceholder={maxTagPlaceholder}
106+
>
107+
<Option value="one">One</Option>
108+
<Option value="two">Two</Option>
109+
<Option value="three">Three</Option>
110+
</Select>,
111+
);
112+
expect(tagRender).toHaveBeenCalledWith(expect.objectContaining({
113+
isMaxTag: false,
114+
label: 'One',
115+
}));
116+
expect(tagRender).toHaveBeenCalledWith(expect.objectContaining({
117+
isMaxTag: false,
118+
label: 'Two',
119+
}));
120+
expect(tagRender).toHaveBeenCalledWith(expect.objectContaining({
121+
isMaxTag: true,
122+
label: '1 values omitted',
123+
}));
124+
});
93125
});
94126
}

tests/utils/common.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import { act } from 'react';
12
import { createEvent, fireEvent } from '@testing-library/react';
2-
import { act } from 'react-dom/test-utils';
33

44
export function expectOpen(wrapper: any, open: boolean = true) {
55
if (wrapper instanceof HTMLElement) {
@@ -86,7 +86,7 @@ export function injectRunAllTimers(jest: Jest) {
8686
originRunAllTimers = jest.runAllTimers;
8787

8888
jest.runAllTimers = () => {
89-
let result: typeof jest;
89+
let result;
9090
act(() => {
9191
result = originRunAllTimers();
9292
});

0 commit comments

Comments
 (0)