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,} from 'react-magma-dom';export function Example() {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();if (!state.firstName) {setErrors(prevErrors => ({ ...prevErrors, firstName: true }));}if (!state.lastName) {setErrors(prevErrors => ({ ...prevErrors, lastName: true }));}if (!state.email) {setErrors(prevErrors => ({ ...prevErrors, email: true }));}};const cancel = () => {setState({firstName: '',lastName: '',email: '',});setErrors({firstName: false,lastName: false,email: false,});};const errorMessage = React.useMemo(() => {let message = '';if (Object.values(errors).some(error => error)) {message += 'Please fix the following errors:\n';}for (const error in errors) {if (errors[error]) {switch (error) {case 'firstName':message += '· First Name is required\n';break;case 'lastName':message += '· Last Name is required\n';break;case 'email':message += '· Email is required\n';break;default:return;}}}return message;}, [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'}/><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'}/><Spacer size="12" /><InputlabelText="Email (required)"value={state.email}onChange={event =>setState(prevState => ({...prevState,email: event.target.value,}))}errorMessage={errors.email && 'Email is required'}/><Spacer size="24" /></></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" /><Spacer size="12" /><Input labelText="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