import { JsxChildren } from 'dom-renderer'; import { observable } from 'mobx'; import { attribute, component, formField, observer, WebCellProps, WebField } from 'web-cell'; export interface RangeInputProps extends WebCellProps { icon?: JsxChildren | ((itemValue: number) => JsxChildren); } export interface RangeInput extends WebField {} @component({ tagName: 'range-input', mode: 'open' }) @formField @observer export class RangeInput extends HTMLElement implements WebField { @attribute @observable accessor type = 'range'; @attribute @observable accessor min: number | undefined; @attribute @observable accessor max: number | undefined; @attribute @observable accessor step = 1; @observable accessor icon: RangeInputProps['icon'] | undefined; connectedCallback() { this.classList.add('d-inline-block', 'position-relative'); } handleChange = ({ currentTarget }: Event) => { this.value = (currentTarget as HTMLInputElement).value; this.emit('change'); }; renderItem(index: number) { const { icon, step, value } = this; const fullValue = step * index; const itemValue = Math.max(Math.min(+value - fullValue, step), 0); return (
  • {typeof icon === 'function' ? icon(itemValue) : icon}
  • ); } render() { const { icon, min, max = icon ? 5 : 100, value = min || 0 } = this; return ( <>
      {Array.from({ length: max }, (_, index) => this.renderItem(index) )}
    ); } }