Skip to content

Commit 2dc4bc2

Browse files
Bhas-karKanchi Bhaskarkennylam2nikhiltomtay1orjones
authored
fix: Select component on hover shows invalid title/tooltip (#18740)
* fix: implemented the useStatefor default and inSet-option Value display * fix: updated Select.stories.js to reflect accurate values for the SelectedItem. * fix: added a test case for the select dropdown text * fix: updated all-contributors * fix: added a child type check * fix: added a child type wrapped check * fix: pushed bug fix code * fix: commit code proposed Co-authored-by: Taylor Jones <tay1orjones@users.noreply.github.com> * fix: fixed build error * fix: fix e2e Select component tests * fix: addressed review comments --------- Co-authored-by: Kanchi Bhaskar <Kanchi.Bhaskar@ibm.com> Co-authored-by: kennylam <909118+kennylam@users.noreply.github.com> Co-authored-by: Nikhil Tomar <63502271+2nikhiltom@users.noreply.github.com> Co-authored-by: Taylor Jones <tay1orjones@users.noreply.github.com>
1 parent 2b024ec commit 2dc4bc2

File tree

6 files changed

+55
-14
lines changed

6 files changed

+55
-14
lines changed

.all-contributorsrc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1790,6 +1790,16 @@
17901790
"contributions": [
17911791
"code"
17921792
]
1793+
},
1794+
{
1795+
"login": "Bhas-kar",
1796+
"name": "Kanchi Bhaskar",
1797+
"avatar_url": "https://avatars.githubusercontent.com/u/183294521?v=4",
1798+
"profile": "https://github.com/Bhas-kar",
1799+
"contributions": [
1800+
"code",
1801+
"doc"
1802+
]
17931803
}
17941804
],
17951805
"commitConvention": "none"

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ check out our [Contributing Guide](/.github/CONTRIBUTING.md) and our
328328
<td align="center"><a href="https://github.com/heloiselui"><img src="https://avatars.githubusercontent.com/u/71858203?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Heloise Lui</b></sub></a><br /><a href="https://github.com/carbon-design-system/carbon/commits?author=heloiselui" title="Code">💻</a> <a href="https://github.com/carbon-design-system/carbon/commits?author=heloiselui" title="Documentation">📖</a> <a href="#a11y-heloiselui" title="Accessibility">️️️️♿️</a> <a href="https://github.com/carbon-design-system/carbon/pulls?q=is%3Apr+reviewed-by%3Aheloiselui" title="Reviewed Pull Requests">👀</a></td>
329329
<td align="center"><a href="https://github.com/Tweakified"><img src="https://avatars.githubusercontent.com/u/58192912?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Daniel Smith</b></sub></a><br /><a href="https://github.com/carbon-design-system/carbon/commits?author=Tweakified" title="Code">💻</a> <a href="#a11y-Tweakified" title="Accessibility">️️️️♿️</a> <a href="https://github.com/carbon-design-system/carbon/commits?author=Tweakified" title="Documentation">📖</a></td>
330330
<td align="center"><a href="https://github.com/enricobguedes"><img src="https://avatars.githubusercontent.com/u/45374536?v=4?s=100" width="100px;" alt=""/><br /><sub><b>enricobguedes</b></sub></a><br /><a href="https://github.com/carbon-design-system/carbon/commits?author=enricobguedes" title="Code">💻</a></td>
331+
<td align="center"><a href="https://github.com/Bhas-kar"><img src="https://avatars.githubusercontent.com/u/183294521?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Kanchi Bhaskar</b></sub></a><br /><a href="https://github.com/carbon-design-system/carbon/commits?author=Bhas-kar" title="Code">💻</a> <a href="https://github.com/carbon-design-system/carbon/commits?author=Bhas-kar" title="Documentation">📖</a></td>
331332
</tr>
332333
</table>
333334

e2e/components/Select/Select-test.avt.e2e.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,6 @@ test.describe('@avt Select', () => {
6161
await expect(select).toHaveValue('');
6262
// Select Option 4
6363
await select.selectOption('Option 4');
64-
await expect(select).toHaveValue('Option 4');
64+
await expect(select).toHaveValue('option-4');
6565
});
6666
});

