import {computed, designComponent, Fragment, getComponentCls, reactive, useClasses, useMounted, useRefs, useStyles} from "@peryl/react-compose"; import {unit} from "@peryl/utils/unit"; import './slider.scss'; import {useEdit} from "../../uses/useEdit"; import {ThemeStatus, useStyle} from "../../uses/useStyle"; import {createSliderHooks, createSliderUtils, iSliderPublicParams, SliderEmitsOption, SliderPropsOption} from "./slider.utils"; import {useWatchAutoClear} from "../../utils/watchEffectAutoClear"; import {useSliderRange} from "./useSlider.range"; import {useSliderSingle} from "./useSlider.single"; import {doNothing} from "@peryl/utils/doNothing"; export const Slider = designComponent({ name: 'slider', props: SliderPropsOption, inheritPropsType: HTMLDivElement, emits: SliderEmitsOption, setup({ props, event }) { const { refs, onRef } = useRefs({ el: HTMLDivElement, }); const hooks = createSliderHooks(); const { editComputed } = useEdit(); const { styleComputed } = useStyle({ status: ThemeStatus.primary }); /** * 总长度 * @author 韦胜健 * @date 2022.12.10 11:59 */ const totalLength = ((): { value: number } => { const isMounted = useMounted(); const innerState = reactive({ value: null as null | number }); return computed(() => { if (!isMounted.value) return 0; if (innerState.value == null) innerState.value = refs.el![!!props.vertical ? 'offsetHeight' : 'offsetWidth']; return innerState.value; }); })(); /** * 内部工具函数 * @author 韦胜健 * @date 2022.12.10 12:50 */ const utils = createSliderUtils({ props, hooks, }); /** * 公共组合参数 * @author 韦胜健 * @date 2022.12.10 12:50 */ const params: iSliderPublicParams = { props, event, hooks, totalLength, utils, editComputed, }; /** * 根节点类名 * @author 韦胜健 * @date 2022.12.10 12:02 */ const classes = useClasses(() => [ getComponentCls('slider'), `slider-status-${styleComputed.value.status}`, props.vertical ? 'slider-vertical' : 'slider-horizontal', { 'slider-disabled': editComputed.value.disabled }, ...hooks.onClasses.exec([]), ]); /** * 根节点行内样式 * @author 韦胜健 * @date 2022.12.10 12:02 */ const styles = useStyles(() => ({ [props.vertical ? 'height' : 'width']: !!props.full ? '100%' : unit(props.width), [props.vertical ? 'width' : 'height']: unit(props.size), })); /** * 选择模式 * @author 韦胜健 * @date 2022.12.10 15:14 */ useWatchAutoClear(() => props.range, range => range ? useSliderRange(params) : useSliderSingle(params)); /** * 分块 * @author 韦胜健 * @date 2022.12.10 15:14 */ useWatchAutoClear(() => props.step, step => { return step == null ? doNothing : hooks.onCheckValue.use((value) => { let length = value / props.total * totalLength.value; const stepLength = totalLength.value / step; const divisor = Number((length / stepLength).toFixed(0)) + 1; if (length > stepLength * divisor) length = stepLength * divisor; else length = stepLength * (divisor - 1); return length / totalLength.value * props.total; }); }); return { refer: { refs }, render: () => (