Skip to content

USWDS - Validation: Allow textareas to use validation (Enhancement) #5233

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

Merged
merged 1 commit into from
Feb 26, 2024

Conversation

danbrady
Copy link
Contributor

Summary

Textarea components can use validation Now you can use validation on textareas, just like you do on inputs.

Breaking change

This is not a breaking change.

Related issue

Closes #4702

Problem statement

Currently, validation can only be used on input elements. This solves most validation needs. However, textarea is a form element in which validation might be desired (e.g. not empty, minimal number of characters or words.)

Solution

  1. Current selector for validation is input[data-validation-element]
  2. Updated validation logic to include textarea[data-validation-element]

Testing and review

Share recommended methods for reviewing this change.

  1. Added textarea to tests

Dependency updates

None.

@amyleadem
Copy link
Contributor

Thanks for this submission! I have tagged this for review and we will take a look at our next available opportunity. If you have any questions in the meantime, please let us know!

Copy link
Contributor

@mejiaj mejiaj left a comment

Choose a reason for hiding this comment

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

@danbrady thanks for the contribution, but I'm not sure I understand the problem.

Based on this comment:

However, textarea is a form element in which validation might be desired (e.g. not empty, minimal number of characters or words.)

This could be solved by using character count. Could you provide a more specific use case?

@danbrady
Copy link
Contributor Author

Good question, @mejiaj. Is it fair to think character count is validation for a single, specific requirement? (I.e. cannot be more than N characters.) From the docs:

Character count helps users know how much text they can enter when there is a limit on the number of characters.

(Admittedly, this is probably the most useful validation for a textarea.)

However, allowing textarea to use the validation package allows it to provide visual feedback for multiple validation requirements that a textarea could have. For example: must have a minimal number of characters/words.

The use cases are likely rare, but are not non-existent.

@danbrady danbrady force-pushed the db-textarea-validation branch from aafc952 to 68f29da Compare April 13, 2023 16:55
Copy link
Contributor

@mejiaj mejiaj left a comment

Choose a reason for hiding this comment

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

Looks good as far as extending the functionality to textarea. Just some comments below.

<label class="usa-label" for="comment">Comment</label>
<textarea class="usa-textarea" id="comment" name="comment" data-validate-empty="\S" data-validation-element="validate-comment"></textarea>
<input class="usa-button" type="submit" value="Submit comment">
</fieldset>
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we separate these tests, one for input and another for text area?

Copy link
Contributor

Choose a reason for hiding this comment

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

Should we add a test that mentions initialization on textarea?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed. I'll separate textarea into separate story, test, etc. and add this to it instead of mixing it up with input. I'll update the PR...

@danbrady danbrady force-pushed the db-textarea-validation branch 6 times, most recently from 73aad4b to 5ed5f75 Compare May 12, 2023 14:27
@danbrady danbrady force-pushed the db-textarea-validation branch from ccdd95d to 0b4aa55 Compare May 12, 2023 15:04
@danbrady
Copy link
Contributor Author

Ok, @mejiaj, I split textarea out. A few observations when looking at the tests on the original input validation:

const VALIDATOR_LABEL = "[data-checklist-label]";

I don't see an element with that data-checklist-label selector in any template?

describe("initialization", () => {
it("creates a hidden span element in each list item", () => {
validators.forEach((checkbox) => {
assert.notStrictEqual(checkbox.queryElement(VALIDATOR_LABEL), null);
});
});
});

queryElement isn't a valid method (perhaps querySelector was intended?), however execution never makes it to that point to throw an error because the validators object is always empty...

const inputSelector = () => document.querySelector(INPUT_SELECTOR);

root = inputSelector();
validated = inputSelector();
validators = root.querySelectorAll(VALIDATORS);