packages/react/src/components/Select/Select.stories.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,10 @@ export const Inline = (args) => {
8787
helperText="Optional helper text"
8888
{...args}>
8989
<SelectItem value="" text="" />
90-
<SelectItem value="Option 1" text="Option 1" />
91-
<SelectItem value="Option 2" text="Option 2" />
92-
<SelectItem value="Option 3" text="Option 3" />
93-
<SelectItem value="Option 4" text="Option 4" />
90+
<SelectItem value="option-1" text="Option 1" />
91+
<SelectItem value="option-2" text="Option 2" />
92+
<SelectItem value="option-3" text="Option 3" />
93+
<SelectItem value="option-4" text="Option 4" />
9494
</Select>
9595
</div>
9696
);
@@ -117,7 +117,7 @@ export const _WithLayer = (args) => (
117117
value="An example option that is really long to show what should be done to handle long text"
118118
text="An example option that is really long to show what should be done to handle long text"
119119
/>
120-
<SelectItem value="Option 2" text="Option 2" />
120+
<SelectItem value="option-2" text="Option 2" />
121121
</Select>
122122
)}
123123
</WithLayer>
@@ -174,9 +174,9 @@ export const withAILabel = (args) => {
174174
value="An example option that is really long to show what should be done to handle long text"
175175
text="An example option that is really long to show what should be done to handle long text"
176176
/>
177-
<SelectItem value="Option 2" text="Option 2" />
178-
<SelectItem value="Option 3" text="Option 3" />
179-
<SelectItem value="Option 4" text="Option 4" />
177+
<SelectItem value="option-2" text="Option 2" />
178+
<SelectItem value="option-3" text="Option 3" />
179+
<SelectItem value="option-4" text="Option 4" />
180180
</Select>
181181
</div>
182182
);
@@ -201,9 +201,9 @@ export const Default = (args) => {
201201
value="An example option that is really long to show what should be done to handle long text"
202202
text="An example option that is really long to show what should be done to handle long text"
203203
/>
204-
<SelectItem value="Option 2" text="Option 2" />
205-
<SelectItem value="Option 3" text="Option 3" />
206-
<SelectItem value="Option 4" text="Option 4" />
204+
<SelectItem value="option-2" text="Option 2" />
205+
<SelectItem value="option-3" text="Option 3" />
206+
<SelectItem value="option-4" text="Option 4" />
207207
</Select>
208208
</div>
209209
);

packages/react/src/components/Select/Select.tsx

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,28 @@ const Select = React.forwardRef(function Select(
169169
const prefix = usePrefix();
170170
const { isFluid } = useContext(FormContext);
171171
const [isFocused, setIsFocused] = useState(false);
172-
const [title, setTitle] = useState('');
173172
const selectInstanceId = useId();
174173

174+
interface SelectItemProps {
175+
value: any;
176+
text: string;
177+
}
178+
// Convert children to an array of valid elements once using type narrowing
179+
const validChildren = React.Children.toArray(children).filter(
180+
(child): child is React.ReactElement<SelectItemProps> =>
181+
React.isValidElement<SelectItemProps>(child)
182+
);
183+
184+
// Find the default option based on the specified defaultValue
185+
const defaultOption = validChildren.find(
186+
(child) => child.props?.value === other?.defaultValue
187+
);
188+
189+
// Use the default option's text if available; otherwise, fallback to the first option's text
190+
const initialTitle =
191+
defaultOption?.props?.text || validChildren[0]?.props?.text || '';
192+
193+
const [title, setTitle] = useState(initialTitle);
175194
const selectClasses = classNames({
176195
[`${prefix}--select`]: true,
177196
[`${prefix}--select--inline`]: inline,
@@ -233,7 +252,8 @@ const Select = React.forwardRef(function Select(
233252
};
234253

235254
const handleChange = (evt) => {
236-
setTitle(evt?.target?.value);
255+
const selectedOption = evt?.target?.options[evt.target.selectedIndex];
256+
setTitle(selectedOption?.text);
237257
};
238258

239259
const readOnlyEventHandlers = {

packages/react/src/components/Select/__tests__/Select-test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,16 @@ describe('Select', () => {
7575
expect(screen.getByText('Option 2')).toHaveAttribute('selected');
7676
});
7777

78+
it('should show SelectItem text as title', () => {
79+
render(
80+
<Select id="select" labelText="Select">
81+
<SelectItem text="Option 1" value="option-1" />
82+
<SelectItem text="Option 2" value="option-2" />
83+
</Select>
84+
);
85+
expect(screen.getByLabelText('Select').title).toEqual('Option 1');
86+
});
87+
7888
it('should respect disabled prop', () => {
7989
render(<Select id="select" labelText="Select" disabled />);
8090

0 commit comments

Comments
 (0)