|
| 1 | +<script lang="ts" setup> |
| 2 | +import { computed, onMounted, ref, watch } from 'vue' |
| 3 | +
|
| 4 | +import { getRandomId } from '../../utils/random-utils' |
| 5 | +
|
| 6 | +const props = withDefaults(defineProps<{ |
| 7 | + id?: string |
| 8 | + min?: number |
| 9 | + max?: number |
| 10 | + modelValue?: number |
| 11 | + lowerValue?: number |
| 12 | + label: string |
| 13 | + hint?: string |
| 14 | + message?: string |
| 15 | + prefix?: string |
| 16 | + suffix?: string |
| 17 | + small?: boolean |
| 18 | + hideIndicators?: boolean |
| 19 | + step?: number |
| 20 | + disabled?: boolean |
| 21 | +}>(), { |
| 22 | + id: () => getRandomId('range'), |
| 23 | + min: 0, |
| 24 | + max: 100, |
| 25 | + modelValue: 0, |
| 26 | + lowerValue: undefined, |
| 27 | + hint: undefined, |
| 28 | + message: undefined, |
| 29 | + prefix: undefined, |
| 30 | + suffix: undefined, |
| 31 | + step: undefined, |
| 32 | +}) |
| 33 | +
|
| 34 | +// eslint-disable-next-line func-call-spacing |
| 35 | +const emit = defineEmits<{ |
| 36 | + (e: 'update:modelValue', payload: string | number): void |
| 37 | + (e: 'update:lowerValue', payload: string | number): void |
| 38 | +}>() |
| 39 | +
|
| 40 | +const input = ref<HTMLInputElement>() |
| 41 | +const output = ref<HTMLSpanElement>() |
| 42 | +const inputWidth = ref() |
| 43 | +
|
| 44 | +const double = computed(() => props.lowerValue !== undefined) |
| 45 | +
|
| 46 | +const outputStyle = computed(() => { |
| 47 | + if (props.lowerValue === undefined) { |
| 48 | + const translateXValue = (props.modelValue - props.min) / (props.max - props.min) * inputWidth.value |
| 49 | + return `transform: translateX(${translateXValue}px) translateX(-${props.modelValue}%);` |
| 50 | + } |
| 51 | + const translateXValue = (props.modelValue + props.lowerValue - props.min) / 2 / (props.max - props.min) * inputWidth.value |
| 52 | + return `transform: translateX(${translateXValue}px) translateX(-${props.lowerValue + ((props.modelValue - props.lowerValue) / 2)}%);` |
| 53 | +}) |
| 54 | +
|
| 55 | +const rangeStyle = computed(() => { |
| 56 | + const progressRight = (props.modelValue - props.min) / (props.max - props.min) * inputWidth.value - (double.value ? 12 : 0) |
| 57 | + const progressLeft = ((props.lowerValue ?? 0) - props.min) / (props.max - props.min) * inputWidth.value |
| 58 | +
|
| 59 | + return { |
| 60 | + '--progress-right': progressRight + 24 + 'px', |
| 61 | + ...(double.value ? { '--progress-left': progressLeft + 12 + 'px' } : {}), |
| 62 | + } |
| 63 | +}) |
| 64 | +
|
| 65 | +watch([() => props.modelValue, () => props.lowerValue], ([upper, lower]) => { |
| 66 | + if (lower === undefined) { |
| 67 | + return |
| 68 | + } |
| 69 | +
|
| 70 | + if (double.value && upper < lower) { |
| 71 | + emit('update:lowerValue', upper) |
| 72 | + } |
| 73 | + if (double.value && lower > upper) { |
| 74 | + emit('update:modelValue', lower) |
| 75 | + } |
| 76 | +}) |
| 77 | +
|
| 78 | +const outputValue = computed(() => { |
| 79 | + return (props.prefix ?? '') |
| 80 | + .concat(double.value ? `${props.lowerValue} - ` : '') |
| 81 | + .concat('' + props.modelValue) |
| 82 | + .concat(props.suffix ?? '') |
| 83 | +}) |
| 84 | +
|
| 85 | +onMounted(() => { |
| 86 | + inputWidth.value = input.value?.offsetWidth |
| 87 | +}) |
| 88 | +</script> |
| 89 | + |
| 90 | +<template> |
| 91 | + <div |
| 92 | + :id="`${id}-group`" |
| 93 | + class="fr-range-group" |
| 94 | + :class="{ 'fr-range-group--error': message }" |
| 95 | + > |
| 96 | + <label |
| 97 | + :id="`${id}-label`" |
| 98 | + class="fr-label" |
| 99 | + > |
| 100 | + <slot name="label"> |
| 101 | + {{ label }} |
| 102 | + </slot> |
| 103 | + <span class="fr-hint-text"> |
| 104 | + <slot name="hint"> |
| 105 | + {{ hint }} |
| 106 | + </slot> |
| 107 | + </span> |
| 108 | + </label> |
| 109 | + <div |
| 110 | + class="fr-range" |
| 111 | + data-fr-js-range="true" |
| 112 | + :class="{ |
| 113 | + 'fr-range--sm': small, |
| 114 | + 'fr-range--double': double, |
| 115 | + 'fr-range-group--disabled': disabled, |
| 116 | + }" |
| 117 | + :data-fr-prefix="prefix ?? undefined" |
| 118 | + :data-fr-suffix="suffix ?? undefined" |
| 119 | + :style="rangeStyle" |
| 120 | + > |
| 121 | + <span |
| 122 | + ref="output" |
| 123 | + class="fr-range__output" |
| 124 | + data-fr-js-range-output="true" |
| 125 | + :style="outputStyle" |
| 126 | + >{{ outputValue }}</span> |
| 127 | + <input |
| 128 | + v-if="double" |
| 129 | + :id="`${id}-2`" |
| 130 | + type="range" |
| 131 | + :min="min" |
| 132 | + :max="max" |
| 133 | + :step="step" |
| 134 | + :value="lowerValue" |
| 135 | + :disabled="disabled" |
| 136 | + :aria-labelledby="`${id}-label`" |
| 137 | + :aria-describedby="`${id}-messages`" |
| 138 | + @input="emit('update:lowerValue', +$event.target?.value)" |
| 139 | + > |
| 140 | + <input |
| 141 | + :id="id" |
| 142 | + ref="input" |
| 143 | + type="range" |
| 144 | + :min="min" |
| 145 | + :max="max" |
| 146 | + :step="step" |
| 147 | + :value="modelValue" |
| 148 | + :disabled="disabled" |
| 149 | + :aria-labelledby="`${id}-label`" |
| 150 | + :aria-describedby="`${id}-messages`" |
| 151 | + @input="emit('update:modelValue', +$event.target?.value)" |
| 152 | + > |
| 153 | + |
| 154 | + <span |
| 155 | + v-if="!hideIndicators" |
| 156 | + class="fr-range__min" |
| 157 | + aria-hidden="true" |
| 158 | + data-fr-js-range-limit="true" |
| 159 | + >{{ min }}</span> |
| 160 | + <span |
| 161 | + v-if="!hideIndicators" |
| 162 | + class="fr-range__max" |
| 163 | + aria-hidden="true" |
| 164 | + data-fr-js-range-limit="true" |
| 165 | + >{{ max }}</span> |
| 166 | + </div> |
| 167 | + <div |
| 168 | + :id="`${id}-messages`" |
| 169 | + class="fr-messages-group" |
| 170 | + aria-live="polite" |
| 171 | + > |
| 172 | + <slot name="messages"> |
| 173 | + <p |
| 174 | + v-if="message" |
| 175 | + :id="`${id}-message-error`" |
| 176 | + class="fr-message fr-message--error" |
| 177 | + > |
| 178 | + {{ message }} |
| 179 | + </p> |
| 180 | + </slot> |
| 181 | + </div> |
| 182 | + </div> |
| 183 | +</template> |
0 commit comments