Détail du package

@react-md/form

mlaursen5.9kMIT5.1.6

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

react-md, material design, react, form

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"));

changelog

Change Log

All notable changes to this project will be documented in this file. See Conventional Commits for commit guidelines.

5.1.6 (2023-12-11)

Bug Fixes

  • select: port fixed positioning fixes back from v6.0.0 (feb9ec6), closes #1461

5.1.4 (2022-06-16)

Note: Version bump only for package @react-md/form

5.1.3 (2022-05-07)

Bug Fixes

  • @react-md/form: fix typo for transparent in disabled toggle hover color (7346587)

Documentation

  • fix typos throughout codebase (725d1a2)

Other Internal Changes

  • typos: fix additional typos throughout repo (ef20132)

5.1.2 (2022-04-02)

Note: Version bump only for package @react-md/form

5.1.1 (2022-04-01)

Note: Version bump only for package @react-md/form

5.1.0 (2022-03-18)

Other Internal Changes

  • run lint-scripts --fix for consistent-type-imports (42d839d)
  • @react-md/form: bump nanoid from 3.2.0 to 3.3.1 (e625488)

5.0.0 (2022-01-31)

Bug Fixes

  • @react-md/form: TextArea applies custom height style when resize="none" (e77d939)

Features

  • @react-md/form: Update TextFieldContainer to optionally fill all space in flex containers (2c8e68c)

Other Internal Changes

  • feat!(menu): Implemented new Menu API (c27bf55)
  • chore!(icon): Renamed the download icon to upload (2752a98)
  • @react-md/form: Updated FileInput snapshots for new icon (f5e43fe)
  • @react-md/menu: Added tests for the new menu API and fixed a few issues (7202dd0)

Breaking Changes

  • Menu buttons will no longer open by pressing the ArrowUp or ArrowDown keys.
  • The DropdownMenu component no longer accepts a list of items and instead the children should be the MenuItem components.
  • The DropdownMenu component no longer supports the menuRenderer and itemRenderer props. Instead, there is built-in support for conditionally rendering as a Sheet component using the renderAsSheet prop.
  • The DropdownMenu component now requires a parent AppSizeListener because of the conditional Sheet rendering functionality. This might require updating your tests to either use the Configuration component from @react-md/layout (recommended) or adding the AppSizeListener to tests that include DropdownMenus.
  • The DropdownMenuItem component is no longer required for nested dropdown menus and is an "internal" component instead that shouldn't really be used.
  • The MenuItemSeparator now renders as an <li> instead of an <hr> or <div>.
  • The useContextMenu now returns an object instead of an ordered list.
  • Using any of the MenuItem components requires the <MenuKeyboardFocusProvider> to be mounted as a parent component which might affect tests. This will not break anything if you are using the DropdownMenu or Menu components.

4.0.3 (2021-12-31)

Other Internal Changes

  • Update tests to use jest.mocked (4bb25fb)
  • Updated all packages' peerDependenciesMeta (60fcd71), closes #1319

4.0.2 (2021-11-30)

Bug Fixes

  • @react-md/form: Prevent scrolling while dragging Slider on mobile (7eb6740)

4.0.1 (2021-11-27)

Bug Fixes

  • @react-md/form: Added fixes required for Concurrent Rendering (b4994f4)
  • @react-md/utils: Update getPercentage to optionally not throw errors (ff8a1d6)

Other Internal Changes

  • Updated imports to use import type when possible (ba96bb6)

4.0.0 (2021-11-24)

Bug Fixes

  • sass: Do not use legacy global functions (6159e16)

Features

  • Update to use new JSX Transform and latest eslint (8111cd3)
  • @react-md/transition: No longer use findDOMNode for transitions (cb952da)
  • @react-md/typography: Renamed Text to Typography (30cf056)

Documentation

  • @react-md/form: Updated hook overrides so documentation appears correctly (436fbff)

Other Internal Changes

  • always skip lib check (229cef1)
  • react-md: Remove prop-types package and usage (2637a6f)
  • stylelint: Updated to use stylelint (22d1598)

Breaking Changes

  • Minimum React version is now 16.14 instead of 16.8
  • @react-md/typography: The Text component has been renamed to Typography to help with auto-imports conflicting with the Text element that exists in lib.d.ts
  • react-md: There will no longer be run-time prop validation with the prop-types package.

3.1.0 (2021-09-10)

