Skip to content

Commit 95f6817

Browse files
authored
fix(dropdown): fix lost values on virtual list (#376)
1 parent 7828c22 commit 95f6817

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

src/dropdown/__tests__/dropdown.test.tsx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,4 +351,34 @@ describe('Test Dropdown.Select Component', () => {
351351
'ant-checkbox-checked'
352352
);
353353
});
354+
355+
it('Should get correct checked values on virtual list', () => {
356+
const fn = jest.fn();
357+
const { getByTestId, getByText } = render(
358+
<Dropdown.Select
359+
value={[2, 1000, 2000]}
360+
options={Array.from({ length: 10000 }).map((_, index) => ({
361+
label: index,
362+
value: index,
363+
}))}
364+
onChange={fn}
365+
getPopupContainer={(node) => node}
366+
>
367+
<Button type="link" data-testid="trigger">
368+
打开下拉
369+
</Button>
370+
</Dropdown.Select>
371+
);
372+
373+
fireEvent.click(getByTestId('trigger'));
374+
act(() => {
375+
jest.runAllTimers();
376+
});
377+
378+
fireEvent.click(getByText('1'));
379+
expect(fn).toBeCalledWith([1000, 2, 2000, 1]);
380+
381+
fireEvent.click(getByText('2'));
382+
expect(fn).toBeCalledWith([1000, 2000]);
383+
});
354384
});

src/dropdown/demos/virtual.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@ import { Button } from 'antd';
33
import { Dropdown } from 'dt-react-component';
44

55
export default () => {
6-
const [selected, setSelected] = useState<number[]>([2]);
6+
const [selected, setSelected] = useState<number[]>([2, 1000, 2000]);
77

88
return (
99
<Dropdown.Select
1010
value={selected}
1111
options={new Array(10000).fill('').map((_, idx) => idx)}
12-
onChange={(val) => setSelected(val as number[])}
12+
onChange={(val) => {
13+
console.log(val);
14+
setSelected(val as number[]);
15+
}}
1316
onSubmit={() => {
1417
console.log('submit');
1518
}}

src/dropdown/select.tsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ export default function Select({
4949
onChange?.(options?.filter((i) => i.disabled).map((i) => i.value) || []);
5050
};
5151

52+
const handleChange = (e: CheckboxChangeEvent) => {
53+
const next = e.target.checked
54+
? [...(value || []), e.target.value]
55+
: value?.filter((i) => i !== e.target.value);
56+
onChange?.(next);
57+
};
58+
5259
const handleShadow = (target: HTMLDivElement) => {
5360
if (target) {
5461
if (!target.querySelector(`.${prefix}__shadow`)) {
@@ -116,7 +123,7 @@ export default function Select({
116123
</Checkbox>
117124
</Col>
118125
<Col span={24} className={`${prefix}__menu`}>
119-
<Checkbox.Group onChange={onChange} value={value}>
126+
<Checkbox.Group value={value}>
120127
<List
121128
data={options}
122129
itemKey="value"
@@ -136,7 +143,11 @@ export default function Select({
136143
key={option.value.toString()}
137144
className={`${prefix}__col`}
138145
>
139-
<Checkbox disabled={option.disabled} value={option.value}>
146+
<Checkbox
147+
disabled={option.disabled}
148+
value={option.value}
149+
onChange={handleChange}
150+
>
140151
{option.label}
141152
</Checkbox>
142153
</Col>

0 commit comments

Comments
 (0)