Skip to content

Commit

Permalink
Fix: Only focus active elements (#631)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
acdibble and sindresorhus committed May 31, 2024
1 parent 5a82d33 commit d8bcff8
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 7 deletions.
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -1845,7 +1845,7 @@ const Example = () => {

Switch focus to the next focusable component.
If there's no active component right now, focus will be given to the first focusable component.
If active component is the last in the list of focusable components, focus will be switched to the first component.
If active component is the last in the list of focusable components, focus will be switched to the first active component.

**Note:** Ink calls this method when user presses <kbd>Tab</kbd>.

Expand Down
9 changes: 6 additions & 3 deletions src/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,9 @@ export default class App extends PureComponent<Props, State> {

focusNext = (): void => {
this.setState(previousState => {
const firstFocusableId = previousState.focusables[0]?.id;
const firstFocusableId = previousState.focusables.find(
focusable => focusable.isActive
)?.id;
const nextFocusableId = this.findNextFocusable(previousState);

return {
Expand All @@ -261,8 +263,9 @@ export default class App extends PureComponent<Props, State> {

focusPrevious = (): void => {
this.setState(previousState => {
const lastFocusableId = previousState.focusables.at(-1)?.id;

const lastFocusableId = previousState.focusables.findLast(
focusable => focusable.isActive
)?.id;
const previousFocusableId = this.findPreviousFocusable(previousState);

return {
Expand Down
67 changes: 65 additions & 2 deletions test/focus.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ const emitReadable = (stdin: NodeJS.WriteStream, chunk: string) => {

type TestProps = {
readonly showFirst?: boolean;
readonly disableFirst?: boolean;
readonly disableSecond?: boolean;
readonly disableThird?: boolean;
readonly autoFocus?: boolean;
readonly disabled?: boolean;
readonly focusNext?: boolean;
Expand All @@ -38,7 +40,9 @@ type TestProps = {

function Test({
showFirst = true,
disableFirst = false,
disableSecond = false,
disableThird = false,
autoFocus = false,
disabled = false,
focusNext = false,
Expand Down Expand Up @@ -73,9 +77,11 @@ function Test({

return (
<Box flexDirection="column">
{showFirst && <Item label="First" autoFocus={autoFocus} />}
{showFirst && (
<Item label="First" autoFocus={autoFocus} disabled={disableFirst} />
)}
<Item label="Second" autoFocus={autoFocus} disabled={disableSecond} />
<Item label="Third" autoFocus={autoFocus} />
<Item label="Third" autoFocus={autoFocus} disabled={disableThird} />
</Box>
);
}
Expand Down Expand Up @@ -442,3 +448,60 @@ test('doesnt crash when focusing previous on unmounted children', async t => {

t.is((stdout.write as any).lastCall.args[0], '');
});

test('focuses first non-disabled component', async t => {
const stdout = createStdout();
const stdin = createStdin();
render(<Test autoFocus disableFirst disableSecond />, {
stdout,
stdin,
debug: true
});

await delay(100);

t.is(
(stdout.write as any).lastCall.args[0],
['First', 'Second', 'Third ✔'].join('\n')
);
});

test('skips disabled elements when wrapping around', async t => {
const stdout = createStdout();
const stdin = createStdin();
render(<Test autoFocus disableFirst />, {
stdout,
stdin,
debug: true
});

await delay(100);
emitReadable(stdin, '\t');
await delay(100);
emitReadable(stdin, '\t');
await delay(100);

t.is(
(stdout.write as any).lastCall.args[0],
['First', 'Second ✔', 'Third'].join('\n')
);
});

test('skips disabled elements when wrapping around from the front', async t => {
const stdout = createStdout();
const stdin = createStdin();
render(<Test autoFocus disableThird />, {
stdout,
stdin,
debug: true
});

await delay(100);
emitReadable(stdin, '\u001B[Z');
await delay(100);

t.is(
(stdout.write as any).lastCall.args[0],
['First', 'Second ✔', 'Third'].join('\n')
);
});
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
],
"sourceMap": true,
"jsx": "react",
"isolatedModules": true
"isolatedModules": true,
"lib": ["ES2023"]
},
"include": ["src"],
"ts-node": {
Expand Down

0 comments on commit d8bcff8

Please sign in to comment.