Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Slider][base] Convert code to TypeScript #35445

Merged
merged 48 commits into from
Jan 16, 2023
Merged
Show file tree
Hide file tree
Changes from 43 commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
a538329
converted SliderUnstyled.js to tsx
sai6855 Dec 10, 2022
73387c3
ownerstate type fix
sai6855 Dec 11, 2022
5e4bede
fix types
sai6855 Dec 11, 2022
c1fa4cd
migrated SliderValueLabelUnstyled
sai6855 Dec 11, 2022
2e756c2
migrated SliderValue.test file to ts
sai6855 Dec 11, 2022
a30aeb6
removed comments
sai6855 Dec 11, 2022
6da4b58
removed type declarations files
sai6855 Dec 11, 2022
8590645
proptypes
sai6855 Dec 11, 2022
4b9c3cd
changed Identity func type
sai6855 Dec 11, 2022
3b8e7ba
prettier
sai6855 Dec 11, 2022
0a0c4aa
type fix
sai6855 Dec 11, 2022
f83fc59
type fix
sai6855 Dec 11, 2022
a02f9f1
type fix
sai6855 Dec 11, 2022
699e4fe
prettier
sai6855 Dec 11, 2022
af693e5
type fix
sai6855 Dec 11, 2022
a534f0c
type fix
sai6855 Dec 11, 2022
01ae70d
yarn proptypes
sai6855 Dec 11, 2022
3726d24
added Identity
sai6855 Dec 11, 2022
da276ef
type fix
sai6855 Dec 11, 2022
92f7dcb
dep path change
sai6855 Dec 11, 2022
624c913
path fix
sai6855 Dec 11, 2022
6c2e9e2
api docs fix
sai6855 Dec 11, 2022
00141ac
lint fix
sai6855 Dec 11, 2022
75f08e5
cycle dependency fix
sai6855 Dec 12, 2022
34a8406
type fix in test
sai6855 Dec 12, 2022
d253ba6
prop type fix
sai6855 Dec 12, 2022
b83a80c
prettier
sai6855 Dec 12, 2022
2a9bd32
import fix
sai6855 Dec 12, 2022
d3d86a9
import fix
sai6855 Dec 12, 2022
03393bc
review changes
sai6855 Dec 12, 2022
a6a6439
api doc fix
sai6855 Dec 12, 2022
9627731
type patches
sai6855 Dec 21, 2022
d107713
inline comment
sai6855 Dec 21, 2022
34b11f8
comment patch
sai6855 Dec 21, 2022
8a172dc
fixes
sai6855 Dec 22, 2022
7cb8746
Merge branch 'master' into slider-typescript
ZeeshanTamboli Dec 22, 2022
c6d9e1b
Merge branch 'master' into slider-typescript
sai6855 Dec 24, 2022
435e0f1
useValueLabelClasses patch
sai6855 Dec 26, 2022
3be15fb
change name
sai6855 Dec 26, 2022
2c13554
lint fix
sai6855 Dec 26, 2022
326b490
reset name
sai6855 Dec 26, 2022
16d08d2
removed open prop
sai6855 Dec 26, 2022
f5f06ca
add ownerstate
sai6855 Dec 27, 2022
1b46fdf
Merge branch 'master' into slider-typescript
ZeeshanTamboli Jan 16, 2023
4967a89
replace container.firstChild with sliderRoot
ZeeshanTamboli Jan 16, 2023
b17e36c
remove unnecessary type casting
ZeeshanTamboli Jan 16, 2023
eed6924
Revert "remove unnecessary type casting"
ZeeshanTamboli Jan 16, 2023
16a378e
fix api docs
ZeeshanTamboli Jan 16, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions docs/pages/base/api/slider-unstyled.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
"type": { "name": "enum", "description": "'horizontal'<br>&#124;&nbsp;'vertical'" },
"default": "'horizontal'"
},
"scale": { "type": { "name": "func" }, "default": "(x) => x" },
"scale": {
"type": { "name": "func" },
"default": "function Identity(x: any) {\n return x;\n}"
},
"slotProps": {
"type": {
"name": "shape",
Expand Down Expand Up @@ -62,7 +65,7 @@
},
"valueLabelFormat": {
"type": { "name": "union", "description": "func<br>&#124;&nbsp;string" },
"default": "(x) => x"
"default": "function Identity(x: any) {\n return x;\n}"
}
},
"name": "SliderUnstyled",
Expand Down Expand Up @@ -115,7 +118,7 @@
},
"spread": true,
"forwardsRefTo": "HTMLSpanElement",
"filename": "/packages/mui-base/src/SliderUnstyled/SliderUnstyled.js",
"filename": "/packages/mui-base/src/SliderUnstyled/SliderUnstyled.tsx",
"inheritance": null,
"demos": "<ul><li><a href=\"/base/react-slider/\">Unstyled Slider</a></li></ul>",
"cssComponent": false
Expand Down
7 changes: 5 additions & 2 deletions docs/pages/material-ui/api/slider.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@
"type": { "name": "enum", "description": "'horizontal'<br>&#124;&nbsp;'vertical'" },
"default": "'horizontal'"
},
"scale": { "type": { "name": "func" }, "default": "(x) => x" },
"scale": {
"type": { "name": "func" },
"default": "function Identity(x: any) {\n return x;\n}"
},
"size": {
"type": {
"name": "union",
Expand Down Expand Up @@ -94,7 +97,7 @@
},
"valueLabelFormat": {
"type": { "name": "union", "description": "func<br>&#124;&nbsp;string" },
"default": "(x) => x"
"default": "function Identity(x: any) {\n return x;\n}"
}
},
"name": "Slider",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"onChange": "Callback function that is fired when the slider&#39;s value changed.<br><br><strong>Signature:</strong><br><code>function(event: Event, value: number | Array&lt;number&gt;, activeThumb: number) =&gt; void</code><br><em>event:</em> The event source of the callback. You can pull out the new value by accessing <code>event.target.value</code> (any). <strong>Warning</strong>: This is a generic event not a change event.<br><em>value:</em> The new value.<br><em>activeThumb:</em> Index of the currently moved thumb.",
"onChangeCommitted": "Callback function that is fired when the <code>mouseup</code> is triggered.<br><br><strong>Signature:</strong><br><code>function(event: React.SyntheticEvent | Event, value: number | Array&lt;number&gt;) =&gt; void</code><br><em>event:</em> The event source of the callback. <strong>Warning</strong>: This is a generic event not a change event.<br><em>value:</em> The new value.",
"orientation": "The component orientation.",
"scale": "A transformation function, to change the scale of the slider.",
"scale": "A transformation function, to change the scale of the slider.<br><br><strong>Signature:</strong><br><code>function(x: any) =&gt; any</code><br>",
"slotProps": "The props used for each slot inside the Slider.",
"slots": "The components used for each slot inside the Slider. Either a string to use a HTML element or a component.",
"step": "The granularity with which the slider can step through values. (A &quot;discrete&quot; slider.) The <code>min</code> prop serves as the origin for the valid values. We recommend (max - min) to be evenly divisible by the step.<br>When step is <code>null</code>, the thumb can only be slid onto marks provided with the <code>marks</code> prop.",
Expand Down
2 changes: 1 addition & 1 deletion docs/translations/api-docs/slider/slider.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"onChange": "Callback function that is fired when the slider&#39;s value changed.<br><br><strong>Signature:</strong><br><code>function(event: Event, value: number | Array&lt;number&gt;, activeThumb: number) =&gt; void</code><br><em>event:</em> The event source of the callback. You can pull out the new value by accessing <code>event.target.value</code> (any). <strong>Warning</strong>: This is a generic event not a change event.<br><em>value:</em> The new value.<br><em>activeThumb:</em> Index of the currently moved thumb.",
"onChangeCommitted": "Callback function that is fired when the <code>mouseup</code> is triggered.<br><br><strong>Signature:</strong><br><code>function(event: React.SyntheticEvent | Event, value: number | Array&lt;number&gt;) =&gt; void</code><br><em>event:</em> The event source of the callback. <strong>Warning</strong>: This is a generic event not a change event.<br><em>value:</em> The new value.",
"orientation": "The component orientation.",
"scale": "A transformation function, to change the scale of the slider.",
"scale": "A transformation function, to change the scale of the slider.<br><br><strong>Signature:</strong><br><code>function(x: any) =&gt; any</code><br>",
"size": "The size of the slider.",
"slotProps": "The props used for each slot inside the Slider.",
"slots": "The components used for each slot inside the Slider. Either a string to use a HTML element or a component.",
Expand Down
16 changes: 0 additions & 16 deletions packages/mui-base/src/SliderUnstyled/SliderUnstyled.d.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import SliderUnstyled, { sliderUnstyledClasses as classes } from '@mui/base/SliderUnstyled';
import SliderUnstyled, {
sliderUnstyledClasses as classes,
SliderUnstyledRootSlotProps,
} from '@mui/base/SliderUnstyled';
import { expect } from 'chai';
import * as React from 'react';
import { spy, stub } from 'sinon';
Expand All @@ -10,7 +13,9 @@ import {
screen,
} from 'test/utils';