(It's looking for the validators within the input and not the validation checklist.)

Amazingly, all the test pass! You can even change all the asserts to anything and it still passes. 😄

I addressed all these in the textarea tests. Please let me know if I'm on the right track with them? And if I am, should I update the tests for input as part of this PR? Thanks!

@mejiaj
Copy link
Contributor

mejiaj commented Nov 24, 2023

Thanks for making these changes and apologies for the late reply.

Data checklist label

I don't see an element with that data-checklist-label selector in any template?

Looks like a regression. This was introduced in #4914. Looks like in rework it was removed in commit c284450, but the test wasn't updated. Thanks for catching.

Unit tests

Validation list items

queryElement isn't a valid method (perhaps querySelector was intended?), however execution never makes it to that point to throw an error because the validators object is always empty...

Yes, that looks incorrect.

Incorrect validators

const inputSelector = () => document.querySelector(INPUT_SELECTOR);

root = inputSelector();
validated = inputSelector();
validators = root.querySelectorAll(VALIDATORS);

(It's looking for the validators within the input and not the validation checklist.)

This should fix this. Validators are set in list items and should be checked there.

It's also a good callout for improving our testing, thank you.


@thisisdano @amyleadem I've removed data from this PR and set that in the original issue instead. Because of the additional work involved, we should move this to 3.8.0.

@mejiaj mejiaj removed this from the uswds 3.7.1 milestone Nov 24, 2023
@mejiaj
Copy link
Contributor

mejiaj commented Feb 1, 2024

Created issue for the additional work #5756.

Todo

  • Remove unused data attributes
  • Remove unused references in unit tests

Copy link
Contributor

@mahoneycm mahoneycm left a comment

Choose a reason for hiding this comment

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

Component, stories, and tests look good! Left two comments for potential future enhancements for the team.

CC: @amyleadem @mejiaj

Comment on lines +6 to +7
const VALIDATE_INPUT =
"input[data-validation-element],textarea[data-validation-element]";
Copy link
Contributor

Choose a reason for hiding this comment

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

Validate input selector

Curious if there is a slightly cleaner way to define this selector. Probably not a blocker because this works well but may be room for a future enhancement in the future 🤔

Copy link
Contributor

Choose a reason for hiding this comment

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

Validation Alert: Defined width (future enhancement)

Currently, the validation alert matches the size of the input it's tied to. With text area, users can resize the text box using the corner of the input and it causes the alert to grow with it.

I'm curious if it'd be a good idea to set a max width on the alert or to limit the text area to only extend vertically. Flagging for consideration!

Kapture.2024-02-05.at.17.21.47.webm

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't mind this behavior, actually, but that might just be personal preference. Happy to discuss this as a possible future change if desired. I don't think it needs to be a blocker for this PR.

Copy link
Contributor

@amyleadem amyleadem left a comment

Choose a reason for hiding this comment

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

Looks good to me!

Here is what I tested:

  • Confirm that the validation component works with textarea elements
  • Confirm that a new story for the textarea variant exists and works as expected
  • Confirm unit tests work as expected

@amyleadem amyleadem requested a review from thisisdano February 22, 2024 23:05
Copy link
Contributor

@thisisdano thisisdano left a comment

Choose a reason for hiding this comment

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

LGTM

@aduth
Copy link
Contributor

aduth commented Mar 1, 2024

It looks like the changes here may have introduced an accessibility build failure which is causing other pull requests to fail.

Edit: See fix at #5795

  1) Components/Validation
       Textarea Validation:
     Detected the following accessibility violations!

     1. label (Form elements must have labels)

        For more info, visit https://dequeuniversity.com/rules/axe/4.8/label?application=axeAPI.

        Check these nodes:

        - html: <textarea class="usa-textarea" id="comment" name="comment" data-validate-empty="\S" data-validation-element="validate-comment"></textarea>
          summary: Fix any of the following:
                     Form element does not have an implicit (wrapped) <label>
                     Form element does not have an explicit <label>
                     aria-label attribute does not exist or is empty
                     aria-labelledby attribute does not exist, references elements that do not exist or references elements that are empty
                     Element has no title attribute
                     Element has no placeholder attribute
                     Element's default semantics were not overridden with role="none" or role="presentation"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Validation Support for textarea input types
6 participants