-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
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
useInput defaults to non-unique IDs #9534
Comments
the same issue applies when using filter inputs and |
Thanks for the report, reproduced! |
Note that this can be worked around by manually specifying an id <TextInput source="title" />
<TextInput source="title" id="title2" /> |
We will support this ticket in react-admin v5. |
a polyfill if you want to support React 16 could be (for example): let globalCounter = 0;
function generateUniqueId() {
const id = globalCounter;
globalCounter++;
return `:${id}:`;
}
const useId = () => useState(generateUniqueId)[0]; |
The problem with this change is that it makes the Besides, it's a big breaking change: any app relying on input ids for styles or imperative JS will break. |
@fzaninotto React's built-in For the other concerns: using IDs in those ways is not recommended anyway (and has been considered an anti-pattern for over a decade). Prefer using The bigger issue is that IDs must be unique within a page, which currently isn't the case in a number of situations when they're being set based on other data (as noted in my original post). Having two elements with the same ID can cause all sorts of issues, some more noticeable than others, and just because it works for you in one browser does not mean it will work in another, or when using accessibility tools, or when using various browser plugins, or even in a different version of the same browser. It's a very risky position to be in! |
I don't object fixing the issue, I object using |
@fzaninotto FTR, I'm rather with @david-bezero on this one. |
Besides, |
all right, I'm convinced, go for |
Fixed by #9788 |
This is mostly relevant when using the enterprise package
@react-admin/ra-form-layout
use-case: using a modal "create" form while on an "edit" page to create a sub-resource of the same type (but could also apply to different types which share property names)
What you were expecting:
All form elements (i.e.
useInput
) should default to having a unique ID which does not conflict with other forms (i.e. via React'suseId
hook)What happened instead:
Form elements default to using the
source
as an ID, which is not unique if multiple forms are on the page.Steps to reproduce:
The browser will complain about multiple fields sharing the same ID, and in some situations this will cause the fields to be impossible to select (e.g. playwright browser testing fails to find the fields)
It is possible to work around this by explicitly providing an
id
to all fields, but this is tedious, easy to forget, and difficult to diagnose failures. Changing the default to a unique ID would make it guaranteed to be correct without extra work for the developer.Environment
The text was updated successfully, but these errors were encountered: