Skip to content
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][Select] Select not propagating readonly attribute to input #42256

Open
Yuwan opened this issue May 16, 2024 · 2 comments · May be fixed by #42425
Open

[material-ui][Select] Select not propagating readonly attribute to input #42256

Yuwan opened this issue May 16, 2024 · 2 comments · May be fixed by #42425
Assignees
Labels
bug 🐛 Something doesn't work component: select This is the name of the generic UI component, not the React module! component: text field This is the name of the generic UI component, not the React module! good first issue Great for first contributions. Enable to learn the contribution process. package: material-ui Specific to @mui/material

Comments

@Yuwan
Copy link

Yuwan commented May 16, 2024

Steps to reproduce

Link to live example: (required)
https://stackblitz.com/edit/react-n9koip-itsgyz?file=index.html,Demo.tsx

Steps:

  1. create a TextField and a SelectTextField and set the attribute to readOnly
  2. add the css with input[readonly] = { color: 'lightgray' }
  3. TextField works well with the correct color
  4. SelectTextField without dropdown list as expected but the css style didn't work.

Current behavior

Ref SelectInput
https://github.com/mui/material-ui/blob/dc922eeec5c1870affd9dfa3c6a351786a2ba451/packages/mui-material/src/Select/SelectInput.js

  • SelectTextField without dropdown list as the logic on line 506
    onMouseDown={disabled || readOnly ? null : handleMouseDown}
    but the Input element without "readonly" attribute
    (please check the imaged below, you cannot find readonly from input element of selectTextfield)
    so in live demo page, the dropdown list didn't show color:lightgray

image

Expected behavior

the behavior should be aligned with the attribute "disabled" or the "readonly" attribute in TextField
Ref SelectInput
https://github.com/mui/material-ui/blob/dc922eeec5c1870affd9dfa3c6a351786a2ba451/packages/mui-material/src/Select/SelectInput.js (line.523-536)

<SelectNativeInput
        aria-invalid={error}
        value={Array.isArray(value) ? value.join(',') : value}
        name={name}
        ref={inputRef}
        aria-hidden
        onChange={handleChange}
        tabIndex={-1}
        disabled={disabled}
        className={classes.nativeInput}
        autoFocus={autoFocus}
        ownerState={ownerState}
        {...other}
      />

I expected there to be an attribute "readonly={readonly}" like disabled={disabled}, so the readonly can apply to the Input element in the end.

Context

Your environment

Browsers:
Chrome: 124.0.6367.201

Search keywords: TextField

Search keywords:

@Yuwan Yuwan added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label May 16, 2024
@LukasTy LukasTy transferred this issue from mui/mui-x May 16, 2024
@zannager zannager added the component: text field This is the name of the generic UI component, not the React module! label May 16, 2024
@ZeeshanTamboli
Copy link
Member

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.

@ZeeshanTamboli ZeeshanTamboli added bug 🐛 Something doesn't work good first issue Great for first contributions. Enable to learn the contribution process. component: select This is the name of the generic UI component, not the React module! package: material-ui Specific to @mui/material and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels May 27, 2024
@ZeeshanTamboli ZeeshanTamboli changed the title SelectTextField not support readonly attruibute [material-ui] Select not propagating readonly attribute to input May 27, 2024
@ZeeshanTamboli ZeeshanTamboli changed the title [material-ui] Select not propagating readonly attribute to input [material-ui][Select] Select not propagating readonly attribute to input May 27, 2024
qiweiii added a commit to qiweiii/material-ui that referenced this issue May 28, 2024
qiweiii added a commit to qiweiii/material-ui that referenced this issue May 28, 2024
@qiweiii qiweiii linked a pull request May 28, 2024 that will close this issue
1 task
@aarongarciah
Copy link
Member

aarongarciah commented May 28, 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something doesn't work component: select This is the name of the generic UI component, not the React module! component: text field This is the name of the generic UI component, not the React module! good first issue Great for first contributions. Enable to learn the contribution process. package: material-ui Specific to @mui/material
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants