Input
用于输入、搜索、密码和验证码的文本字段。
何时使用
输入适合自由文本:名称、地址、搜索查询或标识符。对于多行消息,请使用 TextArea。如果该值涉及计算且数值步长变化,则选择InputNumber。 Select 旨在从现成的选项中进行选择。
标题必须解释该字段的用途,并在输入后保持可见。placeholder 是对 label 的补充,但不能取代它。在Form内部,错误的签名和描述链接到FormItem;为独立字段指定 label 或可访问的名称。
CSS 输入前缀 显示全局 ConfigProvider.prefixCls 和本地 prefixCls,无需重置状态。输入和文本区域使用相同的基本前缀。密码/搜索共享 shell prefixCls 和 inputPrefixCls 字段。 OTP 更改组、单元格和分隔符;内部输入和搜索按钮分别继承提供者的公共 prefix。
如何选择字段
含义、事件和形式
输入、文本区域、输入密码和输入搜索支持 bind:value。 defaultValue指定初始内容;对于后续更新,请使用托管值。 打扫 报告新值并将焦点返回到该字段。
选择一种更新应用程序状态的方法:绑定或 value 与 onValueChange。本机 oninput 接收浏览器事件。兼容的 onChange 在输入和清除时调用,并包含值的快照;原始事件可通过 nativeEvent 获得。无需在多个处理程序中复制一个模型记录。
在 Form 中,传递模型中的值并通过 onValueChange 调用 setFieldValue。不要通过 bind:value 链接只读模型。 输入家庭表格显示检查、发送、reset;密码不会与公开结果一起显示。
在InputSearch 中,onSearch 处理程序与行更改分开:Enter 和按钮开始搜索,清除报告source='clear'。 loading 阻止通过 Enter 和按钮启动,从而节省清洁工作。自定义按钮代码段必须应用 context.getButtonProps() 或 getNativeProps() 才能获取处理程序和搜索状态。这两种情况均出现在 例子 和 加载中 中。
内容、长度和焦点
prefix 和 suffix 将内容放置在字段中; SpaceAddon 紧随其后,结构紧凑。对于动态添加,请使用包装器保留持久片段并更改其中的内容:重建包装器可能会重新创建 input 并失去焦点。 聚焦方法 允许您选择光标位置和选择。
showCount 显示长度,count.max 设置软限制,maxLength 限制输入。默认情况下,计数器使用 UTF-16 单位。对于自定义字符(包括复合表情符号),您需要自己的 strategy 和可选的 exceedFormatter。 字素示例 使用 Intl.Segmenter。此 formatter 不会修剪外部托管值。
TextArea 和 autoSize 测量内容和可用宽度; minRows 和 maxRows 限制高度。固定 CSS 高度会干扰此设置。error 或 warning 状态指定设计,但不检查文本本身:规则属于表单或应用程序。
Input 的外观使用当前主题的设计令牌(design tokens)。
示例
基本输入
bind:value 将本机输入绑定到 Svelte 字符串状态。
<script lang="ts">
import { Input } from '@woodger/antd';
let value = $state('Svelte');
</script>
<style>
.example {
display: grid;
gap: 12px;
}
output {
color: var(--ant-color-text-secondary);
overflow-wrap: anywhere;
}
</style>
<div class="example">
<Input aria-label={("基本字段输入")} bind:value placeholder={("输入文字")} />
<output aria-label={("输入值")}>{("当前值: ")}{(value
|| '—')}</output>
</div>
三种尺寸
large、medium、small。如果没有本地 size,该字段将从 ConfigProvider 继承大小。
<script lang="ts">
import { Input } from '@woodger/antd';
</script>
<style>
.example {
display: grid;
gap: 16px;
}
</style>
{#snippet userIcon()}
<svg
width="1em"
height="1em"
viewBox="0 0 16 16"
fill="none"
stroke="currentColor"
aria-hidden="true"
focusable="false"
><circle
cx="8"
cy="5"
r="3"
/><path
d="M2 15v-2a6 6 0 0 1 12 0v2"
/></svg>
{/snippet}
<div class="example">
<Input
size="large"
prefix={userIcon}
aria-label={("大场")}
placeholder={("large")}
/>
<Input
size="medium"
prefix={userIcon}
aria-label={("常规场")}
placeholder={("medium")}
/>
<Input
size="small"
prefix={userIcon}
aria-label={("小场地")}
placeholder={("small")}
/>
</div>
设计选项
outlined、filled、borderless 和 underlined;复合场也可进行装饰。
<script lang="ts">
import { Input, InputSearch, type InputVariant } from '@woodger/antd';
const variants = ['outlined', 'filled', 'borderless', 'underlined'] satisfies InputVariant[];
</script>
<style>
.example {
display: grid;
gap: 16px;
}
</style>
<div class="example">
{#each variants as variant (variant)}
<Input {variant} aria-label={("选项 " + String((variant)) + "")} placeholder={(variant)} />
{/each}
<InputSearch variant="filled" aria-label={("搜索已满")} placeholder={("Input.Search filled")} />
</div>
紧凑的群体
SpaceCompact 将 Input 连接到 Select、Button 或 SpaceAddon 并匹配它们的大小。
<script lang="ts">
import { Button, Input, Select, SpaceAddon, SpaceCompact } from '@woodger/antd';
let value = $state('woodger.org');
let result = $state('');
const options = [{ value: 'https', label: 'https://' }, { value: 'http', label: 'http://' }];
</script>
<style>
.example {
display: grid;
gap: 20px;
}
output {
color: var(--ant-color-text-secondary);
overflow-wrap: anywhere;
}
</style>
{#snippet protocol()}
<Select aria-label={("协议")} options={(options)} defaultValue="https" style="width:112px" />
{/snippet}
{#snippet address()}
<Input aria-label={("网址")} placeholder={("woodger.org")} style="
min-width: 0;
flex: 1;
" />
{/snippet}
{#snippet field()}<Input
aria-label={("紧致场")}
bind:value
style="
min-width: 0;
flex: 1;
"
/>{/snippet}
{#snippet submit()}
<Button type="primary" onclick={() => { result = value; }}>
{("发送")}
</Button>
{/snippet}
{#snippet prefix()}<SpaceAddon>https://</SpaceAddon>{/snippet}
{#snippet domain()}
<Input aria-label={("带插件的域名")} placeholder={("woodger.org")} style="
min-width: 0;
flex: 1;
" />
{/snippet}
<div class="example">
<SpaceCompact block items={([protocol, address])} />
<SpaceCompact block items={([field, submit])} />
<SpaceCompact block items={([prefix, domain])} />
<output aria-label={("紧凑组结果")}>{(result
|| ("提交字段值"))}</output>
</div>
Input.Search
按 Enter 和按钮搜索,清除按钮片段。 onSearch 报告操作的来源。
<script lang="ts">
import { Button, InputSearch, type InputSearchProps } from '@woodger/antd';
let value = $state('Svelte');
let result = $state('');
const onSearch: InputSearchProps['onSearch'] = (next, _event, info) => {
result = ("" + String((info?.source === 'clear'
? ("打扫")
: ("搜索"))) + ": " + String((next || ("空请求"))) + "");
};
</script>
<style>
.example {
display: grid;
gap: 20px;
}
output {
color: var(--ant-color-text-secondary);
overflow-wrap: anywhere;
}
</style>
<div class="example">
<InputSearch aria-label={("使用图标搜索")} placeholder={("常规按钮")} {onSearch} />
<InputSearch aria-label={("使用主图标搜索")} placeholder={("主按钮")} enterButton {onSearch} />
<InputSearch
aria-label={("按要求搜索")}
bind:value
allowClear
enterButton={("寻找")}
{onSearch}
/>
<InputSearch aria-label={("使用您自己的按钮进行搜索")} placeholder={("Svelte snippet")} {onSearch}>
{#snippet enterButton(context)}
<Button {...context.getButtonProps({ type: 'primary' })}>{("搜索")}</Button>
{/snippet}
</InputSearch>
<output aria-label={("输入搜索结果")}>{(result
|| ("使用 Enter 按钮或按键提交您的请求"))}</output>
</div>
加载中搜索
通过 Enter 和按钮 loading 块搜索。清除该字段仍然可用。
<script lang="ts">
import { InputSearch } from '@woodger/antd';
let result = $state(("加载时搜索被阻止"));
</script>
<style>
.example {
display: grid;
gap: 20px;
}
output { color: var(--ant-color-text-secondary); }
</style>
<div class="example">
<InputSearch loading aria-label={("使用图标加载搜索")} placeholder={("加载中")} />
<InputSearch
loading
enterButton
aria-label={("加载主搜索按钮")}
placeholder={("加载中")}
/>
<InputSearch loading enterButton={("寻找")} allowClear defaultValue="Svelte"
aria-label={("加载搜索并进行清理")} onSearch={(_value, _event, info) => {
result = info?.source === 'clear'
? ("加载期间清除请求")
: ("搜索完成");
}} />
<output aria-label={("搜索加载状态")}>{result}</output>
</div>
TextArea
使用 bind:value 的多 rows 字段、行和本机 maxLength 约束。
<script lang="ts">
import { TextArea } from '@woodger/antd';
let value = $state(("任务描述 其他详细信息"));
</script>
<style>
.example {
display: grid;
gap: 20px;
}
output { color: var(--ant-color-text-secondary); }
</style>
<div class="example">
<TextArea rows={4} aria-label={("多行描述")} bind:value />
<TextArea
rows={2}
maxLength={6}
aria-label={("描述最多六个字符")}
placeholder={("不超过6个字符")}
/>
<output aria-label={("文本区域长度")}>{("人物: ")}{(value.length)}</output>
</div>
自动高度
autoSize 调整高度以适合内容。 minRows 和 maxRows 限制可见行数。
<script lang="ts">
import { TextArea } from '@woodger/antd';
let value = $state(("托管多行字段"));
</script>
<style>
.example {
display: grid;
gap: 24px;
}
</style>
<div class="example">
<TextArea autoSize aria-label={("自动高度")} placeholder={("内容高度")} />
<TextArea autoSize={{ minRows: 2, maxRows: 6 }} aria-label={("高度从两行到六行")}
placeholder={("两到六行")} />
<TextArea bind:value autoSize={{ minRows: 3, maxRows: 5 }} aria-label={("受控文本区域高度")} />
</div>
Input.OTP
带有长度选择、掩码、格式和分隔符的确认码。 onInput 报告单元格,onChange 报告完整代码。
<script lang="ts">
import { InputOTP } from '@woodger/antd';
let length = $state(6);
let masked = $state(false);
let cells = $state('');
let complete = $state('');
</script>
<style>
.example {
display: grid;
gap: 16px;
min-width: 0;
}
.options {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 12px;
}
label {
display: inline-flex;
align-items: center;
gap: 8px;
}
select {
padding: 4px;
border: 1px solid var(--ant-color-border);
border-radius: 4px;
background: var(--ant-color-bg-container);
color: var(--ant-color-text);
font: inherit;
}
.code {
max-width: 100%;
overflow-x: auto;
padding-block: 4px;
}
output {
color: var(--ant-color-text-secondary);
overflow-wrap: anywhere;
}
</style>
<div class="example">
<div class="options">
<label>{("代码长度 ")}<select bind:value={length} onchange={() => {
cells = '';
complete = '';
}}>
<option value={4}>4</option><option value={6}>6</option><option value={8}>8</option>
</select></label>
<label><input type="checkbox" bind:checked={masked} />{(" 隐藏代码")}</label>
</div>
<div class="code">
{#key length}
<InputOTP {length} mask={masked} aria-label={("输入.OTP验证码")}
autoComplete="one-time-code" formatter={value => value.toUpperCase()}
separator={index => index === Math.floor(length / 2) - 1 ? '—' : undefined}
onInput={next => { cells = next.map(cell => cell || '·').join(''); }}
onChange={next => { complete = next; }} />
{/key}
</div>
<output aria-label={("输入.OTP 单元")}>{("细胞: ")}{(cells
|| ("未填写"))}</output>
<output aria-label={("完整输入.OTP 代码")}>{(complete ? `最新完整代码: ${complete}` : "代码尚未完成")}</output>
</div>
在工具提示中设置格式
工具提示显示带有位置分隔符的数字,同时保留文本输入。对于数字步骤,应使用 InputNumber。
<script lang="ts">
import { Input, Tooltip } from '@woodger/antd';
let value = $state('');
let lastValid = '';
const format = new Intl.NumberFormat("zh-CN", { maximumFractionDigits: 12 });
const title = $derived(
value && value !== '-'
? format.format(Number(value))
: "输入号码"
);
function handleInput(event: Event & { currentTarget: HTMLInputElement }) {
const next = event.currentTarget.value;
if (/^-?\d*(\.\d*)?$/.test(next)) {
lastValid = next;
}
else {
event.currentTarget.value = lastValid;
value = lastValid;
}
}
function normalize() {
value = value.replace(/\.$/, '').replace(/^(-?)0+(?=\d)/, '$1');
if (value === '-') value = '';
lastValid = value;
}
</script>
<Tooltip title={title} trigger="focus">
<Input
aria-label={("文本字段中的数字")}
bind:value
inputmode="decimal"
maxLength={16}
placeholder={("输入号码")}
oninput={handleInput}
onblur={normalize}
/>
</Tooltip>
prefix 和 suffix
文本和 Svelte 片段放置在字段内值之前和之后。
<script lang="ts">
import { Input, InputPassword, Tooltip } from '@woodger/antd';
</script>
<style>
.example {
display: grid;
gap: 20px;
}
.info {
display: inline-flex;
align-items: center;
justify-content: center;
width: 18px;
height: 18px;
padding: 0;
border: 1px solid currentColor;
border-radius: 50%;
background: none;
color: inherit;
font-size: 12px;
cursor: help;
}
</style>
{#snippet help()}
<Tooltip title={("请输入您的用户名,不带空格")}>
<button class="info" type="button" aria-label={("名称提示")}>i</button>
</Tooltip>
{/snippet}
<div class="example">
<Input
prefix="@"
suffix={help}
aria-label={("带有提示的名字")}
placeholder={("用户名")}
/>
<Input prefix="₽" suffix="RUB" aria-label={("金额(货币)")} placeholder={("和")} />
<Input
prefix="₽"
suffix="RUB"
disabled
aria-label={("无法使用的金额")}
defaultValue="1200"
/>
<InputPassword
suffix={("钥匙")}
aria-label={("密码带后缀")}
placeholder={("密码")}
/>
</div>
Input.Password
切换可见性、您自己的图标并从外部控制可见性。密码值已保存。
<script lang="ts">
import { Button, InputPassword } from '@woodger/antd';
let visible = $state(false);
</script>
<style>
.example {
display: grid;
gap: 20px;
}
.controlled {
display: grid;
gap: 12px;
justify-items: start;
}
</style>
<div class="example">
<InputPassword aria-label={("普通密码")} placeholder={("输入您的密码")} allowClear />
<InputPassword aria-label={("带有自己图标的密码")} placeholder={("你的开关")}
iconRender={shown => shown ? "隐藏" : "展示"} />
<div class="controlled">
<InputPassword aria-label={("管理密码")} defaultValue="Svelte-5-demo"
visibilityToggle={{ visible, onVisibleChange: next => { visible = next; } }} />
<Button onclick={() => { visible = !visible; }}>{(visible ? "隐藏密码" : "显示密码")}</Button>
</div>
<InputPassword
disabled
aria-label={("密码无法访问")}
defaultValue="password"
/>
</div>
打扫
allowClear 添加清除按钮。 onClear 报告操作;焦点返回到现场。
<script lang="ts">
import { Input, TextArea } from '@woodger/antd';
let input = $state(("清除字段"));
let text = $state(("清除多行字段"));
let result = $state('');
</script>
<style>
.example {
display: grid;
gap: 20px;
}
output { color: var(--ant-color-text-secondary); }
</style>
<div class="example">
<Input aria-label={("输入带清洗")} bind:value={input} allowClear
onClear={() => { result = ("输入清零"); }} />
<TextArea aria-label={("具有清理功能的 TextArea")} bind:value={text} allowClear rows={3}
onClear={() => { result = ("文本区域已清除"); }} />
<output aria-label={("清洗效果")}>{(result
|| ("清除按钮将焦点返回到字段"))}</output>
</div>
字符计数器
showCount 显示长度,maxLength 为Input和TextArea设置硬输入限制。
<script lang="ts">
import { Input, TextArea } from '@woodger/antd';
</script>
<style>
.example {
display: grid;
gap: 24px;
}
</style>
<div class="example">
<Input
showCount
maxLength={20}
aria-label={("带计数器输入")}
placeholder={("最多 20 个字符")}
/>
<TextArea showCount maxLength={100} rows={3} aria-label={("带计数器的文本区域")}
placeholder={("最多 100 个字符")} />
<TextArea showCount maxLength={100} rows={3} aria-label={("不调整大小的文本区域")}
placeholder={("鼠标高度调节已禁用")} style="resize:none" />
</div>
您自己的计数规则
count.max - 软限制。策略对字素进行计数,并且 exceedFormatter 会在必要时修剪用户输入。
<script lang="ts">
import { Input } from '@woodger/antd';
const segmenter = new Intl.Segmenter('ru', { granularity: 'grapheme' });
const characters = (value: string) => Array.from(segmenter.segment(value), part => part.segment);
</script>
<style>
.example {
display: grid;
gap: 24px;
}
</style>
<div class="example">
<Input defaultValue="Hello, Svelte!" count={{ show: true, max: 10 }}
aria-label={("软字符限制")} />
<Input defaultValue="🔥🔥🔥" count={{ show: true, strategy: value => characters(value).length }}
aria-label={("字素计数器")} />
<Input defaultValue="🔥" aria-label={("字素限制")} count={{
show: true,
max: 6,
strategy: value => characters(value).length,
exceedFormatter: (value, { max }) => characters(value).slice(0, max).join('')
}} />
</div>
字段状态
错误、警告、disabled 和 readOnly。 readOnly 允许您选择并复制一个值。
<script lang="ts">
import { Input } from '@woodger/antd';
</script>
<style>
.example {
display: grid;
gap: 16px;
}
</style>
<div class="example">
<Input status="error" aria-label={("错误字段")} placeholder={("错误")} />
<Input status="warning" aria-label={("警告区域")} placeholder={("警告")} />
<Input
prefix="@"
status="error"
aria-label={("前缀错误")}
placeholder={("前缀错误")}
/>
<Input
prefix="@"
status="warning"
aria-label={("带前缀的警告")}
placeholder={("带前缀的警告")}
/>
<Input disabled aria-label={("无法访问输入字段")} defaultValue="Недоступно" />
<Input
readOnly
aria-label={("只读字段")}
defaultValue="Можно выделить и скопировать"
/>
</div>
重点与选择
focus() 方法使用 bind:this 来选择光标位置或突出显示输入和文本区域中的文本。
<script lang="ts">
import { Button, Checkbox, Input, TextArea } from '@woodger/antd';
let multiline = $state(false);
let value = $state(("Ant Design for Svelte"));
let control = $state<ReturnType<typeof Input> | ReturnType<typeof TextArea>>();
</script>
<style>
.example {
display: grid;
gap: 20px;
}
.actions {
display: flex;
flex-wrap: wrap;
gap: 8px;
}
</style>
<div class="example">
<div class="actions">
<Button onclick={() => control?.focus({ cursor: 'start' })}>{("到开始")}</Button>
<Button onclick={() => control?.focus({ cursor: 'end' })}>{("到最后")}</Button>
<Button
onclick={() => control?.focus({ cursor: 'all' })}
>{("选择全部")}</Button>
<Button
onclick={() => control?.focus({ preventScroll: true })}
>{("没有滚动")}</Button>
</div>
<Checkbox bind:checked={multiline}>{("多行字段")}</Checkbox>
{#if multiline}
<TextArea
bind:this={control}
bind:value
aria-label={("焦点控制区域")}
/>
{:else}
<Input bind:this={control} bind:value aria-label={("焦点控制区域")} />
{/if}
</div>
语义风格
classNames 和 styles 决定了族元素的风格。 CSS 字符串由 props 中的对象或函数指定。
<script lang="ts">
import {
Input,
InputOTP,
InputPassword,
InputSearch,
TextArea,
type InputProps
} from '@woodger/antd';
const styles: InputProps['styles'] = ({ props }) => ({
root: props.size === 'small'
? 'border-color:#389e0d'
: 'border-color:#722ed1',
input: 'font-weight:600'
});
</script>
<style>
.example {
display: grid;
gap: 20px;
}
.example :global(.input-semantic-root) { border-radius: 12px; }
.otp {
min-width: 0;
overflow-x: auto;
padding-block: 4px;
}
</style>
<div class="example">
<Input aria-label={("风格输入功能")} placeholder={("按功能划分的款式")}
classNames={{ root: 'input-semantic-root' }} {styles} />
<TextArea showCount defaultValue="TextArea" aria-label={("文本区域样式")}
styles={{
textarea: 'resize:none',
count: 'color:#389e0d'
}} />
<InputPassword aria-label={("输入密码样式")} defaultValue="Password"
styles={{ root: 'border-color:#d46b08' }} />
<InputSearch aria-label={("输入.搜索样式")} placeholder={("搜索按钮样式")} enterButton={("寻找")}
styles={{ button: { root: `
background: #389e0d;
border-color: #389e0d;
` } }} />
<div class="otp">
<InputOTP length={4} separator="—" aria-label={("输入.OTP 样式")}
styles={{ input: 'border-color:#722ed1' }} />
</div>
</div>
以 Svelte 的形式
输入、输入密码和文本区域与表单模型相关联:值的验证、提交和重置。
<script lang="ts">
import { Button, Form, FormItem, Input, InputPassword, TextArea, createForm } from '@woodger/antd';
const form = createForm<{ name: string; password: string; description: string }>({
initialValues: { name: '', password: '', description: '' },
rules: ({
name: [{ required: true, message: "请提供姓名。" }],
password: [{
min: 6,
required: true,
message: "密码必须至少包含 6 个字符。"
}]
})
});
let result = $state('');
</script>
<style>
.actions {
display: flex;
flex-wrap: wrap;
gap: 12px;
}
output {
display: block;
margin-top: 16px;
color: var(--ant-color-text-secondary);
overflow-wrap: anywhere;
}
</style>
<Form {form} onFinish={values => { result = ("已保存: " + String((values.name)) + ""); }}>
<FormItem name="name" label={("参加者姓名")} required>
<Input value={form.values.name} onValueChange={value => form.setFieldValue('name', value)} />
</FormItem>
<FormItem name="password" label={("参加者密码")} required>
<InputPassword value={form.values.password} autoComplete="new-password"
onValueChange={value => form.setFieldValue('password', value)} />
</FormItem>
<FormItem name="description" label={("关于参与者的注意事项")}>
<TextArea value={form.values.description} autoSize={{ minRows: 2, maxRows: 4 }}
onValueChange={value => form.setFieldValue('description', value)} />
</FormItem>
<div class="actions">
<Button type="primary" htmlType="submit">{("保存参与者")}</Button>
<Button
htmlType="reset"
onclick={() => { result = ''; }}
>{("重置表格")}</Button>
</div>
<output aria-label={("输入表格的结果")}>{result}</output>
</Form>
输入和文本区域使用相同的基本前缀。密码/搜索共享 shell prefixCls 和 inputPrefixCls 字段。 OTP 更改组、单元格和分隔符;内部输入和搜索按钮分别继承提供者的公共前缀。
CSS 输入前缀
通用命名空间ConfigProvider和本地 prefixCls;当名称更改时,组件的状态将被保留。
<script lang="ts">
import {
Checkbox,
ConfigProvider,
Input,
TextArea,
InputPassword,
InputSearch,
InputOTP
} from '@woodger/antd';
let custom = $state(true);
let local = $state(true);
const prefix = $derived(custom ? 'suite' : 'ant');
const localPrefix = $derived(local ? 'project-input' : '');
let draft = $state(("草稿场"));
let text = $state(("几行"));
let password = $state('secret');
let query = $state('Svelte');
let code = $state('1234');
let searched = $state('—');
</script>
<style>
.example,
.prefix-stage {
display: grid;
grid-template-columns: minmax(0, 1fr);
gap: 16px;
min-width: 0;
}
.controls {
display: flex;
flex-wrap: wrap;
gap: 12px;
}
p { margin: 0; }
output { overflow-wrap: anywhere; }
</style>
<div class="example">
<div class="controls">
<Checkbox bind:checked={custom}>{("通用前缀输入套件")}</Checkbox>
<Checkbox bind:checked={local}>{("本地前缀输入")}</Checkbox>
</div>
<ConfigProvider prefixCls={prefix} theme={{ cssVar: { prefix: 'input-theme' } }}>
<div class="prefix-stage">
<Input prefixCls={localPrefix} bind:value={draft} allowClear showCount
aria-label={("输入带前缀")} />
<TextArea prefixCls={localPrefix} bind:value={text} autoSize={{ minRows: 2, maxRows: 4 }}
aria-label={("带前缀的文本区域")} />
<InputPassword prefixCls={local ? 'project-password' : ''} inputPrefixCls={localPrefix}
bind:value={password} aria-label={("带前缀的密码")} />
<InputSearch prefixCls={local ? 'project-search' : ''} inputPrefixCls={localPrefix}
bind:value={query} enterButton={("寻找")} onSearch={value => searched = value}
aria-label={("使用前缀搜索")} />
<InputOTP
prefixCls={local ? 'project-otp' : ''}
length={4}
value={code}
onChange={next => code = next}
aria-label={("带前缀的 OTP")}
/>
<output
aria-label={("带前缀的搜索输入")}
>{("成立: ")}{searched}</output>
</div>
</ConfigProvider>
<output
aria-label={("CSS 前缀输入")}
>{("一般的: ")}{prefix}{(";当地的: ")}{(localPrefix
|| ("来自提供商"))}</output>
<p>
{("输入和文本区域使用相同的基本前缀。密码/搜索共享 shell prefixCls 和 inputPrefixCls 字段。 OTP 更改组、单元格和分隔符;内部输入和搜索按钮分别继承提供者的公共前缀。")}
</p>
</div>
API
输入系列的基本参数。确切的签名可在已安装包的公共类型中找到 @woodger/antd。根据这些类型检查表中的属性名称。
Input
| 属性 | 描述 | 类型 | 默认 |
|---|---|---|---|
prefixCls | 组件及其部件的完整 CSS 前缀。非空本地 prop 优先;空字符串和 undefined 继承ConfigProvider。主题的命名空间是单独设置的。 | InputProps['prefixCls'] | ant-input |
value | 字符串值;支持 bind:value。 | InputProps['value'] | "" |
defaultValue | 没有绑定的初始值。后续更改不会重新初始化该字段。 | InputProps['defaultValue'] | — |
size | small、medium 或 large。继承自 SpaceCompact 或 ConfigProvider。 | InputProps['size'] | medium |
variant | 带 outlined、filled、borderless 或 underlined。本地 variant → 最接近的 Form.variant → input.variant → 共享的 ConfigProvider.variant → outlined。 | InputProps['variant'] | outlined |
status | error 或 warning;状态也可以通过 FormItem 设置。 | InputProps['status'] | — |
prefix | 字段内值之前的文本、数字或片段。 | InputProps['prefix'] | — |
suffix | 字段内值后面的内容。 | InputProps['suffix'] | — |
allowClear | 清除按钮:boolean 或 clearIcon 和 disabled 设置。 | InputProps['allowClear'] | false |
showCount | 字符计数器或带有 formatter 的对象。 | InputProps['showCount'] | false |
count | 软最大值、计数策略、显示和可选输入修整。确切的类型是 InputCountConfig。 | InputProps['count'] | — |
maxLength | 本机硬限制;不带 count.max 指定计数器的最大值。 | InputProps['maxLength'] | — |
onValueChange | 获取输入并清除后的字符串;bind:value 现已更新。 | InputProps['onValueChange'] | — |
onChange | 与 target/currentTarget 副本和原始 nativeEvent 兼容的事件。本机 oninput 和 onchange 保持独立。 | InputProps['onChange'] | — |
onClear | 清除时在 onValueChange 和 onChange 之后调用。 | InputProps['onClear'] | — |
onPressEnter | 输入外部 IME 组合。不取代原生 onkeydown。 | InputProps['onPressEnter'] | — |
classNames | 类语义槽:来自 { props } 的对象或函数。 | InputProps['classNames'] | — |
styles | 语义槽的 CSS 字符串:来自 { props } 的对象或函数。 | InputProps['styles'] | — |
inputElement | bind:inputElement 接收本机 input,无论是否存在包装器。 | InputProps['inputElement'] | — |
nativeElement | bind:nativeElement 接收外部 root: input 或包装器。 | InputProps['nativeElement'] | — |
本国的 name, required, disabled, readonly, inputmode 和 aria-* 被传输到现场。 htmlSize 设置 size HTML 属性;视觉尺寸通过设置 size.
TextArea
多行字段有自己的 TextAreaProps,保留本机 textarea 属性并支持变体、status、clear/count 和语义样式。
| 属性 | 描述 | 类型 | 默认 |
|---|---|---|---|
prefixCls | 这部分家族的完整前缀;空字符串/undefined 选择 ConfigProvider。内部输入和按钮保留独立的命名空间。 | TextAreaProps['prefixCls'] | ant-input |
value | 带有 bind:value 的字符串,包括换行符。 | TextAreaProps['value'] | "" |
autoSize | true 或 { minRows, maxRows };高度由内容或 placeholder 测量。 | TextAreaProps['autoSize'] | false |
allowClear | 清除多行值。 | TextAreaProps['allowClear'] | false |
count | 软限制、策略和 exceedFormatter 对于输入是通用的。 | TextAreaProps['count'] | — |
onResize | 获取调整大小时测量的 width 和 height。 | TextAreaProps['onResize'] | — |
nativeElement | bind:nativeElement 始终返回 textarea。 | TextAreaProps['nativeElement'] | — |
rootElement | bind:rootElement 返回外部 textarea 或 span。 | TextAreaProps['rootElement'] | — |
Input.Password
InputPasswordProps 扩展输入:保留字符串值、事件和焦点方法。本机 type 由密码的可见性决定。
| 属性 | 描述 | 类型 | 默认 |
|---|---|---|---|
inputPrefixCls | 嵌套Input的完全独立前缀;不是从 prefixCls shell 输出。空字符串/undefined 继承公共输入 prefix。 | InputPasswordProps['inputPrefixCls'] | ant-input |
prefixCls | 这部分家族的完整前缀;空字符串/undefined 选择 ConfigProvider。内部输入和按钮保留独立的命名空间。 | InputPasswordProps['prefixCls'] | ant-input-password |
visibilityToggle | 默认为 true; false 隐藏单选按钮。具有visible和 onVisibleChange 的对象控制从外部的可见性。 | InputPasswordProps['visibilityToggle'] | true |
iconRender | 可见性函数返回单选按钮的文本或 Svelte 片段。 | InputPasswordProps['iconRender'] | — |
action | 默认 click;悬停时会更改可见性,但离开鼠标不会返回可见性。 | InputPasswordProps['action'] | click |
suffix | 可见性切换之前的附加内容。 | InputPasswordProps['suffix'] | — |
Input.Search
InputSearchProps 扩展输入。处理程序通过以下方式接收按钮 context.getButtonProps() 或者 context.getNativeProps(); 搜索示例 显示连接。
| 属性 | 描述 | 类型 | 默认 |
|---|---|---|---|
inputPrefixCls | 嵌套Input的完全独立前缀;不是从 prefixCls shell 输出。空字符串/undefined 继承公共输入 prefix。 | InputSearchProps['inputPrefixCls'] | ant-input |
prefixCls | 这部分家族的完整前缀;空字符串/undefined 选择 ConfigProvider。内部输入和按钮保留独立的命名空间。 | InputSearchProps['prefixCls'] | ant-input-search |
enterButton | false 是常规图标,true 是主按钮,line 是签名。代码片段获取上下文 getButtonProps/getNativeProps。 | InputSearchProps['enterButton'] | false |
searchIcon | boolean enterButton 的图标; null 或 false 隐藏它。 | InputSearchProps['searchIcon'] | — |
loading | 阻止按钮并按 Enter 搜索,保持清洁的可用性。 | InputSearchProps['loading'] | false |
onSearch | 获取值、事件和 info.source:input 或 clear。 Cleanup 在 onChange 之前调用 onSearch。 | InputSearchProps['onSearch'] | — |
classNames | 输入和按钮插槽:类字符串或 button 插槽对象。 | InputSearchProps['classNames'] | — |
styles | 输入和按钮插槽;对于 button,您可以传递 CSS 字符串 root、icon 和 content 的对象。 | InputSearchProps['styles'] | — |
Input.OTP
| 属性 | 描述 | 类型 | 默认 |
|---|---|---|---|
prefixCls | 这部分家族的完整前缀;空字符串/undefined 选择 ConfigProvider。内部输入和按钮保留独立的命名空间。 | InputOTPProps['prefixCls'] | ant-otp |
length | 单元格数量,默认 6。 | InputOTPProps['length'] | 6 |
defaultValue | 初始值也用于SSR;formatter 被应用一次。 | InputOTPProps['defaultValue'] | "" |
value | 更改 prop 可在安装后同步单元。不提供 bind:value;使用 onInput 和 onChange。 | InputOTPProps['value'] | — |
formatter | 转换整个字符串,并用空格代替长度限制之前的空单元格。 | InputOTPProps['formatter'] | — |
mask | true 隐藏点字符;该行指定其自己的掩码。保留原始值。 | InputOTPProps['mask'] | false |
separator | 单元格之间的内容或索引的函数。对于丰富的 content,该函数返回片段。 | InputOTPProps['separator'] | — |
onInput | 获取每个输入上的单元格数组,包括空位置。 | InputOTPProps['onInput'] | — |
onChange | 仅在更改后的完全填充代码时调用;输入不完整不会触发回调。 | InputOTPProps['onChange'] | — |
inputMode | 原生虚拟键盘提示;本身并不限制输入数字。 | InputOTPProps['inputMode'] | — |
nativeElement | bind:nativeElement 获取组 div,而不是单个输入。 | InputOTPProps['nativeElement'] | — |
紧凑的群体
SpaceCompact 合并相邻字段,SpaceAddon 在字段之前或之后添加标题。
ConfigProvider 设置
一般尺寸和可用性通过设置 componentSize 和 componentDisabled。以下部分用于注册:
ConfigProvider.input
allowClearautoCompletevariantclassstyleclassNamesstylesConfigProvider.textArea
allowClearautoCompletevariantclassstyleclassNamesstylesConfigProvider.inputPassword
iconRendervariantclassstyleclassNamesstylesConfigProvider.inputSearch
searchIconvariantclassstyleclassNamesstylesConfigProvider.otp
variantclassstyleclassNamesstyles对于 variant,顺序是:本地prop→最近的Form.variant→组件配置→通用ConfigProvider.variant→outlined。自定义后,InputPassword、InputSearch 和 InputOTP 还使用 input.variant。嵌套的 ConfigProvider 会替换传入的整个组件对象。
聚焦方法
通过 bind:this 输入、输入密码和输入搜索可用 focus(), blur(), select() 和 setSelectionRange()。文本区域支持 focus() 和 blur(); InputOTP - 聚焦第一个单元格并移除焦点。 例子 显示光标选择和文本选择。
Semantic DOM
选择一个组件和元素以查看其在实际字段上的边框。这些名称对应于包的公共语义槽。 classNames 定义类,并且 styles — CSS 字符串。
密码槽与输入相同。搜索添加 button,TextArea使用 textarea,和 OTP - input 对于每个单元格和 separator 用于分隔符。
注册令牌
家人使用 ConfigProvider.theme.components.Input,包括文本区域和内部字段密码、搜索和 OTP。姓名由以下人员验证 ComponentTokenMap['Input'];搜索按钮还采用了按钮设计。
<script lang="ts">
import { ConfigProvider, Input, InputPassword, TextArea } from '@woodger/antd';
</script>
<style>
.example {
display: grid;
gap: 20px;
}
</style>
<ConfigProvider theme={{
cssVar: {},
components: {
Input: {
activeBorderColor: '#389e0d',
hoverBorderColor: '#73d13d',
activeShadow: '0 0 0 2px rgba(82,196,26,.15)',
paddingInline: 14
}
}
}}>
<div class="example">
<Input aria-label={("自定义输入主题")} placeholder={("指向或改变焦点")} />
<InputPassword aria-label={("自定义密码主题")} placeholder={("共享输入令牌")} />
<TextArea aria-label={("自己的主题文本区域")} rows={2} placeholder={("TextArea 具有相同的标记")} />
</div>
</ConfigProvider>
| 代币 | 目的 |
|---|---|
paddingInline | 常规边距的水平填充。 |
paddingInlineSM | 小字段的水平填充。 |
paddingInlineLG | 大字段的水平填充。 |
paddingBlock | 常规字段的垂直填充。 |
paddingBlockSM | 小字段的垂直填充。 |
paddingBlockLG | 大字段的垂直边距。 |
addonBg | 外部添加的背景。 |
hoverBorderColor | 框架悬停颜色。 |
activeBorderColor | 对焦框颜色。 |
activeShadow | 焦点阴影。 |
errorActiveShadow | 将阴影聚焦在错误上。 |
warningActiveShadow | 将阴影集中在警报上。 |
hoverBg | 悬停时的背景。 |
activeBg | 焦点对准时的背景。 |
inputFontSize | 常规字段字体大小。 |
inputFontSizeLG | 大边距字体大小。 |
inputFontSizeSM | 小边距字体大小。 |
当前主题的常用标记
通过获得的值 theme.useToken() 并与 Ant Design 标头中的设置一起更新。
| 代币 | 目的 | 当前值 |
|---|---|---|
colorPrimary | 交互状态的主要颜色。 | #2f54eb |
colorBgContainer | 常规字段的背景。 | #ffffff |
colorBorder | 镜框颜色。 | #d9d9d9 |
colorError | 错误颜色。 | #ff4d4f |
controlHeight | 正常场的高度。 | 32 |
controlHeightSM | 小田地的高度。 | 24 |
controlHeightLG | 大田地的高度。 | 40 |
fontSize | 基本文字大小。 | 14 |
FAQ
如何将字段与状态、表单关联起来?
对于独立文本字段,请使用 bind:value。在Form中,从form.values传递值,并通过onValueChange更新它。 表格示例显示验证、发送、重置,不显示密码。
onChange 和 oninput 有什么区别?
oninput 是原生 Svelte 事件。 onChange 在输入和清理时被调用,并包含 target/currentTarget 中的值的副本;原始事件在 nativeEvent 中。对于没有事件对象的字符串,请使用 onValueChange。
为什么 count.max 不限制长度?
它显示保存输入时的超调。硬限制由 maxLength 设置。对于计数规则,请指定 strategy,如有必要,请指定 exceedFormatter。该 formatter 不会修剪外部值。
为什么托管 TextArea 可以包含超过 maxLength 的内容?
内容定义了应用程序模型。外部条目不应不知不觉地改变其含义。如果数据需要限制,请在录入和提交时勾选;单个 HTML 属性不能替代模型规则。
如何将表情符号算作一个字符?
默认长度为 UTF-16。 您自己的柜台 使用 Intl.Segmenter 来计算字素,包括复合表情符号。该规则与 OTP 单元的数量不同。
为什么添加 prefix、suffix 或计数器时焦点会发生变化?
shell 的出现或消失会改变 DOM。保留带有包装元素的持久片段以进行动态添加;改变其内部的各个部件。要明确聚焦,请通过 bind:this 使用公共方法。
如何管理 Input.OTP 以及SSR期间显示什么?
InputOTP 没有 bind:value。 onInput 报告具有空位置的单元格数组,onChange 仅报告完全填充的更改代码。 Cell存储1个UTF-16单元;该字段接受文本,而不仅仅是数字。 value挂载后同步; defaultValue 用于初始服务器显示。代码审查属于应用程序。
为什么 TextArea 不降低其高度?
对于 autoSize,会考虑文本、placeholder、宽度和 minRows/maxRows。不要同时设置固定高度。 自动高度示例 允许您检查字段的增加和减少。
如何格式化复合字段的部分内容?
classNames 定义语义槽类,styles 定义 CSS 字符串。搜索添加了 button 槽、OTP - input 和 separator。 Semantic DOM 显示实际边界;总体尺寸和变型通过 ConfigProvider 指定。