Input
An input is a text field. Input is one line, InputTextArea several, and InputPassword, InputSearch and InputOTP are a password, a search and a one-time code. Each renders a native <input> or <textarea>.
When to use
- Use an input when the user has to type a short text: a name, an email, a query.
- Use
InputTextAreafor longer text,InputPasswordfor a secret,InputSearchfor a query with an action andInputOTPfor a code. - To pick one or more items from a list, a checkbox is the better fit.
Examples
The name is "" no change yet
<script lang="ts">
import { Input } from 'svelte-ant-design';
let name = $state('');
let last = $state('no change yet');
</script>
<!-- bind:value keeps a variable in sync with the field; onchange receives the new text and the native input event. -->
<Input
bind:value={name}
aria-label="Name"
placeholder="Your name"
onchange={(value, event) => (last = `onchange("${value}", ${event.type})`)}
/>
<span>The name is "{name}"</span>
<span role="status">{last}</span>
<script lang="ts">
import { Input } from 'svelte-ant-design';
</script>
<!-- large is 40px high, medium 32px, small 24px. middle and default are aliases of medium. -->
<Input size="large" aria-label="Large" placeholder="large" />
<Input size="medium" aria-label="Medium" placeholder="medium" />
<Input size="small" aria-label="Small" placeholder="small" />
<Input size="middle" aria-label="Middle" placeholder="middle (alias of medium)" />
<Input size="default" aria-label="Default" placeholder="default (alias of medium)" />
<script lang="ts">
import { Input } from 'svelte-ant-design';
</script>
<Input variant="outlined" aria-label="Outlined" placeholder="outlined" />
<Input variant="filled" aria-label="Filled" placeholder="filled" />
<Input variant="borderless" aria-label="Borderless" placeholder="borderless" />
<Input variant="underlined" aria-label="Underlined" placeholder="underlined" />
<script lang="ts">
import { Input } from 'svelte-ant-design';
</script>
<!-- error also sets aria-invalid="true"; warning is a look only. -->
<Input status="error" aria-label="Error" placeholder="Error" value="not an email" />
<Input status="warning" aria-label="Warning" placeholder="Warning" value="maybe a typo" />
<Input status="error" variant="filled" aria-label="Filled error" placeholder="Filled, error" />
<Input status="warning" variant="underlined" aria-label="Underlined warning" placeholder="Underlined, warning" />
<script lang="ts">
import { Input } from 'svelte-ant-design';
</script>
<!-- A disabled field is not focusable and is not submitted; a read-only one is focusable and can be copied. -->
<Input disabled aria-label="Disabled" value="Disabled" />
<Input disabled variant="filled" aria-label="Disabled, filled" value="Disabled, filled" />
<Input readonly aria-label="Read only" value="Read only" />
$ USD kg
<script lang="ts">
import { Input } from 'svelte-ant-design';
</script>
{#snippet at()}
<span aria-hidden="true">@</span>
{/snippet}
{#snippet unit()}
<em>kg</em>
{/snippet}
<!-- prefix and suffix take plain text or a snippet, and sit inside the border. -->
<Input prefix="$" suffix="USD" aria-label="Amount" placeholder="0.00" />
<Input prefix={at} aria-label="User name" placeholder="username" />
<Input suffix={unit} aria-label="Weight" placeholder="Weight" />
https:// .com https:// .org
<script lang="ts">
import { Input } from 'svelte-ant-design';
</script>
{#snippet domain()}
<strong>.com</strong>
{/snippet}
<!-- addonBefore and addonAfter are cells outside the border; they take plain text or a snippet. -->
<Input addonBefore="https://" aria-label="Site" placeholder="example" />
<Input addonAfter={domain} aria-label="Domain" placeholder="example" />
<Input addonBefore="https://" addonAfter=".org" aria-label="Both" placeholder="example" size="small" />
onclear called 0 times
<script lang="ts">
import { Input } from 'svelte-ant-design';
let cleared = $state(0);
</script>
{#snippet bin()}
<span aria-hidden="true">×</span>
{/snippet}
<!-- The clear button shows while there is text. It does not take focus from the field; focus stays in it. -->
<Input allowClear value="Clear me" aria-label="Clearable" onclear={() => cleared++} />
<!-- The object form names the button, takes it out of the Tab order and replaces the icon (text or a snippet). -->
<Input
allowClear={{ label: 'Erase the text', tabIndex: -1, clearIcon: bin }}
value="Custom button"
aria-label="Custom clear button"
/>
<Input allowClear={{ clearIcon: 'x' }} value="Text icon" aria-label="Text icon" />
<span role="status">onclear called {cleared} times</span>
0 0 / 20 0 of 10 used
<script lang="ts">
import { Input } from 'svelte-ant-design';
</script>
<!-- showCount shows the length; with maxlength it shows "n / max" and the browser stops the input at the limit. -->
<Input showCount aria-label="Counted" placeholder="Type something" />
<Input showCount maxlength={20} aria-label="Limited to 20" placeholder="At most 20 characters" />
<Input
showCount={{ formatter: ({ count, maxLength }) => `${count} of ${maxLength} used` }}
maxlength={10}
aria-label="Formatted count"
placeholder="Formatted count"
/>
24 / 10 6 words 0 / 8
<script lang="ts">
import { Input } from 'svelte-ant-design';
// An emoji is one code point, so it is one character here (and a combined emoji is several).
const words = (value: string) => value.trim().split(/\s+/).filter(Boolean).length;
</script>
<!-- max is a soft limit: past it the count turns red and the field is invalid, the text is kept. -->
<Input count={{ max: 10, show: true }} value="more than ten characters" aria-label="Soft limit" />
<!-- strategy replaces the count; show can be a function that formats it. -->
<Input
count={{ strategy: words, show: ({ count }) => `${count} words` }}
value="count the words, not the letters"
aria-label="Words"
/>
<!-- exceedFormatter cuts the text when it passes max (after an IME composition ends, not in the middle of it). -->
<Input
count={{
max: 8,
show: true,
exceedFormatter: (value, { max }) => [...value].slice(0, max).join('')
}}
aria-label="Cut at 8"
placeholder="Cut at 8 characters"
/>
Entered:
<script lang="ts">
import { Input } from 'svelte-ant-design';
let digits = $state('');
let searched = $state('');
</script>
<!-- A function binding can refuse an edit: the field goes back to the last accepted text. Digits only here. -->
<Input
bind:value={() => digits, (next) => (/^\d*$/.test(next) ? (digits = next) : undefined)}
aria-label="Digits only"
placeholder="Digits only"
/>
<!-- onpressenter runs on Enter, but not on the Enter that confirms an IME candidate. -->
<Input
aria-label="Press Enter"
placeholder="Type and press Enter"
onpressenter={(event) => (searched = (event.currentTarget as HTMLInputElement).value)}
/>
<span role="status">Entered: {searched}</span>
0 / 120 nothing yet; Enter pressed 0 times 44 / 20
<script lang="ts">
import { InputTextArea } from 'svelte-ant-design';
let notes = $state('');
let log = $state('nothing yet');
let presses = $state(0);
</script>
<!-- A native textarea with the props of the input: value, allowClear, showCount, maxlength and the callbacks. -->
<InputTextArea
bind:value={notes}
rows={3}
allowClear
showCount
maxlength={120}
aria-label="Notes"
placeholder="Notes"
onchange={(value) => (log = `onchange: ${[...value].length} characters`)}
onpressenter={() => presses++}
onclear={() => (log = 'onclear')}
/>
<span role="status">{log}; Enter pressed {presses} times</span>
<InputTextArea count={{ max: 20, show: true }} value="a note that is longer than twenty characters" aria-label="Soft limit" />
<InputTextArea status="error" variant="filled" aria-label="Filled error" value="Filled, error" />
<InputTextArea size="small" variant="underlined" aria-label="Small underlined" placeholder="Small, underlined" />
<InputTextArea disabled aria-label="Disabled" value="Disabled" />
<InputTextArea readonly aria-label="Read only" value="Read only" />
<script lang="ts">
import { InputTextArea } from 'svelte-ant-design';
let story = $state('Type here and the field grows.\nPress Enter for a new line.');
</script>
<!-- autoSize follows the content, within minRows and maxRows lines; past maxRows the field scrolls. -->
<InputTextArea bind:value={story} autoSize aria-label="Grows with the text" />
<InputTextArea
autoSize={{ minRows: 2, maxRows: 4 }}
aria-label="Two to four rows"
placeholder="Two to four rows"
/>
The password is hidden; the first field toggled 0 times
<script lang="ts">
import { InputPassword } from 'svelte-ant-design';
let shown = $state(false);
let changes = $state(0);
</script>
{#snippet word(visible: boolean)}
<small>{visible ? 'hide' : 'show'}</small>
{/snippet}
<!-- The toggle is a native button named "Show password"; aria-pressed follows the state. -->
<InputPassword aria-label="Password" placeholder="Password" onvisiblechange={() => changes++} />
<!-- bind:visible controls the state from outside. -->
<InputPassword bind:visible={shown} aria-label="Controlled password" value="secret" />
<span role="status">The password is {shown ? 'shown' : 'hidden'}; the first field toggled {changes} times</span>
<!-- visibilityToggle: false hides the button; the object form names it and sets its tab index. -->
<InputPassword visibilityToggle={false} aria-label="No toggle" value="secret" />
<InputPassword
visibilityToggle={{ label: 'Reveal the password', tabIndex: -1 }}
aria-label="Named toggle"
value="secret"
/>
<!-- iconRender replaces the icon and receives the visibility. -->
<InputPassword iconRender={word} aria-label="Text toggle" value="secret" />
nothing searched yet
<script lang="ts">
import { InputSearch } from 'svelte-ant-design';
let log = $state('nothing searched yet');
let busy = $state(false);
function search(value: string, _event: Event, info: { source: 'input' | 'clear' }) {
log = `onsearch("${value}", source: ${info.source})`;
busy = true;
setTimeout(() => (busy = false), 1500);
}
</script>
{#snippet find()}
Find
{/snippet}
{#snippet lens()}
<span aria-hidden="true">?</span>
{/snippet}
<!-- The icon inside the field is a button. A click, Enter and the clear button call onsearch. -->
<InputSearch aria-label="Search" placeholder="Search" allowClear onsearch={search} loading={busy} />
<span role="status">{log}</span>
<!-- enterButton: true is a primary button with the icon; a string or a snippet is its content. -->
<InputSearch aria-label="Search with a button" placeholder="Search" enterButton />
<InputSearch aria-label="Search with text" placeholder="Search" enterButton="Search" size="large" />
<InputSearch aria-label="Search with a snippet" placeholder="Search" enterButton={find} size="small" />
<!-- loading shows a spinner and ignores the search action. -->
<InputSearch aria-label="Loading" value="slow query" enterButton loading />
<InputSearch aria-label="Custom icon" searchIcon={lens} placeholder="Custom icon" />
Six cells, the default The code is "" onchange (all filled): ""; oninput:
Four cells
<script lang="ts">
import { InputOTP } from 'svelte-ant-design';
let code = $state('');
let done = $state('');
let last = $state('');
</script>
<div class="stack">
<div class="field">
<span class="caption">Six cells, the default</span>
<!-- Cells take one character each; focus moves on, Backspace goes back, a pasted code is spread over the cells. -->
<InputOTP
bind:value={code}
aria-label="Verification code"
onchange={(value) => (done = value)}
oninput={(values) => (last = JSON.stringify(values))}
/>
<span>The code is "{code}"</span>
<span role="status">onchange (all filled): "{done}"; oninput: {last}</span>
</div>
<div class="field">
<span class="caption">Four cells</span>
<InputOTP length={4} aria-label="Four digits" />
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 20px;
}
.field {
display: grid;
justify-items: start;
gap: 8px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
mask
mask with a string
formatter
separator as text
- - - - -
separator as a snippet
size, variant, status
size, variant
disabled
<script lang="ts">
import { InputOTP } from 'svelte-ant-design';
</script>
{#snippet dash(index: number)}
<span aria-hidden="true">{index === 1 ? '·' : '-'}</span>
{/snippet}
<div class="stack">
<!-- mask hides the characters (true is a bullet, a string is the character shown); the value is not changed. -->
<div class="field">
<span class="caption">mask</span>
<InputOTP mask aria-label="Masked" length={4} value="1234" />
</div>
<div class="field">
<span class="caption">mask with a string</span>
<InputOTP mask="*" aria-label="Masked with a star" length={4} value="1234" />
</div>
<!-- formatter maps the whole text to the displayed text. -->
<div class="field">
<span class="caption">formatter</span>
<InputOTP formatter={(value) => value.toUpperCase()} aria-label="Uppercase" length={4} value="ab" />
</div>
<!-- separator is text, or a snippet that receives the index of the cell before it. -->
<div class="field">
<span class="caption">separator as text</span>
<InputOTP separator="-" aria-label="Separated" length={6} />
</div>
<div class="field">
<span class="caption">separator as a snippet</span>
<InputOTP separator={dash} aria-label="Separated by a snippet" length={4} />
</div>
<!-- size, variant and status apply to every cell; autocomplete helps the browser fill a code. -->
<div class="field">
<span class="caption">size, variant, status</span>
<InputOTP size="large" variant="filled" status="error" autocomplete="one-time-code" aria-label="Large, filled, error" length={4} />
</div>
<div class="field">
<span class="caption">size, variant</span>
<InputOTP size="small" variant="underlined" aria-label="Small, underlined" length={4} />
</div>
<div class="field">
<span class="caption">disabled</span>
<InputOTP disabled aria-label="Disabled" length={4} value="12" />
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 20px;
}
.field {
display: grid;
justify-items: start;
gap: 8px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
$ 6 USD 15
/ / /
<script lang="ts">
import { Input, InputOTP, InputSearch, InputTextArea } from 'svelte-ant-design';
</script>
<!-- Each component names its parts: classNames adds a class to a part, styles adds inline styles to it. -->
<Input
prefix="$"
suffix="USD"
allowClear
showCount
value="Styled"
aria-label="Styled input"
class="own-root"
style="max-width: 320px"
classNames={{ root: 'own-root-part', prefix: 'own-prefix', suffix: 'own-suffix', clear: 'own-clear', count: 'own-count' }}
styles={{
input: { 'font-weight': 600 },
prefix: { color: '#1677ff' },
suffix: { 'font-style': 'italic' },
count: { 'letter-spacing': '1px' }
}}
/>
<!-- numbers are written as they are (no automatic px); custom properties work too. -->
<InputTextArea
value="Styled textarea"
showCount
aria-label="Styled textarea"
class="own-area"
style="max-width: 360px"
classNames={{ textarea: 'own-textarea' }}
styles={{ textarea: { 'font-family': 'monospace', '--own': 1 }, count: { color: 'crimson' } }}
/>
<!-- InputSearch has one more part, the button. -->
<InputSearch
enterButton="Go"
aria-label="Styled search"
class="own-search"
style="max-width: 360px"
classNames={{ button: 'own-button' }}
styles={{ button: { 'letter-spacing': '1px' } }}
/>
<InputOTP
length={4}
separator="/"
aria-label="Styled code"
class="own-otp"
style="gap: 4px"
classNames={{ input: 'own-cell', separator: 'own-separator' }}
styles={{ input: { 'font-weight': 700 }, separator: { opacity: 0.5 } }}
/>
City The city you live in 0 / 80 A short line about you
<script lang="ts">
import { Input, InputTextArea } from 'svelte-ant-design';
</script>
<!-- A field has no label of its own: name it with an external label, aria-label or aria-labelledby. -->
<label for="input-labels-email">Email</label>
<Input id="input-labels-email" type="email" placeholder="you@example.com" />
<Input aria-label="Search the catalogue" title="Search the catalogue" placeholder="aria-label and title" />
<span id="input-labels-city">City</span>
<Input aria-labelledby="input-labels-city" aria-describedby="input-labels-city-hint" placeholder="aria-labelledby" />
<small id="input-labels-city-hint">The city you live in</small>
<!-- With a count, the count is joined to the field by aria-describedby, after your own ids. -->
<label for="input-labels-bio">Bio</label>
<InputTextArea id="input-labels-bio" showCount maxlength={80} aria-describedby="input-labels-bio-hint" />
<small id="input-labels-bio-hint">A short line about you</small>
<script lang="ts">
import { Button, Input, InputPassword, InputTextArea } from 'svelte-ant-design';
let email = $state('');
let password = $state('');
let about = $state('');
let result = $state('');
function submit(event: SubmitEvent) {
event.preventDefault();
const data = new FormData(event.currentTarget as HTMLFormElement);
result = `Submitted: ${JSON.stringify([...data.entries()])}`;
}
// A native reset does not change the state of the components: reset it here.
function reset() {
email = '';
password = '';
about = '';
result = '';
}
</script>
<!-- name, required and type are native: the form submits the text of each field. -->
<form onsubmit={submit} onreset={reset}>
<Input bind:value={email} type="email" name="email" required aria-label="Email" placeholder="Email" />
<InputPassword bind:value={password} name="password" required autocomplete="new-password" aria-label="New password" />
<InputTextArea bind:value={about} name="about" aria-label="About you" placeholder="About you" />
<Input type="number" name="age" aria-label="Age" placeholder="Age" />
<span>
<Button htmlType="submit" type="primary">Save</Button>
<Button htmlType="reset">Reset</Button>
</span>
</form>
<span role="status">{result}</span>
API
| Property | Description | Type | Default |
|---|---|---|---|
| value | The text. Use bind:value for two-way binding; a plain value is the initial text. A function binding can refuse an edit; null counts as empty. | string | '' |
| type | Native type. Text-like types are supported (text, search, email, tel, url, number). For a password use InputPassword. | string | 'text' |
| disabled | Native disabled: not focusable, not editable, not submitted. | boolean | false |
| readonly | Native readonly: focusable and copyable, not editable. The clear button is not shown. | boolean | false |
| size | Size of the field: 40px, 32px or 24px high. middle and default are compatibility aliases of medium. In a SpaceCompact a control without a size takes the one of the block. | 'large' | 'medium' | 'middle' | 'small' | 'default' | 'medium' |
| variant | Visual variant. The borderless field draws a focus outline. | 'outlined' | 'filled' | 'borderless' | 'underlined' | 'outlined' |
| status | Validation look. error also sets aria-invalid="true"; warning changes only the look. | 'error' | 'warning' | — |
| prefix | Content before the text, inside the border: plain text or a snippet. | string | Snippet | — |
| suffix | Content after the text, inside the border, after the clear button and the count: plain text or a snippet. | string | Snippet | — |
| addonBefore | A cell before the field, outside the border: plain text or a snippet. While an addon is set, the field sits in a group element; adding the first addon or removing the last one creates the input again (it loses focus). | string | Snippet | — |
| addonAfter | A cell after the field, outside the border: plain text or a snippet. | string | Snippet | — |
| allowClear | Shows a native clear button while there is text (not while disabled or read-only). A press keeps focus in the field and calls onchange("") then onclear. The object form sets the icon, the accessible name (default Clear) and the tab index. | boolean | { clearIcon?: string | Snippet; label?: string; tabIndex?: number } | false |
| showCount | Shows the length of the text. With maxlength it shows n / max. The object form formats the text; the result is a string. | boolean | { formatter: (info: CountInfo) => string } | false |
| count | Character counting: a soft limit max (past it the count is red and the field is invalid, the text is kept), a strategy that replaces the count (the default counts code points), show (as showCount, and it wins over it) and an exceedFormatter that cuts the text. | { max?: number; strategy?: (value: string) => number; show?: boolean | ((info: CountInfo) => string); exceedFormatter?: (value: string, config: { max: number }) => string } | — |
| maxlength | Native maxlength: the browser stops the input at the limit. For a limit that only warns use count.max. | number | — |
| onchange | Called once per user edit with the new text and the event of the edit: the native input event, or compositionend when a browser (for example Firefox) ends an IME composition before its last input event. As onChange in React. Not called when the parent changes value, nor while disabled. The native oninput is forwarded as well and runs first. | (value: string, event: Event) => void | — |
| onpressenter | Called on Enter, but not on the Enter that confirms an IME candidate. | (event: KeyboardEvent) => void | — |
| onclear | Called after the clear button emptied the text. | (event: MouseEvent) => void | — |
| classNames | Extra classes for the parts, appended to the component's own. | Partial<Record<'root' | 'prefix' | 'input' | 'suffix' | 'clear' | 'count', string>> | — |
| styles | Inline styles for the parts. Property names are kebab-case or custom properties, numbers are written as they are (no automatic px). They win over the component's styles. | Partial<Record<'root' | 'prefix' | 'input' | 'suffix' | 'clear' | 'count', Record<string, string | number>>> | — |
| class | An additional CSS class for the root. | string | — |
| style | Inline style of the root; styles.root is written after it. | string | — |
| native attributes | Native attributes of the input (id, name, placeholder, required, autocomplete, inputmode, title, aria-*, data-*, onfocus, onblur, onkeydown, oncompositionstart and others) are forwarded to the <input>. The component owns value, disabled, readonly, maxlength and onchange. | HTMLInputAttributes | — |
Differences from Ant Design:
onchange receives (value, event) on every edit (React passes the event); there is no defaultValue, because bind:value covers it; the parts are separate components (Svelte has no Input.TextArea); the formatters of showCount and count.show return a string (React: a node); visible and onvisiblechange of the password are props of their own (React nests them in visibilityToggle); bordered is not taken, use variant="borderless"; Input.Group and compact are not taken.InputTextArea API
| Property | Description | Type | Default |
|---|---|---|---|
| value | As in Input. | string | '' |
| disabled | As in Input. | boolean | false |
| readonly | As in Input. | boolean | false |
| size | As in Input. | 'large' | 'medium' | 'middle' | 'small' | 'default' | 'medium' |
| variant | As in Input. | 'outlined' | 'filled' | 'borderless' | 'underlined' | 'outlined' |
| status | As in Input. | 'error' | 'warning' | — |
| allowClear | As in Input; the button sits at the top corner of the box. | boolean | { clearIcon?: string | Snippet; label?: string; tabIndex?: number } | false |
| showCount | As in Input; the count has its own row at the bottom of the box. | boolean | { formatter: (info: CountInfo) => string } | false |
| count | As in Input. | { max?: number; strategy?: (value: string) => number; show?: boolean | ((info: CountInfo) => string); exceedFormatter?: (value: string, config: { max: number }) => string } | — |
| maxlength | As in Input. | number | — |
| autoSize | The height follows the content, within minRows and maxRows lines of the line height (a minimum of one line); past maxRows the field scrolls. It is measured again when the text, the width or the font changes. The manual resize handle is hidden while it is on. | boolean | { minRows?: number; maxRows?: number } | false |
| onchange | As in Input. | (value: string, event: Event) => void | — |
| onpressenter | As in Input. | (event: KeyboardEvent) => void | — |
| onclear | As in Input. | (event: MouseEvent) => void | — |
| classNames | Extra classes for the parts, appended to the component's own. | Partial<Record<'root' | 'textarea' | 'clear' | 'count', string>> | — |
| styles | Inline styles for the parts; they win over the component's styles. | Partial<Record<'root' | 'textarea' | 'clear' | 'count', Record<string, string | number>>> | — |
| class | As in Input. | string | — |
| style | As in Input. | string | — |
| native attributes | Native attributes of the textarea (rows, cols, placeholder, name, id, required, aria-*, data-*, onfocus and others) are forwarded to the <textarea>. | HTMLTextareaAttributes | — |
InputPassword API
InputPassword takes every prop of Input except type (the component sets it) and adds these.
| Property | Description | Type | Default |
|---|---|---|---|
| visible | Whether the text is shown. Use bind:visible for two-way binding; a plain value is the initial state. | boolean | false |
| visibilityToggle | Shows the toggle button. false hides it, so visible is the only control. The object form sets the accessible name (default Show password; the button has aria-pressed, so the name does not change) and the tab index. | boolean | { tabIndex?: number; label?: string } | true |
| onvisiblechange | Called once per user toggle with the new visibility; not when the parent changes visible. | (visible: boolean) => void | — |
| iconRender | Custom content of the toggle button; it receives the current visibility. | Snippet<[visible: boolean]> | — |
InputSearch API
InputSearch takes every prop of Input and adds these. Its classNames and styles have one more part, button.
| Property | Description | Type | Default |
|---|---|---|---|
| enterButton | A button after the field: true is a primary button with the search icon, a string or a snippet is its content. false leaves only the icon button inside the field. It conflicts with addonAfter: outside production using both throws, in production the button wins. | boolean | string | Snippet | false |
| loading | Shows a spinner in place of the search icon; the search action (click and Enter) is ignored meanwhile. | boolean | false |
| onsearch | Called on a click of the icon or the button, on Enter (not while composing) and on the clear button. source is "clear" for the clear button and "input" otherwise; for the clear button the text is empty. | (value: string, event: Event, info: { source: 'input' | 'clear' }) => void | — |
| searchIcon | A custom search icon: plain text or a snippet. | string | Snippet | — |
| classNames | The parts of Input and the search button. | Partial<Record<'root' | 'prefix' | 'input' | 'suffix' | 'clear' | 'count' | 'button', string>> | — |
| styles | Inline styles for the same parts. | Partial<Record<'root' | 'prefix' | 'input' | 'suffix' | 'clear' | 'count' | 'button', Record<string, string | number>>> | — |
| class / style | The root, which wraps the field and the button. type is search unless you set another. | string | — |
InputOTP API
| Property | Description | Type | Default |
|---|---|---|---|
| value | The characters entered so far, without gaps. Use bind:value for two-way binding; a plain value is the initial text. Cell i shows the i-th character. | string | '' |
| length | Number of cells. A value that is not a positive integer throws outside production and means 6 in production. | number | 6 |
| mask | Hides the characters: true shows a bullet, a string is the single character shown instead. The value is not changed. | boolean | string | false |
| formatter | Maps the whole text to the displayed text; the cell at index i shows the i-th character of the result. The mask wins over it. | (value: string) => string | — |
| separator | Rendered after the cell at the given index (not after the last one): plain text, or a snippet that receives the index. | string | Snippet<[index: number]> | — |
| disabled | Disables every cell. | boolean | false |
| size | Size of the cells; a cell is as wide as the field is high. middle and default are aliases of medium. | 'large' | 'medium' | 'middle' | 'small' | 'default' | 'medium' |
| variant | Visual variant of the cells. | 'outlined' | 'filled' | 'borderless' | 'underlined' | 'outlined' |
| status | Validation look of the cells; error sets aria-invalid on each. | 'error' | 'warning' | — |
| autocomplete | Native autocomplete of every cell; one-time-code enables autofill of a code. | HTMLInputAttributes['autocomplete'] | — |
| onchange | Called when every cell is filled, with the whole text; not for a change of value made by the parent. | (value: string) => void | — |
| oninput | Called on each change, with one entry per cell ("" for an empty one). | (values: string[]) => void | — |
| classNames | Extra classes for the root, the cells (input) and the separator. | Partial<Record<'root' | 'input' | 'separator', string>> | — |
| styles | Inline styles for the same parts. | Partial<Record<'root' | 'input' | 'separator', Record<string, string | number>>> | — |
| class | An additional CSS class for the root. | string | — |
| style | Inline style of the root; styles.root is written after it. | string | — |
| native attributes | Native attributes of the group (id, aria-label, aria-labelledby, aria-describedby, data-*, handlers) are forwarded to the <div>, which keeps role="group". Each cell is named Character i of n (English; a locale mechanism is planned). | HTMLAttributes<HTMLDivElement> | — |
Methods
All five components have these methods, through bind:this.
| Method | Description | Type |
|---|---|---|
| focus | Moves focus to the field (to the first empty cell of InputOTP). cursor puts the caret at the start or the end, or selects all the text; a type with no selection (email, number) ignores it. | (options?: { preventScroll?: boolean; cursor?: 'start' | 'end' | 'all' }) => void |
| blur | Removes focus from the field. | () => void |
Design tokens
| Token | Description | Default |
|---|---|---|
| --ads-input-font-size | Font size of a medium field. | 14px |
| --ads-input-font-size-lg | Font size of a large field. | 16px |
| --ads-input-font-size-sm | Font size of a small field. | 14px |
| --ads-input-padding-block | Vertical padding of a medium field; with the line height and the border it gives the 32px height. | 4px |
| --ads-input-padding-block-lg | Vertical padding of a large field (40px high). | 7px |
| --ads-input-padding-block-sm | Vertical padding of a small field (24px high). | 0px |
| --ads-input-padding-inline | Horizontal padding of a medium field. | 11px |
| --ads-input-padding-inline-lg | Horizontal padding of a large field. | 11px |
| --ads-input-padding-inline-sm | Horizontal padding of a small field. | 7px |
| --ads-input-radius | Corner radius of a medium field. | 6px |
| --ads-input-radius-lg | Corner radius of a large field. | 8px |
| --ads-input-radius-sm | Corner radius of a small field. | 4px |
| --ads-input-border-width | Thickness of the border. | 1px |
| --ads-input-line-height | Line height of a medium and a small field. | 1.5714285714285714 |
| --ads-input-line-height-lg | Line height of a large field. | 1.5 |
| --ads-input-gap | Space between the text and the prefix, the clear button, the count and the suffix. | 4px |
| --ads-input-icon-size | Size of the clear, toggle and search icons. | 14px |
| --ads-input-addon-padding-inline | Horizontal padding of an addon cell. | 11px |
| --ads-input-otp-gap | Space between the cells of InputOTP. | 8px |
| --ads-input-bg | Background of the field. | #ffffff |
| --ads-input-bg-hover | Background on hover. | #ffffff |
| --ads-input-bg-active | Background while focused. | #ffffff |
| --ads-input-border | Border (the boundary of the field). tokens-accessible.css darkens it to #737373 to keep 3:1 against the page. | #d9d9d9 |
| --ads-input-border-hover | Border on hover. tokens-accessible.css darkens it to #0958d9 (#4096ff is 2.99:1). | #4096ff |
| --ads-input-border-active | Border while focused. tokens-accessible.css darkens it to #1668dc. | #1677ff |
| --ads-input-shadow-active | Shadow ring while focused. | 0 0 0 2px rgba(5, 145, 255, 0.1) |
| --ads-input-placeholder | Colour of the placeholder (ours: the documentation lists none). tokens-accessible.css uses #6b6b6b for 4.5:1. | rgba(0, 0, 0, 0.25) |
| --ads-input-icon-color | Colour of the clear, toggle and search icons. tokens-accessible.css uses #737373. | rgba(0, 0, 0, 0.45) |
| --ads-input-icon-color-hover | Colour of those icons on hover. | rgba(0, 0, 0, 0.65) |
| --ads-input-count-color | Colour of the count. tokens-accessible.css uses #666666 for 4.5:1. Past a soft limit it is --ads-color-danger-text. | rgba(0, 0, 0, 0.45) |
| --ads-input-addon-bg | Background of an addon cell. | rgba(0, 0, 0, 0.02) |
| --ads-input-filled-bg | Background of the filled variant. | rgba(0, 0, 0, 0.04) |
| --ads-input-filled-bg-hover | Background of the filled variant on hover (ours). | rgba(0, 0, 0, 0.06) |
| --ads-input-disabled-bg | Background of a disabled field. | rgba(0, 0, 0, 0.04) |
| --ads-input-disabled-border | Border of a disabled field. | #d9d9d9 |
| --ads-input-error | Border of the error status. | #ff4d4f |
| --ads-input-error-hover | Border of the error status on hover. tokens-accessible.css darkens it to #d9363e. | #ff7875 |
| --ads-input-error-shadow | Shadow ring of the error status while focused. | 0 0 0 2px rgba(255, 38, 5, 0.06) |
| --ads-input-error-bg | Background of the filled variant with the error status. | #fff2f0 |
| --ads-input-warning | Border of the warning status. tokens-accessible.css darkens it to #ad6800 (1.9:1 otherwise). | #faad14 |
| --ads-input-warning-hover | Border of the warning status on hover (ours). tokens-accessible.css uses #d46b08. | #ffc53d |
| --ads-input-warning-shadow | Shadow ring of the warning status while focused. | 0 0 0 2px rgba(255, 215, 5, 0.1) |
| --ads-input-warning-bg | Background of the filled variant with the warning status. | #fffbe6 |
Accessibility
- The fields are native
<input>and<textarea>elements: Tab, selection, autofill, spellcheck and forms come from the browser. A field has no label of its own: name it with<label for>,aria-labeloraria-labelledby. status="error"and a count pastcount.maxsetaria-invalid="true". The count is joined to the field byaria-describedbyand is not a live region, so it does not announce every key press.- The clear, show-password and search buttons are native buttons with an accessible name (
Clear,Show passwordandSearch, in English; change them with thelabeloptions). They stay in the Tab order, and a press does not take focus from the field. - IME:
onpressenterandonsearchdo not fire on the Enter that confirms a candidate, andcount.exceedFormattercuts the text only after the composition ends. InputOTPis arole="group"of cells namedCharacter i of n: Backspace goes back, the arrow keys move between cells (mirrored in right-to-left layouts) and a paste fills the cells. A cell past the first empty one cannot take focus.- The default light colors are Ant Design's own and do not reach WCAG: the border is 1.41:1 and the placeholder 1.83:1. Import
tokens-accessible.cssfor 3:1 on the border and 4.5:1 on the placeholder and the count; the dark theme needs no file. Thefilledandborderlessvariants have no boundary by design: useoutlinedorunderlinedwhere the boundary must reach 3:1.