Skip to content

improvement: associating label with input#112

Merged
nlopin merged 3 commits intofreenowtech:mainfrom
lloydaf:improvement/associating-label-with-input
Jun 15, 2021
Merged

improvement: associating label with input#112
nlopin merged 3 commits intofreenowtech:mainfrom
lloydaf:improvement/associating-label-with-input

Conversation

@lloydaf
Copy link
Contributor

@lloydaf lloydaf commented Jun 9, 2021

What:

​This PR associates the label in the SelectList component with the input element that it contains
Why:

​This is to improve accessibility, before this change, there was a label and there was a select dropdown, but both were not tied to each other
How:

​1. Generate an ID property
2. Assign that to the dropdown
3. Use that id with htmlFor for the label
Media:

https://www.loom.com/share/736830f773bb4343a8ee2232d6e067df
Checklist:

  • Ready to be merged

Copy link
Contributor

@duranmla duranmla left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! thanks for this @lloydaf what wasn't clear for me is that I can still provide a custom id for my component and it will be bound to the label isn't?

const { widthProps, restProps } = extractWidthProps(withoutMargin);
const { components, isDisabled, variant, inverted, size, error, label } = restProps;

const id = useGeneratedId();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lloydaf thanks!
I see a slightly different usage here https://github.com/freenowtech/wave/pull/86/files that allows to devs to pass their own id and whenever it is not present then wave will generate one. It seems to not be the case for this implementation, or I am missing something?

Copy link
Contributor Author

@lloydaf lloydaf Jun 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As of now (according to this PR), it is auto generated, as the purpose is only to link the Label in the SelectList with the input field.

If we want to make this configurable as a prop, imo we should make the Label configurable as well, because otherwise, if you pass id as a prop and have a label outside SelectList referring to that id, you will end up with two labels for the input field (one from outside and one from inside SelectList), which I think can potentially cause weird accessibility issues. (I could be wrong, maybe the intention is to have multiple labels)

Just to keep in mind, don't know if I missed something.

Do you want to make id configurable as a prop?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need the id to be configurable as a prop but we can't have the odd behavior you describe above because indeed is wrong. What I am expecting is to:

<SelectList
  id="select-list-playground"
  label="City"
  onChange={change => console.log(change)}
  options={[
    {
      label: 'Barcelona',
      value: 'bcn',
    },
    {
      label: 'Hamburg',
      value: 'ham',
    },
    {
      label: 'Paris',
      value: 'par',
      isDisabled: true,
    },
  ]}
/>

This should render a select with a label "City" that is associated to the input. Isn't? Maybe I am missing something.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get the intention, but here's what I see could be a problem:

<SelectList
  id="select-list-playground"
  label="City"
  onChange={change => console.log(change)}
  options={[
    {
      label: 'Barcelona',
      value: 'bcn',
    },
    {
      label: 'Hamburg',
      value: 'ham',
    },
    {
      label: 'Paris',
      value: 'par',
      isDisabled: true,
    },
  ]}
/>
<label htmlFor="select-list-playground">Second Label for the city</label>

consider this usecase. If we generate a new id inside SelectList, then this will work fine. The second label will refer to the SelectList component. If we change this to a prop, and pass that prop as the id of the Input, then the second label will refer to the Input. Which I think could be problematic. So imo the current implementation works fine, unless you have a strong opinion about making id configurable, if you can think of a usecase

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to hear @nlopin or @snapsnapturtle in this matter as I am a bit lost with the component. But thanks for outline more context @lloydaf ! 🚀

Copy link
Contributor

@nlopin nlopin Jun 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lloydaf the example above is not a valid HTML. According to the HTML spec:

The LABEL element may be used to attach information to controls. Each LABEL element is associated with exactly one form control.

The id prop should be available to be set by a developer in case it is needed, if not provided, we generate the id automatically

Copy link
Contributor Author

@lloydaf lloydaf Jun 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nlopin in the next paragraph, it says

More than one LABEL may be associated with the same control by creating multiple references via the for attribute.

This was the usecase I was trying to show above.

As a user (developer), it is not clear to me that the "id" that is assigned to the SelectList component is used to assign to the Input field. Semantically it would make more sense to me if we use inputId as the propname the same way react-select does. Also it still seems to be syntactically possible to have multiple labels point to the same control, according to the spec.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made an inputId prop to pass down to SelectList, updated documentation

Copy link
Contributor

@nlopin nlopin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job! Sorry that it took a lot of time from our side

@nlopin nlopin merged commit a5ede85 into freenowtech:main Jun 15, 2021
github-actions bot pushed a commit that referenced this pull request Jun 15, 2021
## [1.5.0](v1.4.4...v1.5.0) (2021-06-15)

### Features

* **SelectList:** associate label with the input ([#112](#112)) ([a5ede85](a5ede85))
@github-actions
Copy link
Contributor

🎉 This PR is included in version 1.5.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@lloydaf lloydaf deleted the improvement/associating-label-with-input branch June 15, 2021 10:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Development

Successfully merging this pull request may close these issues.

4 participants