Inputs
Text field
A text field takes a short typed value, such as a description or an amount, under a label that stays visible above it; use it when the answer can't be picked from a list.
In kanso-ui as TextField
Web and native
TextField in kanso-ui 0.59.0, for React.
import { TextField } from '@kanso-labs/kanso-ui'A labelled single-line text input, drawn as the Material Design text field in a filled or an outlined box.
It follows the text fields page: a label that floats out of the empty box, 24dp leading and trailing icons, a prefix and suffix on the value's line, and a character counter opposite the supporting text. The box, label, input and message are the chrome every field in the library shares.
What you provide
label(required): what the field is for. It is also the field's accessible name.- A value:
defaultValueto let the field keep its own, orvaluewithonChangeto control it.onChangereceives the string, not the event. - A width. The field fills its container, so size the container or the field.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
characterCount | boolean | false | Whether to count the characters the field holds at the end of the supporting line, against `maxLength` where there is one, as the text fields page's character counter does. The count is written in the `I18nProvider`'s digits and grouping. |
characterLimitLabel | string | — | How the limit is said to a screen reader, where there is a `maxLength`. Read with the field on focus rather than as the value changes, so it names the limit and not what is left of it. The visible count is hidden from the tree, which is what this replaces. Left out, it is the sentence for it in the I18nProvider's locale — "Up to 20 characters" in English, singular for a limit of one. |
characterLimitReachedLabel | string | — | What a screen reader is told once the length reaches `maxLength`, where `characterCount` draws the count: the count is hidden from the tree, so this is how a reader learns the field has stopped taking characters. It is said once as the limit is reached, and again only after the length has dropped below it. Left out, it is the sentence for it in the I18nProvider's locale — "Character limit reached" in English. |
description | string | — | A hint shown under the field. Replaced by `error` when there is one, so the two never stack. |
error | string | — | The problem with the current value, in words, written as the fix: "Enter a number, like 12.50". Its presence is what puts the field in its error state — the 2px `critical` border, the message after its `error` icon, and `aria-invalid` all follow from it. Outside a `Form` the message line arrives with the message, so the field grows when this does and moves what is under it. A `Form` holds that space from the start; so does a permanent `description`. |
floatingLabel | boolean | — | Whether the label floated in Material Design's text field. Kanso's label sits above the box in every state, so this changes nothing and warns in development. |
inputRef | Ref<HTMLInputElement> | — | A ref to the field's own `<input>`, which is the element a call site moves focus to — after a server's error, say. `ref` is the field as a whole. |
label required | string | — | What the field is for, drawn above the box and always visible. Required rather than optional: a text field with no label is a box a screen reader cannot name, and a placeholder is no substitute — it vanishes at the first keystroke. |
leadingIcon | ReactNode | — | An icon at the start of the box, before the value: Kanso's 20px icon, in `ink-muted`. An icon drawn in `em` takes that size from the slot. |
numeric | boolean | false | Renders the value in the mono face with tabular figures, for amounts and other numbers meant to be compared down a column. The value runs left to right whatever the page's direction, so a minus sign stays before the digits under a right-to-left locale, and a phone shows the decimal keypad. That keypad has no minus key on iOS or Android, so a field taking negative amounts gives an `inputMode` of its own, which replaces it, or is a `NumberField`, which picks a keypad for each platform. |
placeholder | string | — | An example value, shown in `ink-muted` while the field is empty, as Kanso's page draws it — "Ada Lovelace", say. It never stands in for the label, which stays above the box: it vanishes at the first keystroke and translation tools often skip it. Rules and instructions belong in `description`. |
prefix | ReactNode | — | Text before the value on its line, in `ink-muted`. |
suffix | ReactNode | — | Text after the value on its line, in `ink-muted`. |
trailingIcon | ReactNode | — | An icon at the end of the box, after the value: Kanso's 20px trailing icon, in `ink`. An icon drawn in `em` takes that size from the slot. It may be a control rather than a picture, such as an `IconButton` that clears the field. A press on it is the button's own, where a press anywhere else in the box focuses the input. The `xs` button is 32px around its 20px icon, so an inline margin of -6px on each side puts the icon where a bare one sits, and its target still reaches 48px. |
variant | FieldVariant | — | Which of Material Design's two text fields to draw. Kanso has one, a bordered box, which both values draw, so this changes nothing and warns in development. |
Also takes the props of Omit<TextFieldProps$1, 'children' | 'isInvalid' | 'prefix' | 'validationBehavior'>.
Example
import { TextField } from '@kanso-labs/kanso-ui'
<TextField label="Label" defaultValue="Value" description="Supporting line" />
<TextField label="Label" variant="outlined" error="Enter a value." />The design’s classes
k-field, k-field__label, k-field__input, k-field__input--mono, k-field__help, k-field--error, k-field--compact, and k-icon k-icon--sm for the error icon.
TextField in kanso-ui and react-native-kanso-ui, restyled to this language.