<div class="demo-row"> <div class="icen-segmented icen-control" role="radiogroup" aria-label="统计周期"> <button class="icen-segmented-item icen-motion icen-pressable" type="button" role="radio" aria-checked="false">日</button> <button class="icen-segmented-item icen-motion icen-pressable is-active" type="button" role="radio" aria-checked="true">周</button> <button class="icen-segmented-item icen-motion icen-pressable" type="button" role="radio" aria-checked="false">月</button> </div> <div class="icen-segmented icen-segmented--sm icen-control" role="radiogroup" aria-label="小号分段"> <button class="icen-segmented-item icen-motion icen-pressable is-active" type="button" role="radio" aria-checked="true">列表</button> <button class="icen-segmented-item icen-motion icen-pressable" type="button" role="radio" aria-checked="false">网格</button> </div></div><div class="demo-row" style="margin-top:16px"> <div class="icen-toggle-group icen-control" role="group" aria-label="对齐方式"> <button class="icen-toggle-item icen-motion icen-pressable is-active" type="button">左对齐</button> <button class="icen-toggle-item icen-motion icen-pressable" type="button">居中</button> <button class="icen-toggle-item icen-motion icen-pressable" type="button">右对齐</button> </div></div><div class="demo-row" style="margin-top:16px"> <div class="icen-stepper icen-control"> <button class="icen-stepper-btn icen-pressable" type="button" data-icen-step="-1" aria-label="减少"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" width="16" height="16" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round" focusable="false" class="icen-symbol" aria-hidden="true"><path d="M4 10h12"></path></svg></button> <input class="icen-stepper-input" type="number" value="3" min="0" max="10" step="1" aria-label="数量" /> <button class="icen-stepper-btn icen-pressable" type="button" data-icen-step="1" aria-label="增加"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" width="16" height="16" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round" focusable="false" class="icen-symbol" aria-hidden="true"><path d="M10 4v12M4 10h12"></path></svg></button> </div> <div class="icen-otp" role="group" aria-label="短信验证码"> <input class="icen-otp-cell" maxlength="1" inputmode="numeric" aria-label="第 1 位" /> <input class="icen-otp-cell" maxlength="1" inputmode="numeric" aria-label="第 2 位" /> <input class="icen-otp-cell" maxlength="1" inputmode="numeric" aria-label="第 3 位" /> <input class="icen-otp-cell" maxlength="1" inputmode="numeric" aria-label="第 4 位" /> </div></div>
特性
- 分段:
icen-segmented(--sm小号)是完整 radiogroup:方向键移动并选中(RTL 镜像)、Home/End 跳首尾,派icen:segmented-change - 切换组:
icen-toggle-group+icen-toggle-item纯 CSS 契约,选中态由使用方切换 - 步进器:
icen-stepper±step 并按 min/max 钳制,派icen:stepper-change - 验证码:
icen-otp-cell输入自动跳格、粘贴分摊到各格
与工具条(toolbar)分工:segmented 是紧贴的互斥单选;toolbar 是宽松容器。
接口
- 导出
-
initInputinitSegmentedinitStepper共享行为模块,清单含族内导出 - 事件
-
icen:check-changeicen:page-changeicen:rating-changeicen:segmented-changeicen:sort-changeicen:step-changeicen:stepper-changeicen:switch-changedetail 契约见IcenEventMap;共享行为模块,清单含族内事件 - 类型
dist/behaviors/controls.d.mts(随 npm 包发布,含全部 JSDoc 约束)
安装
bun add @icen.ai/ui pnpm add @icen.ai/ui npm install @icen.ai/ui yarn add @icen.ai/ui 一行引入该组件(CSS 与 behavior 自动带上,无需关心内部 CSS 文件名):
import '@icen.ai/ui/kit/segmented';import { initInput, initSegmented, initStepper } from '@icen.ai/ui';initInput();initSegmented();initStepper();
也可以用脚手架直接打印引入行:
bunx --bun @icen.ai/ui add segmented
Astro 项目注意:kit 入口里的 CSS import 在页面 <script> 里会被 Astro 的 client bundle 摇掉——请改在布局 frontmatter 里引 CSS(import '@icen.ai/ui/components/segmented.css';),JS 行为仍可走 kit。Vite SPA / webpack 项目无此问题。
用法
import { initSegmented, initStepper, initInput } from '@icen.ai/ui/kit/segmented';initSegmented(); // 点击 + ←/→/Home/End(RTL 镜像),选中跟随焦点,派 icen:segmented-changeinitStepper(); // ±step 并 clamp 到 min/max,派 icen:stepper-changeinitInput(); // OTP 单元格的输入跳格 / 粘贴分摊// toggle-group 为纯 CSS 契约,选中态由使用方切换