question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support Missing `name` prop for `<Select>`

See original GitHub issue

Reproduction link (click button below)

Demo Bug on CodeSandbox

Steps to reproduce

Attempt to pass name property to <Select> element CleanShot 2022-07-12 at 11 44 45@2x



What is expected?

  1. You should be able to pass name prop to <Select> element. After passing name prop, it should get passed down to the input element that gets rendered in the DOM.
  2. Allowing user to pass name also aligns with other Ant form elements such as <Input> which does accept name 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>

CleanShot 2022-07-12 at 11 54 45@2x

Per regular HTML rules / specifications, HTML <select> is allowed to have a name attribute on it.



Why is this Change Important?

  1. 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

  1. When you submit an HTML form with attribute method=get, the native HTML form element does not find the name 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”):

https://user-images.githubusercontent.com/6743796/178580314-55669e6b-29cf-4727-9016-83220cb7f798.mp4

  1. 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?

  1. When pass name prop you get a typescript error/warning.
  2. 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:closed
  • Created a year ago
  • Reactions:1
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

6reactions
nnmaxcommented, Nov 28, 2022

Why doesn’t Select support the name attribute? This issue should not be closed until a reasonable explanation is given.

5reactions
cliffordfajardocommented, Jul 13, 2022

I suppose my main 2 questions are 😅 :

1.Why not support name if its supported with <Input>?

  • if a regular <select> supports name prop, why not for <Select>?
    • not supporting name is inconsistent with how ant’s <Input> supports name prop like regular HTML standard which supports name

2.How can I pass a name prop to <Select> field (technically <Select> renders to an <input> in DOM per video linked above)?

I 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 or react-component/select repoistiory

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found