Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Slider): lower value not updated when is equal to upper value #1078

Merged
merged 3 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 3 additions & 12 deletions packages/beeq/src/components/slider/bq-slider.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Component, Element, Event, EventEmitter, h, Prop, State, Watch } from '@stencil/core';

import { TSliderType, TSliderValue } from './bq-slider.types';
import { debounce, isNil, isString, TDebounce } from '../../shared/utils';
import { clamp, debounce, isNil, isString, TDebounce } from '../../shared/utils';

/**
* @part base - The component's base wrapper.
Expand Down Expand Up @@ -176,21 +176,12 @@ export class BqSlider {
this.setThumbPosition();
};

private calculateMinValue = (value: TSliderValue) => {
const isMaxValue = (this.minValue ?? value[0]) === this.max;
const isGapExceeded = value[0] + this.gap > this.max;
// Make sure that the min value gets adjusted according to the gap value
return isMaxValue || isGapExceeded ? this.max - this.gap : value[0];
};

private calculateMaxValue = (value: TSliderValue, minValue: number) => Math.max(value[1], minValue + this.gap);

private setState = (newValue: TSliderValue) => {
const isRangeType = this.isRangeType;
const value = this.stringToObject(newValue);

this.minValue = isRangeType ? this.calculateMinValue(value) : value;
this.maxValue = isRangeType ? this.calculateMaxValue(value, this.minValue) : this.minValue;
this.minValue = isRangeType ? clamp(value[0], this.min, this.max - this.gap) : value;
this.maxValue = isRangeType ? clamp(value[1], this.minValue + this.gap, this.max) : this.minValue;
};

private setThumbPosition = () => {
Expand Down
19 changes: 19 additions & 0 deletions packages/beeq/src/shared/utils/__tests__/clamp.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { clamp } from '..';

describe(clamp.name, () => {
it('should return value', () => {
expect(clamp(10)).toBe(10);
});

it('should return lower value', () => {
expect(clamp(10, 20, 30)).toBe(20);
});

it('should upper value', () => {
dgonzalezr marked this conversation as resolved.
Show resolved Hide resolved
expect(clamp(40, 20, 30)).toBe(30);
});

it('should return value unchanged', () => {
expect(clamp(25, 20, 30)).toBe(25);
});
});
11 changes: 11 additions & 0 deletions packages/beeq/src/shared/utils/clamp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Restricts value to a specified interval [min, max]
*
* @param value - value
* @param min - lower end of the interval
* @param max - upper end of the interval
* @returns - value in interval [min, max]
*/
export const clamp = (value: number, min = Number.MIN_SAFE_INTEGER, max = Number.MAX_SAFE_INTEGER) => {
return Math.min(Math.max(min, value), max);
};
1 change: 1 addition & 0 deletions packages/beeq/src/shared/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './assetsPath';
export * from './clamp';
export * from './cssVariables';
export * from './debounce';
export * from './event';
Expand Down