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

[material-ui][TextField][FormLabel][InputLabel][FormControl] Use exact children type to allow React children type augmentation #38872

Merged
merged 9 commits into from
Jan 4, 2024
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ jobs:
- run:
name: Test module augmentation
command: |
yarn workspace @mui/material typescript:module-augmentation
yarn workspace @mui/material typescript:augmentation
yarn workspace @mui/base typescript:module-augmentation
yarn workspace @mui/joy typescript:module-augmentation
- run:
Expand Down
2 changes: 1 addition & 1 deletion packages/mui-material/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"release": "yarn build && npm publish build",
"test": "cd ../../ && cross-env NODE_ENV=test mocha 'packages/mui-material/**/*.test.{js,ts,tsx}'",
"typescript": "tsc -p tsconfig.json",
"typescript:module-augmentation": "node scripts/testModuleAugmentation.js"
"typescript:augmentation": "node scripts/testAugmentation.js"
},
"dependencies": {
"@babel/runtime": "^7.23.5",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async function test(tsconfigPath) {
}

/**
* Tests various module augmentation scenarios.
* Tests various augmentation scenarios.
* We can't run them with a single `tsc` run since these apply globally.
* Running them all would mean they're not isolated.
* Each test case represents a section in our docs.
Expand All @@ -30,7 +30,7 @@ async function test(tsconfigPath) {
* This script also allows us to test in parallel which we can't do with our mocha tests.
*/
async function main() {
const tsconfigPaths = await glob('test/typescript/moduleAugmentation/*.tsconfig.json', {
const tsconfigPaths = await glob('test/typescript/*Augmentation/*.tsconfig.json', {
absolute: true,
cwd: packageRoot,
});
Expand Down
2 changes: 1 addition & 1 deletion packages/mui-material/src/FormControl/FormControl.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export interface FormControlOwnProps {
/**
* The content of the component.
*/
children?: React.ReactNode;
children?: React.HTMLAttributes<HTMLDivElement>['children'];
/**
* Override or extend the styles applied to the component.
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/mui-material/src/FormLabel/FormLabel.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export interface FormLabelOwnProps {
/**
* The content of the component.
*/
children?: React.ReactNode;
children?: React.LabelHTMLAttributes<HTMLLabelElement>['children'];
/**
* Override or extend the styles applied to the component.
*/
Expand Down
6 changes: 1 addition & 5 deletions packages/mui-material/src/InputLabel/InputLabel.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ import { OverridableComponent, OverrideProps } from '../OverridableComponent';

export interface InputLabelPropsSizeOverrides {}

export interface InputLabelOwnProps {
/**
* The content of the component.
*/
children?: React.ReactNode;
export interface InputLabelOwnProps extends Pick<FormLabelProps, 'children'> {
/**
* Override or extend the styles applied to the component.
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/mui-material/src/TextField/TextField.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export interface BaseTextFieldProps
/**
* @ignore
*/
children?: React.ReactNode;
children?: FormControlProps['children'];
/**
* Override or extend the styles applied to the component.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as React from 'react';
ZeeshanTamboli marked this conversation as resolved.
Show resolved Hide resolved
import Autocomplete from '@mui/material/Autocomplete';
import TextField from '@mui/material/TextField';

type AugmentedChildren = React.ReactNode | Record<string, unknown>;

// Update React's children prop type
declare module 'react' {
interface HTMLAttributes<T> {
children?: AugmentedChildren | Iterable<AugmentedChildren>;
}
}

// Rendering a TextField for the Autocomplete should work
<Autocomplete options={[{ label: 'test' }]} renderInput={(params) => <TextField {...params} />} />;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"compilerOptions": {
"skipLibCheck": true
},
"extends": "../../../../../tsconfig",
"files": ["children.spec.tsx"]
}
6 changes: 5 additions & 1 deletion packages/mui-material/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{
"extends": "../../tsconfig.json",
"include": ["src/**/*", "test/**/*"],
"exclude": ["test/typescript/moduleAugmentation", "src/types/OverridableComponentAugmentation.ts"]
"exclude": [
"test/typescript/moduleAugmentation",
"test/typescript/reactAugmentation",
"src/types/OverridableComponentAugmentation.ts"
]
}
Loading