Bug Fixes

  • @react-md/form: TooManyFilesError is only used if all the other validation has passed (6ed3f54)
  • @react-md/form: ensure file names end with a period for useFileUpload extensions (9238140)
  • @react-md/form: prevent infinite rerenders when calling useFileUpload's reset in useEffect (b2875b1)
  • typescript: updated all array types to be readonly (8f71bcb)

Features

  • @react-md/form: add isValidFileName option to useFileUpload (dbd0375)

Other Internal Changes

  • ran yarn format to include new files (48d3d7f)
  • typescript: support typescript@v4.4.2 (5a9dd72)

3.0.1 (2021-08-15)

Bug Fixes

  • Updated peerDependencies to fix yarn berry peer requirements (250efcd), closes #1224

3.0.0 (2021-08-13)

Bug Fixes

  • sass: use math.div instead of division since it's deprecated (d8c3f12)

Other Internal Changes

  • @react-md/dev-utils: updated variables command to work with sass (5376be1)
  • @react-md/form: removed deprecated implementation in useIndeterminateChecked (6b7871f)
  • @react-md/utils: removed InteractionModeListener alias (216c8ef)

Breaking Changes

  • @react-md/utils: Removed InteractionModeListener since it was an alias for UserInteractionModeListener
  • @react-md/form: The second argument for useIndeterminateChecked is now an object of options

2.9.1 (2021-07-27)

Other Internal Changes

  • install: slighly reduce install size by excluding tests in publish (9d01a44)

2.9.0 (2021-07-18)

Bug Fixes

  • @react-md/form: FileInput correctly center the icon when children aren't provided (3a6ab33)

Features

  • @react-md/form: FileInput automatically swaps button type to text if children exist (e5585e1)
  • @react-md/form: FormMessageCounter component added to public API (1508812)
  • @react-md/form: Added a useFileUpload hook to upload files to the browser (efb3f2f), closes #1159

Other Internal Changes

  • @react-md/form: Added tests for useFileUpload (49ce4d9)
  • react-md.dev: Added examples for useFileUpload (8f9002e)

2.8.5 (2021-07-03)

Bug Fixes

  • @react-md/form: MenuItemCheckbox added missing indeterminate state (aa2c552), closes #1186
  • @react-md/form: useIndeterminateChecked correctly uses readonly prefix (7f69a71)
  • @react-md/form: useIndeterminateChecked supports MenuItemCheckbox with new option (9ab67bf)
  • @react-md/form: MenuItemCheckbox, MenuItemRadio, and MenuItemSwitch styles on light themes (fc4dcd9)

Documentation

  • @react-md/form: Updated documentation for useIndeterminateChecked (8646c28)

Other Internal Changes

  • @react-md/form: Added tests for useIndeterminateChecked (cc2a422)

2.8.4 (2021-06-10)

Bug Fixes

  • @react-md/form: Pass checked prop to component (bffae6f)
  • @react-md/form: Fixed controlled behavior for Switch components (8c65df6), closes #1175

Other Internal Changes

  • ran prettier after upgrading to v2.3.0 (3ce236a)

2.8.3 (2021-05-18)

Documentation

  • react-md.dev: updated tsdoc to work with typedoc (cf54c35)

2.8.2 (2021-04-23)

Note: Version bump only for package @react-md/form

2.8.1 (2021-04-23)

Bug Fixes

  • @react-md/form: MenuItemSwitch spacing styles (8ac8299), closes #1126
  • @react-md/form: Checkbox, Radio, and Switch color (9315eff)

2.8.0 (2021-04-22)

Features

  • @react-md/form: Implemented Form Menu Item Components (fed2b9f)
  • @react-md/transition: Updated useFixedPositioning to merge style objects (1ab84d7)
  • typescript: bump typescript version to v4.2.3 (b094b36)

Documentation

  • @react-md/form: Updated form menu components for better documentation and examples in code (d9695b7)

Other Internal Changes

  • @react-md/form: Added new for menu item tests (5cf4f8a)
  • @react-md/form: Created SwitchTrack and InputToggleIcon components (d9278b3)
  • @react-md/form: moved some toggle styles into separate mixins (517f199)
  • @react-md/form: simplified toggle icon styles (adb6b06)
  • @react-md/form: Updated MenuItemRadio usage to be wrapped in a group for a11y (01caa0b)
  • @react-md/form: Updated SliderValue to use non-portalled tooltip for existing test (b41136f)
  • tsconfig: separate tsconfig by package instead of a single root (b278230)

