import { tag, WeElement, h, extractClass } from 'omi' import * as css from './index.scss' //@ts-ignore import '../theme.ts' interface Props { total: number half: boolean value: number } interface Data { } @tag('m-rate') export default class Rate extends WeElement{ static css = css static defaultProps = { value: 0 } static propTypes = { value: Number, half: Boolean, total: Number, color: String } _current = 0 _rect = null _hover = false onSelect = (evt) => { this._rect = this.base.getBoundingClientRect() const dx = evt.pageX - this._rect.left const value = dx / this._rect.width * this.props.total const intValue = Math.floor(value) let v = intValue + (value - intValue > 0.5 ? 1 : 0.5) if(!this.props.half) v = Math.ceil(v) this.props.value = v //@ts-ignore this.fire('selected', v) this.update() } onMouseMove = (evt) => { this._rect = this.base.getBoundingClientRect() const dx = evt.pageX - this._rect.left this._current = dx / this._rect.width * this.props.total this.update() } base:HTMLElement installed(){ this.base = this.shadowRoot.querySelector('ul') //update δΈε†δ»Ž attr 取 prop this.normalizedNodeName = 'm-rate' } onMouseEnter = () => { this._hover = true } onMouseLeave = () => { this._hover = false this.update() } _getClass = (i, current) => { if (i < current) { if (this.props.half && current - i <= 0.5) { return '_star _star-half ' } return '_star' } else { return '_star _star-empty ' } } render(props) { return ( ) } }