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

fix: moves auro library to dependencies in package.json #18 #19

Closed
wants to merge 1 commit into from
Closed
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
8 changes: 0 additions & 8 deletions .eslintrc

This file was deleted.

13 changes: 4 additions & 9 deletions dist/validation.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
export class AuroFormValidation {
/**
* Generates a camelCase version of the tag name for an element.
* @private
* @param {object} elem - HTML element with tagname to convert.
* @returns {string} Tag name in camelCase syntax.
*/
private getCamelCaseName;
export default class AuroFormValidation {
runtimeUtils: any;
/**
* Determines the validity state of the element based on the common attribute restrictions (pattern).
* @private
Expand All @@ -23,9 +17,10 @@ export class AuroFormValidation {
/**
* Determines the validity state of the element.
* @param {object} elem - HTML element to validate.
* @param {boolean} force - Boolean that forces validation to run.
* @returns {void}
*/
validate(elem: object): void;
validate(elem: object, force: boolean): void;
/**
* Gets all the HTML5 `inputs` in the element shadow DOM.
* @private
Expand Down
2 changes: 1 addition & 1 deletion dist/validation.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 21 additions & 40 deletions dist/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,13 @@

// ---------------------------------------------------------------------

/* eslint-disable complexity, max-depth, no-extra-parens, no-magic-numbers, line-comment-position, no-inline-comments */
/* eslint-disable complexity, max-depth, no-extra-parens, no-magic-numbers, line-comment-position, no-inline-comments, prefer-destructuring */

export class AuroFormValidation {
import AuroLibraryRuntimeUtils from '@aurodesignsystem/auro-library/scripts/utils/runtimeUtils.mjs';

/**
* Generates a camelCase version of the tag name for an element.
* @private
* @param {object} elem - HTML element with tagname to convert.
* @returns {string} Tag name in camelCase syntax.
*/
getCamelCaseName(elem) {
const tagName = elem.tagName.toLowerCase();
const tagNameDivider = tagName.indexOf('-');
const nameSpace = tagName.substring(0, tagNameDivider);
const name = tagName.slice(tagNameDivider + 1);
const camelCaseName = nameSpace + name.charAt(0).toUpperCase() + name.slice(1);

return camelCaseName;
export default class AuroFormValidation {
constructor() {
this.runtimeUtils = new AuroLibraryRuntimeUtils();
}

/**
Expand All @@ -37,10 +26,10 @@ export class AuroFormValidation {
elem.validity = 'badInput';
elem.setCustomValidity = elem.setCustomValidityBadInput || '';
}
} else if (elem.value.length > 0 && elem.value.length < elem.minLength) {
} else if (elem.value && elem.value.length > 0 && elem.value.length < elem.minLength) {
elem.validity = 'tooShort';
elem.setCustomValidity = elem.setCustomValidityTooShort || '';
} else if (elem.value.length > elem.maxLength) {
} else if (elem.value && elem.value.length > elem.maxLength) {
elem.validity = 'tooLong';
elem.setCustomValidity = elem.setCustomValidityTooLong || '';
}
Expand Down Expand Up @@ -82,7 +71,7 @@ export class AuroFormValidation {
elem.type === 'month-fullYear' ||
elem.type === 'year-month-day'
) {
if (elem.value.length > 0 && elem.value.length < elem.dateStrLength) {
if (elem.value && elem.value.length > 0 && elem.value.length < elem.dateStrLength) {
elem.validity = 'tooShort';
elem.setCustomValidity = elem.setCustomValidityForType || '';
} else {
Expand Down Expand Up @@ -115,14 +104,15 @@ export class AuroFormValidation {
/**
* Determines the validity state of the element.
* @param {object} elem - HTML element to validate.
* @param {boolean} force - Boolean that forces validation to run.
* @returns {void}
*/
validate(elem) {
validate(elem, force) {
Copy link

Choose a reason for hiding this comment

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

suggestion: Document the usage of the new 'force' parameter

The addition of the 'force' parameter provides more control over validation timing. Consider adding a brief comment or updating the method's JSDoc to explain when and how to use this parameter.

/**
 * Validates the input element(s).
 * @param {HTMLElement} elem - The element to validate.
 * @param {boolean} [force=false] - If true, forces validation regardless of current state.
 * @returns {void}
 */
validate(elem, force = false) {

this.getInputElements(elem);
this.getAuroInputs(elem);

// Validate only if noValidate is not true and the input does not have focus
const validationShouldRun = (!elem.contains(document.activeElement) && elem.value !== undefined) || elem.validateOnInput;
const validationShouldRun = force || (!elem.contains(document.activeElement) && elem.value !== undefined) || elem.validateOnInput;

if (elem.hasAttribute('error')) {
elem.validity = 'customError';
Expand Down Expand Up @@ -150,7 +140,7 @@ export class AuroFormValidation {
if (!hasValue && elem.required) {
elem.validity = 'valueMissing';
elem.setCustomValidity = elem.setCustomValidityValueMissing || '';
} else if (elem.tagName.toLowerCase() === 'auro-input') {
} else if (this.runtimeUtils.elementMatch(elem, 'auro-input')) {
this.validateType(elem);
this.validateAttributes(elem);
}
Expand Down Expand Up @@ -182,11 +172,12 @@ export class AuroFormValidation {

this.getErrorMessage(elem);

elem.dispatchEvent(new CustomEvent(`${this.getCamelCaseName(elem)}-validated`, {
elem.dispatchEvent(new CustomEvent('auroFormElement-validated', {
bubbles: true,
composed: true,
detail: {
validity: elem.validity
validity: elem.validity,
message: elem.errorMessage
}
}));
}
Expand All @@ -209,7 +200,7 @@ export class AuroFormValidation {
* @returns {void}
*/
getAuroInputs(elem) {
this.auroInputElements = elem.renderRoot.querySelectorAll('auro-input');
this.auroInputElements = elem.shadowRoot.querySelectorAll('auro-input, [auro-input]');
}

/**
Expand All @@ -220,23 +211,21 @@ export class AuroFormValidation {
*/
getErrorMessage(elem) {
if (elem.validity !== 'valid') {
this.getAuroInputs(elem);

if (elem.setCustomValidity) {
elem.errorMessage = elem.setCustomValidity;
} else if (elem.tagName.toLowerCase() === 'auro-input') {
} else if (this.runtimeUtils.elementMatch(elem, 'auro-input')) {
const input = elem.renderRoot.querySelector('input');

if (input.validationMessage.length > 0) {
elem.errorMessage = input.validationMessage;
}
} else if (this.auroInputElements && this.auroInputElements.length > 0) {
const firstInput = this.auroInputElements[0].renderRoot.querySelector('input');
} else if (this.inputElements && this.inputElements.length > 0) {
const firstInput = this.inputElements[0];

if (firstInput.validationMessage.length > 0) {
elem.errorMessage = firstInput.validationMessage;
} else if (this.auroInputElements.length === 2) {
const secondInput = this.auroInputElements[1].renderRoot.querySelector('input');
} else if (this.inputElements.length === 2) {
const secondInput = this.inputElements[1];

if (secondInput.validationMessage.length > 0) {
elem.errorMessage = secondInput.validationMessage;
Expand All @@ -246,13 +235,5 @@ export class AuroFormValidation {
} else {
elem.errorMessage = undefined;
}

elem.dispatchEvent(new CustomEvent(`${this.getCamelCaseName(elem)}-helpText`, {
bubbles: true,
composed: true,
detail: {
message: this.errorMessage
}
}));
}
}
13 changes: 13 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import path from "node:path";
import { fileURLToPath } from "node:url";
import js from "@eslint/js";
import { FlatCompat } from "@eslint/eslintrc";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const compat = new FlatCompat({
baseDirectory: __dirname,
recommendedConfig: js.configs.recommended,
allConfig: js.configs.all
});
export default [...compat.extends("@aurodesignsystem/eslint-config")];
Loading