2.7.1 (2021-03-23)

Bug Fixes

  • @react-md/form: Select correctly respects the readOnly prop (d9a0262), closes #1089
  • @react-md/form: Select correctly updates for the dense spec (2930595), closes #1089

Other Internal Changes

  • ts: stopped using FC type (c5daa47)

2.7.0 (2021-02-28)

Bug Fixes

  • @react-md/form: fixed listbox color when dark theme elevation is enabled (b68ac04), closes #1075

Documentation

  • tsdoc: fixed remaining tsdoc syntax warnings (946f4dd)
  • tsdoc: fixed some tsdoc annotations and styling (0449b86)
  • tsdoc: updated \@since annotations (c62027e)

Other Internal Changes

  • updated test coverage to not include conditional component PropTypes (24e5df1)
  • @react-md/utils: added tryToSubmitRelatedForm util to help with additional a11y (0566e14)

2.6.0 (2021-02-13)

Note: Version bump only for package @react-md/form

2.5.5 (2021-01-30)

Note: Version bump only for package @react-md/form

2.5.4 (2021-01-27)

Note: Version bump only for package @react-md/form

2.5.3 (2021-01-12)

Bug Fixes

2.5.2 (2021-01-12)

Bug Fixes

2.5.1 (2020-12-16)

Note: Version bump only for package @react-md/form

2.5.0 (2020-12-15)

Bug Fixes

Features

2.4.3 (2020-11-14)

Note: Version bump only for package @react-md/form

2.4.2 (2020-10-23)

Note: Version bump only for package @react-md/form

2.4.1 (2020-10-17)

Note: Version bump only for package @react-md/form

2.4.0 (2020-10-17)

Bug Fixes

Features

2.3.1 (2020-09-15)

Note: Version bump only for package @react-md/form

2.3.0 (2020-09-10)

Bug Fixes

Features

2.2.2 (2020-09-02)

Note: Version bump only for package @react-md/form

2.2.1 (2020-09-02)

Note: Version bump only for package @react-md/form

2.2.0 (2020-08-11)

Bug Fixes

  • listbox: render 0 as a valid display value (d02b7a9)

Features

2.1.2 (2020-08-01)

Note: Version bump only for package @react-md/form

2.1.1 (2020-07-21)

Note: Version bump only for package @react-md/form

2.1.0 (2020-07-12)

Features

  • Improved Dark Mode using Raising Elevation (547877c), closes #860

2.0.3 (2020-07-07)

Bug Fixes

2.0.2 (2020-06-30)

Bug Fixes

  • LICENSE: Removed the time range from license since it was incorrect (50c9021)
  • Added sideEffects field to package.json (31820b9)
  • sideEffects formatting (78a7b6b)

v2.0.1

No changes.

v2.0.0

Starting with v2 of react-md, all checkbox and radio inputs will now correctly work like native <input type="checkbox" /> and <input type="radio" /> without any additional wrappers.

