@ssa-ui-kit/core 1.0.18 → 1.0.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ssa-ui-kit/core",
3
- "version": "1.0.18",
3
+ "version": "1.0.19",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "private": false,
@@ -141,6 +141,68 @@ describe('Dropdown', () => {
141
141
  }
142
142
  });
143
143
 
144
+ it('Selected item changed successfully', async () => {
145
+ const selectedItem = items[2];
146
+ const {
147
+ user,
148
+ mockOnChange,
149
+ getByRole,
150
+ queryByRole,
151
+ getByTestId,
152
+ rerender,
153
+ } = setup({
154
+ selectedItem,
155
+ });
156
+
157
+ expect(mockOnChange).not.toBeCalled();
158
+
159
+ let dropdownEl = getByTestId('dropdown');
160
+
161
+ let dropdownToggleEl = within(dropdownEl).getByRole('combobox');
162
+
163
+ let listboxEl = queryByRole('listbox');
164
+
165
+ await user.click(dropdownToggleEl);
166
+
167
+ listboxEl = getByRole('listbox');
168
+ const listItemEls = within(listboxEl).getAllByRole('listitem');
169
+
170
+ dropdownToggleEl = within(dropdownEl).getByRole('combobox');
171
+
172
+ await within(dropdownToggleEl).findByTitle('Carrot up');
173
+
174
+ for (let i = 0; i < items.length; ++i) {
175
+ const listItem = items[i];
176
+ const listItemEl = listItemEls[i];
177
+
178
+ const itemListValue = getListItemValue(listItem);
179
+
180
+ await within(listItemEl).findByText(itemListValue);
181
+ expect(within(listItemEl).getByRole('button')).toHaveTextContent(
182
+ itemListValue,
183
+ );
184
+
185
+ if (listItem.id === selectedItem.id) {
186
+ expect(listItemEl).toHaveAttribute('aria-selected', 'true');
187
+ expect(listItemEl).toHaveStyle('background: #DEE1EC');
188
+ } else {
189
+ expect(listItemEl).toHaveAttribute('aria-selected', 'false');
190
+ }
191
+ }
192
+
193
+ rerender(
194
+ <Dropdown onChange={mockOnChange} selectedItem={items[0]}>
195
+ {items.map((item, index) => (
196
+ <DropdownOption key={index} value={item.value} />
197
+ ))}
198
+ </Dropdown>,
199
+ );
200
+
201
+ dropdownEl = getByTestId('dropdown');
202
+ dropdownToggleEl = within(dropdownEl).getByRole('combobox');
203
+ expect(dropdownToggleEl).toHaveTextContent(getListItemValue(items[0]));
204
+ });
205
+
144
206
  it('Renders with an empty items array', async () => {
145
207
  const mockOnChange = jest.fn();
146
208
  const { getByTestId, queryByRole, getByRole } = render(
@@ -1,9 +1,9 @@
1
+ import { useState } from 'react';
1
2
  import type { Meta, StoryObj } from '@storybook/react';
2
3
  import styled from '@emotion/styled';
3
4
  import { css } from '@emotion/react';
4
-
5
5
  import DropdownOption from '@components/DropdownOption';
6
-
6
+ import Button from '@components/Button';
7
7
  import Dropdown from './Dropdown';
8
8
  import { DropdownProps } from './types';
9
9
  import { DropdownOptionProps } from '../..';
@@ -153,3 +153,28 @@ export const Custom: StoryObj = (args: Args) => {
153
153
  Custom.args = {
154
154
  isDisabled: false,
155
155
  };
156
+
157
+ export const DynamicallyChangedSelectedItem: StoryObj = (args: Args) => {
158
+ const [selectedIndex, setSelectedIndex] = useState(1);
159
+
160
+ const handleUpdate = () => {
161
+ const newIndex = selectedIndex < items.length - 1 ? selectedIndex + 1 : 0;
162
+ setSelectedIndex(newIndex);
163
+ };
164
+ return (
165
+ <div>
166
+ <Dropdown
167
+ isDisabled={args.isDisabled}
168
+ selectedItem={items[selectedIndex]}>
169
+ {items.map((item) => (
170
+ <DropdownOption key={item.id} value={item.value} />
171
+ ))}
172
+ </Dropdown>
173
+ <Button variant="info" css={{ marginLeft: 10 }} onClick={handleUpdate}>
174
+ Update selected item
175
+ </Button>
176
+ </div>
177
+ );
178
+ };
179
+
180
+ DynamicallyChangedSelectedItem.args = { isDisabled: false };
@@ -77,6 +77,10 @@ const Dropdown = <T extends DropdownOptionProps>({
77
77
  }
78
78
  }, [isOpen, isDisabled, isFocused]);
79
79
 
80
+ useEffect(() => {
81
+ setActiveItem(selectedItem);
82
+ }, [selectedItem]);
83
+
80
84
  useEffect(() => {
81
85
  if (isDisabled && isOpen) {
82
86
  setIsOpen(false);