Skip to content

Commit a8bc1e2

Browse files
committed
fix(range): prevent negative horizontal paddings
1 parent 1b233af commit a8bc1e2

File tree

3 files changed

+83
-3
lines changed

3 files changed

+83
-3
lines changed

packages/ods-react/src/components/range/src/components/range-tick/RangeTick.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ const RangeTick: FC<RangeTickProp> = ({
3434
if (index === 0) {
3535
setRootPadding((padding) => ({
3636
...padding,
37-
left: (width / 2) - (THUMB_SIZE / 2),
37+
left: Math.max(0, (width / 2) - (THUMB_SIZE / 2)),
3838
}));
3939
} else if (isLast) {
4040
setRootPadding((padding) => ({
4141
...padding,
42-
right: (width / 2) - (THUMB_SIZE / 2),
42+
right: Math.max(0, (width / 2) - (THUMB_SIZE / 2)),
4343
}));
4444
}
4545

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,60 @@
11
import 'jest-puppeteer';
2+
import { type Page } from 'puppeteer';
23
import { gotoStory } from '../../../../helpers/test';
34

5+
async function getRangePadding(page: Page): Promise<{ bottom?: number, left?: number, right?: number, top?: number }> {
6+
return page.evaluate(() => {
7+
function stripPx(value: string): number {
8+
return parseInt(value.split('px')[0], 10);
9+
}
10+
11+
const range = document.querySelector('[data-ods="range"]');
12+
13+
if (!range) {
14+
return {};
15+
}
16+
17+
const style = window.getComputedStyle(range);
18+
19+
return {
20+
bottom: stripPx(style.getPropertyValue('padding-bottom')),
21+
left: stripPx(style.getPropertyValue('padding-left')),
22+
right: stripPx(style.getPropertyValue('padding-right')),
23+
top: stripPx(style.getPropertyValue('padding-top')),
24+
};
25+
});
26+
}
27+
428
describe('Range rendering', () => {
529
it('should render the web component', async() => {
630
await gotoStory(page, 'rendering/render');
731

832
expect(await page.waitForSelector('[data-ods="range"]')).not.toBeNull();
933
});
34+
35+
describe('custom ticks', () => {
36+
it('should render with no horizontal padding if bound labels are small', async() => {
37+
await gotoStory(page, 'rendering/custom-small-ticks');
38+
await page.waitForSelector('[data-ods="range"]');
39+
40+
const padding = await getRangePadding(page);
41+
42+
expect(padding.bottom).toBeGreaterThan(0);
43+
expect(padding.left).toBe(0);
44+
expect(padding.right).toBe(0);
45+
expect(padding.top).toBeGreaterThan(0);
46+
});
47+
48+
it('should render with some horizontal padding if bound labels are large', async() => {
49+
await gotoStory(page, 'rendering/custom-large-ticks');
50+
await page.waitForSelector('[data-ods="range"]');
51+
52+
const padding = await getRangePadding(page);
53+
54+
expect(padding.bottom).toBeGreaterThan(0);
55+
expect(padding.left).toBeGreaterThan(0);
56+
expect(padding.right).toBeGreaterThan(0);
57+
expect(padding.top).toBeGreaterThan(0);
58+
});
59+
});
1060
});

packages/ods-react/src/components/range/tests/rendering/range.stories.tsx

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,36 @@ export default {
55
title: 'Tests rendering',
66
};
77

8-
export const render = () => (
8+
export const CustomLargeTicks = () => (
9+
<Range
10+
displayBounds={ false }
11+
displayTooltip={ false }
12+
max={ 5 }
13+
min={ 1 }
14+
ticks={[
15+
{ label: 'Very Poor', value: 1 },
16+
{ label: 'Poor', value: 2 },
17+
{ label: 'Average', value: 3 },
18+
{ label: 'Good', value: 4 },
19+
{ label: 'Excellent', value: 5 },
20+
]} />
21+
);
22+
23+
export const CustomSmallTicks = () => (
24+
<Range
25+
displayBounds={ false }
26+
displayTooltip={ false }
27+
max={ 5 }
28+
min={ 1 }
29+
ticks={[
30+
{ label: 'V', value: 1 },
31+
{ label: 'Poor', value: 2 },
32+
{ label: 'Average', value: 3 },
33+
{ label: 'Good', value: 4 },
34+
{ label: 'E', value: 5 },
35+
]} />
36+
);
37+
38+
export const Render = () => (
939
<Range />
1040
);

0 commit comments

Comments
 (0)