TextField
Inmatningsfält när användaren ska fylla i kortare information, tex namn, personnummer eller epostadress. För längre inmatning används TextArea.
<TextField
label='Etikett'
description='Beskrivning'
/>
Installation
- npm
- Yarn
- pnpm
npm install @midas-ds/components
yarn add @midas-ds/components
pnpm add @midas-ds/components
import { TextField } from '@midas-ds/components'
Beskrivning
TextField är en komposition av <Input>
, <Label>
, <FieldError>
samt <Text>
som alla är React Aria komponenter.
Beskrivning av hur de hänger ihop finns på React Arias dokumentation för TextField.
Elementen renderas som vanliga html <label>
och <input>
med fördelen att tillgänglighet avseende korrekt <label>
är inbyggt
samt att validering fungerar både med native HTML eller med realtime/serverside validering. MIDAS TextField erbjuder därmed via
React Arias komponenter:
- Standard HTML-element renderade enligt MIDAS utseende
- Tillgänglighet via semantiskt sammankopplade labels och description
- Inbyggd formulärsvalidering för native HTML samt validering via andra bibliotek
Användning i formulär
React Aria stöder native HTML-formulär via name
prop och eftersom TextField komponenten i grunden rymmer en standard <input>
fungerar
det som vanligt. Komponenten går också bra att använda i andra bibliotek för formulär. Se React Arias dokumentation för
formulär för detaljer om hur den kan integreras i till exempel
React Hook Form eller server-side validation.
Uncontrolled value
Använd defaultValue
för att sätta ett uncontrolled value på ett TextField.
<TextField
label={'Skriv din favoritfrukt'}
defaultValue={'Banan'}
/>
Controlled value
Använd value
och onChange
för att använda controlled value på TextField/Input. Använd prop isInvalid
för att
kontrollera fältets validering via state.
const ControlledValue = () => {
const [text, setText] = React.useState('')
return (
<>
<TextField value={text} onChange={setText} label={'Controlled value'}/>
<Text>Text value: {text}</Text>
</>
)
Inbyggd validering
TextField kan valideras precis som en standard <input>
via HTML constraint validation genom att sätta exempelvis
type="email"
, isRequired
, minLength
etc, eller ett eget pattern
för godtycklig regular expression. Felmeddelanden
renderas automatiskt på valt språk i browsern. Se API för möjliga varianter.
Egen validering
Komponenten kan valideras med en egen funktion validate()
.
<TextField
label='Skriv in frukt'
isRequired
validate={value => (value === 'frukt' ? true : 'Du måste skriva "frukt"')}
/>
Läs mer om validering i React Arias dokumentation.
Varianter
Varianterna är tillägg till React Arias ursprungliga implementation.
Character counter
Använd showCounter
för att visa antalet tecken som skrivits in i fältet.
<TextField
label='Skriv in frukt'
maxLength={10}
showCounter
/>
Lösenord
Använd type={'password'}
för att aktivera en knapp för att visa/dölja inmatat värde.
<TextField
label='Lösenord'
type={'password'}
/>
API
TextField
Name | Type | Default | Description |
---|---|---|---|
className | string | ((values: InputRenderProps & { defaultClassName: string; }) => string) | - | |
type | "search" | (string & {}) | "text" | "tel" | "url" | "email" | "password" | - | The type of input to render. See MDN. |
isDisabled | boolean | - | Whether the input is disabled. |
style | CSSProperties | ((values: TextFieldRenderProps & { defaultStyle: CSSProperties; }) => CSSProperties) | - | The inline style for the element. A function may be provided to compute the style based on component state. |
id | string | - | The element's unique identifier. See MDN. |
slot | string | - | A slot name for the component. Slots allow the component to receive props from a parent component.
An explicit |
defaultValue | string | - | The default value (uncontrolled). |
autoFocus | boolean | - | Whether the element should receive focus on render. |
spellCheck | string | - | An enumerated attribute that defines whether the element may be checked for spelling errors. See MDN. |
autoCorrect | string | - | An attribute that takes as its value a space-separated string that describes what, if any, type of autocomplete functionality the input should provide. See MDN. |
inputMode | "none" | "search" | "text" | "tel" | "url" | "email" | "numeric" | "decimal" | - | Hints at the type of data that might be entered by the user while editing the element or its contents. See MDN. |
excludeFromTabOrder | boolean | - | Whether to exclude the element from the sequential tab order. If true, the element will not be focusable via the keyboard by tabbing. This should be avoided except in rare scenarios where an alternative means of accessing the element or its functionality via the keyboard is available. |
name | string | - | The name of the input element, used when submitting an HTML form. See MDN. |
value | string | - | The current value (controlled). |
errorMessage | string | ((validation: ValidationResult) => string) | - | Custom error messages |
isReadOnly | boolean | - | Whether the input can be selected but not changed by the user. |
isInvalid | boolean | - | Whether the value is invalid. |
label | string | - | Specify label displayed above the TextField |
pattern | string | - | Regex pattern that the value of the input must match to be valid. See MDN. |
autoComplete | string | - | Describes the type of autocomplete functionality the input should provide if any. See MDN. |
enterKeyHint | "search" | "enter" | "done" | "go" | "next" | "previous" | "send" | - | An enumerated attribute that defines what action label or icon to preset for the enter key on virtual keyboards. See [https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/enterkeyhint]. |
maxLength | number | - | The maximum number of characters supported by the input. See MDN. |
minLength | number | - | The minimum number of characters required by the input. See MDN. |
validationBehavior | "native" | "aria" | 'native' | Whether to use native HTML form validation to prevent form submission when the value is missing or invalid, or mark the field as required or invalid via ARIA. |
isRequired | boolean | - | Whether user input is required on the input before form submission. |
validate | (value: string) => true | ValidationError | - | A function that returns an error message if a given value is invalid.
Validation errors are displayed to the user when the form is submitted
if |
description | string | - | Specify description displayed below the label |
errorPosition | "top" | "bottom" | - | |
showCounter | boolean | false | Whether to show the character counter or not |