Skip to content

Commit

Permalink
feat(ui-library): bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Arun Kumaar committed Sep 6, 2023
1 parent 56f20b3 commit a1a7c15
Show file tree
Hide file tree
Showing 8 changed files with 223 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { map } from 'lit/directives/map.js';
import { styleCustom } from './index.css';
import { sliderDark, sliderLight } from '../../foundation/component-tokens/slider-legend.css';
import { FormSizesType, ActionVariantType } from '../../globals/types';
import { findToolTipPosition } from '../../utils/range-slider-utils';

import { BlrIconButtonRenderFunction } from '../icon-button';
import { RenderBtnProps } from '../../globals/types';
Expand Down Expand Up @@ -103,23 +104,23 @@ export class BlrRangeLegendMinMaxSlider extends LitElement {
[`${this.size || 'md'}`]: this.size || 'md',
});

const toolTipMinQuerySelector = this.shadowRoot?.querySelector(`#pip-${this.selectedStartIndex}`);
const toolTipMinPos = toolTipMinQuerySelector?.getBoundingClientRect().left;
const toolTipMaxQuerySelector = this.shadowRoot?.querySelector(`#pip-${this.selectedEndIndex}`);
const toolTipMaxPos = toolTipMaxQuerySelector?.getBoundingClientRect().left;
const minSliderId = this.rangeInputId ? `${this.rangeInputId}-1` : `rangeInputId-1`;
const maxSliderId = this.rangeInputId ? `${this.rangeInputId}-2` : `rangeInputId-2`;

const minSlider = this.shadowRoot?.querySelector(`#${minSliderId}`) as HTMLInputElement;
const maxSlider = this.shadowRoot?.querySelector(`#${maxSliderId}`) as HTMLInputElement;

const toolTipMinPos =
minSlider && findToolTipPosition(minSlider.min, minSlider.max, minSlider.offsetWidth, this.selectedStartIndex);
const toolTipMaxPos =
minSlider && findToolTipPosition(maxSlider.min, maxSlider.max, maxSlider.offsetWidth, this.selectedEndIndex);

return html`<style>
${dynamicStyles.map((style) => style)}
</style>
<div class=${classes}>
<fieldset class="range__field">
<div class="input-wrapper">
<div id="tooltip1" class="tooltip" style="bottom:110px; left:${toolTipMinPos! - 15}px; position:absolute;">
${stepsArray[this.selectedStartIndex]}
</div>
<div id="tooltip1" class="tooltip" style="bottom:110px; left:${toolTipMaxPos! - 15}px; position:absolute;">
${stepsArray[this.selectedEndIndex]}
</div>
<div class="min-max-btnwrapper">
${this.renderBtn({
btnId: 'inc_btn_min',
Expand All @@ -135,10 +136,10 @@ export class BlrRangeLegendMinMaxSlider extends LitElement {
<div class="input-row">
<div class="range-wrapper">
<input
id=${this.rangeInputId ? `${this.rangeInputId}-1` : `rangeInputId-1`}
id=${minSliderId}
type="range"
min="0"
value="${this.selectedStartIndex}"
.value="${this.selectedStartIndex}"
max="${stepsArray.length - 1}"
step="${this.stepFactor}"
class="range"
Expand All @@ -148,10 +149,10 @@ export class BlrRangeLegendMinMaxSlider extends LitElement {
?disabled=${this.disabled}
/>
<input
id=${this.rangeInputId ? `${this.rangeInputId}-2` : `rangeInputId-2`}
id=${maxSliderId}
type="range"
min="0"
value="${this.selectedEndIndex}"
.value="${this.selectedEndIndex}"
max="${stepsArray.length - 1}"
step="${this.stepFactor}"
class="range"
Expand All @@ -160,6 +161,12 @@ export class BlrRangeLegendMinMaxSlider extends LitElement {
@input=${showMaxVal}
?disabled=${this.disabled}
/>
<div id="tooltip1" class="tooltip" style="left:${toolTipMinPos}; bottom:0px">
${stepsArray[this.selectedStartIndex]}
</div>
<div id="tooltip1" class="tooltip" style="left:${toolTipMaxPos}; bottom:0px">
${stepsArray[this.selectedEndIndex]}
</div>
</div>
<div class="tick-wrapper">
<div class="range__bar-row">
Expand Down
18 changes: 10 additions & 8 deletions packages/ui-library/src/components/range-legend-slider/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { map } from 'lit/directives/map.js';
import { styleCustom } from './index.css';
import { sliderDark, sliderLight } from '../../foundation/component-tokens/slider-legend.css';
import { FormSizesType, ActionVariantType } from '../../globals/types';
import { findToolTipPosition } from '../../utils/range-slider-utils';

import { BlrIconButtonRenderFunction } from '../icon-button';
import { RenderBtnProps } from '../../globals/types';
Expand Down Expand Up @@ -87,18 +88,16 @@ export class BlrRangeLegendSlider extends LitElement {
[`${this.size || 'md'}`]: this.size || 'md',
});

const toolTipQuerySelector = this.shadowRoot?.querySelector(`#pip-${this.selectedIndex}`);
const toolTipPos = toolTipQuerySelector?.getBoundingClientRect().left;
const inputCmp1 = this.rangeInputId ? `${this.rangeInputId}-1` : `rangeInputId-1`;
const slider = this.shadowRoot?.querySelector(`#${inputCmp1}`) as HTMLInputElement;
const toolTipPos = slider && findToolTipPosition(slider.min, slider.max, slider.offsetWidth, this.selectedIndex);

