Text input
A text input takes one line of text: a name, an email, a password, an ID number. For several lines, use a text area; for amounts, a number input.
Installation
npm gives you every component from the package. shadcn copies the source into your app instead: a copy pinned to this version that you can change. The shadcn commands need the @kanok-ui registry in your components.json once, see Installation.
Use it
- The element fires native
inputon every keystroke andchangewhen the user commits the value, so the React wrapper hasonInputandonChange. - The React + Tailwind
onChangeis React's own (every keystroke). Its second argument is the value without a mask's separators.
Sizes
size: sm 32px, md 40px (the default), lg 48px.
Passwords
type="password" adds Carbon's show / hide button at the end of the field. Its tooltip is also its name and says what a press does next: "Show password", then "Hide password". Change the words with show-password-label and hide-password-label. Each press fires ui-toggle with detail.visible.
Password shown:
Email, phone and web addresses
type also takes email, tel, url, search and number, as on <input>. For the virtual keyboard alone, set inputmode (and enterkeyhint for its Enter key).
Add autocomplete so the browser can fill the field in. The browser checks an email address; a mask keeps a phone number readable.
Currency, units and masks
prefix-text and suffix-text put text inside the field, before or after the value: a currency, a unit. They aren't part of the value, and screen readers hear them as part of the field's description.
mask formats the value as the user types: 0 is a digit, a a letter, * either, and any other character is a separator the field adds by itself. A mask of digits only brings up the numeric keyboard. Put the format in the helper text too, so people know it before they type.
- The element's
valueand the submitted form get the formatted text (1-2345-67890-12-3). ItsrawValueproperty gives what was typed, without separators (1234567890123). In React + Tailwind,onChangegets that as its second argument. - A value your code sets, raw or formatted, shows formatted.
- None of these has a Carbon counterpart (Carbon puts the unit or the format in the label or helper text); they follow USWDS.
Read the value
Listen to input to react on every keystroke, or to change once the user is done. With a mask, value is what the field shows and rawValue is what was typed.
value:
rawValue:
Character counter
max-count shows a counter above the field. It's a soft limit: people can paste a long text and then trim it. Going over turns the counter red, shows an error at once and blocks the form. Screen readers hear when the limit is near, not on every keystroke.
Change the messages with too-long-text ({max} becomes the limit) and near-limit-text. For a hard limit that stops typing, use maxlength. More in Forms and validation.
Helper text, warnings and errors
One line shows under the field at a time: an error beats a warning, and a warning beats the helper text.
- On the element,
required,pattern,minlengthand the browser's checks work as on<input>, and their errors wait until the user leaves the field. The React + Tailwind field shows what you pass ininvalidandinvalidText; validation is your app's, as in Carbon React. - To put a link in a message, use the slot of the same name (
helper-text,warn-text,invalid-text). In React + Tailwind, pass the markup tohelperText,warnTextorinvalidText. - When errors show, custom validity and server errors: Forms and validation.
Label and helper text with markup
For a label or a message with markup, such as a link or an emphasis, use the label and helper-text slots (warn-text and invalid-text work the same way). The React + Tailwind field takes the markup in label and helperText.
Label position, read-only and disabled
hide-labelkeeps the label for screen readers but doesn't show it. Use it only where something else on screen names the field.inlineputs the label before the field and the message after it, in one row (Carbon's inline text input).fluid, or a fluid form around the field, puts the label inside a 64px box (fluid forms).readonlyshows and submits the value, but the user can't change it.disabledgreys the field out; it isn't submitted.
Fluid fields
A fluid field puts its label inside a 64px box, and an error or a warning shows inside the box below a line; there is no helper text. Set fluid on a field, or put the fields in a <ui-fluid-form> (see fluid forms).
Outlined (Kanok design)
In the Kanok design, fields are filled by default: a soft grey fill, no edge. Put data-field-variant="outlined" on an element around the field (a <form>, or <html> for the whole app) for a 1px edge on no fill; focus is still the soft blue halo. The attribute does nothing in the Carbon design, so the example below sits inside data-design="kanok". See Kanok design for the details.
Check it in a form
In a <form>, the element checks required, type, pattern, minlength and the counter, as a native input does. A submit with a problem is blocked, every field with a problem shows its error, and the first one gets focus. Try sending this form empty.
Sent:
Errors from your own checks
setCustomValidity(message) makes the field invalid with your message, as on <input>. Use it for a check the browser can't do, such as a contract number that is already in use. The error shows once the user leaves the field; setCustomValidity('') clears it. More in Forms and validation.
Skeleton
While the data of a form loads, <ui-text-input-skeleton> keeps the field's place: a label bar and a field bar at its size, or a fluid box with fluid. Screen readers skip it, so say "loading" on the form (for example aria-busy="true").
Keyboard
| Key | What it does |
|---|---|
| Enter | Submits the form the field sits in, as a native text input does |
| Enter or Space on the password button | Shows or hides the password |
Accessibility
- The label is a real
<label>for the inner<input>, and the message line is itsaria-describedby.aria-labelon the element does not reach the input; uselabel(withhide-labelif it must not show). - An error sets
aria-invalidon the input. On the element it waits until the user leaves the field, so nobody hears "required" while still typing. - The password button has no
aria-pressed: its name changes with what it does next, as in Carbon. - The prefix and suffix are read as part of the field's description, so "฿" or "kg" isn't lost to screen reader users.
Unstyled React parts with the same behaviour: Headless Text input.
API
Generated from the package. In React, pass a property as a prop of the same name; events are props too (the React column).
<ui-text-input> · React UiTextInput
Attributes
| Attribute | Property | Type | Default | Description |
|---|---|---|---|---|
label |
label |
string |
'' |
Visible label text; the label slot replaces it when markup is needed. |
hide-label |
hideLabel |
boolean |
false |
Keep the label for screen readers but don't show it. |
helper-text |
helperText |
string |
'' |
Helper text under the field, shown while there is no error or warning. |
invalid-text |
invalidText |
string |
'' |
Message shown when invalid. Falls back to the browser's validation message. |
invalid |
invalid |
boolean |
false |
Force the error state (e.g. a server-side error). |
warn |
warn |
boolean |
false |
Non-blocking warning; shown when there is no error. |
warn-text |
warnText |
string |
'' |
Text of the warning shown while warn. |
value |
value |
string |
'' |
|
name |
name |
string |
'' |
Reflected: form-associated elements submit under the name attribute. |
type |
type |
TextInputType |
'text' |
|
size |
size |
TextInputSize |
'md' |
Field height: sm 32px, md 40px, lg 48px (tokens --ui-field-height-*). |
placeholder |
placeholder |
string |
'' |
|
autocomplete |
autocomplete |
string |
'' |
|
inputmode |
inputmode |
string |
'' |
Passed to the text box: the virtual keyboard to show (numeric, decimal, email, tel, url, search…). |
enterkeyhint |
enterkeyhint |
string |
'' |
Passed to the text box: the label of the virtual keyboard's Enter key (search, send, next, done…). |
pattern |
pattern |
string |
'' |
|
prefix-text |
prefixText |
string |
'' |
Text inside the field before the value, e.g. a currency (฿). Part of the field's description for screen readers. |
suffix-text |
suffixText |
string |
'' |
Text inside the field after the value, e.g. a unit (kg). Part of the field's description for screen readers. |
mask |
mask |
string |
'' |
Formats the value as the user types: 0 a digit, a a letter, * either; any other character is a separator added for the user (\ makes a pattern character one). E.g. 0-0000-00000-00-0 (Thai citizen ID), 000-000-0000. value and the form get the formatted text, rawValue what was typed. Put the format in the helper text too (Carbon). |
minlength |
minlength |
number | undefined |
||
maxlength |
maxlength |
number | undefined |
||
max-count |
maxCount |
number | undefined |
Soft limit with a visible counter (unlike maxlength, extra text isn't blocked). | |
too-long-text |
tooLongText |
string |
'' |
Error when over max-count; {max} is replaced with the limit. |
near-limit-text |
nearLimitText |
string |
'' |
Announced once when the remaining characters drop to 10% or less. |
disabled |
disabled |
boolean |
false |
Disables the control; a disabled <fieldset> ancestor does the same. |
readonly |
readonly |
boolean |
false |
Shows and submits the value but blocks changes. |
fluid |
fluid |
boolean |
false |
Carbon FluidTextInput: the label inside a 64px box, an error / warning inside it. <ui-fluid-form> sets it for every field inside. |
required |
required |
boolean |
false |
Blocks form submission while the field is empty. |
show-password-label |
showPasswordLabel |
string |
'' |
The password toggle's name and tooltip while the password is hidden (Carbon showPasswordLabel; default: the translation's "Show password"). |
hide-password-label |
hidePasswordLabel |
string |
'' |
The password toggle's name and tooltip while the password shows (Carbon hidePasswordLabel; default: the translation's "Hide password"). |
inline |
inline |
boolean |
false |
Carbon inline: the label before the field, the helper / error text after it (ignored while fluid). |
Properties
| Property | Type | Description |
|---|---|---|
rawValue |
(read-only) The characters typed, without the mask's separators (value when there is no mask). |
|
willValidate |
(read-only) Whether the field takes part in constraint validation (not while disabled), as natively. | |
labels |
(read-only) The <label>s that point at this field. |
Methods
| Method | Description |
|---|---|
setCustomValidity(message) |
Like <input>.setCustomValidity(): a non-empty message makes the field invalid (and is shown as its error once touched) until cleared with ''. |
Events
| Event | React | Description |
|---|---|---|
change |
onChange |
Re-dispatched from the native input (native change is not composed) |
ui-toggle |
onUiToggle |
detail: { visible } when the password toggle shows or hides the password (Carbon onTogglePasswordVisibility) |
input |
onInput |
Native input event (already composed, so it reaches the host) |
Slots
| Slot | Description |
|---|---|
label |
Label content with markup; the label attribute is the plain-text fallback |
helper-text |
Helper text with markup (links, bold); overrides helper-text |
warn-text |
Warning text shown while warn; overrides warn-text |
invalid-text |
Error text shown while invalid; overrides invalid-text |
CSS parts
| Part | Description |
|---|---|
base |
The field box around the input |
label |
The label |
input |
The native input |
prefix |
The text before the value |
suffix |
The text after the value |
password-toggle |
The show / hide password button |
password-tooltip |
The <ui-tooltip> around it (its name) |
counter |
The character counter |
status-icon |
The error / warning icon |
helper-text |
The helper line |
warning-text |
The warning line |
warn-text |
Same line as warning-text (named after the warn-text attribute / slot) |
error-text |
The error line |
invalid-text |
Same line as error-text (named after the invalid-text attribute / slot) |
CSS custom properties
| Custom property | Default | Description |
|---|---|---|
--ui-field-border |
Border on every side of the box (default none: Carbon's underline only; Kanok: a 1px outline) | |
--ui-field-disabled-border-color |
Border colour when disabled (default: the underline transparent) | |
--ui-field-readonly-border-color |
Border colour when read-only (default: a hairline underline) | |
--ui-field-radius |
Corner radius of the box (default 0; Kanok: 6px) | |
--ui-field-background |
Fill of the box (default --ui-surface-1; Kanok: transparent) |
|
--ui-label-font |
Font of the label (default --ui-font-caption; Kanok: 14px semibold) |
|
--ui-label-color |
Colour of the label (default --ui-ink-muted; Kanok: --ui-ink) |
|
--ui-field-readonly-background |
The box's fill when read-only (default transparent; Kanok: a faint ink tint, so read-only reads as set and never weaker than disabled) | |
--ui-fluid-background |
/ --ui-fluid-gap - A fluid box's fill and the space under it (default the layer's fill, 0: boxes that touch; Kanok: transparent, 8px) | |
--ui-icon-control-radius |
Corners of its small icon buttons, their hover fill and ring (default 0; Kanok: round) |