npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@kcuf/react-hook-controllable

v0.1.2

Published

React hook - useControllable.

Downloads

26

Readme

@kcuf/react-hook-controllable

「可受控」组件 HOOK,跟值及回调在组件的 props 中的名称没有关系。

import useControllable, {
  useControllableSoftTrim,
  useControllableUnprotected
} from '@kcuf/react-hook-controllable';
  • useControllable 受控组件 hook,适用任何类型,一般不会得到 undefined
  • useControllableSoftTrim 受控且软 trim 组件 hook,仅针对 string 类型
  • useControllableSoftTrim 受控组件 hook,适用任何类型,得到的值有可能是 undefined

Why

往往我们在实现受控组件的时候,需要考虑的量有:

  • state.xx
  • props.xx
  • props.defaulXx
  • props.onXxChange

Hook useControllable 可以把这些变数进行优雅整合,避免写可受控组件的场景下遇到的不可受控(比如 props 变化未反应到界面的问题)和代码冗余问题。

注意:Hook useControllable 跟需要受控的值在 props 中的名称无关,也可以实现 visible 的受控:

  • state.visible
  • props.visible
  • props.defaulVisible
  • props.onVisibleChange

另外,对于字符串类型的输入,在输入的时候直接 trim 是非常不好的体验,因为那会让用户输入不了任何形式的空白字符,所以这里提供了 useControllableSoftTrim 来解决此类问题。

利用 useControllable 实现一个「可受控」Input 组件

import {
  InputHTMLAttributes,
  ChangeEvent,
  ReactElement,
  useCallback
} from 'react';

import useControllable from '@kcuf/react-hook-controllable';

interface IInputProps extends Omit<InputHTMLAttributes<HTMLInputElement>, 'value' | 'defaultValue' | 'onChange'> {
  value?: string;
  defaultValue?: string;
  onChange?(value: string, e: ChangeEvent<HTMLInputElement>): void;
}

function Input({
  value,
  defaultValue,
  onChange,
  ...props
}: IInputProps): ReactElement {
  const [controllableValue, setControllableValue] = useControllable('FINAL_DEFAULT', value, defaultValue, onChange);
  const handleOnChange = useCallback((e: ChangeEvent<HTMLInputElement>) => {
    setControllableValue(e.target.value, e);
  }, [setControllableValue]);
  
  return <input {...{
    ...props,
    type: 'text',
    value: controllableValue,
    onChange: handleOnChange
  }} />;
}

利用 useControllableSoftTrim 实现软 trim

import {
  InputHTMLAttributes,
  ChangeEvent,
  ReactElement,
  useCallback
} from 'react';

import {
  useControllableSoftTrim
} from '@kcuf/react-hook-controllable';

interface IInputProps extends Omit<InputHTMLAttributes<HTMLInputElement>, 'value' | 'defaultValue' | 'onChange'> {
  trim?: boolean;
  value?: string;
  defaultValue?: string;
  onChange?(value: string, e: ChangeEvent<HTMLInputElement>): void;
}

function Input({
  trim,
  value,
  defaultValue,
  onChange,
  ...props
}: IInputProps): ReactElement {
  const [controllableValue, setControllableValue] = useControllableSoftTrim(trim, value, defaultValue, onChange);
  const handleOnChange = useCallback((e: ChangeEvent<HTMLInputElement>) => {
    setControllableValue(e.target.value, e);
  }, [setControllableValue]);
  
  return <input {...{
    ...props,
    type: 'text',
    value: controllableValue,
    onChange: handleOnChange
  }} />;
}

这样,在敲空格的时候,是不会触发 onChange 的。