Skip to content

Commit

Permalink
Created new typeguards/instances utils
Browse files Browse the repository at this point in the history
  • Loading branch information
alexiglesias93 committed Oct 28, 2022
1 parent 0024923 commit a57b300
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/long-seahorses-argue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@finsweet/ts-utils': minor
---

Created new typeguards/instances utils
3 changes: 2 additions & 1 deletion src/helpers/clearFormField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { FORM_CSS_CLASSES } from '../webflow/css';
import { simulateEvent } from './simulateEvent';

import type { FormField } from '../types';
import { isHTMLInputElement } from '../type-guards';

const {
radioInput: radioInputCSSClass,
Expand All @@ -19,7 +20,7 @@ const {
export const clearFormField = (field: FormField, omitEvents: Parameters<typeof simulateEvent>['1'] = []): void => {
const { type } = field;

if (field instanceof HTMLInputElement && ['checkbox', 'radio'].includes(type)) {
if (isHTMLInputElement(field) && ['checkbox', 'radio'].includes(type)) {
if (!field.checked) return;

// Reset the field's value
Expand Down
4 changes: 3 additions & 1 deletion src/helpers/findTextNode.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { isHTMLElement } from '../type-guards';

/**
* Finds the first child text node of an element
* @param element The element to search into.
Expand All @@ -6,7 +8,7 @@ export const findTextNode = (element: HTMLElement): ChildNode | undefined => {
let textNode: ChildNode | undefined;

for (const node of element.childNodes) {
if (node instanceof HTMLElement && node.childNodes.length) textNode = findTextNode(node);
if (isHTMLElement(node) && node.childNodes.length) textNode = findTextNode(node);
else if (node.nodeType === Node.TEXT_NODE && node.textContent?.trim()) textNode = node;

if (textNode) break;
Expand Down
3 changes: 2 additions & 1 deletion src/helpers/getFormFieldValue.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isHTMLInputElement } from '../type-guards';
import type { FormField } from '../types/FormField';

/**
Expand All @@ -14,7 +15,7 @@ export const getFormFieldValue = (input: FormField): string => {
const checkedOption = input.closest('form')?.querySelector(`input[name="${input.name}"]:checked`);

// If exists, set its value
value = checkedOption instanceof HTMLInputElement ? checkedOption.value : '';
value = isHTMLInputElement(checkedOption) ? checkedOption.value : '';
}

return value.toString();
Expand Down
3 changes: 2 additions & 1 deletion src/helpers/setFormFieldValue.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { simulateEvent } from '.';

import type { FormField } from '..';
import { isHTMLInputElement } from '..';

/**
* Sets a value to a FormField element and emits `click`, `input` and `change` Events.
Expand All @@ -16,7 +17,7 @@ export const setFormFieldValue = (element: FormField, value: string | boolean):

if (isRadio || isCheckbox) {
if (
!(element instanceof HTMLInputElement) ||
!isHTMLInputElement(element) ||
typeof value !== 'boolean' ||
value === element.checked ||
(isRadio && value === false)
Expand Down
1 change: 1 addition & 0 deletions src/type-guards/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './instances';
export { isFormField } from './isFormField';
export { isKeyOf } from './isKeyOf';
export { isNotEmpty } from './isNotEmpty';
Expand Down
13 changes: 13 additions & 0 deletions src/type-guards/instances.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export const isNode = (target: unknown): target is Node => target instanceof Node;

export const isElement = (target: unknown): target is Element => target instanceof Element;

export const isHTMLElement = (target: unknown): target is HTMLElement => target instanceof HTMLElement;

export const isHTMLInputElement = (target: unknown): target is HTMLInputElement => target instanceof HTMLInputElement;

export const isHTMLSelectElement = (target: unknown): target is HTMLSelectElement =>
target instanceof HTMLSelectElement;

export const isHTMLTextAreaElement = (target: unknown): target is HTMLTextAreaElement =>
target instanceof HTMLTextAreaElement;
3 changes: 2 additions & 1 deletion src/type-guards/isFormField.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { FormField } from '../types/FormField';
import { isHTMLInputElement, isHTMLSelectElement, isHTMLTextAreaElement } from './instances';

/**
* Checks if an element is a form field element
* @param element
*/
// prettier-ignore
export const isFormField = (element: Element | EventTarget | null): element is FormField => element instanceof HTMLInputElement || element instanceof HTMLSelectElement || element instanceof HTMLTextAreaElement;
export const isFormField = (element: Element | EventTarget | null): element is FormField => isHTMLInputElement(element) || isHTMLSelectElement(element) || isHTMLTextAreaElement(element);

0 comments on commit a57b300

Please sign in to comment.