Skip to content
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

feat(text-area): add limitText prop to prevent input beyond max-length #11440

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -147,21 +147,27 @@ describe("calcite-text-area", () => {
expect(inputEventSpy).not.toHaveReceivedEvent();
});

it("should be able to enter characters beyond max-length", async () => {
const page = await newE2EPage();
await page.setContent("<calcite-text-area></calcite-text-area>");
describe("max-length", () => {
const inputText = "rocky mountains";
async function testMaxLength(pageContent: string, inputText: string, expectedValue: string): Promise<void> {
const page = await newE2EPage();
await page.setContent(pageContent);
const element = await page.find("calcite-text-area");

const element = await page.find("calcite-text-area");
element.setAttribute("max-length", "5");
await page.waitForChanges();
await element.callMethod("setFocus");
await page.keyboard.type(inputText);
await page.waitForChanges();

await page.keyboard.press("Tab");
await page.waitForChanges();
expect(await element.getProperty("value")).toBe(expectedValue);
}

await page.keyboard.type("rocky mountains");
await page.waitForChanges();
it("should be able to enter characters beyond max-length by default", async () => {
await testMaxLength("<calcite-text-area max-length='5'></calcite-text-area>", inputText, inputText);
});

expect(await element.getProperty("value")).toBe("rocky mountains");
it("can follow native max-length behavior and restrict input", async () => {
await testMaxLength("<calcite-text-area limit-text max-length='5'></calcite-text-area>", inputText, "rocky");
});
});

it("should have footer--slotted class when slotted at both start and end", async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ export class TextArea
/** Accessible name for the component. */
@property() label: string;

/**
* When `true`, prevents input beyond the maximum length, mimicking native `<textarea>` behavior.
*/
@property() limitText = false;
jcfranco marked this conversation as resolved.
Show resolved Hide resolved

/**
* When the component resides in a form,
* specifies the maximum number of characters allowed.
Expand Down Expand Up @@ -462,6 +467,7 @@ export class TextArea
}}
cols={this.columns}
disabled={this.disabled}
maxLength={this.limitText ? this.maxLength : -1}
eriklharper marked this conversation as resolved.
Show resolved Hide resolved
name={this.name}
onChange={this.handleChange}
onInput={this.handleInput}
Expand Down
Loading