From aabc216a20ec96870e77eab3ac59392314d5f81f Mon Sep 17 00:00:00 2001 From: "e.muhamethanov" Date: Thu, 15 May 2025 12:55:55 +0300 Subject: [PATCH] fix(ChipsSelect): fix onChange with custom fields --- .../components/ChipsInput/useChipsInput.ts | 2 +- .../ChipsSelect/ChipsSelect.test.tsx | 42 +++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/packages/vkui/src/components/ChipsInput/useChipsInput.ts b/packages/vkui/src/components/ChipsInput/useChipsInput.ts index 8b96573fc7..496c60d25c 100644 --- a/packages/vkui/src/components/ChipsInput/useChipsInput.ts +++ b/packages/vkui/src/components/ChipsInput/useChipsInput.ts @@ -115,7 +115,7 @@ export const useChipsInput = ({ ? getNewOptionData(option.value, option.label) : getNewOptionData(option, typeof option === 'string' ? option : ''); resolvedNextOptionsSet.add(resolvedOption.value); - return resolvedOption; + return isLikeObjectOption ? { ...option, ...resolvedOption } : resolvedOption; }); const nextValue = prevValue.filter( diff --git a/packages/vkui/src/components/ChipsSelect/ChipsSelect.test.tsx b/packages/vkui/src/components/ChipsSelect/ChipsSelect.test.tsx index 3a2593de85..ac49450d91 100644 --- a/packages/vkui/src/components/ChipsSelect/ChipsSelect.test.tsx +++ b/packages/vkui/src/components/ChipsSelect/ChipsSelect.test.tsx @@ -159,6 +159,48 @@ describe('ChipsSelect', () => { expect(onClose).toHaveBeenCalledTimes(1); }); + it('should check custom fields when onChange', async () => { + const onChange = jest.fn(); + const options = colors.map((color, index) => ({ + ...color, + custom: index.toString(), + })); + + const result = render( + , + ); + const inputLocator = result.getByRole('combobox'); + await userEvent.click(inputLocator); + await waitForFloatingPosition(); + + const dropdownOption = within(result.getByTestId('dropdown')).getByRole('option', { + name: withRegExp(FIRST_OPTION.label), + }); + await userEvent.hover(dropdownOption); // для вызова onDropdownMouseLeave + await userEvent.hover(inputLocator); + await userEvent.click(dropdownOption); + + result.rerender( + , + ); + expect( + result.getByRole('option', { + name: withRegExp(FIRST_OPTION.label), + }), + ).toBeTruthy(); + expect(onChange).toHaveBeenCalledWith([{ ...FIRST_OPTION, custom: '0' }]); + }); + it.each(['{ArrowDown}', 'typing text'])( 'closes dropdown on {Escape} and open when %s', async (type) => {