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

Can't seem to autofocus the searchbar #95

Open
MartinJohannesNilsen opened this issue Mar 27, 2021 · 5 comments
Open

Can't seem to autofocus the searchbar #95

MartinJohannesNilsen opened this issue Mar 27, 2021 · 5 comments

Comments

@MartinJohannesNilsen
Copy link

I am trying to automatically focus this component on render.

Have tried passing autoFocus as a prop, as the ReadMe states that "Any other properties supplied will be spread to the underlying Input component." Am I doing something wrong here?

@MartinJohannesNilsen
Copy link
Author

MartinJohannesNilsen commented Mar 28, 2021

#15

I have tried following these suggestions, but can't seem to get it working. I am using Typescript and Hooks

@MartinJohannesNilsen
Copy link
Author

Have tried using the following approach:

searchbar in view:

[...]
const [inputRef, setInputFocus] = useFocus();
useEffect(() => {
    setTimeout(() => {
	setInputFocus();
    }, 1000);
}, []);
[...]
<SearchBar
	{...inputRef}
	className={classes.searchBar}
	value={searchString}
	onChange={(newString: string) => setSearchString(newString)}
	onRequestSearch={handleSearch}
	searchIcon={<SearchIcon color="primary" />}
	closeIcon={<CloseIcon color="primary" />}
/>

useFocus.ts

import { MutableRefObject, useRef } from "react";
const useFocus = (): [any, () => void] => {
  const htmlElRef: MutableRefObject<any> = useRef(null);
  const setFocus = (): void => {
    htmlElRef?.current?.focus?.();
  };
  return [htmlElRef, setFocus];
};
export default useFocus;

source

In which I thought was going to work, but it did not. I even increased the timer to a second instead of 100ms.

@MartinJohannesNilsen
Copy link
Author

MartinJohannesNilsen commented Mar 29, 2021

I see that in this example, the autoFocus-prop is properly working. I've tested it, and it seems to work in jsx, but not in tsx.

Type '{ autoFocus: true; className: string; value: string; onChange: (newString: string) => void; onRequestSearch: () => void; placeholder: string; searchIcon: Element; closeIcon: Element; }' is not assignable to type 'IntrinsicAttributes & SearchBarProps & { children?: ReactNode; }'.
  Property 'autoFocus' does not exist on type 'IntrinsicAttributes & SearchBarProps & { children?: ReactNode; }'.

@MartinJohannesNilsen
Copy link
Author

Fixed it locally by changing index.d.ts for now. You may have a better solution for this, but I just added autoFocus?: boolean to the interface SearchBarProps.

declare module 'material-ui-search-bar' {
  export interface SearchBarProps {
      [...]
      autoFocus?: boolean;
  }
  const SearchBar: React.ComponentType<SearchBarProps>;
  export default SearchBar;
}

@MartinJohannesNilsen
Copy link
Author

@leMaik Could you take a look at the PR? This issue can be closed

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

1 participant