Skip to content

Commit 89472e2

Browse files
committed
* Add End and Home button listeners
* Fix list border * The list no more uses buttons as inner component for list items * fix component to use the theme font * fix scroll behavior and remove smooth scroll
1 parent 113417a commit 89472e2

File tree

2 files changed

+54
-36
lines changed

2 files changed

+54
-36
lines changed

src/SelectBox/SelectBox.js

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,44 @@
11
import React, { useRef, useEffect } from 'react';
22
import propTypes from 'prop-types';
3-
import {
4-
StyledOptionsList,
5-
StyledOptionsListItemInnerButton,
6-
StyledOptionsListItem
7-
} from './SelectBox.styles';
3+
import { StyledOptionsList, StyledOptionsListItem } from './SelectBox.styles';
84
import { StyledCutout } from '../Cutout/Cutout';
95

106
const SelectBox = React.forwardRef(function SelectBox(props) {
117
const { options, value, onSelect, width, height } = props;
128
const selectedListItemRef = useRef(null);
139
const listRef = useRef(null);
1410

15-
const handleKeyDown = ({ key }) => {
16-
switch (key) {
11+
const handleKeyDown = event => {
12+
switch (event.key) {
1713
case 'ArrowDown':
18-
if (value < options.length - 1) onSelect(value + 1);
14+
if (value < options.length - 1) {
15+
event.preventDefault();
16+
onSelect(value + 1);
17+
listRef.current.childNodes[value + 1].scrollIntoView({
18+
block: 'end'
19+
});
20+
}
1921
break;
2022
case 'ArrowUp':
21-
if (value > 0) onSelect(value - 1);
23+
if (value > 0) {
24+
event.preventDefault();
25+
onSelect(value - 1);
26+
listRef.current.childNodes[value - 1].scrollIntoView({
27+
block: 'nearest'
28+
});
29+
}
30+
break;
31+
case 'Home':
32+
onSelect(0);
33+
listRef.current.childNodes[0].scrollIntoView({
34+
block: 'start'
35+
});
36+
break;
37+
case 'End':
38+
onSelect(options.length - 1);
39+
listRef.current.childNodes[options.length - 1].scrollIntoView({
40+
block: 'end'
41+
});
2242
break;
2343
default:
2444
break;
@@ -27,29 +47,34 @@ const SelectBox = React.forwardRef(function SelectBox(props) {
2747

2848
useEffect(() => {
2949
selectedListItemRef.current.scrollIntoView({
30-
behavior: 'smooth',
3150
block: 'start'
3251
});
3352
}, [selectedListItemRef]);
3453

54+
const handleClickOnItem = itemValue => {
55+
onSelect(itemValue);
56+
listRef.current.childNodes[itemValue].scrollIntoView({
57+
block: 'nearest'
58+
});
59+
};
60+
3561
return (
3662
<StyledCutout>
3763
<StyledOptionsList
3864
style={{ width, height }}
3965
ref={listRef}
4066
onKeyDown={handleKeyDown}
67+
tabIndex={-1}
4168
>
4269
{options.map(option => (
43-
<StyledOptionsListItem key={option.value.toString()}>
44-
<StyledOptionsListItemInnerButton
45-
onClick={() => onSelect(option.value)}
46-
type='button'
47-
autoFocus={option.value === value}
48-
isSelected={option.value === value}
49-
ref={option.value === value ? selectedListItemRef : null}
50-
>
51-
{option.label}
52-
</StyledOptionsListItemInnerButton>
70+
<StyledOptionsListItem
71+
key={option.value.toString()}
72+
onClick={() => handleClickOnItem(option.value)}
73+
type='button'
74+
isSelected={option.value === value}
75+
ref={option.value === value ? selectedListItemRef : null}
76+
>
77+
{option.label}
5378
</StyledOptionsListItem>
5479
))}
5580
</StyledOptionsList>

src/SelectBox/SelectBox.styles.js

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,26 @@ export const StyledOptionsList = styled.ul`
88
box-sizing: border-box;
99
background-color: #fff;
1010
font-size: 1rem;
11-
border-style: solid;
12-
border-width: 2px;
13-
border-left-color: ${({ theme }) => theme.borderDark};
14-
border-top-color: ${({ theme }) => theme.borderDark};
15-
border-right-color: ${({ theme }) => theme.borderLightest};
16-
border-bottom-color: ${({ theme }) => theme.borderLightest};
1711
line-height: 1.5;
1812
overflow-y: auto;
1913
${({ variant }) => createScrollbars(variant)}
14+
outline: none;
2015
`;
2116

2217
export const StyledOptionsListItem = styled.li`
18+
box-sizing: border-box;
2319
margin: 0;
2420
padding: 0;
2521
height: ${blockSizes.md};
26-
`;
27-
28-
export const StyledOptionsListItemInnerButton = styled.button`
29-
outline: 0;
30-
border: none;
31-
width: 100%;
32-
height: 100%;
33-
text-align: left;
34-
font-size: 1rem;
22+
line-height: calc(${blockSizes.md} - 4px);
3523
background: ${({ theme, isSelected }) =>
3624
isSelected ? theme.hoverBackground : 'none'};
3725
color: ${({ theme, isSelected }) =>
3826
isSelected ? theme.canvasTextInvert : '#000'};
39-
outline: 0;
27+
width: 100%;
28+
padding-left: 8px;
29+
white-space: nowrap;
30+
overflow: hidden;
31+
text-overflow: ellipsis;
32+
user-select: none;
4033
`;

0 commit comments

Comments
 (0)