Skip to content

Commit

Permalink
[codemod] Rename shouldDisableTime prop (#7709)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfauquette authored Jan 26, 2023
1 parent 6171427 commit e7d92df
Show file tree
Hide file tree
Showing 13 changed files with 127 additions and 7 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ We'd like to offer a big thanks to the 9 contributors who made this release poss
```diff
<DateTimePicker
- shouldDisableTime={(timeValue, view) => view === 'hours' && timeValue < 12}
+ shouldDisableTime={(time, view) => view === 'hours' && value.hour() < 12}
+ shouldDisableTime={(value, view) => view === 'hours' && value.hour() < 12}
/>
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,9 @@ The picker components no longer have a keyboard view to render the input inside
At some point, the mobile pickers should have a prop allowing to have an editable field without opening the modal.
:::

### Rename `shouldDisableTime` prop
### Rename `shouldDisableTime` prop

The `shouldDisableTime` prop signature has been changed. Either rename the prop usage to `shouldDisableClock` or refactor usage.
The `shouldDisableTime` prop signature has been changed. Either rename the prop to `shouldDisableClock` or refactor usage.

```diff
<DateTimePicker
Expand All @@ -150,7 +150,7 @@ The `shouldDisableTime` prop signature has been changed. Either rename the prop

<DateTimePicker
- shouldDisableTime={(timeValue, view) => view === 'hours' && timeValue < 12}
+ shouldDisableTime={(time, view) => view === 'hours' && value.hour() < 12}
+ shouldDisableTime={(value, view) => view === 'hours' && value.hour() < 12}
/>
```

Expand Down
2 changes: 1 addition & 1 deletion docs/scripts/addDemoItemsAttributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default function transformer(file, api, options) {
root
.find(j.ImportDeclaration)
.filter(({ node }) => {
return node.source.value.startsWith('@mui/x-date-picker');
return node.source.value.startsWith('@mui/x-date-pickers');
})
.forEach((importPath) => {
importPath.node.specifiers.forEach((node) => {
Expand Down
16 changes: 16 additions & 0 deletions packages/x-codemod/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ The list includes these transformers
- [`replace-toolbar-props-by-slot`](#replace-toolbar-props-by-slot)
- [`migrate-to-components-componentsProps`](#migrate-to-components-componentsProps)
- [`replace-arrows-button-slot`](#replace-arrows-button-slot)
- [`rename-should-disable-time`](#rename-should-disable-time)

#### `adapter-change-import`

Expand Down Expand Up @@ -325,6 +326,21 @@ Replace `LeftArrowButton` and `RightArrowButton` slots for navigation buttons by
npx @mui/x-codemod v6.0.0/pickers/replace-arrows-button-slot <path>
```

#### `rename-should-disable-time`

Replace `shouldDisableTime` by `shouldDisableClock`.

```diff
<DateTimePicker
- shouldDisableTime={(timeValue, view) => view === 'hours' && timeValue < 12}
+ shouldDisableClock={(timeValue, view) => view === 'hours' && timeValue < 12}
/>
```

```sh
npx @mui/x-codemod v6.0.0/pickers/rename-should-disable-time <path>
```

#### `rename-components-to-slots`

Renames the `components` and `componentsProps` props to `slots` and `slotProps`, respectively.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ function App() {
dateRangeIcon={<LightModeIcon />}
TransitionComponent={Fade}
DialogProps={{ backgroundColor: 'red' }}
shouldDisableTime={(timeValue, view) => view === 'hours' && timeValue < 12}
/>
<DateRangePicker
ToolbarComponent={CustomToolbarComponent}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ function App() {
okButtonLabel: "string_okText"
}} />
<DateTimePicker
shouldDisableClock={(timeValue, view) => view === 'hours' && timeValue < 12}
components={{
DesktopTransition: Fade,
}}
Expand Down
2 changes: 2 additions & 0 deletions packages/x-codemod/src/v6.0.0/pickers/preset-safe/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import transformReplaceTabsProps from '../replace-tabs-props';
import transformReplaceToolbarPropsBySlot from '../replace-toolbar-props-by-slot';
import transformMigrateToComponentsComponetsProps from '../migrate-to-components-componentsProps';
import transformReplaceArrowsButtonSlot from '../replace-arrows-button-slot';
import transformRenameShouldDisableTime from '../rename-should-disable-time';

import { JsCodeShiftAPI, JsCodeShiftFileInfo } from '../../../types';

Expand All @@ -23,6 +24,7 @@ export default function transformer(file: JsCodeShiftFileInfo, api: JsCodeShiftA
file.source = transformReplaceToolbarPropsBySlot(file, api, options);
file.source = transformMigrateToComponentsComponetsProps(file, api, options);
file.source = transformReplaceArrowsButtonSlot(file, api, options);
file.source = transformRenameShouldDisableTime(file, api, options);

return file.source;
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default function transformer(file, api, options) {
root
.find(j.ImportDeclaration)
.filter(({ node }) => {
return node.source.value.startsWith('@mui/x-date-picker');
return node.source.value.startsWith('@mui/x-date-pickers');
})

.forEach((path) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { DateTimePicker } from '@mui/x-date-pickers/DateTimePicker';
import { TimePicker } from '@mui/x-date-pickers/TimePicker';

const shouldDisable = (timeValue, view) => view === 'hours' && timeValue < 12;

<div>
<DateTimePicker shouldDisableTime={(timeValue, view) => view === 'hours' && timeValue < 12} />
<TimePicker shouldDisableTime={shouldDisable} />
</div>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { DateTimePicker } from '@mui/x-date-pickers/DateTimePicker';
import { TimePicker } from '@mui/x-date-pickers/TimePicker';

const shouldDisable = (timeValue, view) => view === 'hours' && timeValue < 12;

<div>
<DateTimePicker shouldDisableClock={(timeValue, view) => view === 'hours' && timeValue < 12} />
<TimePicker shouldDisableClock={shouldDisable} />
</div>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* @param {import('jscodeshift').FileInfo} file
* @param {import('jscodeshift').API} api
*/
export default function transformer(file, api, options) {
const j = api.jscodeshift;

const printOptions = options.printOptions;

const root = j(file.source);

root
.find(j.ImportDeclaration)
.filter(({ node }) => {
return node.source.value.startsWith('@mui/x-date-pickers');
})

.forEach((path) => {
path.node.specifiers.forEach((node) => {
// Process only date-pickers components
root.findJSXElements(node.local.name).forEach((elementPath) => {
if (elementPath.node.type !== 'JSXElement') {
return;
}

elementPath.node.openingElement.attributes.forEach((elementNode) => {
if (elementNode.type !== 'JSXAttribute') {
return;
}
if (elementNode.name.name === 'shouldDisableTime') {
elementNode.name.name = 'shouldDisableClock';
}
});
});
});
});

const transformed = root.findJSXElements();

return transformed.toSource(printOptions);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import path from 'path';
import { expect } from 'chai';
import jscodeshift from 'jscodeshift';
import transform from './index';
import readFile from '../../../util/readFile';

function read(fileName) {
return readFile(path.join(__dirname, fileName));
}

describe('v6.0.0/pickers', () => {
describe('rename-should-disable-time', () => {
it('transforms props as needed', () => {
const actual = transform(
{
source: read('./actual-props.spec.js'),
path: require.resolve('./actual-props.spec.js'),
},
{ jscodeshift },
{},
);

const expected = read('./expected-props.spec.js');
expect(actual).to.equal(expected, 'The transformed version should be correct');
});

it('should be idempotent for expression', () => {
const actual = transform(
{
source: read('./expected-props.spec.js'),
path: require.resolve('./expected-props.spec.js'),
},
{ jscodeshift },
{},
);

const expected = read('./expected-props.spec.js');
expect(actual).to.equal(expected, 'The transformed version should be correct');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default function transformer(file, api, options) {
root
.find(j.ImportDeclaration)
.filter(({ node }) => {
return node.source.value.startsWith('@mui/x-date-picker');
return node.source.value.startsWith('@mui/x-date-pickers');
})

.forEach((path) => {
Expand Down

0 comments on commit e7d92df

Please sign in to comment.