ant-design: Support Missing `name` prop for `
Reproduction 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
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 6
- Comments: 18 (4 by maintainers)
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
repoistioryThanks for taking a look 🙏
I am seeing an
<input>
element whenshowSearch={false}
as well (video shows) Stackblitz Example: https://stackblitz.com/edit/react-ltsgbj?file=demo.jshttps://user-images.githubusercontent.com/6743796/178735079-0bddf898-3843-492a-9909-08a9cd94b350.mp4
Is there a strong reason why we don’t support passing
name
prop on<Select>
like we allow for<Input>
?Ant’s
<Input>
element:name
prop; follows standard behavior of HTML (docs)name
prop is supported, you can wrap regular HTML<form>
element around ant design elementsAnt’s
<Select>
element:❌ supports
name
prop; follows standard behavior of HTML (docs. For example a regular<select>
element supportsname
❌ because
name
prop is not supported, you cant wrap regular HTML<form>
element around ant design elements (ifname
prop is supported this can be true ✅ )Although I do care about the HTML standard, its not the motivation for this “bug” report.
The motivation is:
<form>
around<Select>
because it doesn’t allow me add aname
prop to it.<Input>
which does supportname
prop which means I can use a regular<form>
<Form>
element to do data submission’s because thename
property is missing on<Select>
element🤦
@afc163 - can we revisit this? Remix & NextJS rely on the expectation that a select element can have a
name
prop. The motivation is:<form>
around<Select>
because it doesn’t allow me add a name prop to it. There is no issue with ant’s<Input>
which does support name prop which means I can use a regular<form>
<Form>
element to do data submission’s because the name property is missing on<Select>
element this is also standard HTML behavior and I outlined a solution to move forwardKindly review the initial detailed issues description at the top of the page 🙏
I have the same issue with Remix + Antd and most of the projects I created are stuck on
select
element or data entry input which does not support native html input. I hope Antd team can solve this problem soon.Need name for Select
Still No support?
looking for the same… Following
Why not support this? Is there any specific reason? We are using react-hook-form and it validates/controls form elements using the name property. I am pretty sure other form libraries and native form elements also use the name attribute for submission and validation.
So are you not going to support it?