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

[TabsListUnstyled] Define ownerState and slot props' types #32925

Merged
merged 2 commits into from
Jun 3, 2022
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as React from 'react';
import TabsListUnstyled, { TabsListUnstyledRootSlotProps } from '@mui/base/TabsListUnstyled';

function Root(props: TabsListUnstyledRootSlotProps) {
const { ownerState, ...other } = props;
return <div data-orientation={ownerState.orientation} {...other} />;
}

const styledTabsList = <TabsListUnstyled components={{ Root }} />;
27 changes: 14 additions & 13 deletions packages/mui-base/src/TabsListUnstyled/TabsListUnstyled.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ import PropTypes from 'prop-types';
import clsx from 'clsx';
import { OverridableComponent } from '@mui/types';
import composeClasses from '../composeClasses';
import { appendOwnerState } from '../utils';
import { appendOwnerState, WithOptionalOwnerState } from '../utils';
import { getTabsListUnstyledUtilityClass } from './tabsListUnstyledClasses';
import TabsListUnstyledProps, { TabsListUnstyledTypeMap } from './TabsListUnstyledProps';
import {
TabsListUnstyledProps,
TabsListUnstyledRootSlotProps,
TabsListUnstyledTypeMap,
} from './TabsListUnstyled.types';
import useTabsList from './useTabsList';

const useUtilityClasses = (ownerState: { orientation: 'horizontal' | 'vertical' }) => {
Expand Down Expand Up @@ -42,23 +46,20 @@ const TabsListUnstyled = React.forwardRef<unknown, TabsListUnstyledProps>((props
const classes = useUtilityClasses(ownerState);

const TabsListRoot: React.ElementType = component ?? components.Root ?? 'div';
const tabsListRootProps = appendOwnerState(
const tabsListRootProps: WithOptionalOwnerState<TabsListUnstyledRootSlotProps> = appendOwnerState(
TabsListRoot,
{ ...other, ...componentsProps.root },
{
...getRootProps(),
...other,
...componentsProps.root,
className: clsx(className, componentsProps.root?.className, classes.root),
},
ownerState,
);

const processedChildren = processChildren();

return (
<TabsListRoot
{...getRootProps()}
{...tabsListRootProps}
className={clsx(className, componentsProps.root?.className, classes.root)}
>
{processedChildren}
</TabsListRoot>
);
return <TabsListRoot {...tabsListRootProps}>{processedChildren}</TabsListRoot>;
}) as OverridableComponent<TabsListUnstyledTypeMap>;

TabsListUnstyled.propTypes /* remove-proptypes */ = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import { OverrideProps } from '@mui/types';
import { UseTabsListRootSlotProps } from './useTabsList.types';

interface TabsListUnstyledComponentsPropsOverrides {}

Expand Down Expand Up @@ -31,7 +32,7 @@ export interface TabsListUnstyledTypeMap<P = {}, D extends React.ElementType = '
defaultComponent: D;
}

type TabsListUnstyledProps<
export type TabsListUnstyledProps<
D extends React.ElementType = TabsListUnstyledTypeMap['defaultComponent'],
P = {},
> = OverrideProps<TabsListUnstyledTypeMap<P, D>, D> & {
Expand All @@ -43,4 +44,12 @@ type TabsListUnstyledProps<
component?: D;
};

export default TabsListUnstyledProps;
export type TabsListUnstyledOwnerState = TabsListUnstyledProps & {
isRtl: boolean;
orientation: 'horizontal' | 'vertical';
};

export type TabsListUnstyledRootSlotProps = UseTabsListRootSlotProps & {
className?: string;
ownerState: TabsListUnstyledOwnerState;
};
6 changes: 4 additions & 2 deletions packages/mui-base/src/TabsListUnstyled/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export { default } from './TabsListUnstyled';
export * from './TabsListUnstyled.types';

export { default as tabsListUnstyledClasses } from './tabsListUnstyledClasses';
export * from './tabsListUnstyledClasses';
export type { default as TabsListUnstyledProps } from './TabsListUnstyledProps';

export { default as useTabsList } from './useTabsList';
export * from './useTabsList';
export * from './useTabsList.types';
26 changes: 10 additions & 16 deletions packages/mui-base/src/TabsListUnstyled/useTabsList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
import { isFragment } from 'react-is';
import { useTabContext } from '../TabsUnstyled';
import extractEventHandlers from '../utils/extractEventHandlers';
import { UseTabsListParameters, UseTabsListRootSlotProps } from './useTabsList.types';
import { EventHandlers } from '../utils';

const nextItem = (list: Element | null, item: Element | null): Element | null => {
if (!list) {
Expand Down Expand Up @@ -66,18 +68,8 @@ const moveFocus = (
}
};

export interface UseTabsListProps {
'aria-label'?: string;
'aria-labelledby'?: string;
/**
* The content of the component.
*/
children?: React.ReactNode;
ref: React.Ref<unknown>;
}

const useTabsList = (props: UseTabsListProps) => {
const { 'aria-label': ariaLabel, 'aria-labelledby': ariaLabelledBy, children, ref } = props;
const useTabsList = (parameters: UseTabsListParameters) => {
const { 'aria-label': ariaLabel, 'aria-labelledby': ariaLabelledBy, children, ref } = parameters;

const tabsListRef = React.createRef<any>();
const handleRef = useForkRef(tabsListRef, ref);
Expand Down Expand Up @@ -138,22 +130,24 @@ const useTabsList = (props: UseTabsListProps) => {
otherHandlers.onKeyDown?.(event);
};

const getRootProps = (otherHandlers?: Record<string, React.EventHandler<any>>) => {
const propsEventHandlers = extractEventHandlers(props);
const getRootProps = <TOther extends EventHandlers = {}>(
otherHandlers: TOther = {} as TOther,
): UseTabsListRootSlotProps<TOther> => {
const propsEventHandlers = extractEventHandlers(parameters);
const externalEventHandlers = { ...propsEventHandlers, ...otherHandlers };

const ownEventHandlers = {
onKeyDown: createHandleKeyDown(externalEventHandlers),
};

const mergedEventHandlers: Record<string, React.EventHandler<any>> = {
const mergedEventHandlers = {
...externalEventHandlers,
...ownEventHandlers,
};
return {
'aria-label': ariaLabel,
'aria-labelledby': ariaLabelledBy,
'aria-orientation': orientation === 'vertical' ? 'vertical' : null,
'aria-orientation': orientation === 'vertical' ? 'vertical' : undefined,
role: 'tablist',
ref: handleRef,
...mergedEventHandlers,
Expand Down
20 changes: 20 additions & 0 deletions packages/mui-base/src/TabsListUnstyled/useTabsList.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import * as React from 'react';

export interface UseTabsListParameters {
'aria-label'?: string;
'aria-labelledby'?: string;
/**
* The content of the component.
*/
children?: React.ReactNode;
ref: React.Ref<unknown>;
}

export type UseTabsListRootSlotProps<TOther = {}> = TOther & {
'aria-label'?: React.AriaAttributes['aria-label'];
'aria-labelledby'?: React.AriaAttributes['aria-labelledby'];
'aria-orientation'?: React.AriaAttributes['aria-orientation'];
role: React.AriaRole;
ref: React.Ref<any>;
onKeyDown?: React.KeyboardEventHandler<HTMLElement>;
};