Skip to content
This repository has been archived by the owner on Aug 28, 2024. It is now read-only.

Commit

Permalink
convert slider to typescript (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
ToBinio authored Nov 11, 2023
1 parent ca54544 commit b60e781
Showing 1 changed file with 42 additions and 59 deletions.
101 changes: 42 additions & 59 deletions lib/components/base/Slider.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
<div class="snap-points-wrapper">
<div class="snap-points">
<div
v-for="snapPoint in props.snapPoints"
v-for="snapPoint in snapPoints"
:key="snapPoint"
class="snap-point"
:class="{ green: snapPoint <= currentValue }"
:style="{ left: ((snapPoint - props.min) / (props.max - props.min)) * 100 + '%' }"
:style="{ left: ((snapPoint - min) / (max - min)) * 100 + '%' }"
></div>
</div>
</div>
Expand All @@ -30,7 +30,7 @@
'--min-value': min,
'--max-value': max,
}"
@input="onInputWithSnap($refs.input.value)"
@input="onInputWithSnap(($event.target as HTMLInputElement).value)"
/>
<div class="slider-range">
<span> {{ min }} {{ unit }} </span>
Expand All @@ -44,74 +44,57 @@
type="text"
class="slider-input"
:disabled="disabled"
@change="onInput($refs.value.value)"
@change="onInput(($event.target as HTMLInputElement).value)"
/>
</div>
</template>

<script setup>
<script setup lang="ts">
import { ref } from 'vue'
const emit = defineEmits(['update:modelValue'])
const props = defineProps({
modelValue: {
type: Number,
default: 0,
},
min: {
type: Number,
default: 0,
},
max: {
type: Number,
default: 100,
},
step: {
type: Number,
default: 10,
},
forceStep: {
type: Boolean,
default: true,
},
snapPoints: {
type: Array,
default: () => [],
},
snapRange: {
type: Number,
default: 100,
},
disabled: {
type: Boolean,
default: false,
},
unit: {
type: String,
default: '',
},
})
let emit = defineEmits<{ 'update:modelValue': [number] }>()
interface Props {
modelValue?: number
min: number
max: number
step?: number
forceStep?: boolean
snapPoints?: number[]
snapRange?: number
disabled?: boolean
unit?: string
}
let currentValue = ref(Math.max(props.min, props.modelValue).toString())
const props = withDefaults(defineProps<Props>(), {
modelValue: 0,
min: 0,
max: 100,
step: 10,
forceStep: true,
snapPoints: () => [],
snapRange: 100,
disabled: false,
unit: '',
})
const inputValueValid = (newValue) => {
const parsedValue = parseInt(newValue)
let currentValue = ref(Math.max(props.min, props.modelValue))
if (parsedValue < props.min) {
currentValue.value = props.min.toString()
} else if (parsedValue > props.max) {
currentValue.value = props.max.toString()
} else if (!parsedValue) {
currentValue.value = props.min.toString()
const inputValueValid = (newValue: number) => {
if (newValue < props.min) {
currentValue.value = props.min
} else if (newValue > props.max) {
currentValue.value = props.max
} else if (!newValue) {
currentValue.value = props.min
} else {
currentValue.value = (parsedValue - (props.forceStep ? parsedValue % props.step : 0)).toString()
currentValue.value = newValue - (props.forceStep ? newValue % props.step : 0)
}
emit('update:modelValue', parseInt(currentValue.value))
emit('update:modelValue', currentValue.value)
}
const onInputWithSnap = (value) => {
const onInputWithSnap = (value: string) => {
let parsedValue = parseInt(value)
for (let snapPoint of props.snapPoints) {
Expand All @@ -125,8 +108,8 @@ const onInputWithSnap = (value) => {
inputValueValid(parsedValue)
}
const onInput = (value) => {
inputValueValid(value)
const onInput = (value: string) => {
inputValueValid(parseInt(value))
}
</script>

Expand Down

0 comments on commit b60e781

Please sign in to comment.