New Behavior and Features

  • all components now correctly forward the ref to the DOM element
  • implemented the new themes for form controls so that there is now a "underline", "filled", and "outline" state for TextField and Select
  • changed the default theme for to be "outline" instead of "underline"
  • implemented a new theming API for all the form controls utilizing CSS variables and provided mixins.
  • changed the "focus" styles for all form controls to use a blue focus box-shadow by default to match other focused elements
  • implemented better native keyboard functionality for all form controls
  • added a new NativeSelect component to render a <select> with text field styling
  • added a Form component that renders a <form> element that just prevents default form submission for easy "enter submit" functionality
  • added a Fieldset component to group related form elements together with an accessible legend that can be conditionally rendered for screen readers only
  • updated the Checkbox to support an indeterminate state
  • hopefully fixed some styling issues for the Checkbox and Radio components so it is easier to provide custom styles and implementations
  • added a new hook for handling indeterminate states with useIndeterminateChecked (need a better name)
  • added a Label and FloatingLabel component for reusable label styles
  • all form controls no longer nest the <input /> element as a child of the <label>
  • the FileInput no longer renders the <label> as an AccessibleFakeButton since it never should have
  • the FileInput no longer renders with a surrounding <div> element`
  • added a Listbox component to be able to create an accessible custom <select> component
  • added an Option component to be able to implement a custom <option> widget if desired
  • fixed the keyboard functionality for the Select component so that every option can now be "keyboard searched"
  • updated the Listbox/Select options to support a readOnly view
  • fixed some of the accessibility issues with the Select component by adding the required aria-* attributes and rendering the options with role="option"
  • removed some of the built-in functionality for error and warning states to simplify the messaging behavior
  • added a new FormMessage component that should be able to alert screenreaders when new errors or messages are displayed in real time
  • added a new Password component for handling password fields instead of having it built-in to the TextField component
  • added new components for creating a custom TextField with the TextFieldContainer and TextFieldAddon components
  • added an AsyncSwitch component to be able to render a CircularProgress indicator within a Switch while some asynchronous action is waiting
  • added a few hooks that might be helpful for the form control states: useChoice, useSelectState, useChecked (might change going forward)
  • updated the Select component to use fixed positioning instead of relative positioning by default so that the menu always appears within the viewport

Breaking Changes

  • there is no longer a concept of "selection control" and instead has been changed to a "toggle" concept
  • the SelectionControl and SelectionControlGroup components no longer exists and is no longer required
  • the FileUpload component was removed since it didn't seem extremely helpful

New SCSS Variables, Functions, and Mixins

  • $rmd-label-padding: 0.25rem !default - the amount of horizontal padding to use for a floating label
  • $rmd-toggle-border-radius: 50% !default - the border radius for the checkbox and radio components
  • $rmd-toggle-inset: 0.3125rem !default - The distance the background layer for the checkbox and radio components should be inset relative to the container element
  • $rmd-toggle-dense-inset: 0.25rem !default - The distance the background layer for the checkbox and radio components should be inset relative to the container element when the dense spec i enabled
  • $rmd-checkbox-indeterminate-height: 0.15rem !default - the height for the indeterminate checkbox's state line that covers the icon
  • $rmd-checkbox-indeterminate-dense-height: 0.125rem !default - the height for the indeterminate checkbox's state line that covers the icon when the dense spec is enabled
  • $rmd-switch-ball-border-radius: 50% !default - the border radius for the switch's ball
  • $rmd-switch-container-vertical-padding: 0.5rem !default - the vertical padding for the switch's container
  • $rmd-switch-container-horizontal-padding: $rmd-switch-ball-size / 2 !default the horizontal padding for the switch's container
  • $rmd-switch-ball-disabled-color: rmd-theme-get-swatch($rmd-theme-secondary, 200, false, darken($rmd-theme-secondary, 5%), rmd-switch-ball-disabled-color) !default - the color to use for the switch's ball when it is toggled on and disabled
  • $rmd-switch-progress-width: 12 !default - the width for the circular progress bar in the AsyncSwitch
  • $rmd-switch-progress-background-color: $rmd-white-base !default - the background color to use for the switch's ball while the AsyncSwitch is loading
  • $rmd-switch-progress-padding: 0.125rem !default - the amount of padding to apply to the AsyncSwitch's progress bar

Renamed SCSS Variables, Functions, and Mixins

  • renamed $md-text-field-error-color to $rmd-form-error-color
  • renamed $md-switch-track-height to $rmd-switch-track-height and changed the default value from 16px to 1rem
  • renamed $md-switch-track-width to $rmd-switch-track-width and changed the default value from 40px to 2.25rem
  • renamed $md-switch-thumb-size to $rmd-switch-ball-size and changed the default value from 24px to 1.25rem
  • renamed $md-switch-track-radius to $rmd-switch-track-border-radius and changed the default value from 8px to 0.5rem
  • renamed $md-switch-thumb-offset to $rmd-switch-ball-offset and changed the default value from 4px to 0.25rem

Removed SCSS Variables Placeholders, and Mixins

  • removed $md-selection-controls-include-group since it is no longer required
  • removed $md-selection-controls-include-switches since they are always included
  • removed $md-switch-height since it is no longer needed
  • removed $md-switch-light-theme-thumb-off, $md-switch-light-theme-track-off, $md-switch-light-theme-thumb-disabled, $md-switch-light-theme-track-disabled, $md-switch-dark-theme-thumb-off, $md-switch-dark-theme-track-off, $md-switch-dark-theme-thumb-disabled, $md-switch-dark-theme-track-disabled, $md-switch-ball-fallback-color since they are no longer required
  • removed $md-text-field-autococompleted-shadow since it is no longer required