From 0e3b76012cc329e480a6eca64bcc84205f330ee1 Mon Sep 17 00:00:00 2001 From: Alex Iglesias Date: Mon, 4 Oct 2021 17:37:05 +0200 Subject: [PATCH] Refactored `getCollectionElements` with `WebflowElements` types --- package.json | 2 +- webflow/getCollectionElements.ts | 38 +++++++++++++++++++++++--------- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index badfdbc..a0e0b19 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@finsweet/ts-utils", - "version": "0.15.0", + "version": "0.15.1", "description": "Typescript utils for custom Webflow projects.", "main": "index.ts", "module": "index.ts", diff --git a/webflow/getCollectionElements.ts b/webflow/getCollectionElements.ts index ae19079..102d785 100644 --- a/webflow/getCollectionElements.ts +++ b/webflow/getCollectionElements.ts @@ -1,5 +1,12 @@ import { CMS_CSS_CLASSES } from '.'; +import type { + CollectionItemElement, + CollectionListElement, + CollectionListWrapperElement, + PaginationButtonElement, +} from '..'; + const { wrapper, list, item, paginationNext, paginationPrevious } = CMS_CSS_CLASSES; /** @@ -10,33 +17,44 @@ const { wrapper, list, item, paginationNext, paginationPrevious } = CMS_CSS_CLAS * @param page The page document. * @returns The specified collection element/elements. */ -export function getCollectionElements(reference: string | Element, target: 'items', page?: Document): HTMLDivElement[]; +export function getCollectionElements( + reference: string | Element, + target: 'items', + page?: Document +): CollectionItemElement[]; export function getCollectionElements( reference: string | Element, target: 'next' | 'previous', page?: Document -): HTMLAnchorElement | null | undefined; +): PaginationButtonElement | null | undefined; export function getCollectionElements( reference: string | Element, target: 'wrapper' | 'list', page?: Document -): HTMLDivElement | null | undefined; +): CollectionListWrapperElement | CollectionListElement | null | undefined; export function getCollectionElements( reference: string | Element, target: 'wrapper' | 'list' | 'items' | 'next' | 'previous', page: Document = document -): HTMLDivElement | HTMLDivElement[] | HTMLAnchorElement | null | undefined { - const referenceElement = typeof reference === 'string' ? page.querySelector(reference) : reference; +): + | CollectionListWrapperElement + | CollectionListElement + | CollectionItemElement[] + | PaginationButtonElement + | null + | undefined { + const referenceElement = typeof reference === 'string' ? page.querySelector(reference) : reference; if (!referenceElement) return; - if (target === 'wrapper') return referenceElement.closest(`.${wrapper}`); + if (target === 'wrapper') return referenceElement.closest(`.${wrapper}`); - if (target === 'items') return [...referenceElement.querySelectorAll(`.${item}`)]; + if (target === 'items') return [...referenceElement.querySelectorAll(`.${item}`)]; - if (target === 'next') return referenceElement.querySelector(`.${paginationNext}`); - if (target === 'previous') return referenceElement.querySelector(`.${paginationPrevious}`); + if (target === 'next') return referenceElement.querySelector(`.${paginationNext}`); + if (target === 'previous') return referenceElement.querySelector(`.${paginationPrevious}`); return ( - referenceElement.querySelector(`.${list}`) || referenceElement.closest(`.${list}`) + referenceElement.querySelector(`.${list}`) || + referenceElement.closest(`.${list}`) ); }