Support Missing `name` prop for `<Select>`
See original GitHub issueReproduction link (click button below)
Steps to reproduce
Attempt to pass name
property to <Select>
element
What is expected?
- You should be able to pass
name
prop to<Select>
element. After passingname
prop, it should get passed down to theinput
element that gets rendered in the DOM. - Allowing user to pass
name
also aligns with other Ant form elements such as<Input>
which does acceptname
attribute
<Input
placeholder="Employee name"
name="name" // ✅
/>
<Select
// name="" ❌ `name` prop should be allowed. `name` is valid attribute on <select> elements in HTML standard: https://www.w3schools.com/tags/att_select_name.asp --- https://developer.mozilla.org/en-US/docs/Web/HTML/Element/select#attr-name
placeholder="Select a department"
showSearch
allowClear
defaultValue=""
>
{["Engineering", "Sales", "Finance", "Marketing", "Legal"].map(
(option) => (
<Select.Option
key={option}
value={option}
name="department"
>
{option}
</Select.Option>
)
)}
</Select>
Per regular HTML rules / specifications, HTML <select>
is allowed to have a name
attribute on it.
Why is this Change Important?
- When you submit an HTML form, the
name
attribute on the form elements (input, select etc ) is used to create a key inside the form object. For example, you can verify this by filling in the form in the codesandbox demo and calling the function below inside your browser developer tools (NOTE the code snippet below is not how I write regular react code reaching into the DOM, the method below is for illustrative debugging purpose)::
const getFormData = () => {
const formObject = new FormData(document.querySelector("#my-form") as HTMLFormElement) as any;
const formData = Object.fromEntries(formObject);
console.log(`form values`, formData);
};
getFormData() // call from javascript console after filling out form
Codesandbox: https://codesandbox.io/s/antd-select-missing-name-attribute-forked-tmlsg0?file=/src/App.tsx
https://user-images.githubusercontent.com/6743796/178577370-7d28378c-3036-41fc-ba49-80827c089176.mp4
- When you submit an HTML form with attribute
method=get
, the native HTML form element does not find thename
attribute associated with the ant’s<Select>
element so the name attribute is not being added to the URL Demo URL (open the preview in a new tab by clicking “Open in New Window”):
- Stackblitz URL:
- code (click “Open in New Window” to see URL param updates): https://stackblitz.com/edit/node-6z19rg?file=package.json,app%2Froutes%2Findex.tsx
- code preview URL: https://node-6z19rg--3000.local.webcontainer.io/
https://user-images.githubusercontent.com/6743796/178580314-55669e6b-29cf-4727-9016-83220cb7f798.mp4
- Many people would like to use Ant’s form element’s for decorative purposes only and often cannot use the
<Form>
element from Ant. (example stackblitz sandbox)
What is actually happening?
- When pass
name
prop you get a typescript error/warning. - Passing
name
prop with value does not attach name prop with value to the<input>
element associated with the<Select>
element in the DOM (see attached video)
Environment | Info |
---|---|
antd | 4.21.6 |
React | 18.0.0 |
System | MacOS |
Browser | Chrome latest version |
Related
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:7 (3 by maintainers)
Top Results From Across the Web
react-hook-form material-ui dropdown Field is missing `name ...
I get the value of the dropdown {node: input, value: "MANAG", focus: ƒ} ERROR: Field is missing name attribute: MISSING THE NAME ATTRIBUTE...
Read more >API - React Select
Pass in the name of the property as the first argument, and the current props as the second argument. See the styles object...
Read more >SyntaxError: missing name after . operator - JavaScript | MDN
The JavaScript exception "missing name after . operator" occurs when there is a problem with how the dot operator ( . ) is...
Read more >QRadar: Custom Event Property "Rule Name" is missing ... - IBM
QRadar: Custom Event Property "Rule Name" is missing from the drop-down menu when selecting rules for a Routing Rule · Problem · Symptom...
Read more >Select-Object - PowerShell - Microsoft Learn
Beginning in PowerShell 6, Select-Object supports selecting the keys of hashtable input as properties. The following example selects the weight and name keys...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Why doesn’t Select support the name attribute? This issue should not be closed until a reasonable explanation is given.
I suppose my main 2 questions are 😅 :
1.Why not support
name
if its supported with<Input>
?<select>
supportsname
prop, why not for<Select>
?name
is inconsistent with how ant’s<Input>
supportsname
prop like regular HTML standard which supports name2.How can I pass a
name
prop to<Select>
field (technically<Select>
renders to an<input>
in DOM per video linked above)?<form>
or Remix’s <Form> elementI am happy to open a PR if you are open to the idea; I’m just not sure if the change is in the
ant-design
repo orreact-component/select
repoistiory