Giter Site home page Giter Site logo

Comments (4)

aarongarciah avatar aarongarciah commented on June 15, 2024 2

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.

  1. Using v5 (currently 5.15.19): CodeSandbox
  2. 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>: pass InputProps={{ readOnly: true }} (or using slotProps.input once InputProps gets deprecated in v6).
  • <Select>: pass readOnly. inputProps.readOnly doesn't work, neither in the PR version.
  • <NativeSelect>: pass readOnly. 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 the readonly 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 passing InputProps={{ 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.

DiegoAndai avatar DiegoAndai commented on June 15, 2024 1

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.

ZeeshanTamboli avatar ZeeshanTamboli commented on June 15, 2024

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.

aarongarciah avatar aarongarciah commented on June 15, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.