Skip to content

Commit 25849ee

Browse files
authored
fix: Select autofocus (#141)
1 parent b7d85c1 commit 25849ee

12 files changed

+15
-8
lines changed

packages/fuselage-ui-kit/src/StaticSelect.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22
import {
3-
Select,
4-
MultiSelect,
3+
SelectFiltered,
4+
MultiSelectFiltered,
55
} from '@rocket.chat/fuselage';
66

77
import { useBlockContext } from './hooks';
@@ -15,7 +15,7 @@ export const StaticSelect = ({
1515
...element
1616
}) => {
1717
const [{ loading, value }, action] = useBlockContext(element, context);
18-
return <Select
18+
return <SelectFiltered
1919
value={value}
2020
mod-loading={loading}
2121
options={options.map((option) => [option.value, parser.text(option.text)])}
@@ -32,7 +32,7 @@ export const MultiStaticSelect = ({
3232
...element
3333
}) => {
3434
const [{ loading, value }, action] = useBlockContext(element, context);
35-
return <MultiSelect
35+
return <MultiSelectFiltered
3636
value={value}
3737
mod-loading={loading}
3838
options={options.map((option) => [option.value, parser.text(option.text)])}
Loading
Loading
Loading
Loading
Loading
Loading

packages/fuselage/src/components/Options/index.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ export const useCursor = (initial, options, onChange) => {
9696
const lastIndex = options.length - 1;
9797
const { keyCode, key } = e;
9898
if (AnimatedVisibility.HIDDEN === visibility && keyCode !== ACTIONS.ESC && keyCode !== ACTIONS.TAB) {
99-
return show();
99+
show();
100100
}
101101
switch (keyCode) {
102102
case ACTIONS.HOME:
@@ -138,8 +138,10 @@ export const useCursor = (initial, options, onChange) => {
138138
}
139139
break;
140140
default:
141-
const index = options.findIndex(([, label]) => label[0] === key);
142-
setCursor(index);
141+
if (key.match(/^[\d\w]$/i)) {
142+
const index = options.findIndex(([, label]) => label[0].toLowerCase() === key);
143+
~index && setCursor(index);
144+
}
143145
}
144146
};
145147

packages/fuselage/src/components/Select/MultiSelect.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export const MultiSelect = ({
7272
<Margins all='neg-x8'>
7373
<Chip.Wrapper role='listbox'>
7474
<Anchor disabled={disabled} ref={ref} aria-haspopup='listbox' onClick={show} onBlur={hide} onKeyUp={handleKeyUp} onKeyDown={handleKeyDown} style={{ order: 1 }} mod-undecorated children={option || placeholder}/>
75-
{currentValue.map((value) => <SelectedOptions role='option' key={value} onMouseDown={(e) => prevent(e) & internalChanged([value]) && false} children={getLabel(options.find(([val]) => val === value))}/>)}
75+
{currentValue.map((value) => <SelectedOptions tabIndex={-1} role='option' key={value} onMouseDown={(e) => prevent(e) & internalChanged([value]) && false} children={getLabel(options.find(([val]) => val === value))}/>)}
7676
</Chip.Wrapper>
7777
</Margins>
7878
</Box>

packages/fuselage/src/components/Select/Select.js

+5
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export const Select = ({
3333
const option = options.find((option) => getValue(option) === currentValue);
3434
const index = options.indexOf(option);
3535

36+
const isFirstRun = useRef(true);
3637

3738
const internalChanged = ([value]) => {
3839
setInternalValue(value);
@@ -55,6 +56,10 @@ export const Select = ({
5556
const containerRef = useRef();
5657

5758
useLayoutEffect(() => {
59+
if (isFirstRun.current) {
60+
isFirstRun.current = false;
61+
return;
62+
}
5863
hide();
5964
ref.current.focus();
6065
}, [internalValue]);

0 commit comments

Comments
 (0)