Comments (4)
Important context: the readOnly
HTML is only supported on input
(only with some types like text
, password
, etc.) and textarea
elements. Read more in MDN. readOnly
is not supported on select
elements.
I've been digging deeper into this problem and it's more complex than it seems. I've created these two demos to showcase the issues.
- Using v5 (currently 5.15.19): CodeSandbox
- Using the PR version: CodeSandbox
You can see the differences between when the .Mui-readOnly
class is applied and when the readonly
attribute ends up in the DOM between the two versions. We should rely on the .Mui-readOnly
class name being present to style read-only inputs and not in the readonly
attribute is present.
To get the .Mui-readOnly
present, this is the way to do it as of today:
<TextField select>
: passInputProps={{ readOnly: true }}
(or usingslotProps.input
onceInputProps
gets deprecated in v6).<Select>
: passreadOnly
.inputProps.readOnly
doesn't work, neither in the PR version.<NativeSelect>
: passreadOnly
. This gets the.Mui-readOnly
class applied but the field is not read-only in practice.
Conclusions:
- Developers should rely on
.Mui-readOnly
class names to style read-only inputs and not in thereadonly
attribute being present or not. - In the future, I think we should consider removing the support for read-only
Select
as it deviates from the native HTML element. Or at least make it accessible since it's no the case at the moment. - Components that accept a read-only state like
TextField
should have a dedicated prop for it and developers shouldn't rely on passingInputProps={{ readOnly: true }}
and the like. - I think we should close the PR since it's not really solving anything if my analysis is correct.
cc @DiegoAndai
from material-ui.
I agree with your analysis @aarongarciah 👌🏼 as well as with closing the PR
I'm adding this issue to the Base UI milestone so we can revisit it when the Select component is refactored on top of Base UI. When that happens, we can revisit the following:
- In the future, I think we should consider removing the support for read-only Select as it deviates from the native HTML element. Or at least make it accessible since it's no the case at the moment.
- Components that accept a read-only state like TextField should have a dedicated prop for it and developers shouldn't rely on passing InputProps={{ readOnly: true }} and the like.
from material-ui.
I confirm it's a bug. The readOnly
attribute is not passed to the hidden input. Would you like to create a PR? We would also need to add a test.
from material-ui.
Note that even when we fix the issue and readOnly
is propagated to the underlying input
element, applying styles such as input[readonly] { color: lightgray }
won't work because in the case of Select
the input is visually hidden.
Also, although we support readOnly
in Select
, native select
elements don't support readonly
attributes (see HTML spec). It has no effect and even HTML validators will point it as an error. For example, readOnly
in NativeSelect
(which renders a plain select
) end up in the DOM but has no effect.
Side note: we're not doing a good job of signaling states to assistive tech. When focusing the inputs in this demo https://mui.com/material-ui/react-select/#other-props the error, read-only, and required "states" are not announced.
from material-ui.
Related Issues (20)
- [material-ui][Text FIeld] Outlined variant label is broken
- [docs] Material Icon Imports wrong in docs
- [joy-ui][Tooltip] Not working with custom sub-components
- [material-ui][Select] Cycle On Focus Without Having Options Open
- [material-ui][TablePagination] TablePagination component throws `You have provided an out-of-range value `4` for the select component.` warning HOT 1
- Using material ui styles with remix v2
- {...other} would overwrites the ownerState
- [RFC] Any Roadmaps or Plans for fully support for Next.js App Router and Server Components?
- [docs] Change dark theme component background color without affecting other states
- [core] Adopt eslint-plugin-react-compiler
- Textfield add autoSelect prop HOT 1
- When trying to link an application using MUI v5, application gets "TypeError: Cannot read properties of null (reading 'useContext')"
- can't set drawer root element HOT 1
- Footer pagination moves when appear / disappear the vertical scrollbar HOT 1
- [material-ui][Rating] Screen reader announces incorrect value for maximum amount of stars
- [core] Fix eslint-plugin-react-compiler issues HOT 8
- Issue with JAWS on DateTimePicker component v5.0.20
- Maps autocomplete example documentation incomplete
- [examples] Migrate Next.js config.next.js to config.next.mjs HOT 1
- dashed borderStyle does not work for Divider with text HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from material-ui.