The Form component is a relatively simple container. It is used to contain interactive controls and submit information to a web server.
Basic Usage
import React from 'react';import {Button,ButtonType,ButtonColor,ButtonGroup,Form,Input,Spacer,Announce,VisuallyHidden,} from 'react-magma-dom';export function Example() {const [announcement, setAnnouncement] = React.useState('');const firstNameRef = React.useRef<any>();const lastNameRef = React.useRef<any>();const emailRef = React.useRef<any>();const [state, setState] = React.useState({firstName: '',lastName: '',email: '',});const [errors, setErrors] = React.useState({firstName: false,lastName: false,email: false,});const resetErrors = () => {setErrors({firstName: false,lastName: false,email: false,});};const onSubmit = event => {event.preventDefault();resetErrors();const errors = {firstName: !state.firstName,lastName: !state.lastName,email: !state.email,};setErrors(errors);if (errors.firstName && firstNameRef.current) {firstNameRef.current.focus();return;}if (errors.lastName && lastNameRef.current) {lastNameRef.current.focus();return;}if (errors.email && emailRef.current) {emailRef.current.focus();}};const cancel = () => {setState({firstName: '',lastName: '',email: '',});setErrors({firstName: false,lastName: false,email: false,});setAnnouncement('');setTimeout(() => {setAnnouncement('Changes discarded. Form inputs have been reset to default.');}, 10);};const errorMessage = React.useMemo(() => {const errorCount = Object.values(errors).filter(Boolean).length;if (!errorCount) {return '';}if (errorCount === 1) {return 'There is 1 error in the form. Please review the field below.';}return `There are ${errorCount} errors in the form. Please review the fields below.`;}, [errors]);return (<FormonSubmit={onSubmit}header="Form Heading"description="Some Form Description"errorMessage={errorMessage}actions={<ButtonGroup><Button color={ButtonColor.secondary} onClick={cancel}>Cancel</Button><Button type={ButtonType.submit}>Submit</Button></ButtonGroup>}><><InputlabelText="First Name (required)"value={state.firstName}onChange={event =>setState(prevState => ({...prevState,firstName: event.target.value,}))}errorMessage={errors.firstName && 'First Name is required'}autoComplete="given-name"ref={firstNameRef}/><Spacer size="12" /><InputlabelText="Last Name (required)"value={state.lastName}onChange={event =>setState(prevState => ({...prevState,lastName: event.target.value,}))}errorMessage={errors.lastName && 'Last Name is required'}autoComplete="family-name"ref={lastNameRef}/><Spacer size="12" /><InputlabelText="Email (required)"value={state.email}onChange={event =>setState(prevState => ({...prevState,email: event.target.value,}))}errorMessage={errors.email && 'Email is required'}autoComplete="email"ref={emailRef}/><Spacer size="24" /><Announce><VisuallyHidden>{announcement}</VisuallyHidden></Announce></></Form>);}
Inverse
import React from 'react';import {Button,ButtonType,ButtonColor,ButtonGroup,Form,Input,Spacer,Card,CardBody,TypographyVisualStyle,TypographyContextVariant,} from 'react-magma-dom';export function Example() {return (<Card isInverse><CardBody><FormonSubmit={() => alert('form submitted')}header="Form Heading"description="Some Form Description"errorMessage="Some Form Error"headingVisualStyle={TypographyVisualStyle.headingLarge}headingContextVariant={TypographyContextVariant.expressive}isInverseactions={<ButtonGroup><Button color={ButtonColor.secondary}>Cancel</Button><Button type={ButtonType.submit}>Submit</Button></ButtonGroup>}><><Input labelText="Name" autoComplete="given-name" /><Spacer size="12" /><Input labelText="Email" autoComplete="email" /><Spacer size="24" /></></Form></CardBody></Card>);}
Form Props
Any other props supplied will be provided to the wrapping form element.
actions
Description
React Node containing the form action buttons; will appear under the form fields
Type
React.ReactNode
Default
-
children
Description
The content of the component
Type
ReactNode | undefined
Default
-
description
Description
General description of the form
Type
string
Default
-
errorMessage
Description
Additional form level validation message
Type
string
Default
-
header
Description
Title of the form
Type
string
Default
-
headingContextVariant
Description
Additional styles to the form heading for typography based on the context of the content
Type
enum, one of:
TypographyContextVariant.default
TypographyContextVariant.expressive
TypographyContextVariant.narrative
Default
TypographyColor.default
headingLevel
Description
Number to indicate which level heading will render for the form header (e.g. h1, h2 etc.)
Type
1 | 2 | 3 | 4 | 5 | 6
Default
3
headingVisualStyle
Description
Applies visual styles to the form heading including font-size, font-weight, line-height and margins
Type
enum, one of:
TypographyVisualStyle.bodyLarge
TypographyVisualStyle.bodyMedium
TypographyVisualStyle.bodySmall
TypographyVisualStyle.bodyXSmall
TypographyVisualStyle.heading2XLarge
TypographyVisualStyle.heading2XSmall
TypographyVisualStyle.headingLarge
TypographyVisualStyle.headingMedium
TypographyVisualStyle.headingSmall
TypographyVisualStyle.headingXLarge
TypographyVisualStyle.headingXSmall
Default
-
isInverse
Description
If true, the component will have inverse styling to better appear on a dark background
Type
boolean
Default
false
On this page