Skip to content

Commit

Permalink
feat(stark-ui): adapt stark-slider component to work with the latest …
Browse files Browse the repository at this point in the history
…version of `nouislider` library

breaking change: `StarkSliderConfig` extends nouislider `Options` therefore the start property is now required. In the next major release, the `StarkSliderConfig` interface may be removed.
  • Loading branch information
mhenkens committed Oct 11, 2024
1 parent 8ff2820 commit a9db3b0
Show file tree
Hide file tree
Showing 10 changed files with 19 additions and 127 deletions.
11 changes: 0 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
"@types/karma": "^6.3.8",
"@types/lodash-es": "^4.17.4",
"@types/node": "^18.19.4",
"@types/nouislider": "^15.0.0",
"@types/prismjs": "^1.16.3",
"@types/uuid": "^10.0.0",
"@types/webpack": "^5.28.0",
Expand Down
1 change: 0 additions & 1 deletion packages/stark-ui/ng-package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
"@prettier/plugin-xml",
"@sqltools/formatter",
"@types/lodash-es",
"@types/nouislider",
"@types/prismjs",
"angular2-text-mask",
"normalize.css",
Expand Down
3 changes: 1 addition & 2 deletions packages/stark-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@
"@angular/material-moment-adapter": "^16.2.14",
"@mdi/angular-material": "^4.0.96",
"@sqltools/formatter": "^1.2.3",
"@types/nouislider": "^9.0.10",
"@types/prismjs": "^1.16.3",
"angular2-text-mask": "^9.0.0",
"normalize.css": "^8.0.1",
"nouislider": "^14.6.3",
"nouislider": "^15.8.1",
"prettier": "~3.3.3",
"prismjs": "^1.23.0",
"text-mask-addons": "^3.8.0",
Expand Down
1 change: 0 additions & 1 deletion packages/stark-ui/src/modules/slider/components.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from "./components/slider.component";
export * from "./components/slider-config.intf";
export * from "./components/slider-pips.intf";
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
@use "sass:map";
@use "@angular/material" as mat;
@use "../../../../styles/old-variables" as old-vars;
@import "~nouislider/distribute/nouislider.css";
@import "~nouislider/dist/nouislider.css";

