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

@react-md/form

v5.1.6

Published

This package is for creating all the different form input types.

Downloads

8,203

Readme

@react-md/form

Create material design form elements with a lot of customization. This package exports the following form components:

  • Form - A simple wrapper for a <form> element that just prevents default submit behavior for convenience
  • Fieldset - A simple wrapper for the <fieldset> element that removes some of the default styles and integrates a <legend> that be be conditionally rendered for screen readers only
  • Label - A <label> element that is also built-in to the majority of the other form controls
  • FileInput - A wrapper for <input type="file" /> that gains the <Button /> styles
  • NativeSelect - A wrapper for the native <select> element that updates the select to have the same styles as a TextField. You are unable to style the <option>s due to styling restrictions
  • Select - A component that allows you to create an accessible listbox that behaves like a native <select> element but also allows for additional styling from the @react-md/list package
  • TextField - A styled <input type="text" /> that supports a few themes as well as other input types. Note:
  • Password - A wrapper for the TextField to render as a type="password" that has built-in functionality to temporarily show the password to the user with an inline visibility toggle button
  • TextArea - A styled <textarea> that has a few themes and can animate the height as the user types
  • Checkbox - A wrapper for an <input type="checkbox" />
  • Radio - A wrapper for an <input type="radio" />
  • Switch - A wrapper for an <input type="checkbox" /> that looks like a toggleable switch
  • AsyncSwitch - A wrapper for the Switch component that has built-in support for displaying a circular progress in the Switch during asynchronous actions
  • FormMessage - A component that can be used to display accessible help and error messages along with other form components that will be read out to screen readers.

This package also exports the following helper components and hooks:

  • useChecked - A simple hook that controls the checked state for the Checkbox or Switch components
  • useIndeterminateChecked - A hook that can be used for checkbox groups with an indeterminate state
  • useChoice - A simple hook that can be used to control the state of a radio group or select components while type-casting the value for Typescript users. Note: This does not validate the value string
  • useSelectState - A hook for Typescript users that type-casts the value. Note: This does not validate the value string
  • FloatingLabel - A <label> element that can be used to animate a label out of an <input type="text" /> or <textarea> if additional customization is required
  • Listbox - A component that implements the listbox widget specifications with keyboard search and movement built-in.
  • Option - A wrapper for the SimpleListItem from @react-md/list that allows for additional styling and accessibility requirements for an "option" role
  • TextFieldContainer - A styled <div> that is used for render the different themes
  • TextFieldAddon - A component that might not be used much externally, but it can be used to gain the styles for the addons for a TextArea and TextField (built-in)
  • InputToggle - A component that is used to render either a "checkbox" or "radio" element
  • ToggleContainer - A helper component that is used to wrap either a "checkbox" or "radio" for additional styles

Installation

npm install --save @react-md/form

It is also recommended to install the other packages if you have not done so:

npm install --save @react-md/theme \
  @react-md/typography \

Documentation

You should check out the full documentation for live examples and more customization information, but an example usage is shown below.

Usage

It is recommended to check out the documentation site for a better example, but here's a simple one that you should really not copy:

import { useState } from "react";
import { render } from "react-dom";
import { Button } from "@react-md/button";
import { Form, TextField, Password, useChecked } from "@react-md/form";
import { EmailSVGIcon, PasswordSVGIcon } from "@react-md/material-icons";

const App = () => {
  const [email, setEmail] = useState("");
  const [password, setPassword] = useState("");
  const [rememberMe, setRememberMe] = useChecked(false);
  const [errors, setErrors] = useState<readonly string[]>([]);

  const handleSubmit = async () => {
    const response = await fetch("/login", {
      method: "POST",
      headers: {
        Accept: "application/json",
        "Content-Type": "application/json",
      },
      body: JSON.stringify({ email, password }),
    });

    const json = await response.json();
    if (json.errors) {
      setErrors(errors);
    } else {
      // do something
    }
  };

  return (
    <Form onSubmit={handleSubmit}>
      <FormMessage id="errors" role="alert" error disableWrap>
        {errors.length && (
          <ul>
            {errors.map((error) => (
              <li key={error}>{error}</li>
            ))}
          </ul>
        )}
      </FormMessage>
      <TextField
        aria-describedby="errors"
        id="email"
        label="Email"
        type="email"
        name="email"
        value={email}
        onChange={(event) => setEmail(event.currentTarget.value)}
        required
        leftAddon={<EmailSVGIcon />}
      />
      <Password
        aria-describedby="errors"
        id="password"
        label="Password"
        name="password"
        value={password}
        onChange={(event) => setPassword(event.currentTarget.value)}
        required
        leftAddon={<PasswordSVGIcon />}
      />
      <Checkbox
        id="remember-me"
        name="rememberMe"
        label="Remember me?"
        checked={rememberMe}
        onChange={setRememberMe}
      />
      <Button id="submit" type="submit">
        Log in
      </Button>
    </Form>
  );
};

render(<App />, document.getElementById("root"));