iter(g145): 滑块支持刻度标注
This commit is contained in:
@@ -1,14 +1,16 @@
|
||||
import { effect, h } from '@ui-arch/frame';
|
||||
import type { SliderProps } from './types.js';
|
||||
|
||||
/*** 滑块:原生 range 承载交互与键盘,value 信号双向绑定;轨道配色走 accent-color 令牌 ***/
|
||||
/*** 滑块:原生 range 承载交互与键盘,value 信号双向绑定;marks 在轨道下方标出刻度文案 ***/
|
||||
export function slider(props: SliderProps): HTMLElement {
|
||||
const value = props.value; // 必需信号:组件不持有数值,一律委托调用方
|
||||
const min = props.min ?? 0; // 与 input 的缺省保持一致,供刻度换算百分比
|
||||
const max = props.max ?? 100;
|
||||
const input = h('input', {
|
||||
type: 'range',
|
||||
class: 'ua-slider-input',
|
||||
min: props.min ?? 0,
|
||||
max: props.max ?? 100,
|
||||
min,
|
||||
max,
|
||||
step: props.step ?? 1,
|
||||
disabled: props.disabled, // 布尔或信号禁用,h() 直接透传
|
||||
}) as HTMLInputElement;
|
||||
@@ -23,10 +25,19 @@ export function slider(props: SliderProps): HTMLElement {
|
||||
if (input.value !== next) input.value = next; // 外部赋值同步滑块位置(等值不重写,避免打断拖动)
|
||||
});
|
||||
|
||||
// 刻度:按值算百分比定位,纯装饰不进读屏(数值本身由原生 input 播报)
|
||||
const marks = props.marks?.length
|
||||
? h('div', { class: 'ua-slider-marks', 'aria-hidden': 'true' },
|
||||
...props.marks.map((mark) => h('span', {
|
||||
class: 'ua-slider-mark',
|
||||
style: `left:${((mark.value - min) / (max - min || 1)) * 100}%`, // 分母兜底,避免 min=max 时除零
|
||||
}, mark.label ?? String(mark.value))))
|
||||
: null;
|
||||
|
||||
return h(
|
||||
'div',
|
||||
{ class: 'ua-slider' },
|
||||
input,
|
||||
h('div', { class: 'ua-slider-track' }, input, marks), // 轨道包一层:刻度只跟滑块对齐,不影响右侧数值
|
||||
props.showValue === false ? null : h('span', { class: 'ua-slider-value' }, () => String(value())),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -181,6 +181,7 @@ export interface SliderProps {
|
||||
step?: number; // 步长;缺省 1
|
||||
disabled?: boolean | Signal<boolean>; // 禁用态:布尔直接置位,传信号则动态同步
|
||||
showValue?: boolean; // 是否右侧显示当前值;缺省 true
|
||||
marks?: { value: number; label?: string }[]; // 刻度标注:按值定位在轨道下方,label 缺省显示数值;纯装饰不进读屏
|
||||
onChange?: (value: number) => void; // 拖动回调
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user