@mixin stark-slider-theme($material-theme-config, $stark-theme-config) {
.stark-slider {
Expand Down Expand Up @@ -96,6 +96,10 @@
height: 300px;
width: 5px;

.noUi-origin {
top: 0;
}

.noUi-handle {
transform: translate(-10px, -50%);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,84 +1,9 @@
import { StarkSliderPips } from "./slider-pips.intf";

/**
* Interface to be used together with the {@link StarkSliderConfig}
* when using advanced formatting options for the {@link StarkSliderComponent}.
*/
export interface StarkSliderFormatter {
/**
* Function to convert a number to a string with a specific format
*/
to(rawValue: number): string; // format a number

/**
* Function to parse the formatted string back to a number
*/
from(formattedValue: string): number; // get a number back
}
import { Options } from "nouislider";

/**
* @deprecated please use the `Options` interface from "nouislider" library directly
* Interface to be implemented when using the {@link StarkSliderComponent}.
* It is in fact a subset of the options supported by the {@link https://github.com/leongersen/noUiSlider/|noUiSlider} library.
*/
export interface StarkSliderConfig {
/**
* Several ways to handle user interaction.
*
* See {@link https://refreshless.com/nouislider/behaviour-option/|noUiSlider API: Behaviour}
*/
behaviour?: string;

/**
* This can be used to control the (green) bar between the handles, or the edges of the slider.
*
* See {@link https://refreshless.com/nouislider/slider-options/#section-connect|noUiSlider API: Connect}
*/
connect?: boolean | boolean[];

/**
* Formatter containing `to()` function to encode the values and a `from()` function to decode them.
*/
format?: StarkSliderFormatter;

/**
* Slider's orientation: `"horizontal"` or `"vertical"`.
*
* **In case of vertical sliders, a default height is set via CSS rules which you can override if needed.**
*
* Default: `"horizontal"`
*/
orientation?: "horizontal" | "vertical";

/**
* Config object to define how the pips will be displayed in the slider.
*/
pips?: StarkSliderPips;

/**
* All the values that are part of the range. The object should contain at least `min` and `max` properties.
*
* See {@link https://refreshless.com/nouislider/slider-values/#section-range|noUiSlider API: Range}
*/
range: {
min: number | number[];
max: number | number[];
[value: string]: number | number[];
};

/**
* The minimum amount of units that an slider can change within the range.
*
* See {@link https://refreshless.com/nouislider/slider-values/#section-step|noUiSlider API: Step}
*/
step?: number;

/**
* Enable/disable the display of tooltips.
* You can also pass a formatter function to format the tooltips content.
*
* See: {@link https://refreshless.com/nouislider/slider-options/#section-tooltips|noUiSlider API: Tooltips}
*
* Default: `false`
*/
tooltips?: boolean | boolean[] | StarkSliderFormatter;
}
export interface StarkSliderConfig extends Options {}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Component, ViewChild } from "@angular/core";
import { STARK_LOGGING_SERVICE } from "@nationalbankbelgium/stark-core";
import { MockStarkLoggingService } from "@nationalbankbelgium/stark-core/testing";
import { StarkSliderComponent } from "./slider.component";
import { StarkSliderConfig } from "./slider-config.intf";
import { Options } from "nouislider";
import Spy = jasmine.Spy;

/**
Expand All @@ -27,7 +27,7 @@ class TestHostComponent {

public sliderId?: string;
public sliderValues?: number[];
public sliderConfig?: StarkSliderConfig;
public sliderConfig?: Options;

/**
* Simulates the OnValueChanges event of the slider component
Expand All @@ -44,7 +44,8 @@ describe("SliderComponent", () => {
let hostComponent: TestHostComponent;
let hostFixture: ComponentFixture<TestHostComponent>;

const mockConfig: StarkSliderConfig = {
const mockConfig: Options = {
start: 5,
range: {
min: 5,
max: 95
Expand Down Expand Up @@ -76,7 +77,6 @@ describe("SliderComponent", () => {
hostComponent.sliderValues = mockValues;
hostComponent.sliderId = mockSliderId;

spyOn(component.noUiSliderLibrary, "create").and.callThrough();
spyOn(component, "updateSliderInstanceValues").and.callThrough();
spyOn(component.changed, "emit").and.callThrough();

Expand Down Expand Up @@ -153,7 +153,6 @@ describe("SliderComponent", () => {

describe("createSliderInstance", () => {
it("should create the slider instance and assign it to the internal variable", () => {
expect(component.noUiSliderLibrary.create).toHaveBeenCalledTimes(1);
expect(component.slider).toBeDefined();
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ import {
SimpleChanges,
ViewEncapsulation
} from "@angular/core";
import * as noUiSliderLibrary from "nouislider";
import { STARK_LOGGING_SERVICE, StarkLoggingService } from "@nationalbankbelgium/stark-core";
import { StarkDOMUtil } from "@nationalbankbelgium/stark-ui/src/util";
import { StarkSliderConfig } from "./slider-config.intf";
import { AbstractStarkUiComponent } from "@nationalbankbelgium/stark-ui/src/internal-common";
import { API, Options, create } from "nouislider";

/**
* @ignore
Expand Down Expand Up @@ -51,7 +50,7 @@ export class StarkSliderComponent extends AbstractStarkUiComponent implements Af
* Configuration object for the slider instance to be created.
*/
@Input()
public sliderConfig!: StarkSliderConfig;
public sliderConfig!: Options;

/**
* HTML "id" attribute of the element.
Expand Down Expand Up @@ -86,12 +85,7 @@ export class StarkSliderComponent extends AbstractStarkUiComponent implements Af
/**
* a reference to the `noUiSlider` component
*/
public slider!: noUiSliderLibrary.noUiSlider;

/**
* a reference to the noUiSlider library
*/
public noUiSliderLibrary: any;
public slider!: API;

/**
* Class constructor
Expand All @@ -105,7 +99,6 @@ export class StarkSliderComponent extends AbstractStarkUiComponent implements Af
elementRef: ElementRef
) {
super(renderer, elementRef);
this.noUiSliderLibrary = noUiSliderLibrary;
}

/**
Expand Down Expand Up @@ -152,16 +145,15 @@ export class StarkSliderComponent extends AbstractStarkUiComponent implements Af
StarkDOMUtil.getElementsBySelector(this.elementRef.nativeElement, ".stark-slider .slider")[0]
);

const sliderOptions: noUiSliderLibrary.Options = { ...this.sliderConfig, start: this.values };
this.noUiSliderLibrary.create(sliderElement, sliderOptions);
this.slider = (<noUiSliderLibrary.Instance>sliderElement).noUiSlider;
const sliderOptions: Options = { ...this.sliderConfig, start: this.values };
this.slider = create(sliderElement, sliderOptions);
}

/**
* Attach the update handler to the slider component
*/
public attachSliderInstanceUpdateHandler(): void {
this.slider.on("update", (_values: string[], _handle: number, unencodedValues: number[]) => {
this.slider.on("update", (_values: (string | number)[], _handle: number, unencodedValues: number[]) => {
if (!isEqual(this.values, unencodedValues)) {
this.values = unencodedValues;
this.latestUnencodedValues = unencodedValues;
Expand Down

0 comments on commit a9db3b0

Please sign in to comment.