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

[Option][base] Drop component prop #37052

Merged
merged 4 commits into from
Apr 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 0 additions & 1 deletion docs/pages/base/api/option.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"props": {
"value": { "type": { "name": "any" }, "required": true },
"component": { "type": { "name": "elementType" } },
"disabled": { "type": { "name": "bool" }, "default": "false" },
"label": { "type": { "name": "string" } },
"slotProps": {
Expand Down
1 change: 0 additions & 1 deletion docs/translations/api-docs-base/option/option.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"componentDescription": "An unstyled option to be used within a Select.",
"propDescriptions": {
"component": "The component used for the root node. Either a string to use a HTML element or a component.",
"disabled": "If <code>true</code>, the option will be disabled.",
"label": "A text representation of the option&#39;s content. Used for keyboard text navigation matching.",
"slotProps": "The props used for each slot inside the Option.",
Expand Down
34 changes: 28 additions & 6 deletions packages/mui-base/src/Option/Option.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,43 @@ const polymorphicComponentTest = () => {
{/* @ts-expect-error */}
<Option invalidProp={0} />

<Option value={1} component="a" href="#" />
<Option<number, 'a'>
value={1}
slots={{
root: 'a',
}}
href="#"
/>

<Option value={1} component={CustomComponent} stringProp="test" numberProp={0} />
<Option<number, typeof CustomComponent>
value={1}
slots={{
root: CustomComponent,
}}
stringProp="test"
numberProp={0}
/>
{/* @ts-expect-error */}
<Option value={1} component={CustomComponent} />
<Option<number, typeof CustomComponent>
value={1}
slots={{
root: CustomComponent,
}}
/>

<Option
<Option<number, 'button'>
value={1}
component="button"
slots={{
root: 'button',
}}
onClick={(e: React.MouseEvent<HTMLButtonElement>) => e.currentTarget.checkValidity()}
/>

<Option<number, 'button'>
value={1}
component="button"
slots={{
root: 'button',
}}
ref={(elem) => {
expectType<HTMLButtonElement | null, typeof elem>(elem);
}}
Expand Down
1 change: 1 addition & 0 deletions packages/mui-base/src/Option/Option.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ describe('<Option />', () => {
},
},
skip: [
'componentProp',
'reactTestRenderer', // Need to be wrapped in SelectContext
],
}));
Expand Down
18 changes: 2 additions & 16 deletions packages/mui-base/src/Option/Option.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,9 @@ const Option = React.forwardRef(function Option<
OptionValue,
RootComponentType extends React.ElementType,
>(props: OptionProps<OptionValue, RootComponentType>, forwardedRef: React.ForwardedRef<Element>) {
const {
children,
component,
disabled = false,
label,
slotProps = {},
slots = {},
value,
...other
} = props;
const { children, disabled = false, label, slotProps = {}, slots = {}, value, ...other } = props;

const Root = component || slots.root || 'li';
const Root = slots.root ?? 'li';

const optionRef = React.useRef<HTMLElement>(null);
const combinedRef = useForkRef(optionRef, forwardedRef);
Expand Down Expand Up @@ -84,11 +75,6 @@ Option.propTypes /* remove-proptypes */ = {
* @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,
/**
* If `true`, the option will be disabled.
* @default false
Expand Down
23 changes: 9 additions & 14 deletions packages/mui-base/src/Option/Option.types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react';
import { DefaultComponentProps, OverrideProps, Simplify } from '@mui/types';
import { Simplify } from '@mui/types';
import { UseOptionRootSlotProps } from '../useOption';
import { SlotComponentProps } from '../utils';
import { PolymorphicProps, SlotComponentProps } from '../utils';

export interface OptionRootSlotPropsOverrides {}

Expand Down Expand Up @@ -57,22 +57,17 @@ export interface OptionTypeMap<
export type OptionProps<
OptionValue,
RootComponentType extends React.ElementType = OptionTypeMap<OptionValue>['defaultComponent'],
> = OverrideProps<OptionTypeMap<OptionValue, {}, RootComponentType>, RootComponentType> & {
component?: RootComponentType;
};
> = PolymorphicProps<OptionTypeMap<OptionValue, {}, RootComponentType>, RootComponentType>;

export interface OptionType {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OptionType should look similar to PolymorphicComponent (but with an additional generic OptionValue parameter):

export type OptionType = {
  <OptionValue, RootComponentType extends React.ElementType>(
    props: PolymorphicProps<OptionTypeMap<OptionValue>, RootComponentType>,
  ): JSX.Element | null;
  propTypes?: any;
  displayName?: string | undefined;
};

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed

<OptionValue, RootComponentType extends React.ElementType>(
props: {
/**
* The component used for the root node.
* Either a string to use a HTML element or a component.
*/
component: RootComponentType;
} & OverrideProps<OptionTypeMap<OptionValue>, RootComponentType>,
<
OptionValue,
RootComponentType extends React.ElementType = OptionTypeMap<OptionValue>['defaultComponent'],
>(
props: PolymorphicProps<OptionTypeMap<OptionValue>, RootComponentType>,
): JSX.Element | null;
<OptionValue>(props: DefaultComponentProps<OptionTypeMap<OptionValue>>): JSX.Element | null;
propTypes?: any;
displayName?: string | undefined;
}

export type OptionOwnerState<OptionValue> = Simplify<
Expand Down