ahooks#useIsomorphicLayoutEffect TypeScript Examples

The following examples show how to use ahooks#useIsomorphicLayoutEffect. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: index.tsx    From ui with MIT License 4 votes vote down vote up
InputLicenseKeyBoard = forwardRef<
  InputLicenseKeyBoardRef,
  InputLicenseKeyBoardProps
>((p, ref) => {
  const props = mergeProps(defaultProps, p);
  const [value, setValue] = usePropsValue(props);
  const [hasFocus, setHasFocus] = useState(false);
  const nativeInputRef = useRef<HTMLInputElement>(null);
  const [visible, setVisible] = useState<boolean>(false);
  useImperativeHandle(ref, () => ({
    clear: () => {
      setValue('');
    },
    focus: () => {
      nativeInputRef.current?.focus();
    },
    blur: () => {
      nativeInputRef.current?.blur();
    },
  }));

  useIsomorphicLayoutEffect(() => {
    if (!props.enterKeyHint) return;
    nativeInputRef.current?.setAttribute('enterkeyhint', props.enterKeyHint);
    return () => {
      nativeInputRef.current?.removeAttribute('enterkeyhint');
    };
  }, [props.enterKeyHint]);

  const onClose = () => {
    setVisible(false);
  };
  const onInput = (v: string) => {
    setValue(value + v);
  };

  const onDelete = () => {
    setValue(value.slice(0, value.length - 1));
  };

  return withNativeProps(
    props,
    <div
      className={classnames(
        styles.inputLicenseKeyBoard,
        props.disabled && styles.inputLicenseKeyBoardDisabled,
        props.className,
      )}
      onClick={() => !props.disabled && setVisible(true)}
    >
      <input
        ref={nativeInputRef}
        className={`-element`}
        value={value}
        onChange={e => {
          setValue(e.target.value);
        }}
        onFocus={e => {
          setHasFocus(true);
          props.onFocus?.(e);
        }}
        onBlur={e => {
          setHasFocus(false);
          props.onBlur?.(e);
        }}
        id={props.id}
        placeholder={props.placeholder}
        disabled={props.disabled}
        autoComplete={props.autoComplete}
        pattern={props.pattern}
        inputMode={props.inputMode}
        autoCapitalize={props.autoCapitalize}
        autoCorrect={props.autoCorrect}
        onKeyUp={props.onKeyUp}
        onCompositionStart={props.onCompositionStart}
        onCompositionEnd={props.onCompositionEnd}
        readOnly={true}
      />
      {props.clearable && !!value && (
        <div
          className={styles.inputLicenseKeyBoardClear}
          onMouseDown={e => {
            e.preventDefault();
          }}
          onClick={e => {
            e.stopPropagation();
            setValue('');
            props.onClear?.();
          }}
        >
          <Icon name={'kq-clear2'} size={34} color={'#ccc'} />
        </div>
      )}
      <LicenseKeyBoard
        visible={visible}
        value={value}
        title={props.title}
        confirmText={props.confirmText}
        safeArea={props.safeArea}
        onClose={onClose}
        onInput={onInput}
        onDelete={onDelete}
      />
    </div>,
  );
})