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 5, 2023
1 parent 26e550f commit e436ab8
Show file tree
Hide file tree
Showing 4 changed files with 194 additions and 17 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,28 @@ export class BlrRangeLegendMinMaxSlider extends LitElement {
[`${this.size || 'md'}`]: this.size || 'md',
});

const toolTipMinQuerySelector = this.shadowRoot?.querySelector(`#pip-${this.selectedStartIndex}`);
/*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 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,7 +141,7 @@ 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}"
Expand All @@ -148,7 +154,7 @@ 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}"
Expand All @@ -160,6 +166,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
16 changes: 9 additions & 7 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,9 +106,9 @@ 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}"
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
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);
});
});
});
14 changes: 14 additions & 0 deletions packages/ui-library/src/utils/range-slider-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@ import { css } from 'nested-css-to-flat/lit-css';
import { renderThemedCssStrings } from '../foundation/_tokens-generated/index.pseudo.generated';
import { ThemeType } from '../foundation/_tokens-generated/index.themes';

export const findToolTipPosition = (minVal: string, maxVal: string, offsetWidthVal: number, value: number) => {
const min = parseFloat(minVal);
const max = parseFloat(maxVal);
const width = offsetWidthVal;

// Calculate the percentage value
const percentage = ((value - min) / (max - min)) * 100;

// Calculate the tooltip position based on the percentage and width
const tooltipPosition = (percentage / 100) * width - 10;

return `${tooltipPosition.toString()}px`;
};

export const findNearestValue = (min: number, max: number, givenValue: number) => {
const percentage = givenValue / 100;
const range = max - min;
Expand Down

0 comments on commit e436ab8

Please sign in to comment.