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

When setting a focus by default it can never be changed #515

Open
manast opened this issue Apr 18, 2022 · 3 comments
Open

When setting a focus by default it can never be changed #515

manast opened this issue Apr 18, 2022 · 3 comments

Comments

@manast
Copy link

manast commented Apr 18, 2022

Consider this code based on the example code https://github.com/vadimdemedes/ink/tree/master/examples/use-focus-with-id:

import { render, Box, Text, useFocus, useInput, useFocusManager } from "ink";
import { useEffect } from "react";
export const App = () => {
  const { focus } = useFocusManager();

  useInput((input) => {
    if (input === "1") {
      focus("1");
    }

    if (input === "2") {
      focus("2");
    }

    if (input === "3") {
      focus("3");
    }
  });

  // Sets default focus on element with id === "1"
  focus("1");

  return (
    <Box flexDirection="column" padding={1}>
      <Box marginBottom={1}>
        <Text>
          Press Tab to focus next element, Shift+Tab to focus previous element,
          Esc to reset focus.
        </Text>
      </Box>
      <Item id="1" label="Press 1 to focus" />
      <Item id="2" label="Press 2 to focus" />
      <Item id="3" label="Press 3 to focus" />
    </Box>
  );
};

const Item = ({ label, id }: { label: string; id: string }) => {
  const { isFocused } = useFocus({ id });

  return (
    <Text>
      {label} {isFocused && <Text color="green">(focused)</Text>}
    </Text>
  );
};

When running the app it will show the following:

 Press Tab to focus next element, Shift+Tab to focus previous element, Esc to reset focus.

 Press 1 to focus (focused)
 Press 2 to focus
 Press 3 to focus

However, it is not possible to change focus now, neither with "tab" nor with the number keys 1,2,3, the focus is actually changed but jumps back directly to the initial component with id === "1".

I have tried to put the call to focus inside the useEffect hook with the exact same results:

useEffect( () => {
  focus("1")
})

Any ideas, or is this just a bug?

@manast
Copy link
Author

manast commented Apr 18, 2022

After some more experiments, it looks as if the component is re-rendered when pressing "tab", so that's why it goes back to the initial focus status, it kind of makes sense, but how to work around this issue?

@manast
Copy link
Author

manast commented Apr 18, 2022

Ok, the solution is simply to use the "autoFocus" property. I paste here the code as a reference for others:

const Item = ({ label, id }: { label: string; id: string }) => {
  const { isFocused } = useFocus({ id, autoFocus: id === "1" });

  return (
    <Text>
      {label} {isFocused && <Text color="green">(focused)</Text>}
    </Text>
  );
};

@manast manast closed this as completed Apr 18, 2022
@vadimdemedes
Copy link
Owner

Going to re-open this one to investigate later.

@vadimdemedes vadimdemedes reopened this Jul 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants