Input输入框
输入框是文本字段。Input 为单行,InputTextArea 为多行,InputPassword、InputSearch 与 InputOTP 分别用于密码、搜索与一次性验证码。每个组件都渲染原生 <input> 或 <textarea>。
何时使用
- 当用户需要输入简短文本(姓名、邮箱、查询词)时使用输入框。
- 较长文本用
InputTextArea,密码用InputPassword,带操作的查询用InputSearch,验证码用InputOTP。 - 如需从列表中选取一项或多项,多选框更合适。
代码演示
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
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| value | 文本。使用 bind:value 双向绑定;普通值为初始文本。函数绑定可拒绝某次编辑;null 视为空。 | string | '' |
| type | 原生 type。支持文本类类型(text、search、email、tel、url、number)。密码请使用 InputPassword。 | string | 'text' |
| disabled | 原生 disabled:不可聚焦、不可编辑、不被提交。 | boolean | false |
| readonly | 原生 readonly:可聚焦、可复制但不可编辑,不显示清除按钮。 | boolean | false |
| size | 输入框大小:高度 40px、32px 或 24px。middle 与 default 为 medium 的兼容别名。在 SpaceCompact 中,未指定 size 的控件采用区块尺寸。 | 'large' | 'medium' | 'middle' | 'small' | 'default' | 'medium' |
| variant | 视觉形态。无边框形态聚焦时会绘制轮廓。 | 'outlined' | 'filled' | 'borderless' | 'underlined' | 'outlined' |
| status | 校验外观。error 还会设置 aria-invalid="true";warning 仅改变外观。 | 'error' | 'warning' | — |
| prefix | 文本前的内容,位于边框内:文本或 snippet。 | string | Snippet | — |
| suffix | 文本后的内容,位于边框内、清除按钮与计数之后:文本或 snippet。 | string | Snippet | — |
| addonBefore | 位于输入框前、边框外的单元格:文本或 snippet。设置了标签时输入框位于分组元素内;添加第一个或移除最后一个标签会重新创建 input(失去焦点)。 | string | Snippet | — |
| addonAfter | 位于输入框后、边框外的单元格:文本或 snippet。 | string | Snippet | — |
| allowClear | 有文本时显示原生清除按钮(禁用或只读时不显示)。点击后焦点留在输入框,并依次调用 onchange("") 与 onclear。对象形式可设置图标、可访问名称(默认 Clear)与 tabindex。 | boolean | { clearIcon?: string | Snippet; label?: string; tabIndex?: number } | false |
| showCount | 显示文本长度。配合 maxlength 显示 n / max。对象形式可格式化文本,结果为字符串。 | boolean | { formatter: (info: CountInfo) => string } | false |
| count | 字符计数:软上限 max(超出后计数变红、输入框无效,文本保留)、替换计数方式的 strategy(默认按 code point 计数)、show(同 showCount,且优先于它)以及截断文本的 exceedFormatter。 | { max?: number; strategy?: (value: string) => number; show?: boolean | ((info: CountInfo) => string); exceedFormatter?: (value: string, config: { max: number }) => string } | — |
| maxlength | 原生 maxlength:浏览器会在上限处阻止输入。仅做提醒的上限请使用 count.max。 | number | — |
| onchange | 每次用户编辑调用一次,参数为新文本和该次编辑的事件:原生 input 事件;若浏览器(例如 Firefox)在最后一个 input 事件之前结束输入法组合,则为 compositionend。等同 React 的 onChange。父组件修改 value 或禁用时不会调用。原生 oninput 也会转发,且先执行。 | (value: string, event: Event) => void | — |
| onpressenter | 按回车时调用;确认输入法候选词的回车不会触发。 | (event: KeyboardEvent) => void | — |
| onclear | 清除按钮清空文本后调用。 | (event: MouseEvent) => void | — |
| classNames | 各部分的额外类名,追加在组件自身类名之后。 | Partial<Record<'root' | 'prefix' | 'input' | 'suffix' | 'clear' | 'count', string>> | — |
| styles | 各部分的内联样式。属性名为 kebab-case 或自定义属性,数字按原样写入(不自动加 px),优先于组件自身样式。 | Partial<Record<'root' | 'prefix' | 'input' | 'suffix' | 'clear' | 'count', Record<string, string | number>>> | — |
| class | 根元素附加的 CSS 类名。 | string | — |
| style | 根元素的内联样式;styles.root 写在其后。 | string | — |
| native attributes | input 的原生属性(id、name、placeholder、required、autocomplete、inputmode、title、aria-*、data-*、onfocus、onblur、onkeydown、oncompositionstart 等)会转发到 <input>。组件自身管理 value、disabled、readonly、maxlength 与 onchange。 | HTMLInputAttributes | — |
与 Ant Design 的差异:
onchange 在每次编辑时接收 (value, event)(React 传入事件);没有 defaultValue,由 bind:value 替代;各部分是独立组件(Svelte 没有 Input.TextArea);showCount 与 count.show 的格式化函数返回字符串(React 返回节点);密码框的 visible 与 onvisiblechange 是独立属性(React 嵌套在 visibilityToggle 内);不提供 bordered,请用 variant="borderless";不提供 Input.Group 与 compact。InputTextArea 的 API
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| value | 与 Input 相同。 | string | '' |
| disabled | 与 Input 相同。 | boolean | false |
| readonly | 与 Input 相同。 | boolean | false |
| size | 与 Input 相同。 | 'large' | 'medium' | 'middle' | 'small' | 'default' | 'medium' |
| variant | 与 Input 相同。 | 'outlined' | 'filled' | 'borderless' | 'underlined' | 'outlined' |
| status | 与 Input 相同。 | 'error' | 'warning' | — |
| allowClear | 与 Input 相同;按钮位于输入框右上角。 | boolean | { clearIcon?: string | Snippet; label?: string; tabIndex?: number } | false |
| showCount | 与 Input 相同;计数在输入框底部独占一行。 | boolean | { formatter: (info: CountInfo) => string } | false |
| count | 与 Input 相同。 | { max?: number; strategy?: (value: string) => number; show?: boolean | ((info: CountInfo) => string); exceedFormatter?: (value: string, config: { max: number }) => string } | — |
| maxlength | 与 Input 相同。 | number | — |
| autoSize | 高度跟随内容,范围为 minRows 到 maxRows 行(至少一行);超过 maxRows 后滚动。文本、宽度或字体变化时重新测量。开启时隐藏手动调整手柄。 | boolean | { minRows?: number; maxRows?: number } | false |
| onchange | 与 Input 相同。 | (value: string, event: Event) => void | — |
| onpressenter | 与 Input 相同。 | (event: KeyboardEvent) => void | — |
| onclear | 与 Input 相同。 | (event: MouseEvent) => void | — |
| classNames | 各部分的额外类名,追加在组件自身类名之后。 | Partial<Record<'root' | 'textarea' | 'clear' | 'count', string>> | — |
| styles | 各部分的内联样式,优先于组件自身样式。 | Partial<Record<'root' | 'textarea' | 'clear' | 'count', Record<string, string | number>>> | — |
| class | 与 Input 相同。 | string | — |
| style | 与 Input 相同。 | string | — |
| native attributes | textarea 的原生属性(rows、cols、placeholder、name、id、required、aria-*、data-*、onfocus 等)会转发到 <textarea>。 | HTMLTextareaAttributes | — |
InputPassword 的 API
InputPassword 接受 Input 的所有属性(type 除外,由组件设置),并新增以下属性。
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| visible | 文本是否可见。使用 bind:visible 双向绑定;普通值为初始状态。 | boolean | false |
| visibilityToggle | 显示切换按钮。false 隐藏按钮,此时只能通过 visible 控制。对象形式可设置可访问名称(默认 Show password;按钮带有 aria-pressed,名称不变)与 tabindex。 | boolean | { tabIndex?: number; label?: string } | true |
| onvisiblechange | 用户每次切换时调用一次,参数为新的可见状态;父组件修改 visible 时不调用。 | (visible: boolean) => void | — |
| iconRender | 自定义切换按钮的内容;接收当前的可见状态。 | Snippet<[visible: boolean]> | — |
InputSearch 的 API
InputSearch 接受 Input 的所有属性,并新增以下属性。其 classNames 与 styles 多一个部分:button。
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| enterButton | 输入框后的按钮:true 为带搜索图标的主按钮,字符串或 snippet 为其内容。false 仅保留框内的图标按钮。与 addonAfter 冲突:非生产环境同时使用会抛错,生产环境以按钮为准。 | boolean | string | Snippet | false |
| loading | 用加载图标替换搜索图标;加载期间忽略搜索操作(点击与回车)。 | boolean | false |
| onsearch | 点击图标或按钮、按回车(输入法组合期间除外)以及点击清除按钮时调用。清除按钮的 source 为 "clear",其余为 "input";清除时文本为空。 | (value: string, event: Event, info: { source: 'input' | 'clear' }) => void | — |
| searchIcon | 自定义搜索图标:文本或 snippet。 | string | Snippet | — |
| classNames | Input 的各部分,以及搜索按钮 button。 | Partial<Record<'root' | 'prefix' | 'input' | 'suffix' | 'clear' | 'count' | 'button', string>> | — |
| styles | 同样各部分的内联样式。 | Partial<Record<'root' | 'prefix' | 'input' | 'suffix' | 'clear' | 'count' | 'button', Record<string, string | number>>> | — |
| class / style | 根元素,包含输入框与按钮。除非另行设置,type 为 search。 | string | — |
InputOTP 的 API
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| value | 已输入的字符,无空位。使用 bind:value 双向绑定;普通值为初始文本。第 i 格显示第 i 个字符。 | string | '' |
| length | 格数。非正整数在非生产环境抛错,生产环境按 6 处理。 | number | 6 |
| mask | 隐藏字符:true 显示圆点,字符串为替代显示的单个字符。值本身不变。 | boolean | string | false |
| formatter | 把整段文本映射为显示文本;第 i 格显示结果的第 i 个字符。mask 优先于它。 | (value: string) => string | — |
| separator | 渲染在指定索引的格之后(最后一格之后不渲染):文本,或接收索引的 snippet。 | string | Snippet<[index: number]> | — |
| disabled | 禁用每个格子。 | boolean | false |
| size | 格子大小;格子宽度等于输入框高度。middle 与 default 为 medium 的别名。 | 'large' | 'medium' | 'middle' | 'small' | 'default' | 'medium' |
| variant | 格子的视觉形态。 | 'outlined' | 'filled' | 'borderless' | 'underlined' | 'outlined' |
| status | 格子的校验外观;error 会给每格设置 aria-invalid。 | 'error' | 'warning' | — |
| autocomplete | 每格的原生 autocomplete;one-time-code 可启用验证码自动填充。 | HTMLInputAttributes['autocomplete'] | — |
| onchange | 所有格子填满时调用,参数为完整文本;父组件修改 value 时不调用。 | (value: string) => void | — |
| oninput | 每次变化时调用,每格一项(空格为 "")。 | (values: string[]) => void | — |
| classNames | 根元素、格子(input)与分隔符的额外类名。 | Partial<Record<'root' | 'input' | 'separator', string>> | — |
| styles | 同样各部分的内联样式。 | Partial<Record<'root' | 'input' | 'separator', Record<string, string | number>>> | — |
| class | 根元素附加的 CSS 类名。 | string | — |
| style | 根元素的内联样式;styles.root 写在其后。 | string | — |
| native attributes | 分组的原生属性(id、aria-label、aria-labelledby、aria-describedby、data-*、事件处理器)会转发到 <div>,它保留 role="group"。每格的名称为 Character i of n(英文;本地化机制已列入计划)。 | HTMLAttributes<HTMLDivElement> | — |
方法
五个组件都有这些方法,通过 bind:this 调用。
| 方法 | 说明 | 类型 |
|---|---|---|
| focus | 让输入框获得焦点(InputOTP 为第一个空格)。cursor 可将光标置于开头或末尾,或选中全部文本;不支持选区的类型(email、number)会忽略它。 | (options?: { preventScroll?: boolean; cursor?: 'start' | 'end' | 'all' }) => void |
| blur | 让输入框失去焦点。 | () => void |
设计令牌
| 令牌 | 说明 | 默认值 |
|---|---|---|
| --ads-input-font-size | 中号输入框字号。 | 14px |
| --ads-input-font-size-lg | 大号输入框字号。 | 16px |
| --ads-input-font-size-sm | 小号输入框字号。 | 14px |
| --ads-input-padding-block | 中号输入框的垂直内边距;与行高、边框共同得到 32px 的高度。 | 4px |
| --ads-input-padding-block-lg | 大号输入框的垂直内边距(高 40px)。 | 7px |
| --ads-input-padding-block-sm | 小号输入框的垂直内边距(高 24px)。 | 0px |
| --ads-input-padding-inline | 中号输入框的水平内边距。 | 11px |
| --ads-input-padding-inline-lg | 大号输入框的水平内边距。 | 11px |
| --ads-input-padding-inline-sm | 小号输入框的水平内边距。 | 7px |
| --ads-input-radius | 中号输入框圆角半径。 | 6px |
| --ads-input-radius-lg | 大号输入框圆角半径。 | 8px |
| --ads-input-radius-sm | 小号输入框圆角半径。 | 4px |
| --ads-input-border-width | 边框粗细。 | 1px |
| --ads-input-line-height | 中号与小号输入框的行高。 | 1.5714285714285714 |
| --ads-input-line-height-lg | 大号输入框的行高。 | 1.5 |
| --ads-input-gap | 文本与前缀、清除按钮、计数、后缀之间的间距。 | 4px |
| --ads-input-icon-size | 清除、切换与搜索图标的大小。 | 14px |
| --ads-input-addon-padding-inline | 前置/后置标签单元格的水平内边距。 | 11px |
| --ads-input-otp-gap | InputOTP 各格之间的间距。 | 8px |
| --ads-input-bg | 输入框背景。 | #ffffff |
| --ads-input-bg-hover | 悬停时的背景。 | #ffffff |
| --ads-input-bg-active | 聚焦时的背景。 | #ffffff |
| --ads-input-border | 边框(输入框的边界)。tokens-accessible.css 将其加深为 #737373,以与页面保持 3:1。 | #d9d9d9 |
| --ads-input-border-hover | 悬停时的边框。tokens-accessible.css 将其加深为 #0958d9(#4096ff 为 2.99:1)。 | #4096ff |
| --ads-input-border-active | 聚焦时的边框。tokens-accessible.css 将其加深为 #1668dc。 | #1677ff |
| --ads-input-shadow-active | 聚焦时的阴影光圈。 | 0 0 0 2px rgba(5, 145, 255, 0.1) |
| --ads-input-placeholder | 占位符颜色(自定:文档未给出)。tokens-accessible.css 使用 #6b6b6b 以达到 4.5:1。 | rgba(0, 0, 0, 0.25) |
| --ads-input-icon-color | 清除、切换与搜索图标的颜色。tokens-accessible.css 使用 #737373。 | rgba(0, 0, 0, 0.45) |
| --ads-input-icon-color-hover | 这些图标悬停时的颜色。 | rgba(0, 0, 0, 0.65) |
| --ads-input-count-color | 计数颜色。tokens-accessible.css 使用 #666666 以达到 4.5:1。超出软上限时为 --ads-color-danger-text。 | rgba(0, 0, 0, 0.45) |
| --ads-input-addon-bg | 前置/后置标签单元格背景。 | rgba(0, 0, 0, 0.02) |
| --ads-input-filled-bg | filled 形态的背景。 | rgba(0, 0, 0, 0.04) |
| --ads-input-filled-bg-hover | filled 形态悬停时的背景(自定)。 | rgba(0, 0, 0, 0.06) |
| --ads-input-disabled-bg | 禁用输入框的背景。 | rgba(0, 0, 0, 0.04) |
| --ads-input-disabled-border | 禁用输入框的边框。 | #d9d9d9 |
| --ads-input-error | error 状态的边框。 | #ff4d4f |
| --ads-input-error-hover | error 状态悬停时的边框。tokens-accessible.css 将其加深为 #d9363e。 | #ff7875 |
| --ads-input-error-shadow | error 状态聚焦时的阴影光圈。 | 0 0 0 2px rgba(255, 38, 5, 0.06) |
| --ads-input-error-bg | filled 形态在 error 状态下的背景。 | #fff2f0 |
| --ads-input-warning | warning 状态的边框。tokens-accessible.css 将其加深为 #ad6800(否则为 1.9:1)。 | #faad14 |
| --ads-input-warning-hover | warning 状态悬停时的边框(自定)。tokens-accessible.css 使用 #d46b08。 | #ffc53d |
| --ads-input-warning-shadow | warning 状态聚焦时的阴影光圈。 | 0 0 0 2px rgba(255, 215, 5, 0.1) |
| --ads-input-warning-bg | filled 形态在 warning 状态下的背景。 | #fffbe6 |
无障碍
- 输入框是原生
<input>与<textarea>元素:Tab、选区、自动填充、拼写检查与表单均由浏览器提供。输入框自身没有标签:请用<label for>、aria-label或aria-labelledby命名。 status="error"以及超出count.max的计数都会设置aria-invalid="true"。计数通过aria-describedby与输入框关联,且不是实时区域,因此不会在每次按键时播报。- 清除、显示密码与搜索按钮都是带可访问名称的原生按钮(
Clear、Show password、Search,为英文;可通过label选项修改)。它们保留在 Tab 顺序中,点击不会夺走输入框的焦点。 - 输入法:确认候选词的回车不会触发
onpressenter与onsearch,count.exceedFormatter仅在组合输入结束后才截断文本。 InputOTP是role="group",其中各格名称为Character i of n:退格后移,方向键在格间移动(从右到左布局中镜像),粘贴会填充各格。位于第一个空格之后的格子无法获得焦点。- 默认的浅色取自 Ant Design,未达到 WCAG:边框为 1.41:1,占位符为 1.83:1。引入
tokens-accessible.css后,边框达到 3:1,占位符与计数达到 4.5:1;深色主题无需该文件。filled与borderless形态按设计没有边界:需要边界达到 3:1 时请使用outlined或underlined。