Skip to content

Commit

Permalink
[core] Handle refType in the proptypes generation
Browse files Browse the repository at this point in the history
  • Loading branch information
flaviendelangle committed Sep 11, 2023
1 parent cda1ffe commit fd79bd4
Show file tree
Hide file tree
Showing 16 changed files with 152 additions and 126 deletions.
10 changes: 2 additions & 8 deletions packages/mui-base/src/Button/Button.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use client';
import * as React from 'react';
import { refType } from '@mui/utils';
import PropTypes from 'prop-types';
import { PolymorphicComponent } from '../utils/PolymorphicComponent';
import { unstable_composeClasses as composeClasses } from '../composeClasses';
Expand Down Expand Up @@ -97,14 +98,7 @@ Button.propTypes /* remove-proptypes */ = {
/**
* A ref for imperative actions. It currently only supports `focusVisible()` action.
*/
action: PropTypes.oneOfType([
PropTypes.func,
PropTypes.shape({
current: PropTypes.shape({
focusVisible: PropTypes.func.isRequired,
}),
}),
]),
action: refType,
/**
* @ignore
*/
Expand Down
8 changes: 2 additions & 6 deletions packages/mui-base/src/Input/Input.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use client';
import * as React from 'react';
import { refType } from '@mui/utils';
import PropTypes from 'prop-types';
import { PolymorphicComponent } from '../utils/PolymorphicComponent';
import { isHostComponent } from '../utils/isHostComponent';
Expand Down Expand Up @@ -244,12 +245,7 @@ Input.propTypes /* remove-proptypes */ = {
/**
* @ignore
*/
inputRef: PropTypes.oneOfType([
PropTypes.func,
PropTypes.shape({
current: PropTypes.object,
}),
]),
inputRef: refType,
/**
* Maximum number of rows to display when multiline option is set to true.
*/
Expand Down
11 changes: 2 additions & 9 deletions packages/mui-base/src/Tab/Tab.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use client';
import * as React from 'react';
import PropTypes from 'prop-types';
import { unstable_useForkRef as useForkRef } from '@mui/utils';
import { unstable_useForkRef as useForkRef, refType } from '@mui/utils';
import { unstable_composeClasses as composeClasses } from '../composeClasses';
import { getTabUtilityClass } from './tabClasses';
import { TabProps, TabTypeMap, TabRootSlotProps, TabOwnerState } from './Tab.types';
Expand Down Expand Up @@ -87,14 +87,7 @@ Tab.propTypes /* remove-proptypes */ = {
/**
* A ref for imperative actions. It currently only supports `focusVisible()` action.
*/
action: PropTypes.oneOfType([
PropTypes.func,
PropTypes.shape({
current: PropTypes.shape({
focusVisible: PropTypes.func.isRequired,
}),
}),
]),
action: refType,
/**
* @ignore
*/
Expand Down
15 changes: 6 additions & 9 deletions packages/mui-joy/src/Button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import PropTypes from 'prop-types';
import { useButton } from '@mui/base/useButton';
import { unstable_composeClasses as composeClasses } from '@mui/base/composeClasses';
import { Interpolation } from '@mui/system';
import { unstable_capitalize as capitalize, unstable_useForkRef as useForkRef } from '@mui/utils';
import {
unstable_capitalize as capitalize,
unstable_useForkRef as useForkRef,
refType,
} from '@mui/utils';
import { styled, Theme, useThemeProps } from '../styles';
import { useColorInversion } from '../styles/ColorInversion';
import useSlot from '../utils/useSlot';
Expand Down Expand Up @@ -325,14 +329,7 @@ Button.propTypes /* remove-proptypes */ = {
/**
* A ref for imperative actions. It currently only supports `focusVisible()` action.
*/
action: PropTypes.oneOfType([
PropTypes.func,
PropTypes.shape({
current: PropTypes.shape({
focusVisible: PropTypes.func.isRequired,
}),
}),
]),
action: refType,
/**
* @ignore
*/
Expand Down
15 changes: 6 additions & 9 deletions packages/mui-joy/src/IconButton/IconButton.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
'use client';
import * as React from 'react';
import PropTypes from 'prop-types';
import { unstable_capitalize as capitalize, unstable_useForkRef as useForkRef } from '@mui/utils';
import {
unstable_capitalize as capitalize,
unstable_useForkRef as useForkRef,
refType,
} from '@mui/utils';
import { useButton } from '@mui/base/useButton';
import { unstable_composeClasses as composeClasses } from '@mui/base/composeClasses';
import { styled, useThemeProps } from '../styles';
Expand Down Expand Up @@ -201,14 +205,7 @@ IconButton.propTypes /* remove-proptypes */ = {
/**
* A ref for imperative actions. It currently only supports `focusVisible()` action.
*/
action: PropTypes.oneOfType([
PropTypes.func,
PropTypes.shape({
current: PropTypes.shape({
focusVisible: PropTypes.func.isRequired,
}),
}),
]),
action: refType,
/**
* @ignore
*/
Expand Down
15 changes: 6 additions & 9 deletions packages/mui-joy/src/ListItemButton/ListItemButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
import * as React from 'react';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import { unstable_capitalize as capitalize, unstable_useForkRef as useForkRef } from '@mui/utils';
import {
unstable_capitalize as capitalize,
unstable_useForkRef as useForkRef,
refType,
} from '@mui/utils';
import { unstable_composeClasses as composeClasses } from '@mui/base/composeClasses';
import { useButton } from '@mui/base/useButton';
import { styled, useThemeProps } from '../styles';
Expand Down Expand Up @@ -213,14 +217,7 @@ ListItemButton.propTypes /* remove-proptypes */ = {
/**
* A ref for imperative actions. It currently only supports `focusVisible()` action.
*/
action: PropTypes.oneOfType([
PropTypes.func,
PropTypes.shape({
current: PropTypes.shape({
focusVisible: PropTypes.func.isRequired,
}),
}),
]),
action: refType,
/**
* If `true`, the list item is focused during the first mount.
* Focus will also be triggered if the value changes from false to true.
Expand Down
15 changes: 6 additions & 9 deletions packages/mui-joy/src/Select/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import * as React from 'react';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import { OverrideProps, DefaultComponentProps } from '@mui/types';
import { unstable_capitalize as capitalize, unstable_useForkRef as useForkRef } from '@mui/utils';
import {
unstable_capitalize as capitalize,
unstable_useForkRef as useForkRef,
refType,
} from '@mui/utils';
import { Popper, PopperProps } from '@mui/base/Popper';
import { useSelect, SelectProvider } from '@mui/base/useSelect';
import { SelectOption } from '@mui/base/useOption';
Expand Down Expand Up @@ -625,14 +629,7 @@ Select.propTypes /* remove-proptypes */ = {
/**
* A ref for imperative actions. It currently only supports `focusVisible()` action.
*/
action: PropTypes.oneOfType([
PropTypes.func,
PropTypes.shape({
current: PropTypes.shape({
focusVisible: PropTypes.func.isRequired,
}),
}),
]),
action: refType,
/**
* If `true`, the select element is focused during the first mount
* @default false
Expand Down
15 changes: 6 additions & 9 deletions packages/mui-joy/src/Tab/Tab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
import * as React from 'react';
import PropTypes from 'prop-types';
import { OverridableComponent } from '@mui/types';
import { unstable_capitalize as capitalize, unstable_useForkRef as useForkRef } from '@mui/utils';
import {
unstable_capitalize as capitalize,
unstable_useForkRef as useForkRef,
refType,
} from '@mui/utils';
import { unstable_composeClasses as composeClasses } from '@mui/base';
import { useTab } from '@mui/base/useTab';
import { StyledListItemButton } from '../ListItemButton/ListItemButton';
Expand Down Expand Up @@ -215,14 +219,7 @@ Tab.propTypes /* remove-proptypes */ = {
/**
* A ref for imperative actions. It currently only supports `focusVisible()` action.
*/
action: PropTypes.oneOfType([
PropTypes.func,
PropTypes.shape({
current: PropTypes.shape({
focusVisible: PropTypes.func.isRequired,
}),
}),
]),
action: refType,
/**
* @ignore
*/
Expand Down
10 changes: 1 addition & 9 deletions packages/mui-material-next/src/ButtonBase/ButtonBase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -299,15 +299,7 @@ ButtonBase.propTypes /* remove-proptypes */ = {
/**
* A ref that points to the `TouchRipple` element.
*/
touchRippleRef: PropTypes.oneOfType([
PropTypes.func,
PropTypes.shape({
current: PropTypes.shape({
start: PropTypes.func.isRequired,
stop: PropTypes.func.isRequired,
}),
}),
]),
touchRippleRef: refType,
/**
* Type attribute applied to the root component.
* @default 'button'
Expand Down
11 changes: 1 addition & 10 deletions packages/mui-material/src/ButtonBase/ButtonBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -521,16 +521,7 @@ ButtonBase.propTypes /* remove-proptypes */ = {
/**
* A ref that points to the `TouchRipple` element.
*/
touchRippleRef: PropTypes.oneOfType([
PropTypes.func,
PropTypes.shape({
current: PropTypes.shape({
pulsate: PropTypes.func.isRequired,
start: PropTypes.func.isRequired,
stop: PropTypes.func.isRequired,
}),
}),
]),
touchRippleRef: refType,
/**
* @ignore
*/
Expand Down
8 changes: 8 additions & 0 deletions packages/typescript-to-proptypes/src/createType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
StringType,
ObjectType,
NumericType,
ReactRefType,
} from './models';

export function createAnyType(init: { jsDoc: string | undefined }): AnyType {
Expand Down Expand Up @@ -64,6 +65,13 @@ export function createElementType(init: {
};
}

export function createReactRefType(init: { jsDoc: string | undefined }): ReactRefType {
return {
type: 'ReactRefNode',
jsDoc: init.jsDoc,
};
}

export function createFunctionType(init: { jsDoc: string | undefined }): FunctionType {
return {
type: 'FunctionNode',
Expand Down
4 changes: 4 additions & 0 deletions packages/typescript-to-proptypes/src/generatePropTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ export function generatePropTypes(
return `${importedName}.bool`;
}

if (propType.type === 'ReactRefNode') {
return 'refType';
}

if (propType.type === 'NumericNode') {
return `${importedName}.number`;
}
Expand Down
18 changes: 18 additions & 0 deletions packages/typescript-to-proptypes/src/getPropTypesFromFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
createNumericType,
createObjectType,
createStringType,
createReactRefType,
} from './createType';
import { PropTypeDefinition, PropTypesComponent, PropType } from './models';

Expand Down Expand Up @@ -291,6 +292,23 @@ function checkSymbol({
ts.isTypeReferenceNode(declaration.type)
) {
const name = declaration.type.typeName.getText();

if (name === 'React.Ref') {
return {
$$id: project.createPropTypeId(symbol),
name: symbol.getName(),
jsDoc,
filenames: symbolFilenames,
propType: createUnionType({
jsDoc,
types: [
createUndefinedType({ jsDoc: undefined }),
createReactRefType({ jsDoc: undefined }),
],
}),
};
}

if (
name === 'React.ElementType' ||
name === 'React.JSXElementConstructor' ||
Expand Down
Loading

0 comments on commit fd79bd4

Please sign in to comment.