Skip to content

Commit

Permalink
feat(filters): add Slider filter track filled track color (#795)
Browse files Browse the repository at this point in the history
  • Loading branch information
ghiscoding authored Nov 5, 2022
1 parent 617c88d commit 5fbd9c9
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ $control-height: 2.4em;
@import '@slickgrid-universal/common/dist/styles/sass/slickgrid-theme-material.scss';
@import 'bulma/bulma';

:root {
// You can change the slider track filled color via the option "sliderTrackFilledColor" or the CSS variable "--slick-slider-filter-filled-track-color"
--slick-slider-filter-filled-track-color: #9ac49c;
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export class Example2 {
filter: {
model: Filters.slider,
operator: '>=',
filterOptions: { hideSliderNumber: true, enableSliderTrackColoring: true, sliderTrackFilledColor: '#9ac49c' } as SliderOption
filterOptions: { hideSliderNumber: true, enableSliderTrackColoring: true } as SliderOption
},
sortable: true,
type: FieldType.number,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ describe('CompoundSliderFilter', () => {
const filterElms = divContainer.querySelectorAll<HTMLInputElement>('.search-filter.slider-container.filter-duration input');
filterElms[0].dispatchEvent(new Event('change'));

expect(filter.sliderOptions?.sliderTrackBackground).toBe('linear-gradient(to right, #eee 0%, var(--slick-slider-filter-thumb-color, #86bff8) 0%, var(--slick-slider-filter-thumb-color, #86bff8) 80%, #eee 80%)');
expect(filter.sliderOptions?.sliderTrackBackground).toBe('linear-gradient(to right, #eee 0%, #86bff8 0%, #86bff8 80%, #eee 80%)');
});

it('should click on the slider track and expect handle to move to the new position', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ describe('SingleSliderFilter', () => {
const filterElms = divContainer.querySelectorAll<HTMLInputElement>('.search-filter.slider-container.filter-duration input');
filterElms[0].dispatchEvent(new Event('change'));

expect(filter.sliderOptions?.sliderTrackBackground).toBe('linear-gradient(to right, #eee 0%, var(--slick-slider-filter-thumb-color, #86bff8) 0%, var(--slick-slider-filter-thumb-color, #86bff8) 80%, #eee 80%)');
expect(filter.sliderOptions?.sliderTrackBackground).toBe('linear-gradient(to right, #eee 0%, #86bff8 0%, #86bff8 80%, #eee 80%)');
});

it('should click on the slider track and expect handle to move to the new position', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,8 @@ describe('SliderRangeFilter', () => {
filterElms[0].dispatchEvent(new CustomEvent('change'));
const sliderTrackElm = divContainer.querySelector('.slider-track') as HTMLDivElement;

// expect(sliderTrackElm.style.background).toBe('linear-gradient(to right, #eee 2%, var(--slick-slider-filter-thumb-color, #86bff8) 2%, var(--slick-slider-filter-thumb-color, #86bff8) 80%, #eee 80%)');
expect(filter.sliderOptions?.sliderTrackBackground).toBe('linear-gradient(to right, #eee 2%, var(--slick-slider-filter-thumb-color, #86bff8) 2%, var(--slick-slider-filter-thumb-color, #86bff8) 80%, #eee 80%)');
// expect(sliderTrackElm.style.background).toBe('linear-gradient(to right, #eee 2%, #86bff8 2%, #86bff8 80%, #eee 80%)');
expect(filter.sliderOptions?.sliderTrackBackground).toBe('linear-gradient(to right, #eee 2%, #86bff8 2%, #86bff8 80%, #eee 80%)');
});

it('should click on the slider track and expect left handle to move to the new position when calculated percent is below 50%', () => {
Expand Down
7 changes: 6 additions & 1 deletion packages/common/src/filters/sliderFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { mapOperatorToShorthandDesignation } from '../services/utilities';
import { buildSelectOperator, compoundOperatorNumeric } from './filterUtilities';

declare const Slick: SlickNamespace;
const DEFAULT_SLIDER_TRACK_FILLED_COLOR = '#86bff8';
const GAP_BETWEEN_SLIDER_HANDLES = 0;
const Z_INDEX_MIN_GAP = 20; // gap in Px before we change z-index so that lowest/highest handle doesn't block each other

Expand All @@ -46,6 +47,7 @@ export class SliderFilter implements Filter {
protected _sliderTrackElm!: HTMLDivElement;
protected _sliderLeftElm?: HTMLInputElement;
protected _sliderRightElm?: HTMLInputElement;
protected _sliderTrackFilledColor = DEFAULT_SLIDER_TRACK_FILLED_COLOR;
sliderType: SliderType = 'double';
grid!: SlickGrid;
searchTerms: SearchTerm[] = [];
Expand Down Expand Up @@ -118,6 +120,9 @@ export class SliderFilter implements Filter {
this.searchTerms = args?.searchTerms ?? [];
this._argFilterContainerElm = args.filterContainerElm;

// get slider track filled color from CSS variable when exist
this._sliderTrackFilledColor = window.getComputedStyle(document.documentElement).getPropertyValue('--slick-slider-filter-filled-track-color') || DEFAULT_SLIDER_TRACK_FILLED_COLOR;

// step 1, create the DOM Element of the filter & initialize it if searchTerm is filled
this._filterElm = this.createDomFilterElement(this.searchTerms);
}
Expand Down Expand Up @@ -527,7 +532,7 @@ export class SliderFilter implements Filter {
const percent2 = ((+this._sliderRightElm.value - +this._sliderRightElm.min) / (this.sliderOptions?.maxValue ?? 0 - +this._sliderRightElm.min)) * 100;
const bg = 'linear-gradient(to right, %b %p1, %c %p1, %c %p2, %b %p2)'
.replace(/%b/g, '#eee')
.replace(/%c/g, (this.getFilterOptionByName('sliderTrackFilledColor') ?? 'var(--slick-slider-filter-thumb-color, #86bff8)') as string)
.replace(/%c/g, this.getFilterOptionByName('sliderTrackFilledColor') || this._sliderTrackFilledColor || DEFAULT_SLIDER_TRACK_FILLED_COLOR)
.replace(/%p1/g, `${percent1}%`)
.replace(/%p2/g, `${percent2}%`);

Expand Down
5 changes: 4 additions & 1 deletion packages/common/src/interfaces/sliderOption.interface.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
export interface SliderOption {
/** Defaults to false, do we want to show slider track coloring? */
/**
* Defaults to false, do we want to show slider track coloring?
* You can change the slider track filled color via the option "sliderTrackFilledColor" or the CSS variable "--slick-slider-filter-filled-track-color"
*/
enableSliderTrackColoring?: boolean;

/** Defaults to true, hide the slider number shown on the right side */
Expand Down

0 comments on commit 5fbd9c9

Please sign in to comment.