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: only focus active elements #631

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion readme.md
Expand Up @@ -1858,7 +1858,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
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
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
Expand Up @@ -4,7 +4,8 @@
"outDir": "build",
"sourceMap": true,
"jsx": "react",
"isolatedModules": true
"isolatedModules": true,
"lib": ["ES2023"]
},
"include": ["src"],
"ts-node": {
Expand Down