Visitar URL original
USWDS - Validation: Allow textareas to use validation (Enhancement) by danbrady · Pull Request #5233 · uswds/uswds · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/usa-validation/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ const validate = require("../../uswds-core/src/js/utils/validate-input");
const { prefix: PREFIX } = require("../../uswds-core/src/js/config");
const selectOrMatches = require("../../uswds-core/src/js/utils/select-or-matches");

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

Copy link
Copy Markdown
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 🤔

const CHECKLIST_ITEM = `.${PREFIX}-checklist__item`;

// Trigger validation on input change
Expand Down
13 changes: 13 additions & 0 deletions packages/usa-validation/src/test/template-textarea.html

Copy link
Copy Markdown
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
Copy Markdown
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.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<div>
<ul class="usa-checklist" id="textarea_validation_list">
<li class="usa-checklist__item" data-validator="empty">Cannot by empty</li>
</ul>
<textarea
class="usa-textarea"
id="comment"
name="comment"
aria-describedby="textarea_validation_list"
data-validate-empty="\S"
data-validation-element="textarea_validation_list"
></textarea>
</div>
81 changes: 81 additions & 0 deletions packages/usa-validation/src/test/validator-textarea.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
const fs = require("fs");
const path = require("path");
const assert = require("assert");
const validator = require("../index");

const TEMPLATE = fs.readFileSync(
path.join(__dirname, "/template-textarea.html")
);

const TEXTAREA_SELECTOR = "[data-validation-element]";
const VALIDATORS = "[data-validator]";
const VALIDATOR_SUMMARY = "[data-validation-status]";
const CHECKED_CLASS = "usa-checklist__item--checked";

const textareaSelector = () => document.querySelector(TEXTAREA_SELECTOR);

const keyup = (el) => {
el.dispatchEvent(new Event("change", { bubbles: true }));
};

const tests = [
{ name: "document.body", selector: () => document.body },
{ name: "textarea", selector: textareaSelector },
];

tests.forEach(({ name, selector: containerSelector }) => {
describe(`validator component initialized at ${name}`, () => {
const { body } = document;

let validated;
let validators;

beforeEach(() => {
body.innerHTML = TEMPLATE;

validator.on(containerSelector());

validated = textareaSelector();
validators = Array.from(document.querySelectorAll(VALIDATORS));
});

afterEach(() => {
validator.off(containerSelector());
body.textContent = "";
});

describe("initialization", () => {
it("creates a hidden span element for accessibility status summary readout", () => {
if (validators) {
assert.notStrictEqual(
document.querySelector(VALIDATOR_SUMMARY),
null
);
}
});
});

describe("validation state", () => {
it(`adds .${CHECKED_CLASS} for all successful validations`, () => {
validated.value = "This is one great comment!";
keyup(validated);
validators.forEach((checkbox) => {
assert.strictEqual(checkbox.classList.contains(CHECKED_CLASS), true);
});
});

it(`removes .${CHECKED_CLASS} for failed validations`, () => {
validated.value = "";
keyup(validated);
validators.forEach((checkbox) => {
const checked = checkbox.classList.contains(CHECKED_CLASS);
if (checkbox.getAttribute("data-validator") === "empty") {
assert.strictEqual(checked, false);
} else {
assert.strictEqual(checked, true);
}
});
});
});
});
});
16 changes: 16 additions & 0 deletions packages/usa-validation/src/usa-validation--textarea.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<form class="usa-form">
<fieldset class="usa-fieldset">
<legend class="usa-legend usa-legend--large">Enter a comment</legend>
<div class="usa-alert usa-alert--info usa-alert--validation">
<div class="usa-alert__body">
<h3 class="usa-alert__heading">Comment requirements</h3>
<ul class="usa-checklist" id="validate-comment">
<li class="usa-checklist__item" data-validator="empty">Cannot be empty</li>
</ul>
</div>
</div>
<label class="usa-label" for="code">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 code">
</fieldset>
</form>
5 changes: 4 additions & 1 deletion packages/usa-validation/src/usa-validation.stories.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import Component from "./usa-validation.twig";
import TextareaComponent from "./usa-validation--textarea.twig";

export default {
title: "Components/Validation",
};

const Template = (args) => Component(args);
const TextareaTemplate = (args) => TextareaComponent(args);

export const Validation = Template.bind({});
export const InputValidation = Template.bind({});
export const TextareaValidation = TextareaTemplate.bind({});