-
Notifications
You must be signed in to change notification settings - Fork 134
Feat/2595 input validation checkbox #2550
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
Draft
khathija-ahamadi
wants to merge
3
commits into
main
Choose a base branch
from
feat/2595-input-validation-checkbox
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
75 changes: 75 additions & 0 deletions
75
packages/core/src/components/checkbox/checkbox-validation.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,75 @@ | ||
| ๏ปฟ/* | ||
| * SPDX-FileCopyrightText: 2024 Siemens AG | ||
| * | ||
| * SPDX-License-Identifier: MIT | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| interface CheckboxElement extends HTMLElement { | ||
| required: boolean; | ||
| checked: boolean; | ||
| } | ||
|
|
||
| export function isFormNoValidate(element: HTMLElement): boolean { | ||
| const form = element.closest('form'); | ||
| if (!form) return false; | ||
| return ( | ||
| form.hasAttribute('novalidate') || | ||
| form.dataset.novalidate !== undefined || | ||
| form.hasAttribute('ngnovalidate') | ||
| ); | ||
| } | ||
|
|
||
| export function setupFormSubmitListener( | ||
| element: HTMLElement, | ||
| callback: () => void | ||
| ): (() => void) | undefined { | ||
| const form = element.closest('form'); | ||
| if (!form) return undefined; | ||
|
|
||
| const handler = () => callback(); | ||
| form.addEventListener('submit', handler); | ||
|
|
||
| return () => { | ||
| form.removeEventListener('submit', handler); | ||
| }; | ||
| } | ||
|
|
||
| export function applyCheckboxValidation( | ||
| checkboxes: NodeListOf<CheckboxElement> | CheckboxElement[], | ||
| group: Element | null, | ||
| touched: boolean, | ||
| formSubmissionAttempted: boolean | ||
| ) { | ||
| const checkboxArr = Array.from(checkboxes); | ||
| const requiredCheckboxes = checkboxArr.filter((el) => el.required); | ||
| const targets = | ||
| requiredCheckboxes.length > 0 ? requiredCheckboxes : checkboxArr; | ||
|
|
||
| const anyChecked = targets.some((el) => el.checked); | ||
| const shouldShowError = !anyChecked && (touched || formSubmissionAttempted); | ||
|
|
||
| checkboxArr.forEach((el) => { | ||
| const isTarget = targets.includes(el); | ||
| el.classList.toggle('ix-invalid--required', isTarget && shouldShowError); | ||
| el.classList.toggle('ix-invalid', isTarget && shouldShowError); | ||
| }); | ||
|
|
||
| if (group) { | ||
| group.classList.toggle('ix-invalid--required', shouldShowError); | ||
| group.classList.toggle('ix-invalid', shouldShowError); | ||
| } | ||
| } | ||
|
|
||
| export function clearCheckboxGroupValidationState( | ||
| group: HTMLElement, | ||
| checkboxes: CheckboxElement[] | NodeListOf<CheckboxElement> | ||
| ) { | ||
| const checkboxArr = Array.from(checkboxes); | ||
| group.classList.remove('ix-invalid--required', 'ix-invalid'); | ||
| checkboxArr.forEach((el) => { | ||
| el.classList.remove('ix-invalid', 'ix-invalid--required'); | ||
| }); | ||
| } |
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 |
|---|---|---|
|
|
@@ -23,14 +23,19 @@ import { | |
| import { a11yBoolean } from '../utils/a11y'; | ||
| import { HookValidationLifecycle, IxFormComponent } from '../utils/input'; | ||
| import { makeRef } from '../utils/make-ref'; | ||
| import { | ||
| isFormNoValidate, | ||
| applyCheckboxValidation, | ||
| setupFormSubmitListener, | ||
| } from './checkbox-validation'; | ||
|
|
||
| /** | ||
| * @form-ready | ||
| */ | ||
| @Component({ | ||
| tag: 'ix-checkbox', | ||
| styleUrl: 'checkbox.scss', | ||
| shadow: true, | ||
| shadow: { delegatesFocus: true }, | ||
| formAssociated: true, | ||
| }) | ||
| export class Checkbox implements IxFormComponent<string> { | ||
|
|
@@ -91,6 +96,8 @@ export class Checkbox implements IxFormComponent<string> { | |
| @Event() ixBlur!: EventEmitter<void>; | ||
|
|
||
| private touched = false; | ||
| private formSubmissionAttempted = false; | ||
| private cleanupFormListener?: () => void; | ||
|
|
||
| private readonly inputRef = makeRef<HTMLInputElement>((checkboxRef) => { | ||
| checkboxRef.checked = this.checked; | ||
|
|
@@ -105,11 +112,61 @@ export class Checkbox implements IxFormComponent<string> { | |
| onCheckedChange() { | ||
| this.touched = true; | ||
| this.updateFormInternalValue(); | ||
| this.syncValidationClasses(); | ||
| } | ||
|
|
||
| private syncValidationClasses() { | ||
| if (isFormNoValidate(this.hostElement)) { | ||
| this.hostElement.classList.remove('ix-invalid--required', 'ix-invalid'); | ||
| return; | ||
| } | ||
|
|
||
| const checkboxGroup = this.hostElement.closest('ix-checkbox-group'); | ||
| let checkboxes: HTMLIxCheckboxElement[]; | ||
| let group: Element | null; | ||
|
|
||
| if (checkboxGroup) { | ||
| checkboxes = Array.from( | ||
| checkboxGroup.querySelectorAll<HTMLIxCheckboxElement>('ix-checkbox') | ||
| ); | ||
| group = checkboxGroup; | ||
| } else { | ||
| checkboxes = [this.hostElement]; | ||
| group = null; | ||
| } | ||
|
|
||
| const hasRequired = checkboxes.some((c) => c.required); | ||
| if (!hasRequired) { | ||
| this.hostElement.classList.remove('ix-invalid--required', 'ix-invalid'); | ||
| return; | ||
| } | ||
|
|
||
| applyCheckboxValidation( | ||
| checkboxes, | ||
| group, | ||
| this.touched, | ||
| this.formSubmissionAttempted | ||
| ); | ||
| } | ||
|
|
||
| @Watch('value') | ||
| onValueChange() { | ||
| this.valueChange.emit(this.value); | ||
| this.syncValidationClasses(); | ||
| } | ||
|
|
||
| connectedCallback(): void { | ||
| this.syncValidationClasses(); | ||
| this.cleanupFormListener = setupFormSubmitListener(this.hostElement, () => { | ||
| this.formSubmissionAttempted = true; | ||
| this.syncValidationClasses(); | ||
| }); | ||
| } | ||
|
|
||
| disconnectedCallback(): void { | ||
| if (this.cleanupFormListener) { | ||
| this.cleanupFormListener(); | ||
| } | ||
| } | ||
|
|
||
| componentWillLoad() { | ||
|
|
@@ -147,6 +204,18 @@ export class Checkbox implements IxFormComponent<string> { | |
| /** This function is intentionally empty */ | ||
| } | ||
|
|
||
| /** | ||
| * Resets the checkbox to its initial unchecked state and clears validation. | ||
| */ | ||
|
Comment on lines
+207
to
+209
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The JSDoc for the new clear method is missing the @SInCE tag required by the repository style guide.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| @Method() | ||
| async clear(): Promise<void> { | ||
| this.checked = false; | ||
| this.touched = false; | ||
| this.formSubmissionAttempted = false; | ||
| this.updateFormInternalValue(); | ||
| this.syncValidationClasses(); | ||
| } | ||
|
|
||
| private renderCheckmark() { | ||
| return ( | ||
| <svg | ||
|
|
@@ -192,7 +261,11 @@ export class Checkbox implements IxFormComponent<string> { | |
| indeterminate: this.indeterminate, | ||
| }} | ||
| onFocus={() => (this.touched = true)} | ||
| onBlur={() => this.ixBlur.emit()} | ||
| onBlur={() => { | ||
| this.ixBlur.emit(); | ||
| this.touched = true; | ||
| this.syncValidationClasses(); | ||
| }} | ||
| > | ||
| <label> | ||
| <input | ||
|
|
||
Oops, something went wrong.
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.
The new clear method is missing JSDoc documentation and the @SInCE tag required by the repository style guide.
References
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.
Reviewer please confirm if we need to add '@SInCE version' for @method() ?