Input
支持受控与非受控值、输入法组合事件及无障碍命名的 Shadcn 风格单行文本输入框。
用于表单与用户数据录入的单行文本输入框,基于 Base Input 提供受控/非受控值管理、规范化的输入法(IME)事件、禁用与只读状态以及无障碍命名属性。本页示例采用 Shadcn Rhea 风格。
Web Components React Vue Vue 2
正在加载交互示例… 正文和源码已可阅读,交互控件就绪后会显示在这里。
<div
id="shadcn-input-demo"
class="flex w-full max-w-sm flex-col gap-5"
>
<div class="flex flex-col gap-2">
<div class="text-sm font-medium">API Key</div>
<proto-ui-shadcn-input-root></proto-ui-shadcn-input-root>
<div class="text-xs text-muted-foreground">
Your API key is encrypted and stored securely.
</div>
</div>
<div class="flex flex-col gap-2">
<div class="text-sm font-medium">Email</div>
<proto-ui-shadcn-input-root></proto-ui-shadcn-input-root>
</div>
<div class="flex flex-col gap-2">
<div class="text-sm font-medium">Workspace URL</div>
<proto-ui-shadcn-input-root></proto-ui-shadcn-input-root>
</div>
</div>
<script type="module">
import { setElementProps } from '@proto.ui/adapter-web-component';
const props = [
{
styleProfile: 'rhea',
defaultValue: 'sk-live-9f8e7d6c',
placeholder: 'sk-...',
ariaLabel: 'API Key',
autoComplete: 'off',
},
{
styleProfile: 'rhea',
defaultValue: 'jordan@example.com',
disabled: true,
inputMode: 'email',
ariaLabel: 'Email',
},
{
styleProfile: 'rhea',
defaultValue: 'https://app.example.com/acme',
readOnly: true,
inputMode: 'url',
ariaLabel: 'Workspace URL',
},
];
document
.querySelectorAll(
'#shadcn-input-demo proto-ui-shadcn-input-root'
)
.forEach((el, index) => {
setElementProps(el, props[index]);
});
</script>此组件由工作区源码提供。请在应用中安装同一工作区提交构建的 CLI、Shadcn 原型包与目标适配器,用该 CLI 初始化项目,再运行本地命令生成组件封装。
npm exec -- proto-ui add wc shadcn-input
把 wc 替换为 react、vue 或 vue2,即可生成对应框架的本地组件封装。也可以直接从 @proto.ui/prototypes-shadcn/input 导入 shadcnInputRoot。
import { ShadcnInputRoot } from '@/components/ui/shadcn-input';
export function Example() { return <ShadcnInputRoot placeholder="Enter text" ariaLabel="Text input" />;}- 外观预设:通过外层 Style Scope 或
styleProfile切换预设;在 Shadcn Rhea 风格下采用h-8(32px)、radius-2xl、bg-input/50(暗色模式bg-input/30)与border-ring ring-3 ring-ring/30焦点环,在视口宽度>= 768px时自动切换为小号字号。 - 表单校验与标签:独立的
Input专注于单行文本编辑;如需标签点击聚焦、帮助说明与校验错误状态,请搭配 Field 使用。 - 无障碍与控制方法:
Root暴露focusSelf()、blurSelf()与resetValue(value?: string),并支持ariaLabel、labelledBy与describedBy。
最简独立文本输入框,通过 placeholder 提供输入提示。
Web Components React Vue Vue 2
正在加载交互示例… 正文和源码已可阅读,交互控件就绪后会显示在这里。
<proto-ui-shadcn-input-root
id="shadcn-input-basic"
></proto-ui-shadcn-input-root>
<script type="module">
import { setElementProps } from '@proto.ui/adapter-web-component';
setElementProps(document.querySelector('#shadcn-input-basic'), {
styleProfile: 'rhea',
placeholder: 'Enter text',
ariaLabel: 'Text input',
});
</script>搭配 Field、FieldLabel、FieldControl 与 FieldDescription 组合完整的表单字段。
Web Components React Vue Vue 2
正在加载交互示例… 正文和源码已可阅读,交互控件就绪后会显示在这里。
<proto-ui-shadcn-field-root id="shadcn-input-field">
<proto-ui-shadcn-field-label>
Username
</proto-ui-shadcn-field-label>
<proto-ui-shadcn-field-control></proto-ui-shadcn-field-control>
<proto-ui-shadcn-field-description>
Choose a unique username for your account.
</proto-ui-shadcn-field-description>
</proto-ui-shadcn-field-root>
<script type="module">
import { setElementProps } from '@proto.ui/adapter-web-component';
setElementProps(
document.querySelector(
'#shadcn-input-field proto-ui-shadcn-field-control'
),
{
styleProfile: 'rhea',
placeholder: 'Enter your username',
autoComplete: 'username',
}
);
</script>设置 disabled 阻止聚焦与编辑,并同步呈现禁用底色与指针反馈。
Web Components React Vue Vue 2
正在加载交互示例… 正文和源码已可阅读,交互控件就绪后会显示在这里。
<proto-ui-shadcn-field-root id="shadcn-input-disabled">
<proto-ui-shadcn-field-label>Email</proto-ui-shadcn-field-label>
<proto-ui-shadcn-field-control></proto-ui-shadcn-field-control>
<proto-ui-shadcn-field-description>
This field is currently disabled.
</proto-ui-shadcn-field-description>
</proto-ui-shadcn-field-root>
<script type="module">
import { setElementProps } from '@proto.ui/adapter-web-component';
const root = document.querySelector('#shadcn-input-disabled');
setElementProps(root, { disabled: true });
setElementProps(
root.querySelector('proto-ui-shadcn-field-control'),
{
styleProfile: 'rhea',
placeholder: 'Email',
inputMode: 'email',
disabled: true,
}
);
</script>在 Field 中设置 invalid 与 errors,自动呈现错误边框、焦点环与无障碍错误说明。
Web Components React Vue Vue 2
正在加载交互示例… 正文和源码已可阅读,交互控件就绪后会显示在这里。
<proto-ui-shadcn-field-root id="shadcn-input-invalid">
<proto-ui-shadcn-field-label>
Invalid Input
</proto-ui-shadcn-field-label>
<proto-ui-shadcn-field-control></proto-ui-shadcn-field-control>
<proto-ui-shadcn-field-error>
This field contains validation errors.
</proto-ui-shadcn-field-error>
</proto-ui-shadcn-field-root>
<script type="module">
import { setElementProps } from '@proto.ui/adapter-web-component';
const root = document.querySelector('#shadcn-input-invalid');
setElementProps(root, {
invalid: true,
errors: ['This field contains validation errors.'],
});
setElementProps(
root.querySelector('proto-ui-shadcn-field-control'),
{
styleProfile: 'rhea',
defaultValue: 'sk-invalid',
placeholder: 'Error',
}
);
</script>将 Input 与 Button 水平排列,并配合 inputMode="search" 与 enterKeyHint="search" 优化软键盘体验。
Web Components React Vue Vue 2
正在加载交互示例… 正文和源码已可阅读,交互控件就绪后会显示在这里。
<div class="flex w-full max-w-sm items-center gap-2">
<proto-ui-shadcn-input-root
id="shadcn-input-search"
></proto-ui-shadcn-input-root>
<proto-ui-shadcn-button id="shadcn-input-search-btn">
Search
</proto-ui-shadcn-button>
</div>
<script type="module">
import { setElementProps } from '@proto.ui/adapter-web-component';
setElementProps(document.querySelector('#shadcn-input-search'), {
styleProfile: 'rhea',
placeholder: 'Search...',
inputMode: 'search',
enterKeyHint: 'search',
ariaLabel: 'Search',
});
setElementProps(
document.querySelector('#shadcn-input-search-btn'),
{ styleProfile: 'rhea' }
);
</script>结合网格布局、Label、Field 与提交/取消按钮构建多字段表单。
Web Components React Vue Vue 2
正在加载交互示例… 正文和源码已可阅读,交互控件就绪后会显示在这里。
<div class="flex w-full max-w-sm flex-col gap-4">
<div class="grid grid-cols-2 gap-4">
<div class="flex flex-col gap-2">
<proto-ui-shadcn-label-root>
First Name
</proto-ui-shadcn-label-root>
<proto-ui-shadcn-input-root
id="form-first-name"
></proto-ui-shadcn-input-root>
</div>
<div class="flex flex-col gap-2">
<proto-ui-shadcn-label-root>
Last Name
</proto-ui-shadcn-label-root>
<proto-ui-shadcn-input-root
id="form-last-name"
></proto-ui-shadcn-input-root>
</div>
</div>
<proto-ui-shadcn-field-root id="form-email-field">
<proto-ui-shadcn-field-label>Email</proto-ui-shadcn-field-label>
<proto-ui-shadcn-field-control></proto-ui-shadcn-field-control>
<proto-ui-shadcn-field-description>
We will send updates to this address.
</proto-ui-shadcn-field-description>
</proto-ui-shadcn-field-root>
<div class="flex items-center gap-2">
<proto-ui-shadcn-button id="form-cancel">
Cancel
</proto-ui-shadcn-button>
<proto-ui-shadcn-button id="form-submit">
Submit
</proto-ui-shadcn-button>
</div>
</div>
<script type="module">
import { setElementProps } from '@proto.ui/adapter-web-component';
setElementProps(document.querySelector('#form-first-name'), {
styleProfile: 'rhea',
placeholder: 'Jordan',
name: 'firstName',
required: true,
});
setElementProps(document.querySelector('#form-last-name'), {
styleProfile: 'rhea',
placeholder: 'Lee',
name: 'lastName',
required: true,
});
setElementProps(document.querySelector('#form-email-field'), {
required: true,
});
setElementProps(
document.querySelector(
'#form-email-field proto-ui-shadcn-field-control'
),
{
styleProfile: 'rhea',
placeholder: 'name@example.com',
inputMode: 'email',
required: true,
}
);
setElementProps(document.querySelector('#form-cancel'), {
styleProfile: 'rhea',
variant: 'outline',
});
setElementProps(document.querySelector('#form-submit'), {
styleProfile: 'rhea',
});
</script>Root 属性
Section titled “Root 属性”| 属性 | 类型 | 默认值 |
|---|---|---|
value | string | undefined |
defaultValue | string | '' |
disabled | boolean | false |
readOnly | boolean | false |
placeholder | string | '' |
required | boolean | false |
name | string | '' |
autoComplete | string | '' |
minLength | number | -1 |
maxLength | number | -1 |
inputMode | 'none' 'text' 'tel' 'url' 'email' 'numeric' 'decimal' 'search' | undefined |
enterKeyHint | 'enter' 'done' 'go' 'next' 'previous' 'search' 'send' | undefined |
ariaLabel | string | '' |
labelledBy | string | '' |
describedBy | string | '' |
styleProfile | 'vega' 'nova' 'maia' 'lyra' 'mira' 'luma' 'sera' 'rhea' 'new-york-v4' | 'nova' |
暴露状态、方法与事件
Section titled “暴露状态、方法与事件”- 状态:
value、disabled、readOnly、focused、focusVisible、composing - 方法:
focusSelf(options?)、blurSelf()、resetValue(value?: string): boolean - 事件:
valueChange、change、compositionStart、compositionUpdate、compositionEnd