-
Notifications
You must be signed in to change notification settings - Fork 9
Add character limit to text fields (D1559) #623
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
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7fc89b3
Add max_length to string column
droberts-ctrlo c0c05ab
Updated views to add max length
droberts-ctrlo 73dc7c3
Add field length control
droberts-ctrlo 222870b
Register component and styles
droberts-ctrlo 3baa7e8
Add validation to length
droberts-ctrlo 858d655
Add backend error
droberts-ctrlo 8c5a79d
Changes as per review
droberts-ctrlo 6d9e084
Add unit test to check for invalid string length
droberts-ctrlo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/frontend/components/form-group/field-length/_field-length.scss
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| .counter { | ||
| @extend .d-flex; | ||
| @extend .justify-content-end; | ||
| @extend .text-muted; | ||
| @extend .small; | ||
|
|
||
| &.invalid { | ||
| @extend .text-danger | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| import { initializeComponent } from "component" | ||
| import FieldLengthComponent from "./lib/component" | ||
|
|
||
| export default (scope) => { | ||
| initializeComponent(scope, "[data-max]", FieldLengthComponent); | ||
| } |
89 changes: 89 additions & 0 deletions
89
src/frontend/components/form-group/field-length/lib/component.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| import {describe, it, expect} from "@jest/globals"; | ||
| import FieldLengthComponent from "./component"; | ||
|
|
||
| describe("FieldLengthComponent", () => { | ||
| it("should error when not passed the correct type of component", () => { | ||
| const div = document.createElement("div"); | ||
| expect(() => new FieldLengthComponent(div)).toThrow("Element must be a textarea or input"); | ||
| }); | ||
|
|
||
| it("should not create a counter if the element lacks the data-max attribute", () => { | ||
| const input = document.createElement("input"); | ||
| document.body.appendChild(input); | ||
| new FieldLengthComponent(input); | ||
| const counter = input.nextElementSibling as HTMLElement; | ||
| expect(counter).toBeNull(); | ||
| }); | ||
|
|
||
| it("should create a counter with the correct values when the input is empty", () => { | ||
| const input = document.createElement("input"); | ||
| input.dataset.max = "10"; | ||
| document.body.appendChild(input); | ||
| new FieldLengthComponent(input); | ||
| const counter = input.nextElementSibling as HTMLElement; | ||
| expect(counter).not.toBeNull(); | ||
| expect(counter.textContent).toBe("0/10"); | ||
| }); | ||
|
|
||
| it("should create a counter with the correct values when the input has some text", () => { | ||
| const input = document.createElement("input"); | ||
| input.dataset.max = "10"; | ||
| input.value = "Hello"; | ||
| document.body.appendChild(input); | ||
| new FieldLengthComponent(input); | ||
| const counter = input.nextElementSibling as HTMLElement; | ||
| expect(counter).not.toBeNull(); | ||
| expect(counter.textContent).toBe("5/10"); | ||
| }); | ||
|
|
||
| it("should update the counter with the correct values when the input value changes", () => { | ||
| const input = document.createElement("input"); | ||
| input.dataset.max = "10"; | ||
| document.body.appendChild(input); | ||
| new FieldLengthComponent(input); | ||
| const counter = input.nextElementSibling as HTMLElement; | ||
| expect(counter).not.toBeNull(); | ||
| input.value = "Hello"; | ||
| input.dispatchEvent(new Event("keyup")); | ||
| expect(counter.textContent).toBe("5/10"); | ||
| }); | ||
|
|
||
| it("should add the invalid class to the counter when the input value exceeds the max length", () => { | ||
| const input = document.createElement("input"); | ||
| input.dataset.max = "10"; | ||
| document.body.appendChild(input); | ||
| new FieldLengthComponent(input); | ||
| const counter = input.nextElementSibling as HTMLElement; | ||
| expect(counter).not.toBeNull(); | ||
| input.value = "Hello World!"; | ||
| input.dispatchEvent(new Event("keyup")); | ||
| expect(counter.classList.contains("invalid")).toBe(true); | ||
| }); | ||
|
|
||
| it("should remove the invalid class from the counter when the input value is reduced to be within the max length", () => { | ||
| const input = document.createElement("input"); | ||
| input.dataset.max = "10"; | ||
| document.body.appendChild(input); | ||
| new FieldLengthComponent(input); | ||
| const counter = input.nextElementSibling as HTMLElement; | ||
| expect(counter).not.toBeNull(); | ||
| input.value = "Hello World!"; | ||
| input.dispatchEvent(new Event("keyup")); | ||
| expect(counter.classList.contains("invalid")).toBe(true); | ||
| input.value = "Hello"; | ||
| input.dispatchEvent(new Event("keyup")); | ||
| expect(counter.classList.contains("invalid")).toBe(false); | ||
| }); | ||
|
|
||
| it("Should work with textarea elements", () => { | ||
| const textarea = document.createElement("textarea"); | ||
| textarea.dataset.max = "10"; | ||
| document.body.appendChild(textarea); | ||
| new FieldLengthComponent(textarea); | ||
| const counter = textarea.nextElementSibling as HTMLElement; | ||
| expect(counter).not.toBeNull(); | ||
| textarea.value = "Hello World!"; | ||
| textarea.dispatchEvent(new Event("keyup")); | ||
| expect(counter.classList.contains("invalid")).toBe(true); | ||
| }); | ||
| }); |
41 changes: 41 additions & 0 deletions
41
src/frontend/components/form-group/field-length/lib/component.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import { Component } from "component"; | ||
|
|
||
| export default class FieldLengthComponent extends Component { | ||
| constructor(element: HTMLElement) { | ||
| super(element); | ||
| if (!(element instanceof HTMLTextAreaElement) && !(element instanceof HTMLInputElement)) { | ||
| throw new Error("Element must be a textarea or input"); | ||
| } | ||
| this.init(); | ||
| } | ||
|
|
||
| private init() { | ||
| const input = this.element as HTMLTextAreaElement | HTMLInputElement; | ||
| if (!input) return; | ||
| if(!input.dataset.max) return; | ||
| this.createCounter(input); | ||
| input.addEventListener("keyup", () => this.updateCounter(input)); | ||
| } | ||
|
|
||
| private createCounter(input: HTMLTextAreaElement | HTMLInputElement) { | ||
| const max = parseInt(input.dataset.max || "0", 10); | ||
| const counter = document.createElement("div"); | ||
| counter.classList.add("counter"); | ||
| counter.textContent = `${input.value.length}/${max}`; | ||
| input.insertAdjacentElement("afterend", counter); | ||
| } | ||
|
|
||
| private updateCounter(input: HTMLTextAreaElement | HTMLInputElement) { | ||
| const max = parseInt(input.dataset.max || "0", 10); | ||
| const counter = input.nextElementSibling as HTMLElement; | ||
| const length = input.value.length; | ||
| counter.textContent = `${length}/${max}`; | ||
| if(length>max) { | ||
| if(!counter.classList.contains("invalid")) { | ||
| counter.classList.add("invalid"); | ||
| } | ||
| } else if(counter.classList.contains("invalid")) { | ||
| counter.classList.remove("invalid"); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just for the record,
hideis not needed, but I'll merge anyway.