function createTouches(touches) {
type Touches = Array<Pick<Touch, 'identifier' | 'clientX' | 'clientY'>>;

function createTouches(touches: Touches) {
return {
changedTouches: touches.map(
(touch) =>
Expand Down Expand Up @@ -61,10 +66,19 @@ describe('<SliderUnstyled />', () => {
let theme = null;

const Root = React.forwardRef(
({ ownerState: ownerStateProp, theme: themeProp, ...other }, ref) => {
(
{
ownerState: ownerStateProp,
theme: themeProp,
...other
}: SliderUnstyledRootSlotProps & {
theme: any;
},
ref: React.ForwardedRef<HTMLSpanElement>,
) => {
ownerState = ownerStateProp;
theme = themeProp;
return <span ref={ref} {...other} />;
return <span {...other} ref={ref} />;
},
);

Expand All @@ -75,7 +89,7 @@ describe('<SliderUnstyled />', () => {
});

it('does not forward style props as DOM attributes if component slot is primitive', () => {
const elementRef = React.createRef();
const elementRef = React.createRef<HTMLSpanElement>();
render(
<SliderUnstyled
slots={{
Expand All @@ -86,8 +100,10 @@ describe('<SliderUnstyled />', () => {
);

const { current: element } = elementRef;
expect(element.getAttribute('ownerState')).to.equal(null);
expect(element.getAttribute('theme')).to.equal(null);
if (element !== null) {
expect(element.getAttribute('ownerState')).to.equal(null);
expect(element.getAttribute('theme')).to.equal(null);
}
});

describe('prop: marks', () => {
Expand Down Expand Up @@ -136,31 +152,42 @@ describe('<SliderUnstyled />', () => {
});
});

[
type Values = Array<[string, number[]]>;

const values = [
['readonly range', Object.freeze([2, 1])],
['range', [2, 1]],
].forEach(([valueLabel, value]) => {
] as Values;
values.forEach(([valueLabel, value]) => {
it(`calls onChange even if the ${valueLabel} did not change`, () => {
const handleChange = spy();

const { container } = render(
<SliderUnstyled min={0} max={5} onChange={handleChange} value={value} />,
);
stub(container.firstChild, 'getBoundingClientRect').callsFake(() => ({
width: 100,
height: 10,
bottom: 10,
left: 0,
}));

if (container.firstChild !== null) {
// @ts-expect-error - container.firstChild doesn't have getBoundingClientRect method.
stub(container.firstChild, 'getBoundingClientRect').callsFake(() => ({
ZeeshanTamboli marked this conversation as resolved.
Show resolved Hide resolved
width: 100,
height: 10,
bottom: 10,
left: 0,
}));
}

// pixel: 0 20 40 60 80 100
// slider: |---|---|---|---|---|
// values: 0 1 2 3 4 5
// value: ↑ ↑
// mouse: ↑
fireEvent.mouseDown(container.firstChild, {
buttons: 1,
clientX: 41,
});

if (container.firstChild !== null) {
fireEvent.mouseDown(container.firstChild, {
buttons: 1,
clientX: 41,
});
}

expect(handleChange.callCount).to.equal(1);
expect(handleChange.args[0][1]).not.to.equal(value);
Expand All @@ -183,17 +210,19 @@ describe('<SliderUnstyled />', () => {

const { getByRole, setProps, container } = render(<SliderUnstyled defaultValue={0} />);

stub(container.firstChild, 'getBoundingClientRect').callsFake(() => ({
width: 100,
height: 10,
bottom: 10,
left: 0,
}));

fireEvent.touchStart(
container.firstChild,
createTouches([{ identifier: 1, clientX: 21, clientY: 0 }]),
);
if (container.firstChild !== null) {
// @ts-expect-error - container.firstChild doesn't have getBoundingClientRect method.
stub(container.firstChild, 'getBoundingClientRect').callsFake(() => ({
width: 100,
height: 10,
bottom: 10,
left: 0,
}));
fireEvent.touchStart(
container.firstChild,
createTouches([{ identifier: 1, clientX: 21, clientY: 0 }]),
);
}

const thumb = getByRole('slider');

Expand All @@ -204,10 +233,12 @@ describe('<SliderUnstyled />', () => {
expect(thumb).not.toHaveFocus();
expect(thumb).not.to.have.class(classes.active);

fireEvent.touchMove(
container.firstChild,
createTouches([{ identifier: 1, clientX: 30, clientY: 0 }]),
);
if (container.firstChild !== null) {
fireEvent.touchMove(
container.firstChild,
createTouches([{ identifier: 1, clientX: 30, clientY: 0 }]),
);
}

expect(thumb).to.have.attribute('aria-valuenow', '21');
});
Expand All @@ -220,18 +251,19 @@ describe('<SliderUnstyled />', () => {

const { getByRole, container } = render(<SliderUnstyled disabled defaultValue={21} />);
const thumb = getByRole('slider');

stub(container.firstChild, 'getBoundingClientRect').callsFake(() => ({
width: 100,
height: 10,
bottom: 10,
left: 0,
}));

fireEvent.touchStart(
container.firstChild,
createTouches([{ identifier: 1, clientX: 21, clientY: 0 }]),
);
if (container.firstChild) {
// @ts-expect-error - container.firstChild doesn't have getBoundingClientRect method.
stub(container.firstChild, 'getBoundingClientRect').callsFake(() => ({
width: 100,
height: 10,
bottom: 10,
left: 0,
}));
fireEvent.touchStart(
container.firstChild,
createTouches([{ identifier: 1, clientX: 21, clientY: 0 }]),
);
}

fireEvent.touchMove(
document.body,
Expand Down
Loading