Skip to content

Commit 20058a9

Browse files
authored
fix(slider): rerender ticks when prop is modified (#7439)
**Related Issue:** #6375 ## Summary This adds a watcher for `ticks` to update slider ticks.
1 parent 2cb12da commit 20058a9

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

packages/calcite-components/src/components/slider/slider.stories.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,37 @@ export const maxTickRendering_TestOnly = (): string => html`
415415
<calcite-slider min="-1000" max="1000" ticks="10"></calcite-slider>
416416
`;
417417

418+
export const rendersWhenTrackRelatedPropChanges_TestOnly = (): string =>
419+
html`
420+
<calcite-slider
421+
id="example-slider"
422+
label-ticks
423+
max="32"
424+
value="24"
425+
min="16"
426+
snap
427+
step="8"
428+
ticks="8"
429+
></calcite-slider>
430+
<script>
431+
(async () => {
432+
await customElements.whenDefined("calcite-slider");
433+
const slider = await document.querySelector("calcite-slider").componentOnReady();
434+
await new Promise((resolve) => requestAnimationFrame(resolve));
435+
436+
slider.max = 64;
437+
slider.min = 48;
438+
slider.step = 16;
439+
slider.ticks = 16;
440+
slider.value = 64;
441+
})();
442+
</script>
443+
`;
444+
445+
rendersWhenTrackRelatedPropChanges_TestOnly.parameters = {
446+
chromatic: { delay: 500 },
447+
};
448+
418449
export const spaceGroupSeparatorNoBreak_TestOnly = (): string => html`
419450
<calcite-slider
420451
lang="ru"

packages/calcite-components/src/components/slider/slider.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,11 @@ export class Slider
182182
/** Displays tick marks on the number line at a specified interval. */
183183
@Prop({ reflect: true }) ticks: number;
184184

185+
@Watch("ticks")
186+
ticksWatcher(): void {
187+
this.tickValues = this.generateTickValues();
188+
}
189+
185190
/** The component's value. */
186191
@Prop({ reflect: true, mutable: true }) value: null | number | number[] = 0;
187192

@@ -226,17 +231,12 @@ export class Slider
226231

227232
componentWillLoad(): void {
228233
setUpLoadableComponent(this);
229-
this.tickValues = this.generateTickValues();
230234
if (!isRange(this.value)) {
231-
this.value = this.clamp(this.value);
235+
this.value = this.snap ? this.getClosestStep(this.value) : this.clamp(this.value);
232236
}
237+
this.ticksWatcher();
238+
this.histogramWatcher(this.histogram);
233239
afterConnectDefaultValueSet(this, this.value);
234-
if (this.snap && !isRange(this.value)) {
235-
this.value = this.getClosestStep(this.value);
236-
}
237-
if (this.histogram) {
238-
this.hasHistogram = true;
239-
}
240240
}
241241

242242
componentDidLoad(): void {

0 commit comments

Comments
 (0)