Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(slider): 修复Slider部分属性丢失响应性问题 #683

Merged
merged 2 commits into from
May 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 21 additions & 18 deletions src/slider/hooks/useSliderInput.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import { computed, ComputedRef } from 'vue';
import { computed, Ref } from 'vue';
import { TdSliderProps } from '../type';
import InputNumber, { InputNumberProps } from '../../input-number';

interface useSliderInputProps {
inputNumberProps: boolean | TdSliderProps['inputNumberProps'];
max: number;
min: number;
step: number;
prefixName: string;
vertical: boolean;
disabled: boolean;
}

/**
* 聚合管理inputNumber渲染逻辑
*/
export const useSliderInput = (
inputNumberProps: boolean | TdSliderProps['inputNumberProps'],
max: number,
min: number,
step: number,
prefixName: string,
isVertical: boolean,
disabled: ComputedRef<boolean>,
) => {
const name = prefixName;
export const useSliderInput = (config: Ref<useSliderInputProps>) => {
const name = config.value.prefixName;

/** 根据传入属性缓存计算inputNumber props */
const sliderInputState = computed(() => {
Expand All @@ -24,8 +26,9 @@ export const useSliderInput = (
inputPlaceholder: '',
inputTheme: 'column' as InputNumberProps['theme'],
};
if (typeof inputNumberProps !== 'boolean') {
const inputNumbeConfig = inputNumberProps as TdSliderProps['inputNumberProps'];
const inputProps = config.value;
if (typeof inputProps.inputNumberProps !== 'boolean') {
const inputNumbeConfig = inputProps.inputNumberProps as TdSliderProps['inputNumberProps'];
const inputDecimalPlaces = inputNumbeConfig.decimalPlaces;
const inputFormat = inputNumbeConfig.format;
const inputPlaceholder = inputNumbeConfig.placeholder;
Expand All @@ -50,7 +53,7 @@ export const useSliderInput = (
return [
`${name}__input`,
{
'is-vertical': isVertical,
'is-vertical': config.value.vertical,
},
];
});
Expand All @@ -60,11 +63,11 @@ export const useSliderInput = (
<InputNumber
class={sliderNumberClass.value}
value={val}
step={step}
step={config.value.step}
onChange={changeFn}
disabled={disabled.value}
min={min}
max={max}
disabled={config.value.disabled}
min={config.value.min}
max={config.value.max}
decimalPlaces={sliderInputState.value.inputDecimalPlaces}
format={sliderInputState.value.inputFormat}
placeholder={sliderInputState.value.inputPlaceholder}
Expand Down
41 changes: 18 additions & 23 deletions src/slider/hooks/useSliderMark.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { computed, VNode } from 'vue';
import { computed, VNode, Ref } from 'vue';
import cloneDeep from 'lodash/cloneDeep';
import { SliderMarks } from '../type';
import { TNode } from '../../common';
Expand All @@ -22,27 +22,22 @@ interface useSliderMarkProps {
/**
* 聚合管理刻度值渲染逻辑
*/
export const useSliderMark = (
max: number,
min: number,
marks: number[] | SliderMarks,
isVertical: boolean,
prefixName: string,
) => {
const name = prefixName;
export const useSliderMark = (config: Ref<useSliderMarkProps>) => {
const name = config.value.prefixName;
const markList = computed(() => {
if (!marks) {
const markProps = config.value;
if (!markProps.marks) {
return [];
}
const legalMarks: Array<MarkItem> = [];
if (Array.isArray(marks)) {
const marksList = cloneDeep(marks).sort((a, b) => a - b);
const maxLimit = Math.max(...marksList, max);
const minLimit = Math.min(...marksList, min);
if (minLimit < min) {
if (Array.isArray(markProps.marks)) {
const marksList = cloneDeep(markProps.marks).sort((a, b) => a - b);
const maxLimit = Math.max(...marksList, markProps.max);
const minLimit = Math.min(...marksList, markProps.min);
if (minLimit < markProps.min) {
log.errorOnce('TSlider', 'marks min value should >= props min');
}
if (maxLimit > max) {
if (maxLimit > markProps.max) {
log.errorOnce('TSlider', 'marks max value should <= props max');
}
marksList.forEach((item) => {
Expand All @@ -53,15 +48,15 @@ export const useSliderMark = (
});
});
} else {
Object.keys(marks)
Object.keys(markProps.marks)
.map(parseFloat)
.sort((a, b) => a - b)
.filter((point) => point <= max && point >= min)
.filter((point) => point <= markProps.max && point >= markProps.min)
.forEach((point) => {
const item: MarkItem = {
point,
position: ((point - min) * 100) / (max - min),
mark: marks[point],
position: ((point - markProps.min) * 100) / (markProps.max - markProps.min),
mark: markProps.marks[point],
};
legalMarks.push(item);
});
Expand All @@ -77,7 +72,7 @@ export const useSliderMark = (
{markList.value.map((item, index) => (
<div
class={`${name}__stop ${name}__mark-stop`}
style={getStopStyle(item.position, isVertical)}
style={getStopStyle(item.position, config.value.vertical)}
key={index}
/>
))}
Expand All @@ -88,8 +83,8 @@ export const useSliderMark = (
mark={item.mark}
point={item.point}
key={key}
style={getStopStyle(item.position, isVertical)}
on-change-value={onChangeFn}
style={getStopStyle(item.position, config.value.vertical)}
onClickMarkPoint={onChangeFn}
/>
))}
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/slider/slider-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export default defineComponent({
value = Number(parseFloat(`${value}`).toFixed(parentProps.precision.value));
ctx.emit('input', value);
nextTick(() => {
popupRef.value && (popupRef.value as ComponentPublicInstance).updatePopper();
popupRef.value && (popupRef.value as ComponentPublicInstance)?.updatePopper?.();
});
};

Expand Down
9 changes: 6 additions & 3 deletions src/slider/slider-mark.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ export default defineComponent({
point: {
type: Number,
},
onClickMarkPoint: {
type: Function,
default: () => {},
},
},
emits: ['change-value'],
setup(props, ctx) {
setup(props) {
const COMPONENT_NAME = usePrefixClass('slider__mark');
const changeValue = (e: MouseEvent) => {
e.stopPropagation();
ctx.emit('change-value', props.point);
props?.onClickMarkPoint?.(props.point);
};

return () => (
Expand Down
28 changes: 18 additions & 10 deletions src/slider/slider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -312,17 +312,25 @@ export default defineComponent({
});

/** -------------------------- 渲染相关逻辑 start -------------------------- */
const renderMask = useSliderMark(props.max, props.min, props.marks, vertical.value, COMPONENT_NAME.value);
const markConfig = computed(() => ({
max: props.max,
min: props.min,
marks: props.marks,
vertical: vertical.value,
prefixName: COMPONENT_NAME.value,
}));
const renderMask = useSliderMark(markConfig);

const renderInputNumber = useSliderInput(
props.inputNumberProps,
props.max,
props.min,
props.step,
COMPONENT_NAME.value,
vertical.value,
disabled,
);
const inputConfig = computed(() => ({
max: props.max,
min: props.min,
inputNumberProps: props.inputNumberProps,
step: props.step,
prefixName: COMPONENT_NAME.value,
vertical: vertical.value,
disabled: disabled.value,
}));
const renderInputNumber = useSliderInput(inputConfig);

const renderInputButton = (): VNode => {
const firstInputVal = props.range ? firstValue.value : (sliderState.prevValue as number);
Expand Down