Skip to content

Commit

Permalink
Improve child prop type check
Browse files Browse the repository at this point in the history
  • Loading branch information
DiegoAndai committed Aug 12, 2024
1 parent dad172d commit 869ff4c
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 11 deletions.
6 changes: 4 additions & 2 deletions packages/mui-joy/src/ButtonGroup/ButtonGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import ButtonGroupContext from './ButtonGroupContext';
import useSlot from '../utils/useSlot';
import buttonClasses from '../Button/buttonClasses';
import iconButtonClasses from '../IconButton/iconButtonClasses';
import { DividerProps } from '../Divider';

const useUtilityClasses = (ownerState: ButtonGroupOwnerState) => {
const { size, variant, color, orientation } = ownerState;
Expand Down Expand Up @@ -236,10 +237,11 @@ const ButtonGroup = React.forwardRef(function ButtonGroup(inProps, ref) {
}
const extraProps: Record<string, any> = {};
if (isMuiElement(child, ['Divider'])) {
extraProps.inset = (child.props as any).inset ?? 'context';
const childProps = child.props as DividerProps;
extraProps.inset = childProps?.inset ?? 'context';

const dividerOrientation = orientation === 'vertical' ? 'horizontal' : 'vertical';
extraProps.orientation = (child.props as any).orientation ?? dividerOrientation;
extraProps.orientation = childProps?.orientation ?? dividerOrientation;
extraProps.role = 'presentation';
extraProps.component = 'span';
}
Expand Down
6 changes: 4 additions & 2 deletions packages/mui-joy/src/Card/Card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { getCardUtilityClass } from './cardClasses';
import { CardProps, CardOwnerState, CardTypeMap } from './CardProps';
import { resolveSxValue } from '../styles/styleUtils';
import useSlot from '../utils/useSlot';
import { DividerProps } from '../Divider';

const useUtilityClasses = (ownerState: CardOwnerState) => {
const { size, variant, color, orientation } = ownerState;
Expand Down Expand Up @@ -166,10 +167,11 @@ const Card = React.forwardRef(function Card(inProps, ref) {
}
const extraProps: Record<string, any> = {};
if (isMuiElement(child, ['Divider'])) {
extraProps.inset = (child.props as any)?.inset ?? 'context';
const childProps = child.props as DividerProps;
extraProps.inset = childProps?.inset ?? 'context';

const dividerOrientation = orientation === 'vertical' ? 'horizontal' : 'vertical';
extraProps.orientation = (child.props as any)?.orientation ?? dividerOrientation;
extraProps.orientation = childProps?.orientation ?? dividerOrientation;
}
if (index === 0) {
extraProps['data-first-child'] = '';
Expand Down
4 changes: 2 additions & 2 deletions packages/mui-joy/src/ListItem/ListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { unstable_composeClasses as composeClasses } from '@mui/base/composeClas
import { styled, useThemeProps } from '../styles';

import useSlot from '../utils/useSlot';
import { ListItemOwnerState, ListItemTypeMap } from './ListItemProps';
import { ListItemOwnerState, ListItemProps, ListItemTypeMap } from './ListItemProps';
import listItemClasses, { getListItemUtilityClass } from './listItemClasses';
import NestedListContext from '../List/NestedListContext';
import RowListContext from '../List/RowListContext';
Expand Down Expand Up @@ -256,7 +256,7 @@ const ListItem = React.forwardRef(function ListItem(inProps, ref) {
...(index === 0 && { 'data-first-child': '' }),
...(isMuiElement(child, ['ListItem']) && {
// The ListItem of ListItem should not be 'li'
component: (child.props as any).component || 'div',
component: (child.props as ListItemProps)?.component || 'div',
}),
})
: child,
Expand Down
6 changes: 4 additions & 2 deletions packages/mui-joy/src/ModalDialog/ModalDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import ModalDialogSizeContext from './ModalDialogSizeContext';
import ModalDialogVariantColorContext from './ModalDialogVariantColorContext';
import useSlot from '../utils/useSlot';
import { StyledCardRoot } from '../Card/Card';
import { DividerProps } from '../Divider';

const useUtilityClasses = (ownerState: ModalDialogOwnerState) => {
const { variant, color, size, layout } = ownerState;
Expand Down Expand Up @@ -195,10 +196,11 @@ const ModalDialog = React.forwardRef(function ModalDialog(inProps, ref) {
}
const extraProps: Record<string, any> = {};
if (isMuiElement(child, ['Divider'])) {
extraProps.inset = (child.props as any).inset ?? 'context';
const childProps = child.props as DividerProps;
extraProps.inset = childProps?.inset ?? 'context';

const dividerOrientation = orientation === 'vertical' ? 'horizontal' : 'vertical';
extraProps.orientation = (child.props as any).orientation ?? dividerOrientation;
extraProps.orientation = childProps?.orientation ?? dividerOrientation;
}
if (index === 0) {
extraProps['data-first-child'] = '';
Expand Down
6 changes: 4 additions & 2 deletions packages/mui-joy/src/ToggleButtonGroup/ToggleButtonGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import useSlot from '../utils/useSlot';
import { StyledButtonGroup } from '../ButtonGroup/ButtonGroup';
import ButtonGroupContext from '../ButtonGroup/ButtonGroupContext';
import ToggleButtonGroupContext from './ToggleButtonGroupContext';
import { DividerProps } from '../Divider';

interface InternalChangeEventHandler<Value> {
(event: React.MouseEvent, value: Value | Array<Value> | null): void;
Expand Down Expand Up @@ -161,10 +162,11 @@ const ToggleButtonGroup = React.forwardRef(function ToggleButtonGroup<
}
const extraProps: Record<string, any> = {};
if (isMuiElement(child, ['Divider'])) {
extraProps.inset = (child.props as any).inset ?? 'context';
const childProps = child.props as DividerProps;
extraProps.inset = childProps?.inset ?? 'context';

const dividerOrientation = orientation === 'vertical' ? 'horizontal' : 'vertical';
extraProps.orientation = (child.props as any).orientation ?? dividerOrientation;
extraProps.orientation = childProps?.orientation ?? dividerOrientation;
extraProps.role = 'presentation';
extraProps.component = 'span';
}
Expand Down
2 changes: 1 addition & 1 deletion packages/mui-system/src/Grid/createGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export default function createGrid(
{React.Children.map(children, (child) => {
if (React.isValidElement(child) && isMuiElement(child, ['Grid'])) {
return React.cloneElement(child, {
unstable_level: (child.props as any).unstable_level ?? level + 1,
unstable_level: (child.props as GridProps)?.unstable_level ?? level + 1,
} as GridProps);
}
return child;
Expand Down

0 comments on commit 869ff4c

Please sign in to comment.