Skip to content

Commit

Permalink
fix(slider): rerender ticks when prop is modified (#7439)
Browse files Browse the repository at this point in the history
**Related Issue:** #6375

## Summary

This adds a watcher for `ticks` to update slider ticks.
  • Loading branch information
jcfranco committed Aug 15, 2023
1 parent 2cb12da commit 20058a9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,37 @@ export const maxTickRendering_TestOnly = (): string => html`
<calcite-slider min="-1000" max="1000" ticks="10"></calcite-slider>
`;

export const rendersWhenTrackRelatedPropChanges_TestOnly = (): string =>
html`
<calcite-slider
id="example-slider"
label-ticks
max="32"
value="24"
min="16"
snap
step="8"
ticks="8"
></calcite-slider>
<script>
(async () => {
await customElements.whenDefined("calcite-slider");
const slider = await document.querySelector("calcite-slider").componentOnReady();
await new Promise((resolve) => requestAnimationFrame(resolve));
slider.max = 64;
slider.min = 48;
slider.step = 16;
slider.ticks = 16;
slider.value = 64;
})();
</script>
`;

rendersWhenTrackRelatedPropChanges_TestOnly.parameters = {
chromatic: { delay: 500 },
};

export const spaceGroupSeparatorNoBreak_TestOnly = (): string => html`
<calcite-slider
lang="ru"
Expand Down
16 changes: 8 additions & 8 deletions packages/calcite-components/src/components/slider/slider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,11 @@ export class Slider
/** Displays tick marks on the number line at a specified interval. */
@Prop({ reflect: true }) ticks: number;

@Watch("ticks")
ticksWatcher(): void {
this.tickValues = this.generateTickValues();
}

/** The component's value. */
@Prop({ reflect: true, mutable: true }) value: null | number | number[] = 0;

Expand Down Expand Up @@ -226,17 +231,12 @@ export class Slider

componentWillLoad(): void {
setUpLoadableComponent(this);
this.tickValues = this.generateTickValues();
if (!isRange(this.value)) {
this.value = this.clamp(this.value);
this.value = this.snap ? this.getClosestStep(this.value) : this.clamp(this.value);
}
this.ticksWatcher();
this.histogramWatcher(this.histogram);
afterConnectDefaultValueSet(this, this.value);
if (this.snap && !isRange(this.value)) {
this.value = this.getClosestStep(this.value);
}
if (this.histogram) {
this.hasHistogram = true;
}
}

componentDidLoad(): void {
Expand Down

0 comments on commit 20058a9

Please sign in to comment.