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

[#780][3.0] slider 컴포넌트 step, mark 관련 반응형 로직 수정 #781

Merged
merged 1 commit into from
Mar 11, 2021
Merged
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
115 changes: 58 additions & 57 deletions src/components/slider/uses.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,56 @@ export const useModel = () => {
const { props, emit } = getCurrentInstance();
const currentValue = ref(props.modelValue);

const setStepList = () => {
if (!props.showStep) {
return [];
}
// step은 이동 단위 기준으로, 입력값에 따라 클릭 및 드레그 시 이동 단위를 나타냄
const sliderRange = props.max - props.min;
const stepCount = sliderRange / props.step;
const stepWidth = convertToPercent(props.step, sliderRange);
const result = [];
for (let ix = 1; ix < stepCount; ix++) {
result.push(ix * stepWidth);
}
return result;
};

const setMarkList = () => {
if (!props.mark || !Object.keys(props.mark).length) {
return [];
}
const getResultList = (obj, type = 'value') => {
if (!obj) {
return [];
}
const style = props.mark.style && (typeof props.mark.style === 'object') ? props.mark.style : {};
kdeun1 marked this conversation as resolved.
Show resolved Hide resolved
const keyList = Object.keys(obj);
const resultList = [];
let markVal;
for (let ix = 0; ix < keyList.length; ix++) {
markVal = +keyList[ix];
if (!isNaN(markVal)) {
resultList.push({
posX: type === 'value' ? convertToPercent(markVal, props.max - props.min) : markVal,
label: obj[markVal],
style,
});
}
}
return resultList;
};

const hasDetailOption = props.mark.value || props.mark.percent;
if (!hasDetailOption) {
return [...getResultList(props.mark)];
}
return [
...getResultList(props.mark.value),
...getResultList(props.mark.percent, 'percent'),
];
};

const state = reactive({
isInit: false,
dragging: false,
Expand All @@ -15,12 +65,12 @@ export const useModel = () => {
rightValue: null,
});
const slider = reactive({
valueRange: null,
stepList: [],
markList: [],
valueRange: 0,
stepList: computed(setStepList),
markList: computed(setMarkList),
offset: {
left: null,
width: null,
left: 0,
width: 0,
},
});
const sliderLine = ref();
Expand Down Expand Up @@ -96,6 +146,9 @@ export const useModel = () => {
currentValue.value = curr;
}
});
watch(() => props.step, () => {
setSliderValue(currentValue.value);
});

return {
currentValue,
Expand Down Expand Up @@ -330,60 +383,8 @@ export const useInit = (params) => {
state.isInit = true;
};

const initStepList = () => {
if (!props.showStep) {
return;
}
// step은 이동 단위 기준으로, 입력값에 따라 클릭 및 드레그 시 이동 단위를 나타냄
const stepCount = slider.valueRange / props.step;
const stepWidth = convertToPercent(props.step, slider.valueRange);
const result = [];
for (let ix = 1; ix < stepCount; ix++) {
result.push(ix * stepWidth);
}
slider.stepList = result;
};

const initMarkList = () => {
if (!props.mark || !Object.keys(props.mark).length) {
return;
}
const getResultList = (obj, type = 'value') => {
if (!obj) {
return [];
}
const style = props.mark.style && (typeof props.mark.style === 'object') ? props.mark.style : {};
const keyList = Object.keys(obj);
const resultList = [];
let markVal;
for (let ix = 0; ix < keyList.length; ix++) {
markVal = +keyList[ix];
if (!isNaN(markVal)) {
resultList.push({
posX: type === 'value' ? convertToPercent(markVal, slider.valueRange) : markVal,
label: obj[markVal],
style,
});
}
}
return resultList;
};

const hasListOption = props.mark.value || props.mark.percent;
if (!hasListOption) {
slider.markList = getResultList(props.mark);
} else {
slider.markList = [
...getResultList(props.mark.value),
...getResultList(props.mark.percent, 'percent'),
];
}
};

onMounted(() => {
validateProps();
initValue();
initStepList();
initMarkList();
});
};