@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 convenienceFieldset
- 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 onlyLabel
- A <label>
element that is also built-in to the majority of the
other form controlsFileInput
- A wrapper for <input type="file" />
that gains the
<Button />
stylesNativeSelect
- 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 restrictionsSelect
- 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
packageTextField
- 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 buttonTextArea
- A styled <texxtarea>
that has a few themes and can animate the
height as the user typesCheckbox
- 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 switchAsyncSwitch
- A wrapper for the Switch
component that has built-in support
for displaying a circular progress in the Switch
during asynchronous actionsFormMessage
- 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
componentsuseIndeterminateChecked
- A hook that can be used for checkbox groups with
an indeterminate stateuseChoice
- 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 stringuseSelectState
- A hook for Typescript users that type-casts the value.
Note: This does not validate the value stringFloatingLabel
- A <label>
element that can be used to animate a label out
of an <input type="text" />
or <textarea>
if additional customization is
requiredListbox
- 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"
roleTextFieldContainer
- A styled <div>
that is used for render the different
themesTextFieldAddon
- 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"
elementToggleCotainer
- 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 {
}
};
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"));
4.0.0 (2021-11-24)
This release focused on updating the @react-md/transition
package to no longer log errors in React.StrictMode
because react-ransition-group
was using ReactDOM.findDOMNode
to handle transitions.
All react-md
packages will no longer use react-transition-group
since all that functionality has been built into @react-md/transition
with a slightly different API.
This release has also included my first attempt at automating upgrading to new major releases by introducing a new @react-md/codemod package that is similar to the react-codemod package. You can automate some of this release by running:
npx @react-md/codemod v3-to-v4/preset
Since I am still learning how to use jscodeshift, it will not be able to migrate everything but should still help with most changes.
Bug Fixes
- @react-md/menu:
DropdownMenu
and Menu
portal by default (98a6a9f), closes #1264 - @react-md/tooltip: cancel timer when element is clicked (5416554)
- sass: Do not use legacy global functions (6159e16)
Features
- Update to use new JSX Transform and latest
eslint
(8111cd3) - @react-md/portal:
ConditionalPortal
supports ReactNode children (c83d578) - @react-md/transition: No longer use findDOMNode for transitions (cb952da)
- @react-md/typography: Renamed Text to
Typography
(30cf056) - @react-md/utils: Export additional positioning types (b50a04c)
- codemod: Created a new @react-md/codemod package to help with new releases (41c1fa6)
Documentation
- Hackily fix codesandbox.io not using
sass
resolutions in package.json (db22cde), closes #1261 - @react-md/form: Updated hook overrides so documentation appears correctly (436fbff)
- react-md.dev: Enable rust compiler by removing custom babelrc (796efd0)
- react-md.dev: Fallback code language to markup instead of none (0efaf9b)
- react-md.dev: Fix alert sandboxes (8f19297)
- react-md.dev: Removed SwipeableTabs demo since it didn't really work (2d79f93)
- react-md.dev: Separate Code and CodeBlock into separate folders (4c492b3)
- react-md.dev: Try to allow custom Code/CodeBlock in sandboxes (5d494bf)
- react-md.dev: update code components to use css modules (9bdf6ba)
- react-md.dev: Use
react-marked-renderer
for markdown stuffs (93ebaa4)
Other Internal Changes
- always skip lib check (229cef1)
- Re-ran
prettier
(9632d82) - update workflows to include node 16 (f756b92)
- updated branches for build, lint, and test (b5eeae9)
- Updated remaining docs and tests for
react-router-dom
v6 (e012ef9) - @react-md/dev-utils: Added error message for combining styles (aa5ecfd)
- @react-md/dev-utils: match quotation marks for
sass
files (98ffe40) - @react-md/dev-utils: Update
sassdoc
to not through uncaught exceptions (8bdf532) - @react-md/dev-utils: Update release for new major versions and legacy docs (86c5c02)
- @react-md/format: ran
prettier
after upgrading to v2.4.0 (06110af) - codemod: Added comment about ignoring CodeQL alert (631d56c)
- examples:
enable
strict mode by default for nextjs-typescript (83e4c44) - examples: added lint command to nextjs examples (788a8b8)
- examples: bump nextjs examples from v11.1.2 to v12.0.2 (be45277)
- examples: fixed prefers-color-scheme in examples (f799d3a)
- examples: Updated
create-react-app
example to use react-router-dom
v6 (3c4d1ea) - examples: Updated create-react-app-typescript example to use
react-router-dom
v6 (ae469ef) - examples: Updated examples to no longer import React (c0b8cb5)
- react-md: Remove prop-types package and usage (2637a6f)
- react-md.dev: Enable React.StrictMode (219937e)
- react-md.dev: Updated some transition documentation (44bfa20)
- stylelint: Updated to use
stylelint
(22d1598) - test: Update coverage for watch mode (74cee51)
- typescript: Stopped using deprecated HTMLTable(Data|Header)CellElement (23ba342)
Breaking Changes
- Minimum React version is now 16.14 instead of 16.8
- @react-md/menu: The
DropdownMenu
and Menu
components portal by
default. This should really only affect snapshot tests - @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.