Skip to content

Commit

Permalink
feat: custom step values
Browse files Browse the repository at this point in the history
  • Loading branch information
freddie-nelson committed Oct 4, 2024
1 parent 972be5f commit d00519e
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 46 deletions.
29 changes: 19 additions & 10 deletions dev/serve.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,18 @@ export default defineComponent({
alwaysShowTooltip: false,
alwaysShowHandle: false,
flipTooltip: false,
customSliderStepsText: "",
};
},
computed: {
customSliderSteps(): number[] | undefined {
if (this.customSliderStepsText) {
return this.customSliderStepsText.split(",").map((step: string) => Number(step.trim()));
}
return undefined;
},
},
methods: {
openControls() {
if (!this.expand) {
Expand Down Expand Up @@ -215,11 +225,7 @@ export default defineComponent({

<div class="item">
<h2>alwaysShowTooltip:</h2>
<input
type="checkbox"
name="alwaysShowTooltip"
v-model="alwaysShowTooltip"
/>
<input type="checkbox" name="alwaysShowTooltip" v-model="alwaysShowTooltip" />
</div>

<div class="item">
Expand All @@ -229,11 +235,7 @@ export default defineComponent({

<div class="item">
<h2>alwaysShowHandle:</h2>
<input
type="checkbox"
name="alwaysShowHandle"
v-model="alwaysShowHandle"
/>
<input type="checkbox" name="alwaysShowHandle" v-model="alwaysShowHandle" />
</div>

<div class="item">
Expand Down Expand Up @@ -270,6 +272,11 @@ export default defineComponent({
<input type="checkbox" name="disabled" v-model="disabled" />
</div>

<div class="item">
<h2>custom slider steps:</h2>
<input type="text" name="customSliderStepsText" v-model="customSliderStepsText" />
</div>

<button @click="sliderVal += 10">Add 10 to slider value</button>
<button @click="sliderVal -= 10">Minus 10 from slider value</button>
</div>
Expand Down Expand Up @@ -302,6 +309,7 @@ export default defineComponent({
:circleOffset="circleOffset"
:circleGap="circleGap"
:disabled="disabled"
:customSliderSteps="customSliderSteps"
/>
<h1>{{ sliderVal }}</h1>
</template>
Expand Down Expand Up @@ -350,6 +358,7 @@ h1 {
.controls.expand .inputs {
height: 770px;
opacity: 1;
overflow-y: scroll;
}
.controls .inputs {
Expand Down
12 changes: 12 additions & 0 deletions src/hooks/useModelValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ export default function(store: Store, props: Props, emit: SetupContext["emit"])
// get between min and max
roundedVal = roundedVal + props.min;

// apply custom steps
if (props.customSliderSteps) {
let closestStep = props.customSliderSteps[0];
for (const step of props.customSliderSteps) {
if (Math.abs(roundedVal - step) < Math.abs(roundedVal - closestStep)) {
closestStep = step;
}
}

roundedVal = closestStep;
}

// apply limit
if (props.limit !== undefined) roundedVal = Math.min(roundedVal, props.limit);

Expand Down
5 changes: 5 additions & 0 deletions src/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export type Props = Readonly<{
circleOffset: number;
circleGap: number;
disabled: boolean;
customSliderSteps?: number[];
}>;

export default {
Expand Down Expand Up @@ -167,4 +168,8 @@ export default {
type: Boolean,
default: false,
},
customSliderSteps: {
type: Array as () => number[] | undefined,
default: undefined,
},
};
59 changes: 23 additions & 36 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,39 +1,26 @@
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"strict": true,
"declaration": true,
"declarationDir": "dist",
"noUnusedLocals": true,
"noUnusedParameters": true,
"importHelpers": true,
"moduleResolution": "node",
"allowJs": true,
"experimentalDecorators": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"sourceMap": true,
"baseUrl": ".",
"types": [
"node",
"vue",
"resize-observer-browser"
],
"paths": {
"@/*": [
"src/*"
]
},
"lib": [
"esnext",
"dom",
"dom.iterable",
"scripthost"
]
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"strict": true,
"noImplicitThis": true,
"declaration": true,
"declarationDir": "dist",
"noUnusedLocals": true,
"noUnusedParameters": true,
"importHelpers": true,
"moduleResolution": "node",
"allowJs": true,
"experimentalDecorators": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"sourceMap": true,
"baseUrl": ".",
"types": ["node", "vue", "resize-observer-browser"],
"paths": {
"@/*": ["src/*"]
},
"exclude": [
"node_modules",
"dist"
]
"lib": ["esnext", "dom", "dom.iterable", "scripthost"]
},
"exclude": ["node_modules", "dist"]
}

0 comments on commit d00519e

Please sign in to comment.