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
[material-ui][Autocomplete] Type issue once wrapped with styled() #21727
Comments
This makes me think of #19842. Do you have a reproduction? |
This comment has been minimized.
This comment has been minimized.
Hi @oliviertassinari TBH not sure how the types working in sandbox ? But I still believe we have kind of the same issue. Please checkout this link: The Hope that can help. |
Thanks for the reproduction, would this help https://codesandbox.io/s/pedantic-tharp-bdwj8?file=/src/Autocomplete.tsx? |
That's actually a solution but dose that considered as permanent solution ? Also why What do you think ? |
I'm not aware of a better solution, but I'm no expert on TypeScript either. The concern seems something we would at the very least to document. |
Just to give an update that might help as input. I tried to apply your solution in my function StyledAutocomplete<
T,
Multiple extends boolean | undefined,
DisableClearable extends boolean | undefined,
FreeSolo extends boolean | undefined
>(props: AutocompleteProps<T, Multiple, DisableClearable, FreeSolo>) {
return <Autocomplete2 {...props} />;
} Linter error said
The interesting thing is in this line with my onChange={(_event: React.ChangeEvent<{}>, value) => { ... }). // <<== When hover on `value` i can see "(parameter) value: string | AutocompleteOptionData | (string | AutocompleteOptionData)[] | null" Note that other events argument in |
Did u make this work ? |
I'm casting argument types as follow: [...]
interface AutocompleteOptionData {
value: string;
label: string;
}
[...]
<StyledAutocomplete
id={id}
options={options}
onChange={(_event: React.ChangeEvent<{}>, value) =>
onChange &&
onChange(
(value as AutocompleteOptionData | null) || {
label: '',
value: '',
}
)
}
value={currentValue}
getOptionLabel={data => (data as AutocompleteOptionData).label}
getOptionSelected={option =>
(option as AutocompleteOptionData).label === currentValue?.label
}
renderInput={params => (
<TextField
{...params}
error={!!error}
name={name}
label={label}
variant="standard"
/>
)}
/>
[...] TBH, Autocomplete is not easy component but i understand that is designed to be generic. |
Thanks this is good I used something similar as well 👍 |
Casting does remove the errors but still feels like a hack. |
The approach in https://codesandbox.io/s/pedantic-tharp-bdwj8?file=/src/Autocomplete.tsx seems to work, I think that we can go with the docs. |
I'm not sure if this topic is related to my issue but it looks very similar. I use Styles look like this import { styled, Autocomplete as AutocompleteOriginal } from '@material-ui/core'
export const Autocomplete = styled(AutocompleteOriginal)(props => ({
// styles go here
})) If I use P.S. I use |
styled-components
@enheit Yes, it's the same issue. I have updated #21727 (comment) to better function. |
@oliviertassinari -- there are two approaches here and comments indicate the second doesn't work -- which approach exactly where you proposing? 1 or 2? |
I found the following code make it works for me. Not sure if it will help or not since I'm using MUI v5 and not the version you mentioned. Post it here in case it helps, const StyledAutocomplete = styled(Autocomplete)`
& .MuiInputBase-root {
height: 4.1rem;
}
& label: {
fontsize: 2rem;
}
` as typeof Autocomplete; |
Revisiting this in Q4 '23, it looks like we could close this issue by documenting @NunchakusLei's solution in the Autocomplete doc. |
I'm trying to use
Autocomplete
withstyled-components
in typescript environment. But when using the component, linter start to complain about styled autocomplete component. I checked for this issue but not sure if it is duplicate. Could not find something related?Current Behavior 😯
I started to add one prop by another then i realised the following:
Expected Behavior 🤔
Would it be possible to take
T
type of theoptions
property and pass it throw all function that working with the same type ?Steps to Reproduce 🕹
Simply styled
Autocomplete
withstyled-components
.Context 🔦
As consistency we use
styled-components
as a generic way of styling. We need to use the same here. The styling suggestion works fine usingmakeStyles
Your Environment 🌎
The text was updated successfully, but these errors were encountered: