Skip to content

Commit 205d8d0

Browse files
HurenkaEkaterina Petrova
andauthored
Bugfix/select (#1397)
* new select styles init * Custom select compound component * Select component fix styles * Added react hook form controller * Moved from compound component * fixed vslues & onChange for controller * fixed tests & code cleanup * fix review * fixed linter * fixed discussions Co-authored-by: Ekaterina Petrova <[email protected]>
1 parent 2b79fee commit 205d8d0

File tree

25 files changed

+674
-412
lines changed

25 files changed

+674
-412
lines changed

kafka-ui-react-app/src/components/Connect/List/__tests__/__snapshots__/ListItem.spec.tsx.snap

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,11 @@ exports[`Connectors ListItem matches snapshot 1`] = `
227227
},
228228
},
229229
"selectStyles": Object {
230+
"backgroundColor": Object {
231+
"active": "#E3E6E8",
232+
"hover": "#E3E6E8",
233+
"normal": "#FFFFFF",
234+
},
230235
"borderColor": Object {
231236
"active": "#454F54",
232237
"disabled": "#E3E6E8",

kafka-ui-react-app/src/components/Connect/New/New.tsx

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ const New: React.FC<NewProps> = ({
112112
return null;
113113
}
114114

115+
const connectOptions = connects.map(({ name: connectName }) => ({
116+
value: connectName,
117+
label: connectName,
118+
}));
119+
115120
return (
116121
<FormProvider {...methods}>
117122
<PageHeading text="Create new connector" />
@@ -121,13 +126,21 @@ const New: React.FC<NewProps> = ({
121126
>
122127
<div className={['field', connectNameFieldClassName].join(' ')}>
123128
<InputLabel>Connect *</InputLabel>
124-
<Select selectSize="M" name="connectName" disabled={isSubmitting}>
125-
{connects.map(({ name }) => (
126-
<option key={name} value={name}>
127-
{name}
128-
</option>
129-
))}
130-
</Select>
129+
<Controller
130+
control={control}
131+
name="connectName"
132+
render={({ field: { name, onChange } }) => (
133+
<Select
134+
selectSize="M"
135+
name={name}
136+
disabled={isSubmitting}
137+
onChange={onChange}
138+
value={connectOptions[0].value}
139+
minWidth="100%"
140+
options={connectOptions}
141+
/>
142+
)}
143+
/>
131144
<FormError>
132145
<ErrorMessage errors={errors} name="connectName" />
133146
</FormError>

kafka-ui-react-app/src/components/ConsumerGroups/Details/Details.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,7 @@ const Details: React.FC = () => {
7777
<PageHeading text={consumerGroupID}>
7878
{!isReadOnly && (
7979
<Dropdown label={<VerticalElipsisIcon />} right>
80-
<DropdownItem onClick={onResetOffsets}>
81-
Reset offsets
82-
</DropdownItem>
80+
<DropdownItem onClick={onResetOffsets}>Reset offset</DropdownItem>
8381
<DropdownItem
8482
style={{ color: Colors.red[50] }}
8583
onClick={() => setIsConfirmationModalVisible(true)}

kafka-ui-react-app/src/components/ConsumerGroups/Details/ResetOffsets/ResetOffsets.tsx

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -182,24 +182,47 @@ const ResetOffsets: React.FC = () => {
182182
<form onSubmit={handleSubmit(onSubmit)}>
183183
<MainSelectorsWrapperStyled>
184184
<div>
185-
<InputLabel htmlFor="topic">Topic</InputLabel>
186-
<Select name="topic" id="topic" selectSize="M">
187-
{uniqueTopics.map((topic) => (
188-
<option key={topic} value={topic}>
189-
{topic}
190-
</option>
191-
))}
192-
</Select>
185+
<InputLabel id="topicLabel">Topic</InputLabel>
186+
<Controller
187+
control={control}
188+
name="topic"
189+
render={({ field: { name, onChange, value } }) => (
190+
<Select
191+
id="topic"
192+
selectSize="M"
193+
aria-labelledby="topicLabel"
194+
minWidth="100%"
195+
name={name}
196+
onChange={onChange}
197+
value={value}
198+
options={uniqueTopics.map((topic) => ({
199+
value: topic,
200+
label: topic,
201+
}))}
202+
/>
203+
)}
204+
/>
193205
</div>
194206
<div>
195-
<InputLabel htmlFor="resetType">Reset Type</InputLabel>
196-
<Select name="resetType" id="resetType" selectSize="M">
197-
{Object.values(ConsumerGroupOffsetsResetType).map((type) => (
198-
<option key={type} value={type}>
199-
{type}
200-
</option>
201-
))}
202-
</Select>
207+
<InputLabel id="resetTypeLabel">Reset Type</InputLabel>
208+
<Controller
209+
control={control}
210+
name="resetType"
211+
render={({ field: { name, onChange, value } }) => (
212+
<Select
213+
id="resetType"
214+
selectSize="M"
215+
aria-labelledby="resetTypeLabel"
216+
minWidth="100%"
217+
name={name}
218+
onChange={onChange}
219+
value={value}
220+
options={Object.values(ConsumerGroupOffsetsResetType).map(
221+
(type) => ({ value: type, label: type })
222+
)}
223+
/>
224+
)}
225+
/>
203226
</div>
204227
<div>
205228
<InputLabel>Partitions</InputLabel>

kafka-ui-react-app/src/components/ConsumerGroups/Details/ResetOffsets/__test__/ResetOffsets.spec.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ const resetConsumerGroupOffsetsMockCalled = () =>
3737
).toBeTruthy();
3838

3939
const selectresetTypeAndPartitions = async (resetType: string) => {
40-
userEvent.selectOptions(screen.getByLabelText('Reset Type'), resetType);
40+
userEvent.click(screen.getByLabelText('Reset Type'));
41+
userEvent.click(screen.getByText(resetType));
4142
userEvent.click(screen.getByText('Select...'));
4243
await waitFor(() => {
4344
userEvent.click(screen.getByText('Partition #0'));
@@ -48,7 +49,9 @@ const resetConsumerGroupOffsetsWith = async (
4849
resetType: string,
4950
offset: null | number = null
5051
) => {
51-
userEvent.selectOptions(screen.getByLabelText('Reset Type'), resetType);
52+
userEvent.click(screen.getByLabelText('Reset Type'));
53+
const options = screen.getAllByText(resetType);
54+
userEvent.click(options.length > 1 ? options[1] : options[0]);
5255
userEvent.click(screen.getByText('Select...'));
5356
await waitFor(() => {
5457
userEvent.click(screen.getByText('Partition #0'));

kafka-ui-react-app/src/components/ConsumerGroups/Details/__tests__/Details.spec.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ describe('Details component', () => {
4141
fetchMock.reset();
4242
});
4343

44-
describe('when consumer gruops are NOT fetched', () => {
44+
describe('when consumer groups are NOT fetched', () => {
4545
it('renders progress bar for initial state', () => {
4646
fetchMock.getOnce(
4747
`/api/clusters/${clusterName}/consumer-groups/${groupId}`,
@@ -73,8 +73,8 @@ describe('Details component', () => {
7373
expect(screen.queryByRole('dialog')).not.toBeInTheDocument();
7474
});
7575

76-
it('handles [Reset offsets] click', async () => {
77-
userEvent.click(screen.getByText('Reset offsets'));
76+
it('handles [Reset offset] click', async () => {
77+
userEvent.click(screen.getByText('Reset offset'));
7878
expect(history.location.pathname).toEqual(
7979
clusterConsumerGroupResetOffsetsPath(clusterName, groupId)
8080
);
@@ -90,7 +90,7 @@ describe('Details component', () => {
9090
expect(screen.queryByRole('dialog')).not.toBeInTheDocument();
9191
});
9292

93-
it('hanles [Delete consumer group] click', async () => {
93+
it('handles [Delete consumer group] click', async () => {
9494
expect(screen.queryByRole('dialog')).not.toBeInTheDocument();
9595
userEvent.click(screen.getByText('Delete consumer group'));
9696

kafka-ui-react-app/src/components/Schemas/Edit/Edit.tsx

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -85,35 +85,44 @@ const Edit: React.FC = () => {
8585
<div>
8686
<div>
8787
<InputLabel>Type</InputLabel>
88-
<Select
88+
<Controller
89+
control={control}
90+
rules={{ required: true }}
8991
name="schemaType"
90-
required
91-
defaultValue={schema.schemaType}
92-
disabled={isSubmitting}
93-
>
94-
{Object.keys(SchemaType).map((type: string) => (
95-
<option key={type} value={type}>
96-
{type}
97-
</option>
98-
))}
99-
</Select>
92+
render={({ field: { name, onChange } }) => (
93+
<Select
94+
name={name}
95+
value={schema.schemaType}
96+
onChange={onChange}
97+
minWidth="100%"
98+
disabled={isSubmitting}
99+
options={Object.keys(SchemaType).map((type) => ({
100+
value: type,
101+
label: type,
102+
}))}
103+
/>
104+
)}
105+
/>
100106
</div>
101107

102108
<div>
103109
<InputLabel>Compatibility level</InputLabel>
104-
<Select
110+
<Controller
111+
control={control}
105112
name="compatibilityLevel"
106-
defaultValue={schema.compatibilityLevel}
107-
disabled={isSubmitting}
108-
>
109-
{Object.keys(CompatibilityLevelCompatibilityEnum).map(
110-
(level: string) => (
111-
<option key={level} value={level}>
112-
{level}
113-
</option>
114-
)
113+
render={({ field: { name, onChange } }) => (
114+
<Select
115+
name={name}
116+
value={schema.compatibilityLevel}
117+
onChange={onChange}
118+
minWidth="100%"
119+
disabled={isSubmitting}
120+
options={Object.keys(
121+
CompatibilityLevelCompatibilityEnum
122+
).map((level) => ({ value: level, label: level }))}
123+
/>
115124
)}
116-
</Select>
125+
/>
117126
</div>
118127
</div>
119128
<S.EditorsWrapper>

kafka-ui-react-app/src/components/Schemas/List/GlobalSchemaSelector/GlobalSchemaSelector.tsx

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,8 @@ const GlobalSchemaSelector: React.FC = () => {
4545
fetchData();
4646
}, []);
4747

48-
const handleChangeCompatibilityLevel = (
49-
event: React.ChangeEvent<HTMLSelectElement>
50-
) => {
51-
setNextCompatibilityLevel(
52-
event.target.value as CompatibilityLevelCompatibilityEnum
53-
);
48+
const handleChangeCompatibilityLevel = (level: string | number) => {
49+
setNextCompatibilityLevel(level as CompatibilityLevelCompatibilityEnum);
5450
setIsConfirmationVisible(true);
5551
};
5652

@@ -62,10 +58,10 @@ const GlobalSchemaSelector: React.FC = () => {
6258
clusterName,
6359
compatibilityLevel: { compatibility: nextCompatibilityLevel },
6460
});
65-
dispatch(fetchSchemas(clusterName));
6661
setCurrentCompatibilityLevel(nextCompatibilityLevel);
6762
setNextCompatibilityLevel(undefined);
6863
setIsConfirmationVisible(false);
64+
dispatch(fetchSchemas(clusterName));
6965
} catch (e) {
7066
const err = await getResponse(e as Response);
7167
dispatch(serverErrorAlertAdded(err));
@@ -81,18 +77,14 @@ const GlobalSchemaSelector: React.FC = () => {
8177
<div>Global Compatibility Level: </div>
8278
<Select
8379
selectSize="M"
84-
value={currentCompatibilityLevel}
80+
defaultValue={currentCompatibilityLevel}
81+
minWidth="200px"
8582
onChange={handleChangeCompatibilityLevel}
8683
disabled={isFetching || isUpdating || isConfirmationVisible}
87-
>
88-
{Object.keys(CompatibilityLevelCompatibilityEnum).map(
89-
(level: string) => (
90-
<option key={level} value={level}>
91-
{level}
92-
</option>
93-
)
84+
options={Object.keys(CompatibilityLevelCompatibilityEnum).map(
85+
(level) => ({ value: level, label: level })
9486
)}
95-
</Select>
87+
/>
9688
<ConfirmationModal
9789
isOpen={isConfirmationVisible}
9890
onCancel={() => setIsConfirmationVisible(false)}

kafka-ui-react-app/src/components/Schemas/List/GlobalSchemaSelector/__test__/GlobalSchemaSelector.spec.tsx

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import { screen, waitFor } from '@testing-library/react';
2+
import { screen, waitFor, within } from '@testing-library/react';
33
import { render } from 'lib/testHelpers';
44
import { CompatibilityLevelCompatibilityEnum } from 'generated-sources';
55
import GlobalSchemaSelector from 'components/Schemas/List/GlobalSchemaSelector/GlobalSchemaSelector';
@@ -10,15 +10,20 @@ import fetchMock from 'fetch-mock';
1010

1111
const clusterName = 'testClusterName';
1212

13-
const selectForwardOption = () =>
14-
userEvent.selectOptions(
15-
screen.getByRole('listbox'),
16-
CompatibilityLevelCompatibilityEnum.FORWARD
13+
const selectForwardOption = () => {
14+
const dropdownElement = screen.getByRole('listbox');
15+
// clicks to open dropdown
16+
userEvent.click(within(dropdownElement).getByRole('option'));
17+
userEvent.click(
18+
screen.getByText(CompatibilityLevelCompatibilityEnum.FORWARD)
1719
);
20+
};
1821

1922
const expectOptionIsSelected = (option: string) => {
20-
const optionElement: HTMLOptionElement = screen.getByText(option);
21-
expect(optionElement.selected).toBeTruthy();
23+
const dropdownElement = screen.getByRole('listbox');
24+
const selectedOption = within(dropdownElement).getAllByRole('option');
25+
expect(selectedOption.length).toEqual(1);
26+
expect(selectedOption[0]).toHaveTextContent(option);
2227
};
2328

2429
describe('GlobalSchemaSelector', () => {

0 commit comments

Comments
 (0)