-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
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
[Paper] Adding a custom PaperComponent property makes the Autocomplete scroll to top on change #38049
Comments
Duplicate of #31073. See the explanation in #31073 (comment). After defining the custom Paper component outside the main component, it works - https://stackblitz.com/edit/stackblitz-starters-erthoc?file=src%2FApp.tsx. |
Is it possible to add a button inside the paper component and enable/Disable it based on changes that happen in the main component? In the next example I have a button and I want to enable/disable it when there is at least one option selected. const CustomPaper = ({ children, ...paperProps }) => ( |
Yes, you can pass custom props to your custom Paper component using |
` import { function sleep(duration: number): Promise { export interface SingleSelectProps { enum SingleSelectActionsTypes { const PaperComponent = (props: PaperProps, loadingInternalOptions: boolean) => ( export const SingleSelect = ({ const internalAxios = useCampxAxios ? campxAxios : axios; const handleOpen = async () => { const handleScroll = async (event: any) => {
}; return ( export default SingleSelect;` |
@shashank-campx Please provide a StackBlitz reproduction or create a new issue with a reproduction. |
@ZeeshanTamboli Hey, I tried doing the same thing you did in the stackblitz but it throws me the next error:
Do you have any idea why it doesnt allow me to add custom props? |
@HelloIAmBanana |
Yeah I got it but why doesn't it throw you an error aswell...? |
@HelloIAmBanana Thanks for pointing this out. You're correct—it does throw an error, but only in a local project, not sure why not on StackBlitz. The issue is that we aren't allowing custom props to be added through To fix this, we can modify the code as follows: --- a/packages/mui-material/src/Autocomplete/Autocomplete.d.ts
+++ b/packages/mui-material/src/Autocomplete/Autocomplete.d.ts
@@ -18,6 +18,13 @@ import useAutocomplete, {
import { AutocompleteClasses } from './autocompleteClasses';
import { CreateSlotsAndSlotProps, SlotProps } from '../utils/types';
+export interface AutocompleteChipSlotPropsOverrides {}
+export interface AutocompleteClearIndicatorSlotPropsOverrides {}
+export interface AutocompleteListboxSlotPropsOverrides {}
+export interface AutocompletePaperSlotPropsOverrides {}
+export interface AutocompletePopperSlotPropsOverrides {}
+export interface AutocompletePopupIndicatorSlotPropsOverrides {}
+
export {
AutocompleteChangeDetails,
AutocompleteChangeReason,
@@ -89,17 +96,19 @@ export interface AutocompleteSlots {
* The component used to render the listbox.
* @default 'ul'
*/
- listbox?: React.JSXElementConstructor<React.HTMLAttributes<HTMLElement>>;
+ listbox?: React.JSXElementConstructor<
+ React.HTMLAttributes<HTMLElement> & AutocompleteListboxSlotPropsOverrides
+ >;
/**
* The component used to render the body of the popup.
* @default Paper
*/
- paper?: React.JSXElementConstructor<PaperProps>;
+ paper?: React.JSXElementConstructor<PaperProps & AutocompletePaperSlotPropsOverrides>;
/**
* The component used to position the popup.
* @default Popper
*/
- popper?: React.JSXElementConstructor<PopperProps>;
+ popper?: React.JSXElementConstructor<PopperProps & AutocompletePopperSlotPropsOverrides>;
}
export type AutocompleteSlotsAndSlotProps<
@@ -113,12 +122,12 @@ export type AutocompleteSlotsAndSlotProps<
{
chip: SlotProps<
React.ElementType<Partial<ChipProps<ChipComponent>>>,
- {},
+ AutocompleteChipSlotPropsOverrides,
AutocompleteOwnerState<Value, Multiple, DisableClearable, FreeSolo, ChipComponent>
>;
clearIndicator: SlotProps<
React.ElementType<Partial<IconButtonProps>>,
- {},
+ AutocompleteClearIndicatorSlotPropsOverrides,
AutocompleteOwnerState<Value, Multiple, DisableClearable, FreeSolo, ChipComponent>
>;
/**
@@ -131,23 +140,23 @@ export type AutocompleteSlotsAndSlotProps<
ref?: React.Ref<Element>;
}
>,
- {},
+ AutocompleteListboxSlotPropsOverrides,
AutocompleteOwnerState<Value, Multiple, DisableClearable, FreeSolo, ChipComponent>
>;
paper: SlotProps<
React.ElementType<Partial<PaperProps>>,
- {},
+ AutocompletePaperSlotPropsOverrides,
AutocompleteOwnerState<Value, Multiple, DisableClearable, FreeSolo, ChipComponent>
>;
popper: SlotProps<
React.ElementType<Partial<PopperProps>>,
- {},
+ AutocompletePopperSlotPropsOverrides,
AutocompleteOwnerState<Value, Multiple, DisableClearable, FreeSolo, ChipComponent>
>;
popupIndicator: SlotProps<
React.ElementType<Partial<IconButtonProps>>,
- {},
+ AutocompletePopupIndicatorSlotPropsOverrides,
AutocompleteOwnerState<Value, Multiple, DisableClearable, FreeSolo, ChipComponent>
>; This would allow custom props to be passed through TypeScript module augmentation, like: import Autocomplete from '@mui/material/Autocomplete';
import Button from '@mui/material/Button';
import Paper, { PaperProps } from '@mui/material/Paper';
import TextField from '@mui/material/TextField';
import * as React from 'react';
declare module '@mui/material/Autocomplete' {
interface AutocompletePaperSlotPropsOverrides {
values: Options[];
}
}
const CustomPaper = ({ children, values, ...paperProps }: PaperProps & { values: Options[] }) => (
<Paper {...paperProps} onMouseDown={(event) => event.preventDefault()}>
{children}
<Button sx={{ width: '90px' }} variant="contained" disabled={values.length === 0}>
Next
</Button>
</Paper>
);
interface Options {
title: string;
year: number;
}
export default function App() {
const [values, setValues] = React.useState<Options[]>([]);
return (
<Autocomplete
openOnFocus
multiple
disableCloseOnSelect
isOptionEqualToValue={(o, v) => o.title == v.title}
renderInput={(params) => <TextField {...params} placeholder={'Select'} />}
onChange={(e, selected) => {
setValues(selected);
}}
getOptionLabel={(option) => `(${option?.year}) ${option?.title}`}
options={[...top100Films]}
value={values}
slots={{ paper: CustomPaper }}
slotProps={{ paper: { values } }}
/>
);
} Would you like to create a new issue for this, and possibly submit a PR to fix it with a test? |
@ZeeshanTamboli Hey, thanks a lot for the quick response and solution! I don't mind creating an issue for it, but as for the PR, I have no clue on how to do it so I would rather not try it and risk messing things up haha 😅 EDIT: |
Duplicates
Latest version
Steps to reproduce 🕹
Link to live example: https://stackblitz.com/edit/stackblitz-starters-egy3ey?file=src%2FApp.tsx
Steps:
Result: You get scrolled to the top of the list.
Current behavior 😯
If I set a custom PaperComponent and I scroll down the list and select an item, I am scrolled back to top automatically.
Expected behavior 🤔
If I set a custom PaperComponent and I scroll down the list and select an item, I should stay scrolled down to that item.
Context 🔦
No response
Your environment 🌎
No response
The text was updated successfully, but these errors were encountered: