@shopware-ag/meteor-component-library
Version:
The meteor component library is a Vue component library developed by Shopware. It is based on the [Meteor Design System](https://shopware.design/).
394 lines (393 loc) • 13.2 kB
JavaScript
import '../mt-slider.css';
import { defineComponent, resolveComponent, openBlock, createBlock, normalizeClass, withCtx, createTextVNode, toDisplayString, createCommentVNode, createElementVNode, createElementBlock, Fragment, renderList, normalizeStyle, withDirectives, vModelText, createVNode, renderSlot } from "vue";
import { M as MtBaseField } from "../mt-base-field-7a978dcf.mjs";
import MtNumberField from "./MtNumberField.js";
import { v as vTooltip } from "../tooltip.directive-a5042569.mjs";
import { _ as _export_sfc } from "../_plugin-vue_export-helper-cc2b3d55.mjs";
import "./MtInheritanceSwitch.js";
import "../mt-icon.vue_vue_type_style_index_0_lang-2cc5f73e.mjs";
import "./MtTooltip.js";
import "../floating-ui.vue-fe27ebef.mjs";
import "../floating-ui.dom-f450fda4.mjs";
import "../useIsInsideTooltip-0c3bd290.mjs";
import "../index-221bad05.mjs";
import "vue-i18n";
import "./MtFieldCopyable.js";
import "./MtHelpText.js";
import "../id-1e5b8276.mjs";
import "../useFutureFlags-2be3e179.mjs";
import "./MtTextField.js";
import "./MtFieldError.js";
import "./MtText.js";
const _sfc_main = defineComponent({
name: "MtSlider",
directives: {
tooltip: vTooltip
},
components: { MtNumberField, MtBaseField },
extends: MtBaseField,
props: {
/**
* Defines the label of the slider
*/
label: {
type: String,
required: true
},
/**
* Defines the minimum value of the number.
*/
min: {
type: Number,
required: false,
default: 0
},
/**
* Defines the maximum value of the number.
*/
max: {
type: Number,
required: false,
default: 100
},
/**
* Defines the amount of which the number is increased or decreased per step.
*/
step: {
type: Number,
required: false,
default: 1
},
/**
* The value of the slider.
* If isRange is true, this should be an array with two values.
* If isRange is false, this should be a single number.
*/
modelValue: {
type: [Number, Array],
required: false,
default: 0,
validator(value) {
if (Array.isArray(value)) {
return value.length === 2;
}
return true;
}
},
/**
* Defines if it is a range slider.
*/
isRange: {
type: Boolean,
required: false,
default: false
},
/**
* Defines the minimum distance between the two sliders.
* Should be a multiple of the step.
*/
minDistance: {
type: Number,
required: false,
default: 0
},
/**
* Defines the amount of marks on the slider.
*/
markCount: {
type: Number,
required: false,
default: 5
}
},
data() {
return {
rangeRightValue: 0,
rangeLeftValue: 0,
activeSlider: null
};
},
watch: {
modelValue: {
handler(value) {
if (this.isArray(value)) {
this.rangeLeftValue = value[0];
this.rangeRightValue = value[1];
} else {
this.rangeRightValue = value;
}
},
immediate: true
},
rangeRightValue: {
handler(value) {
if (typeof value === "string") {
value = parseFloat(value);
}
if (value >= this.max) {
value = this.max;
}
if (this.isRange) {
if (value <= this.min) {
value = this.min + this.minDistance;
}
let newLeftValue = this.rangeLeftValue;
if (value <= this.rangeLeftValue) {
newLeftValue = value - this.minDistance;
}
this.$emit("update:modelValue", [newLeftValue, value]);
} else {
if (value <= this.min) {
value = this.min;
}
this.$emit("update:modelValue", value);
}
},
immediate: true
},
rangeLeftValue: {
handler(value) {
if (typeof value === "string") {
value = parseFloat(value);
}
if (!this.isRange) {
return;
}
if (value >= this.max) {
value = this.max - this.minDistance;
}
let newRightValue = this.rangeRightValue;
if (value >= this.rangeRightValue) {
newRightValue = value + this.minDistance;
}
this.$emit("update:modelValue", [value, newRightValue]);
},
immediate: true
},
// ensure that the range values are within the min and max range
min: {
handler(value) {
if (!this.isRange && this.rangeRightValue < value) {
this.rangeRightValue = value;
} else if (this.rangeLeftValue < value) {
this.rangeLeftValue = value;
}
},
immediate: true
},
max: {
handler(value) {
if (this.rangeRightValue > value) {
this.rangeRightValue = value;
}
},
immediate: true
},
step: {
handler(value) {
if (this.rangeLeftValue % value !== 0) {
this.rangeLeftValue = Math.floor(this.rangeLeftValue / value) * value;
}
if (this.rangeRightValue % value !== 0) {
this.rangeRightValue = Math.floor(this.rangeRightValue / value) * value;
}
},
immediate: true
},
isRange: {
handler(value) {
if (!value)
return;
if (this.rangeRightValue < this.min + this.minDistance) {
this.rangeRightValue = this.min + this.minDistance;
}
if (this.rangeLeftValue > this.rangeRightValue - this.minDistance) {
this.rangeLeftValue = this.rangeRightValue - this.minDistance;
}
},
immediate: true
}
},
computed: {
stringRepresentation() {
return this.modelValue.toString();
},
styleStartPosition() {
if (!this.isRange) {
return "0%";
}
const SLIDER_PADDING = 10;
const totalLength = this.max - this.min;
const factor = (this.rangeLeftValue - this.min) / totalLength;
const percentage = factor * 100;
const left = (1 - factor * 2) * SLIDER_PADDING;
return `calc(${percentage}% + ${left}px)`;
},
styleEndPosition() {
const SLIDER_PADDING = 10;
const totalLength = this.max - this.min;
const factor = (this.max - this.rangeRightValue) / totalLength;
const percentage = factor * 100;
const right = (1 - factor * 2) * SLIDER_PADDING;
return `calc(${percentage}% + ${right}px)`;
},
markStep() {
return (this.max - this.min) / (this.markCount - 1);
},
toolTipText() {
if (!this.activeSlider)
return "";
return this.activeSlider === "left" ? this.rangeLeftValue.toString() : this.rangeRightValue.toString();
},
toolTipStyle() {
if (!this.activeSlider)
return {
display: "none"
};
return this.activeSlider === "left" ? {
left: this.styleStartPosition,
transform: "translateX(-50%)"
} : {
right: this.styleEndPosition,
transform: "translateX(50%)"
};
}
},
methods: {
isArray(value) {
return Array.isArray(value) || typeof value !== "number" && Array.isArray(value.target);
}
}
});
const mtSlider_vue_vue_type_style_index_0_lang = "";
const _hoisted_1 = { class: "mt-slider__slider" };
const _hoisted_2 = { class: "mt-slider__marks" };
const _hoisted_3 = { class: "mt-slider__mark__label" };
const _hoisted_4 = {
class: "mt-slider__bar",
ref: "sliderBar"
};
const _hoisted_5 = ["min", "max", "step", "disabled"];
const _hoisted_6 = ["id", "min", "max", "step", "disabled"];
function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
const _component_mt_number_field = resolveComponent("mt-number-field");
const _component_mt_base_field = resolveComponent("mt-base-field");
return openBlock(), createBlock(_component_mt_base_field, {
class: normalizeClass(["mt-slider", _ctx.$attrs.class]),
disabled: _ctx.disabled,
required: _ctx.required,
"is-inherited": _ctx.isInherited,
"is-inheritance-field": _ctx.isInheritanceField,
"disable-inheritance-toggle": _ctx.disableInheritanceToggle,
copyable: _ctx.copyable,
"copyable-tooltip": _ctx.copyableTooltip,
"copyable-text": _ctx.stringRepresentation,
"has-focus": _ctx.hasFocus,
"help-text": _ctx.helpText,
name: _ctx.name,
size: _ctx.size,
onInheritanceRestore: _cache[8] || (_cache[8] = ($event) => _ctx.$emit("inheritance-restore", $event)),
onInheritanceRemove: _cache[9] || (_cache[9] = ($event) => _ctx.$emit("inheritance-remove", $event))
}, {
label: withCtx(() => [
createTextVNode(toDisplayString(_ctx.label), 1)
]),
element: withCtx(({ identification }) => [
_ctx.isRange ? (openBlock(), createBlock(_component_mt_number_field, {
key: 0,
modelValue: _ctx.rangeLeftValue,
"onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => _ctx.rangeLeftValue = $event),
min: _ctx.min,
max: _ctx.max - _ctx.minDistance,
step: _ctx.step,
disabled: _ctx.disabled,
"number-type": _ctx.step % 1 === 0 ? "int" : "float",
"data-testid": "left-number-field"
}, null, 8, ["modelValue", "min", "max", "step", "disabled", "number-type"])) : createCommentVNode("", true),
createElementVNode("div", _hoisted_1, [
createElementVNode("div", _hoisted_2, [
(openBlock(true), createElementBlock(Fragment, null, renderList(_ctx.markCount, (index) => {
return openBlock(), createElementBlock("div", {
key: index,
class: "mt-slider__mark",
"data-testid": "mark"
}, [
createElementVNode("span", _hoisted_3, toDisplayString((index - 1) * _ctx.markStep + _ctx.min), 1)
]);
}), 128))
]),
createElementVNode("div", _hoisted_4, [
createElementVNode("div", {
class: "mt-slider__value",
style: normalizeStyle({ left: _ctx.styleStartPosition, right: _ctx.styleEndPosition })
}, null, 4)
], 512),
_ctx.isRange ? withDirectives((openBlock(), createElementBlock("input", {
key: 0,
type: "range",
class: "mt-slider__input-slider mt-slider__input-slider__double",
"aria-label": "Left range slider",
min: _ctx.min,
max: _ctx.max,
step: _ctx.step,
disabled: _ctx.disabled,
"onUpdate:modelValue": _cache[1] || (_cache[1] = ($event) => _ctx.rangeLeftValue = $event),
onMouseenter: _cache[2] || (_cache[2] = ($event) => _ctx.activeSlider = "left"),
onMouseleave: _cache[3] || (_cache[3] = ($event) => _ctx.activeSlider = null),
"data-testid": "left-slider"
}, null, 40, _hoisted_5)), [
[
vModelText,
_ctx.rangeLeftValue,
void 0,
{ number: true }
]
]) : createCommentVNode("", true),
withDirectives(createElementVNode("input", {
id: identification,
type: "range",
class: normalizeClass(["mt-slider__input-slider", { "mt-slider__input-slider__double": _ctx.isRange }]),
"aria-label": "Right range slider",
min: _ctx.min,
max: _ctx.max,
step: _ctx.step,
disabled: _ctx.disabled,
"onUpdate:modelValue": _cache[4] || (_cache[4] = ($event) => _ctx.rangeRightValue = $event),
onMouseenter: _cache[5] || (_cache[5] = ($event) => _ctx.activeSlider = "right"),
onMouseleave: _cache[6] || (_cache[6] = ($event) => _ctx.activeSlider = null),
"data-testid": "right-slider"
}, null, 42, _hoisted_6), [
[
vModelText,
_ctx.rangeRightValue,
void 0,
{ number: true }
]
]),
createElementVNode("span", {
class: "mt-slider__input-slider__hint mt-tooltip mt-tooltip--dark mt-tooltip--top mt-tooltip--wrapper",
style: normalizeStyle(_ctx.toolTipStyle)
}, toDisplayString(_ctx.toolTipText), 5)
]),
createVNode(_component_mt_number_field, {
modelValue: _ctx.rangeRightValue,
"onUpdate:modelValue": _cache[7] || (_cache[7] = ($event) => _ctx.rangeRightValue = $event),
min: _ctx.isRange ? _ctx.min + _ctx.minDistance : _ctx.min,
max: _ctx.max,
step: _ctx.step,
disabled: _ctx.disabled,
"number-type": _ctx.step % 1 === 0 ? "int" : "float",
"data-testid": "right-number-field"
}, null, 8, ["modelValue", "min", "max", "step", "disabled", "number-type"])
]),
"field-hint": withCtx(() => [
renderSlot(_ctx.$slots, "hint")
]),
_: 3
}, 8, ["class", "disabled", "required", "is-inherited", "is-inheritance-field", "disable-inheritance-toggle", "copyable", "copyable-tooltip", "copyable-text", "has-focus", "help-text", "name", "size"]);
}
const mtSlider = /* @__PURE__ */ _export_sfc(_sfc_main, [["render", _sfc_render]]);
export {
mtSlider as default
};
//# sourceMappingURL=MtSlider.js.map