Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Select autofocus #141

Merged
merged 1 commit into from
Feb 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions packages/fuselage-ui-kit/src/StaticSelect.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import {
Select,
MultiSelect,
SelectFiltered,
MultiSelectFiltered,
} from '@rocket.chat/fuselage';

import { useBlockContext } from './hooks';
Expand All @@ -15,7 +15,7 @@ export const StaticSelect = ({
...element
}) => {
const [{ loading, value }, action] = useBlockContext(element, context);
return <Select
return <SelectFiltered
value={value}
mod-loading={loading}
options={options.map((option) => [option.value, parser.text(option.text)])}
Expand All @@ -32,7 +32,7 @@ export const MultiStaticSelect = ({
...element
}) => {
const [{ loading, value }, action] = useBlockContext(element, context);
return <MultiSelect
return <MultiSelectFiltered
value={value}
mod-loading={loading}
options={options.map((option) => [option.value, parser.text(option.text)])}
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 5 additions & 3 deletions packages/fuselage/src/components/Options/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export const useCursor = (initial, options, onChange) => {
const lastIndex = options.length - 1;
const { keyCode, key } = e;
if (AnimatedVisibility.HIDDEN === visibility && keyCode !== ACTIONS.ESC && keyCode !== ACTIONS.TAB) {
return show();
show();
}
switch (keyCode) {
case ACTIONS.HOME:
Expand Down Expand Up @@ -138,8 +138,10 @@ export const useCursor = (initial, options, onChange) => {
}
break;
default:
const index = options.findIndex(([, label]) => label[0] === key);
setCursor(index);
if (key.match(/^[\d\w]$/i)) {
const index = options.findIndex(([, label]) => label[0].toLowerCase() === key);
~index && setCursor(index);
}
}
};

Expand Down
2 changes: 1 addition & 1 deletion packages/fuselage/src/components/Select/MultiSelect.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export const MultiSelect = ({
<Margins all='neg-x8'>
<Chip.Wrapper role='listbox'>
<Anchor disabled={disabled} ref={ref} aria-haspopup='listbox' onClick={show} onBlur={hide} onKeyUp={handleKeyUp} onKeyDown={handleKeyDown} style={{ order: 1 }} mod-undecorated children={option || placeholder}/>
{currentValue.map((value) => <SelectedOptions role='option' key={value} onMouseDown={(e) => prevent(e) & internalChanged([value]) && false} children={getLabel(options.find(([val]) => val === value))}/>)}
{currentValue.map((value) => <SelectedOptions tabIndex={-1} role='option' key={value} onMouseDown={(e) => prevent(e) & internalChanged([value]) && false} children={getLabel(options.find(([val]) => val === value))}/>)}
</Chip.Wrapper>
</Margins>
</Box>
Expand Down
5 changes: 5 additions & 0 deletions packages/fuselage/src/components/Select/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export const Select = ({
const option = options.find((option) => getValue(option) === currentValue);
const index = options.indexOf(option);

const isFirstRun = useRef(true);

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

useLayoutEffect(() => {
if (isFirstRun.current) {
isFirstRun.current = false;
return;
}
hide();
ref.current.focus();
}, [internalValue]);
Expand Down