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

@tonz/react-draft-wysiwyg-input

v1.1.1

Published

Port react-draft-wysiwyg to uncontrolled component, compatible with react-hook-form

Downloads

208

Readme

react-draft-wysiwyg-input

This library was generated with Nx.

Video Demo: https://s11.gifyu.com/images/SW2R8.gif

Usage

import {RichTextInput} from '@tonz/react-draft-wysiwyg-input'

import '@tonz/react-draft-wysiwyg-input/style.css';

You can import css from @tonz/react-draft-wysiwyg-input/style.css or from react-draft-wysiwyg/dist/react-draft-wysiwyg.css

Use Case

When you using react-draft-wysiwyg in your form, you cannot handle it as uncontrolled input. With react-hook-form, you only using react-draft-wysiwyg with Controller because react-draft-wysiwyg have properties is difference primitive input props {value, onChange, onBlur, name, ref}

About react-draft-wysiwyg-input

This libray port react-draft-wysiwyg to the interface similar with primitive input with {value, onChange, onBlur, name, ref} Therefore, we can using it as uncontrolled input

Example with react-hook-form

import {RichTextInput} from '@tonz/react-draft-wysiwyg-input'

import '@tonz/react-draft-wysiwyg-input/style.css';

<RichTextInput disabled={disabled} {...register('desc')} />

"use client";
import { classValidatorResolver } from '@hookform/resolvers/class-validator';
import { useForm } from "react-hook-form";
import { RichTextInput } from '@tonz/react-draft-wysiwyg-input';
import '@tonz/react-draft-wysiwyg-input/style.css';
....

const resolver = classValidatorResolver(SignupFormModel);

export default function SignupForm() {
  const router = useRouter();
  const {
    register,
    handleSubmit,
    formState: { errors },
  } = useForm<SignupFormModel>({ resolver });

  const { data: successData, isFetching, callApi } = useCallApiLazy({
    fn: async (data: SignupFormModel) => {
      const response = await fetch('/api/user/register', {
        method: 'POST',
        body: JSON.stringify({
          username: data.username,
          password: data.password,
        })
      });
      return response;
    },
    onSuccess: () => {
      toast('Register success', {
        type: 'success', onClose: () => {
          router.push('/api/auth/signin');
        },
        autoClose: 2000,
      });
    },
    onError: (error) => {
      toast(error || 'Register fail', { type: 'error' });
    }
  })
  const submitForm = useCallback(async (data: SignupFormModel) => {
    console.log('>>>>>>> data:', data);
  }, []);

  const disabled = isFetching || !!successData;
  return (
    <div className="flex items-center justify-center w-full">
      <form
        onSubmit={handleSubmit(submitForm)}
        className="px-8 pt-6 pb-8 mb-4 bg-white rounded-md shadow-md w-96"
      >
        <div className="mb-4">
          <Label className="block mb-2 ">Username</Label>
          <Input disabled={disabled} className="w-full" placeholder="Username" {...register('username')} />
          {errors.username && <ErrorHint data-testid="usernameError">{errors.username.message}</ErrorHint>}
        </div>

        ...

        <div className="mb-6">
          <Label className="block mb-2 ">Desc</Label>
          <RichTextInput disabled={disabled} {...register('desc')} />
          {errors.desc && <ErrorHint data-testid="confirmPasswordError">{errors.desc.message}</ErrorHint>}
        </div>

        <div className="flex items-center space-x-2">
          <PrimaryButton disabled={disabled} loading={isFetching} type="submit">Signup</PrimaryButton>
          <SecondaryButton disabled={disabled}>Cancel</SecondaryButton>
        </div>
      </form>
    </div>
  );
}

Some build-in validators with class-validator

import {

RichTextIsNotEmpty,

ContentHtmlMinLength,

ContentHtmlMaxLength,

} from '@tonz/react-draft-wysiwyg-input/validators';

RichTextIsNotEmpty()

Using for check should not empty value

ContentHtmlMinLength()

Using for check minLength of value (process include convert value to html string, then check min length of html string)

ContentHtmlMaxLength()

Using for check maxLength of value (process include convert value to html string, then check max length of html string)

import { Length, IsNotEmpty } from 'class-validator';
import { Match } from '../validator';
import {
  RichTextIsNotEmpty,
  ContentHtmlMinLength,
  ContentHtmlMaxLength,
} from '@tonz/react-draft-wysiwyg-input/validators';
class SignupFormModel {
  @Length(4, 30)
  @IsNotEmpty()
  username: string;

  @Length(6, 30)
  @IsNotEmpty()
  password: string;

  @Length(6, 30)
  @IsNotEmpty()
  @Match('password')
  confirmPassword: RawDraftContentState;

  @RichTextIsNotEmpty()
  @ContentHtmlMinLength(30)
  @ContentHtmlMaxLength(60)
  desc: RawDraftContentState;
}

Some Utilities function

genEmptyContent()

Generate empty data for input with type RawDraftContentState (https://github.com/facebookarchive/draft-js/blob/main/src/model/encoding/RawDraftContentState.js)

import { genEmptyContent } from '@tonz/react-draft-wysiwyg-input/utils'

const initContent = genEmptyContent()

valueToHtml()

Convert value from RawDraftContentState to html string;

import { valueToHtml } from '@tonz/react-draft-wysiwyg-input/utils'

const htmlStr = valueToHtml()

Next step

You can read code in this library, and you can port any complex component in React-Select, Antd, MUI to uncontrolled component like primitive input.