ideas icon indicating copy to clipboard operation
ideas copied to clipboard

useInputFocus

Open iamsolankiamit opened this issue 5 years ago • 13 comments

Hook which returns whether the input is focused or not. works great when used with useInputValue.

iamsolankiamit avatar Oct 25 '18 20:10 iamsolankiamit

import useInputValue from '@rehooks/input-value';
import useInputFocus from '@rehooks/input-focus';

function MyComponent() {
  let name = useInputValue('Jamie');
  let {isFocused, bind} = useInputFocus()
  // bind = { onFocus: [function], onBlur: [function] }
  // name = { value: 'Jamie', onChange: [Function] }
  return <input {...name} {...bind}/>;
}

iamsolankiamit avatar Oct 25 '18 20:10 iamsolankiamit

Could just be useElementFocus()

jamiebuilds avatar Oct 25 '18 20:10 jamiebuilds

Can I claim this?

pierreneter avatar Oct 27 '18 20:10 pierreneter

What should its name be element-focus or just focus, what do you think?

pierreneter avatar Oct 27 '18 20:10 pierreneter

done at: https://github.com/pierreneter-repositories/element-focus

pierreneter avatar Oct 27 '18 23:10 pierreneter

How about just useFocus?

j-f1 avatar Oct 28 '18 18:10 j-f1

@j-f1 that sounds more natural.

iamsolankiamit avatar Oct 28 '18 19:10 iamsolankiamit

ok, so, i will rename that to focus and useFocus right?

pierreneter avatar Oct 29 '18 03:10 pierreneter

@pierrenetter why not use array destructuring?

const [hasFocus, bind] = useFocus()

j-f1 avatar Oct 29 '18 10:10 j-f1

what do you think @jamiebuilds

pierreneter avatar Oct 30 '18 04:10 pierreneter

ping! noone here :( 😞

pierreneter avatar Nov 02 '18 16:11 pierreneter

/**
 * Hook that returns a boolean `hasFocus` value and a set of even handler props
 * (`onFocus`, `onBlur`) to pass to the target element.
 * @param {any} [defaultValue] The default value. (`false`)
 * @example
 *    // Basic usage
 *    const [hasFocus, focusProps] = useFocus();
 *    React.useEffect(() => {
 *      if (hasFocus) {
 *        setOtherValue("");
 *      }
 *    }, [hasFocus, setOtherValue]);
 *    // Rendering:
 *    return (<>
 *      <input type="text" name="myValue" {...focusProps} />
 *      <input type="text" name="otherValue" value={otherValue} />
 *    </>);
 * @example
 *    // Advanced usage
 *    const [hasFocus, focusProps, setHasFocus] = useFocus();
 *    // ... Same as Basic usage, but you can use setHasFocus to change state...
 */
export function useFocus(defaultValue = false) {
  const [hasFocus, setHasFocus] = React.useState(defaultValue);
  const hasFocusProps = {
    onFocus: React.useCallback(
      /** @param {React.SyntheticEvent<HTMLElement>} e */
      function onFocus(e) {
        setHasFocus(true);
      },
      [],
    ),
    onBlur: React.useCallback(
      /** @param {React.SyntheticEvent<HTMLElement>} e */
      function onBlur(e) {
        setHasFocus(false);
      },
      [],
    ),
  };
  return [hasFocus, hasFocusProps, setHasFocus];
}

waynebloss avatar Oct 07 '19 14:10 waynebloss

Array destructuring should be used IMO, so that you don't have to constantly rename the returned values in the very common situation where you're using 2 or more of the same type of hook.

waynebloss avatar Oct 07 '19 14:10 waynebloss