From d5a8479abce9863a85a93d22b656344d5485bc6c Mon Sep 17 00:00:00 2001 From: Benny Joo Date: Fri, 28 Apr 2023 15:24:42 +0100 Subject: [PATCH] [Switch][base] Drop component prop (#37053) --- .../switch/UnstyledSwitchIntroduction.js | 32 ++++++++++++++++--- .../switch/UnstyledSwitchIntroduction.tsx | 32 ++++++++++++++++--- .../UnstyledSwitchIntroduction.tsx.preview | 4 --- .../components/switch/UnstyledSwitches.js | 32 ++++++++++++++++--- .../components/switch/UnstyledSwitches.tsx | 32 ++++++++++++++++--- .../switch/UnstyledSwitches.tsx.preview | 4 --- docs/data/base/components/switch/switch.md | 26 ++++++++++----- docs/pages/base/api/switch.json | 1 - .../api-docs-base/switch/switch.json | 1 - packages/mui-base/src/Switch/Switch.spec.tsx | 31 ++++++++++++++---- packages/mui-base/src/Switch/Switch.test.tsx | 1 + packages/mui-base/src/Switch/Switch.tsx | 20 ++---------- packages/mui-base/src/Switch/Switch.types.ts | 8 ++--- 13 files changed, 162 insertions(+), 62 deletions(-) delete mode 100644 docs/data/base/components/switch/UnstyledSwitchIntroduction.tsx.preview delete mode 100644 docs/data/base/components/switch/UnstyledSwitches.tsx.preview diff --git a/docs/data/base/components/switch/UnstyledSwitchIntroduction.js b/docs/data/base/components/switch/UnstyledSwitchIntroduction.js index 8e3688bea77832..c1cdcd95790e1c 100644 --- a/docs/data/base/components/switch/UnstyledSwitchIntroduction.js +++ b/docs/data/base/components/switch/UnstyledSwitchIntroduction.js @@ -7,10 +7,34 @@ export default function UnstyledSwitches() { return (
- - - - + + + +
); } diff --git a/docs/data/base/components/switch/UnstyledSwitchIntroduction.tsx b/docs/data/base/components/switch/UnstyledSwitchIntroduction.tsx index 8e3688bea77832..c1cdcd95790e1c 100644 --- a/docs/data/base/components/switch/UnstyledSwitchIntroduction.tsx +++ b/docs/data/base/components/switch/UnstyledSwitchIntroduction.tsx @@ -7,10 +7,34 @@ export default function UnstyledSwitches() { return (
- - - - + + + +
); } diff --git a/docs/data/base/components/switch/UnstyledSwitchIntroduction.tsx.preview b/docs/data/base/components/switch/UnstyledSwitchIntroduction.tsx.preview deleted file mode 100644 index 89da352e81dfa2..00000000000000 --- a/docs/data/base/components/switch/UnstyledSwitchIntroduction.tsx.preview +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/docs/data/base/components/switch/UnstyledSwitches.js b/docs/data/base/components/switch/UnstyledSwitches.js index 8e3688bea77832..c1cdcd95790e1c 100644 --- a/docs/data/base/components/switch/UnstyledSwitches.js +++ b/docs/data/base/components/switch/UnstyledSwitches.js @@ -7,10 +7,34 @@ export default function UnstyledSwitches() { return (
- - - - + + + +
); } diff --git a/docs/data/base/components/switch/UnstyledSwitches.tsx b/docs/data/base/components/switch/UnstyledSwitches.tsx index 8e3688bea77832..c1cdcd95790e1c 100644 --- a/docs/data/base/components/switch/UnstyledSwitches.tsx +++ b/docs/data/base/components/switch/UnstyledSwitches.tsx @@ -7,10 +7,34 @@ export default function UnstyledSwitches() { return (
- - - - + + + +
); } diff --git a/docs/data/base/components/switch/UnstyledSwitches.tsx.preview b/docs/data/base/components/switch/UnstyledSwitches.tsx.preview deleted file mode 100644 index 89da352e81dfa2..00000000000000 --- a/docs/data/base/components/switch/UnstyledSwitches.tsx.preview +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/docs/data/base/components/switch/switch.md b/docs/data/base/components/switch/switch.md index 8390354753d176..b032c117ff7645 100644 --- a/docs/data/base/components/switch/switch.md +++ b/docs/data/base/components/switch/switch.md @@ -53,12 +53,7 @@ The Switch component is composed of a root `` that houses three interior s ``` -### Slot props - -:::info -The following props are available on all non-utility Base components. -See [Usage](/base/getting-started/usage/) for full details. -::: +### Custom structure Use the `slots` prop to override the root or any other interior slot: @@ -66,8 +61,9 @@ Use the `slots` prop to override the root or any other interior slot: ``` -:::warning -If the root element is customized with both the `component` and `slots` props, then `component` will take precedence. +:::info +The `slots` prop is available on all non-utility Base components. +See [Overriding component structure](/base/guides/overriding-component-structure/) for full details. ::: Use the `slotProps` prop to pass custom props to internal slots. @@ -77,6 +73,20 @@ The following code snippet applies a CSS class called `my-thumb` to the thumb sl ``` +#### Usage with TypeScript + +In TypeScript, you can specify the custom component type used in the `slots.root` as a generic parameter of the unstyled component. This way, you can safely provide the custom root's props directly on the component: + +```tsx + slots={{ root: CustomComponent }} customProp /> +``` + +The same applies for props specific to custom primitive elements: + +```tsx + slots={{ root: 'input' }} autoFocus={true} /> +``` + ## Hook ```js diff --git a/docs/pages/base/api/switch.json b/docs/pages/base/api/switch.json index 8adc8d2f94b05c..8c47ea4c85e2a2 100644 --- a/docs/pages/base/api/switch.json +++ b/docs/pages/base/api/switch.json @@ -1,7 +1,6 @@ { "props": { "checked": { "type": { "name": "bool" } }, - "component": { "type": { "name": "elementType" } }, "defaultChecked": { "type": { "name": "bool" } }, "disabled": { "type": { "name": "bool" } }, "onChange": { "type": { "name": "func" } }, diff --git a/docs/translations/api-docs-base/switch/switch.json b/docs/translations/api-docs-base/switch/switch.json index c51aac68be0678..6b8dc299327fc0 100644 --- a/docs/translations/api-docs-base/switch/switch.json +++ b/docs/translations/api-docs-base/switch/switch.json @@ -2,7 +2,6 @@ "componentDescription": "The foundation for building custom-styled switches.", "propDescriptions": { "checked": "If true, the component is checked.", - "component": "The component used for the root node. Either a string to use a HTML element or a component.", "defaultChecked": "The default checked state. Use when the component is not controlled.", "disabled": "If true, the component is disabled.", "onChange": "Callback fired when the state is changed.

Signature:
function(event: React.ChangeEvent<HTMLInputElement>) => void
event: The event source of the callback. You can pull out the new value by accessing event.target.value (string). You can pull out the new checked state by accessing event.target.checked (boolean).", diff --git a/packages/mui-base/src/Switch/Switch.spec.tsx b/packages/mui-base/src/Switch/Switch.spec.tsx index 400507403d327b..96e1ebac994e16 100644 --- a/packages/mui-base/src/Switch/Switch.spec.tsx +++ b/packages/mui-base/src/Switch/Switch.spec.tsx @@ -52,19 +52,38 @@ const polymorphicComponentTest = () => { {/* @ts-expect-error */} - + + slots={{ + root: 'a', + }} + href="#" + /> - + + slots={{ + root: CustomComponent, + }} + stringProp="test" + numberProp={0} + /> {/* @ts-expect-error */} - + + slots={{ + root: CustomComponent, + }} + /> - + slots={{ + root: 'button', + }} onClick={(e: React.MouseEvent) => e.currentTarget.checkValidity()} /> - component="button" + slots={{ + root: 'button', + }} ref={(elem) => { expectType(elem); }} diff --git a/packages/mui-base/src/Switch/Switch.test.tsx b/packages/mui-base/src/Switch/Switch.test.tsx index 0cd7a14e5ea33a..57f527c2a0d93e 100644 --- a/packages/mui-base/src/Switch/Switch.test.tsx +++ b/packages/mui-base/src/Switch/Switch.test.tsx @@ -30,6 +30,7 @@ describe('', () => { isOptional: true, }, }, + skip: ['componentProp'], })); describe('componentState', () => { diff --git a/packages/mui-base/src/Switch/Switch.tsx b/packages/mui-base/src/Switch/Switch.tsx index 754fc0e7b85b67..bba6874734968c 100644 --- a/packages/mui-base/src/Switch/Switch.tsx +++ b/packages/mui-base/src/Switch/Switch.tsx @@ -1,6 +1,6 @@ import * as React from 'react'; import PropTypes from 'prop-types'; -import { OverridableComponent } from '@mui/types'; +import { PolymorphicComponent } from '../utils/PolymorphicComponent'; import composeClasses from '../composeClasses'; import useSwitch from '../useSwitch'; import { @@ -52,7 +52,6 @@ const Switch = React.forwardRef(function Switch = useSlotProps({ elementType: Root, externalSlotProps: slotProps.root, @@ -133,7 +132,7 @@ const Switch = React.forwardRef(function Switch ); -}) as OverridableComponent; +}) as PolymorphicComponent; Switch.propTypes /* remove-proptypes */ = { // ----------------------------- Warning -------------------------------- @@ -144,15 +143,6 @@ Switch.propTypes /* remove-proptypes */ = { * If `true`, the component is checked. */ checked: PropTypes.bool, - /** - * @ignore - */ - children: PropTypes.node, - /** - * The component used for the root node. - * Either a string to use a HTML element or a component. - */ - component: PropTypes.elementType, /** * The default checked state. Use when the component is not controlled. */ @@ -161,10 +151,6 @@ Switch.propTypes /* remove-proptypes */ = { * If `true`, the component is disabled. */ disabled: PropTypes.bool, - /** - * @ignore - */ - id: PropTypes.string, /** * @ignore */ diff --git a/packages/mui-base/src/Switch/Switch.types.ts b/packages/mui-base/src/Switch/Switch.types.ts index ef6fd19c34aa26..be812714ea31e7 100644 --- a/packages/mui-base/src/Switch/Switch.types.ts +++ b/packages/mui-base/src/Switch/Switch.types.ts @@ -1,5 +1,5 @@ -import { OverrideProps, Simplify } from '@mui/types'; -import { SlotComponentProps } from '../utils'; +import { Simplify } from '@mui/types'; +import { PolymorphicProps, SlotComponentProps } from '../utils'; import { UseSwitchInputSlotProps, UseSwitchParameters } from '../useSwitch'; export interface SwitchRootSlotPropsOverrides {} @@ -63,9 +63,7 @@ export interface SwitchTypeMap< export type SwitchProps< RootComponentType extends React.ElementType = SwitchTypeMap['defaultComponent'], -> = OverrideProps, RootComponentType> & { - component?: RootComponentType; -}; +> = PolymorphicProps, RootComponentType>; export type SwitchOwnerState = Simplify< SwitchOwnProps & {