Skip to content

Commit

Permalink
[typescript] Cherry pick React 19 type PRs to v5.x (#44577)
Browse files Browse the repository at this point in the history
  • Loading branch information
DiegoAndai authored Nov 27, 2024
1 parent c371831 commit 55249f5
Show file tree
Hide file tree
Showing 75 changed files with 144 additions and 118 deletions.
2 changes: 1 addition & 1 deletion docs/data/base/components/select/select.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const CustomSelect = React.forwardRef(function CustomSelect<TValue>(
return <Select {...props} ref={ref} />;
}) as <TValue>(
props: SelectProps<TValue> & React.RefAttributes<HTMLUListElement>,
) => JSX.Element;
) => React.JSX.Element;
```

For the sake of brevity, the rest of the demos throughout this doc will not use `forwardRef`.
Expand Down
2 changes: 1 addition & 1 deletion docs/data/joy/components/input/DebouncedInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Box from '@mui/joy/Box';
function DebounceInput(props) {
const { handleDebounce, debounceTimeout, ...rest } = props;

const timerRef = React.useRef();
const timerRef = React.useRef(undefined);

const handleChange = (event) => {
clearTimeout(timerRef.current);
Expand Down
4 changes: 3 additions & 1 deletion docs/data/joy/components/input/DebouncedInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ type DebounceProps = {
function DebounceInput(props: InputProps & DebounceProps) {
const { handleDebounce, debounceTimeout, ...rest } = props;

const timerRef = React.useRef<ReturnType<typeof setTimeout>>();
const timerRef = React.useRef<ReturnType<typeof setTimeout> | undefined>(
undefined,
);

const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
clearTimeout(timerRef.current);
Expand Down
4 changes: 2 additions & 2 deletions docs/data/joy/components/snackbar/SnackbarHideDuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ export default function SnackbarHideDuration() {
const [open, setOpen] = React.useState(false);
const [duration, setDuration] = React.useState();
const [left, setLeft] = React.useState();
const timer = React.useRef();
const timer = React.useRef(undefined);
const countdown = () => {
timer.current = window.setInterval(() => {
timer.current = setInterval(() => {
setLeft((prev) => (prev === undefined ? prev : Math.max(0, prev - 100)));
}, 100);
};
Expand Down
4 changes: 2 additions & 2 deletions docs/data/joy/components/snackbar/SnackbarHideDuration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ export default function SnackbarHideDuration() {
const [open, setOpen] = React.useState(false);
const [duration, setDuration] = React.useState<undefined | number>();
const [left, setLeft] = React.useState<undefined | number>();
const timer = React.useRef<undefined | number>();
const timer = React.useRef<ReturnType<typeof setInterval> | undefined>(undefined);
const countdown = () => {
timer.current = window.setInterval(() => {
timer.current = setInterval(() => {
setLeft((prev) => (prev === undefined ? prev : Math.max(0, prev - 100)));
}, 100);
};
Expand Down
2 changes: 1 addition & 1 deletion docs/data/material/components/app-bar/BackToTop.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ interface Props {
* You won't need it on your project.
*/
window?: () => Window;
children: React.ReactElement<any>;
children: React.ReactElement<unknown>;
}

function ScrollTop(props: Props) {
Expand Down
2 changes: 1 addition & 1 deletion docs/data/material/components/app-bar/ElevateAppBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ interface Props {
* You won't need it on your project.
*/
window?: () => Window;
children: React.ReactElement<any>;
children: React.ReactElement<{ elevation?: number }>;
}

function ElevationScroll(props: Props) {
Expand Down
2 changes: 1 addition & 1 deletion docs/data/material/components/app-bar/HideAppBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ interface Props {
* You won't need it on your project.
*/
window?: () => Window;
children: React.ReactElement<any>;
children: React.ReactElement<unknown>;
}

function HideOnScroll(props: Props) {
Expand Down
12 changes: 8 additions & 4 deletions docs/data/material/components/autocomplete/Virtualize.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,13 @@ const ListboxComponent = React.forwardRef<
React.HTMLAttributes<HTMLElement>
>(function ListboxComponent(props, ref) {
const { children, ...other } = props;
const itemData: React.ReactElement<any>[] = [];
(children as React.ReactElement<any>[]).forEach(
(item: React.ReactElement<any> & { children?: React.ReactElement<any>[] }) => {
const itemData: React.ReactElement<unknown>[] = [];
(children as React.ReactElement<unknown>[]).forEach(
(
item: React.ReactElement<unknown> & {
children?: React.ReactElement<unknown>[];
},
) => {
itemData.push(item);
itemData.push(...(item.children || []));
},
Expand All @@ -73,7 +77,7 @@ const ListboxComponent = React.forwardRef<
const itemCount = itemData.length;
const itemSize = smUp ? 36 : 48;

const getChildSize = (child: React.ReactElement<any>) => {
const getChildSize = (child: React.ReactElement<unknown>) => {
if (child.hasOwnProperty('group')) {
return 48;
}
Expand Down
2 changes: 1 addition & 1 deletion docs/data/material/components/dialogs/FullScreenDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { TransitionProps } from '@mui/material/transitions';

const Transition = React.forwardRef(function Transition(
props: TransitionProps & {
children: React.ReactElement<any>;
children: React.ReactElement<unknown>;
},
ref: React.Ref<unknown>,
) {
Expand Down
2 changes: 1 addition & 1 deletion docs/data/material/components/lists/InteractiveList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import Typography from '@mui/material/Typography';
import FolderIcon from '@mui/icons-material/Folder';
import DeleteIcon from '@mui/icons-material/Delete';

function generate(element: React.ReactElement<any>) {
function generate(element: React.ReactElement<unknown>) {
return [0, 1, 2].map((value) =>
React.cloneElement(element, {
key: value,
Expand Down
2 changes: 1 addition & 1 deletion docs/data/material/components/popper/SpringPopper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Popper from '@mui/material/Popper';
import { useSpring, animated } from '@react-spring/web';

interface FadeProps {
children?: React.ReactElement<any>;
children?: React.ReactElement<unknown>;
in?: boolean;
onEnter?: () => void;
onExited?: () => void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import SaveIcon from '@mui/icons-material/Save';
export default function CircularIntegration() {
const [loading, setLoading] = React.useState(false);
const [success, setSuccess] = React.useState(false);
const timer = React.useRef();
const timer = React.useRef(undefined);

const buttonSx = {
...(success && {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import SaveIcon from '@mui/icons-material/Save';
export default function CircularIntegration() {
const [loading, setLoading] = React.useState(false);
const [success, setSuccess] = React.useState(false);
const timer = React.useRef<ReturnType<typeof setTimeout>>();
const timer = React.useRef<ReturnType<typeof setTimeout> | undefined>(undefined);

const buttonSx = {
...(success && {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Typography from '@mui/material/Typography';
export default function DelayingAppearance() {
const [loading, setLoading] = React.useState(false);
const [query, setQuery] = React.useState('idle');
const timerRef = React.useRef();
const timerRef = React.useRef(undefined);

React.useEffect(
() => () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import Typography from '@mui/material/Typography';
export default function DelayingAppearance() {
const [loading, setLoading] = React.useState(false);
const [query, setQuery] = React.useState('idle');
const timerRef = React.useRef<ReturnType<typeof setTimeout>>();
const timerRef = React.useRef<ReturnType<typeof setTimeout> | undefined>(
undefined,
);

React.useEffect(
() => () => {
Expand Down
2 changes: 1 addition & 1 deletion docs/data/material/components/rating/RadioGroupRating.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const StyledRating = styled(Rating)(({ theme }) => ({

const customIcons: {
[index: string]: {
icon: React.ReactElement<any>;
icon: React.ReactElement<unknown>;
label: string;
};
} = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ const ColorlibStepIconRoot = styled('div')<{
function ColorlibStepIcon(props: StepIconProps) {
const { active, completed, className } = props;

const icons: { [index: string]: React.ReactElement<any> } = {
const icons: { [index: string]: React.ReactElement<unknown> } = {
1: <SettingsIcon />,
2: <GroupAddIcon />,
3: <VideoLabelIcon />,
Expand Down
2 changes: 1 addition & 1 deletion docs/data/material/integrations/routing/ListRouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function Router(props: { children?: React.ReactNode }) {
}

interface ListItemLinkProps {
icon?: React.ReactElement<any>;
icon?: React.ReactElement<unknown>;
primary: string;
to: string;
}
Expand Down
4 changes: 2 additions & 2 deletions docs/pages/base-ui/api/use-autocomplete.json
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@
},
"unstable_isActiveElementInListbox": {
"type": {
"name": "(listbox: React.RefObject&lt;HTMLElement&gt;) =&gt; boolean",
"description": "(listbox: React.RefObject&lt;HTMLElement&gt;) =&gt; boolean"
"name": "(listbox: React.RefObject&lt;HTMLElement | null&gt;) =&gt; boolean",
"description": "(listbox: React.RefObject&lt;HTMLElement | null&gt;) =&gt; boolean"
}
},
"value": {
Expand Down
2 changes: 1 addition & 1 deletion docs/src/components/about/HowToSupport.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function Widget({
}: {
children: React.ReactNode;
title: string;
icon: React.ReactElement<any>;
icon: React.ReactElement<unknown>;
}) {
return (
<Paper
Expand Down
2 changes: 1 addition & 1 deletion docs/src/components/header/HeaderNavBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ const PRODUCT_IDS = [
];

type ProductSubMenuProps = {
icon: React.ReactElement<any>;
icon: React.ReactElement<unknown>;
name: React.ReactNode;
description: React.ReactNode;
chip?: React.ReactNode;
Expand Down
2 changes: 1 addition & 1 deletion docs/src/components/home/MaterialDesignComponents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ function Demo({
}: {
name: string;
theme: Theme | undefined;
children: React.ReactElement<any>;
children: React.ReactElement<unknown>;
control?: { prop: string; values: Array<string>; defaultValue?: string };
}) {
const [propValue, setPropValue] = React.useState(
Expand Down
2 changes: 1 addition & 1 deletion docs/src/components/home/UserFeedbacks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ function Feedback({
avatarSrcSet: string;
name: string;
role: string;
company?: React.ReactElement<any>;
company?: React.ReactElement<unknown>;
};
}) {
return (
Expand Down
2 changes: 1 addition & 1 deletion docs/src/components/pricing/PricingTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ function RowHead({
children,
startIcon,
...props
}: BoxProps & { startIcon?: React.ReactElement<any> }) {
}: BoxProps & { startIcon?: React.ReactElement<unknown> }) {
return (
<Box
{...props}
Expand Down
2 changes: 1 addition & 1 deletion docs/src/components/productX/XRoadmap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function RoadmapStatusDot({ color }: RoadmapStatusDotProps) {
}

export default function XRoadmap() {
function renderList(content: React.ReactElement<any>, nested?: boolean) {
function renderList(content: React.ReactElement<unknown>, nested?: boolean) {
return (
<Box
sx={{
Expand Down
2 changes: 1 addition & 1 deletion docs/src/components/showcase/ViewToggleButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const views = ['quilt', 'module', 'agenda', 'week', 'sidebar'] as const;

type View = (typeof views)[number];

const viewIcons: Record<View, React.ReactElement<any>> = {
const viewIcons: Record<View, React.ReactElement<unknown>> = {
quilt: <ViewQuiltRounded />,
module: <ViewModuleRounded />,
agenda: <ViewAgendaRounded />,
Expand Down
2 changes: 1 addition & 1 deletion docs/src/components/typography/SectionHeadline.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interface SectionHeadlineProps {
*/
inverted?: boolean;
overline?: React.ReactNode;
title: string | React.ReactElement<any>;
title: string | React.ReactElement<React.HTMLAttributes<HTMLElement>>;
}

export default function SectionHeadline(props: SectionHeadlineProps) {
Expand Down
4 changes: 2 additions & 2 deletions docs/src/layouts/HeroContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import { alpha } from '@mui/material/styles';
interface HeroContainerProps {
disableMobileHidden?: boolean;
disableTabExclusion?: boolean;
left: React.ReactElement<any>;
left: React.ReactElement<unknown>;
linearGradient?: boolean;
right: React.ReactElement<any>;
right: React.ReactElement<unknown>;
rightSx?: BoxProps['sx'];
}

Expand Down
21 changes: 13 additions & 8 deletions docs/src/modules/components/JoyThemeBuilder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -983,7 +983,11 @@ function filterGlobalVariantTokens(palette: Partial<PaletteVariant>, variant: Va
return tokens;
}

type StateReducer<T> = (state: T, action: Partial<T>) => T;
type ReducerState = {
hover: boolean;
active: boolean;
disabled: boolean;
};

function GlobalVariantForm({
color,
Expand All @@ -1001,13 +1005,14 @@ function GlobalVariantForm({
onRemove: (token: string) => void;
}) {
const [selectedVariant, setSelectedVariant] = React.useState<VariantProp>('solid');
const [states, setStates] = React.useReducer<
StateReducer<{ hover: boolean; active: boolean; disabled: boolean }>
>((prevState, action) => ({ ...prevState, ...action }), {
hover: false,
active: false,
disabled: false,
});
const [states, setStates] = React.useReducer(
(prevState: ReducerState, action: Partial<ReducerState>) => ({ ...prevState, ...action }),
{
hover: false,
active: false,
disabled: false,
},
);
const themeDefaultValue = filterGlobalVariantTokens(themeDefaultValueProp, selectedVariant);
const value = filterGlobalVariantTokens(valueProp, selectedVariant);
const mergedValue = { ...themeDefaultValue, ...value };
Expand Down
2 changes: 1 addition & 1 deletion docs/src/modules/components/JoyUsageDemo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ interface JoyUsageDemoProps<ComponentProps> {
* A function to override the code block result.
*/
getCodeBlock?: (code: string, props: ComponentProps) => string;
renderDemo: (props: ComponentProps) => React.ReactElement<any>;
renderDemo: (props: ComponentProps) => React.ReactElement<unknown>;
}

export default function JoyUsageDemo<T extends { [k: string]: any } = {}>({
Expand Down
4 changes: 2 additions & 2 deletions docs/src/modules/components/JoyVariablesDemo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function formatSx(sx: { [k: string]: string | number }) {
interface SlotVariablesProps {
slot: string;
data: Array<DataItem>;
renderField: (item: DataItem) => React.ReactElement<any>;
renderField: (item: DataItem) => React.ReactElement<unknown>;
defaultOpen?: boolean;
}

Expand Down Expand Up @@ -88,7 +88,7 @@ export default function JoyVariablesDemo(props: {
componentName: string;
childrenAccepted?: boolean;
data: Array<DataItem | [string, Array<DataItem>, { defaultOpen?: boolean } | undefined]>;
renderDemo: (sx: { [k: string]: string | number }) => React.ReactElement<any>;
renderDemo: (sx: { [k: string]: string | number }) => React.ReactElement<unknown>;
renderCode?: (formattedSx: string) => string;
}) {
const { componentName, data = [], childrenAccepted = false, renderCode } = props;
Expand Down
2 changes: 1 addition & 1 deletion docs/types/docs.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ declare module 'docs/src/modules/components/HighlightedCode' {
component?: React.ElementType;
sx?: object;
}
export default function HighlightedCode(props: Props): React.ReactElement<any>;
export default function HighlightedCode(props: Props): React.ReactElement<unknown>;
}

declare module 'react-imask';
2 changes: 1 addition & 1 deletion packages/mui-base/src/Button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const Button = React.forwardRef(function Button<RootComponentType extends React.
...other
} = props;

const buttonRef = React.useRef<HTMLButtonElement | HTMLAnchorElement | HTMLElement>();
const buttonRef = React.useRef<HTMLButtonElement | HTMLAnchorElement | HTMLElement | null>(null);

let rootElementName = rootElementNameProp;

Expand Down
2 changes: 1 addition & 1 deletion packages/mui-base/src/Tab/Tab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const Tab = React.forwardRef(function Tab<RootComponentType extends React.Elemen
...other
} = props;

const tabRef = React.useRef<HTMLButtonElement | HTMLAnchorElement | HTMLElement>();
const tabRef = React.useRef<HTMLButtonElement | HTMLAnchorElement | HTMLElement | null>(null);
const handleRef = useForkRef(tabRef, forwardedRef);

const { active, highlighted, selected, getRootProps } = useTab({
Expand Down
2 changes: 1 addition & 1 deletion packages/mui-base/src/useAutocomplete/useAutocomplete.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export interface UseAutocompleteProps<
* Temporary for Joy UI because the parent listbox is the document object
* TODO v6: Normalize the logic and remove this param.
*/
unstable_isActiveElementInListbox?: (listbox: React.RefObject<HTMLElement>) => boolean;
unstable_isActiveElementInListbox?: (listbox: React.RefObject<HTMLElement | null>) => boolean;
/**
* If `true`, the portion of the selected suggestion that the user hasn't typed,
* known as the completion string, appears inline after the input cursor in the textbox.
Expand Down
2 changes: 1 addition & 1 deletion packages/mui-base/src/useButton/useButton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export function useButton(parameters: UseButtonParameters = {}): UseButtonReturn
type,
rootElementName: rootElementNameProp,
} = parameters;
const buttonRef = React.useRef<HTMLButtonElement | HTMLAnchorElement | HTMLElement>();
const buttonRef = React.useRef<HTMLButtonElement | HTMLAnchorElement | HTMLElement | null>(null);

const [active, setActive] = React.useState<boolean>(false);

Expand Down
Loading

0 comments on commit 55249f5

Please sign in to comment.