return html`<style>
${dynamicStyles.map((style) => style)}
</style>
<div class=${classes}>
<fieldset class="range__field">
<div class="input-wrapper">
<div id="tooltip1" class="tooltip" style="bottom:90px; left:${toolTipPos! - 20}px; position:absolute;">
${stepsArray[this.selectedIndex]}
</div>
<div class="min-max-btnwrapper">
${this.renderBtn({
btnId: 'dec_btn',
Expand All @@ -107,12 +106,12 @@ export class BlrRangeLegendSlider extends LitElement {
})}
</div>
<div class="input-row">
<div class="range-wrapper">
<div class="range-wrapper" id="range-wrapper">
<input
id=${this.rangeInputId ? `${this.rangeInputId}-1` : `rangeInputId-1`}
id=${inputCmp1}
type="range"
min="0"
value="${this.selectedIndex}"
.value="${this.selectedIndex}"
max="${stepsArray.length - 1}"
step="${this.stepFactor}"
class="range"
Expand All @@ -121,6 +120,9 @@ export class BlrRangeLegendSlider extends LitElement {
@input=${showVal}
?disabled=${this.disabled}
/>
<div id="tooltip1" class="tooltip" style="bottom:0px; left: ${toolTipPos}">
${stepsArray[this.selectedIndex]}
</div>
</div>
<div class="tick-wrapper">
<div class="range__bar-row">
Expand Down
11 changes: 7 additions & 4 deletions packages/ui-library/src/components/range-min-max-slider/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ export class BlrRangeMinMaxSlider extends LitElement {
[`${this.size || 'md'}`]: this.size || 'md',
});

const barClasses = `range__bar blr-slider-bar ${this.disabled ? `bar-disabled` : ``}`;

return html`<style>
${dynamicStyles.map((style) => style)}
</style>
Expand Down Expand Up @@ -147,10 +149,10 @@ export class BlrRangeMinMaxSlider extends LitElement {
id=${this.rangeInputId ? `${this.rangeInputId}-1` : `rangeInputId-1`}
type="range"
min="0"
value=${this.startValueToSlider}
.value=${this.startValueToSlider}
max="100"
step="${this.stepFactor}"
class="range blr-slider-bar"
class="range"
@change=${showMinVal}
@input=${showMinVal}
?disabled=${this.disabled}
Expand All @@ -159,10 +161,10 @@ export class BlrRangeMinMaxSlider extends LitElement {
id=${this.rangeInputId ? `${this.rangeInputId}-2` : `rangeInputId-2`}
type="range"
min="0"
value=${this.endValueToSlider}
.value=${this.endValueToSlider}
max="100"
step="${this.stepFactor}"
class="range blr-slider-bar"
class="range"
@change=${showMaxVal}
@input=${showMaxVal}
?disabled=${this.disabled}
Expand All @@ -174,6 +176,7 @@ export class BlrRangeMinMaxSlider extends LitElement {
<div id="tooltip2" class="tooltip" style="left:${this.endValueToSlider}%">
${this.endValue} ${this.units}
</div>
<div class=${barClasses}></div>
</div>
${this.showLegend ? html`<div class="inline-legend"><p>${this.maxValue} ${this.units}</p></div>` : nothing}
<div class="min-max-btnwrapper">
Expand Down
149 changes: 149 additions & 0 deletions packages/ui-library/src/components/range-slider/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import { BlrRangeSliderType, BlrRangeSliderRenderFunction } from './index';

import { fixture, expect } from '@open-wc/testing';
import { querySelectorDeep, querySelectorAllDeep } from 'query-selector-shadow-dom';

const sampleParams: BlrRangeSliderType = {
theme: 'Light',
rangeInputId: 'range-id',
initialValue: 80,
minValue: 75,
maxValue: 130,
units: '$',
stepFactor: 1,
size: 'md',
btnVariant: 'silent',
showLegend: true,
disabled: false,
incrementIcon: 'blrPlusMd',
decrementIcon: 'blrMinusMd',
onChange: () => null,
};

describe('blr-range-slider', () => {
it('is having a slider containing the right className', async () => {
const element = await fixture(BlrRangeSliderRenderFunction(sampleParams));
const inputWrapper = querySelectorDeep('.range-wrapper', element.getRootNode() as HTMLElement);
const textarea = querySelectorDeep('input', inputWrapper?.getRootNode() as HTMLElement);
const className = textarea?.className;

expect(className).to.contain('range blr-slider-bar');
});

it('is is disabled when attribute disabled is set', async () => {
const element = await fixture(
BlrRangeSliderRenderFunction({
...sampleParams,
disabled: true,
})
);

const textarea = querySelectorDeep('input', element.getRootNode() as HTMLElement);
const disabled = textarea?.getAttribute('disabled');

// in html disabled will become an empty string when its true
expect(disabled).to.be.equal('');
});

it('is is disabled when attribute disabled is set', async () => {
const element = await fixture(
BlrRangeSliderRenderFunction({
...sampleParams,
disabled: false,
})
);

const textarea = querySelectorDeep('input', element.getRootNode() as HTMLElement);
const disabled = textarea?.getAttribute('disabled');

// in html disabled will become null when its false
expect(disabled).to.be.equal(null);
});

it('is not having a legends', async () => {
const element = await fixture(
BlrRangeSliderRenderFunction({
...sampleParams,
showLegend: false,
})
);

const inputWrapper = querySelectorDeep('.input-wrapper', element.getRootNode() as HTMLElement);
const inlineLegendClassElement = querySelectorDeep('.inline-legend', inputWrapper?.getRootNode() as HTMLElement);

// Check if the <p> element exists
expect(inlineLegendClassElement).not.exist;
});

it('should find all legends with min & max values', async () => {
const element = await fixture(
BlrRangeSliderRenderFunction({
...sampleParams,
initialValue: 80,
minValue: 75,
maxValue: 130,
disabled: false,
})
);

const inputWrapper = querySelectorAllDeep('.input-wrapper', element.getRootNode() as HTMLElement);
const expectedLabels = [75, 130];

const inlineLegendElements = [];

for (let i = 0; i < inputWrapper.length; i++) {
const wrapperElement = inputWrapper[i];
const rootNode = wrapperElement.getRootNode() as HTMLElement;

const inlineLegendClassElements = querySelectorAllDeep('.inline-legend', rootNode);

// Push the found .inline-legend elements into the array
inlineLegendElements.push(...inlineLegendClassElements);
}

// Process the collected inlineLegendElements array
inlineLegendElements.forEach((inlineLegendElement, j) => {
// Assuming that the text content of .inline-legend elements is the expected value
const expectedValue = expectedLabels[j];

// Assert that the expected value exists in the text content of the element
expect(inlineLegendElement).to.exist;
expect(inlineLegendElement).to.have.text(`${expectedValue} $`);
});
});

it('should find all buttons for with min & max', async () => {
const element = await fixture(
BlrRangeSliderRenderFunction({
...sampleParams,
initialValue: 80,
minValue: 75,
maxValue: 130,
disabled: false,
})
);

const inputWrapper = querySelectorAllDeep('.input-wrapper', element.getRootNode() as HTMLElement);
const expectedLabels = ['dec_btn', 'inc_btn'];

const inlineButtonElements = [];

for (let i = 0; i < inputWrapper.length; i++) {
const wrapperElement = inputWrapper[i];
const rootNode = wrapperElement.getRootNode() as HTMLElement;

const inlineButtonClassElements = querySelectorAllDeep('.blr-icon-button', rootNode);

// Push the found .inline-legend elements into the array
inlineButtonElements.push(...inlineButtonClassElements);
}

// Process the collected inlineLegendElements array
inlineButtonElements.forEach((inlineButtonElement, j) => {
const expectedValue = expectedLabels[j];

expect(inlineButtonElement).to.exist;
expect(inlineButtonElement).to.have.attr('aria-label', expectedValue);
});
});
});
7 changes: 5 additions & 2 deletions packages/ui-library/src/components/range-slider/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ export class BlrRangeSlider extends LitElement {

const inlineLegendStyles = !this.disabled ? 'inline-legend' : 'inline-legend inline-legend-disabled';

const barClasses = `range__bar blr-slider-bar ${this.disabled ? `bar-disabled` : ``}`;

return html`<style>
${dynamicStyles.map((style) => style)}
</style>
Expand All @@ -111,17 +113,18 @@ export class BlrRangeSlider extends LitElement {
id=${this.rangeInputId || 'rangeInputId'}
type="range"
min="0"
value=${this.valueToSlider}
.value=${this.valueToSlider}
max="100"
step="${this.stepFactor}"
class="range blr-slider-bar"
class="range"
?disabled=${this.disabled}
@change=${showVal}
@input=${showVal}
/>
<div id="tooltip" class="tooltip" style="left:${this.valueToSlider}%">
${this.initialValue} ${this.units}
</div>
<div class=${barClasses}></div>
</div>
${this.showLegend
? html`<div class=${inlineLegendStyles}><p>${this.maxValue} ${this.units}</p></div>`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const { tokenizedLight: sliderLight, tokenizedDark: sliderDark } = render
& > .range-wrapper {
position: relative;
width: 100%;
z-index: 20;
}
}
}
Expand Down Expand Up @@ -56,7 +57,7 @@ export const { tokenizedLight: sliderLight, tokenizedDark: sliderDark } = render
outline: none;
border-radius: 5px;
margin: 10px 0;
background-color: ${Forms.Slider.Track.Stroke.Inactive.Default};
background-color: transparent;
&::-webkit-slider-thumb {
-webkit-appearance: none;
Expand Down
Loading

0 comments on commit a1a7c15

Please sign in to comment.