fix: type definitions for HtmxExtension (#2721)

This commit is contained in:
Johannes Przymusinski 2024-07-13 19:44:08 +02:00 committed by GitHub
parent c8418332de
commit cbb2b46de2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 6 deletions

12
src/htmx.d.ts vendored
View File

@ -15,7 +15,7 @@ declare namespace htmx {
const toggleClass: (elt: string | Element, clazz: string) => void;
const takeClass: (elt: string | Node, clazz: string) => void;
const swap: (target: string | Element, content: string, swapSpec: HtmxSwapSpecification, swapOptions?: SwapOptions) => void;
const defineExtension: (name: string, extension: any) => void;
const defineExtension: (name: string, extension: HtmxExtension) => void;
const removeExtension: (name: string) => void;
const logAll: () => void;
const logNone: () => void;
@ -192,4 +192,12 @@ type HtmxSettleInfo = {
elts: Element[];
title?: string;
};
type HtmxExtension = any;
type HtmxExtension = {
init: (api: any) => void;
onEvent: (name: string, event: Event | CustomEvent) => boolean;
transformResponse: (text: string, xhr: XMLHttpRequest, elt: Element) => string;
isInlineSwap: (swapStyle: HtmxSwapStyle) => boolean;
handleSwap: (swapStyle: HtmxSwapStyle, target: Node, fragment: Node, settleInfo: HtmxSettleInfo) => boolean | Node[];
encodeParameters: (xhr: XMLHttpRequest, parameters: FormData, elt: Node) => any | string | null;
getSelectors: () => string[] | null;
};

View File

@ -1753,7 +1753,7 @@ var htmx = (function() {
try {
const newElements = ext.handleSwap(swapStyle, target, fragment, settleInfo)
if (newElements) {
if (typeof newElements.length !== 'undefined') {
if (Array.isArray(newElements)) {
// if handleSwap returns an array (like) of elements, we handle them
for (let j = 0; j < newElements.length; j++) {
const child = newElements[j]
@ -5121,12 +5121,13 @@ var htmx = (function() {
*/
/**
* @see https://github.com/bigskysoftware/htmx-extensions/blob/main/README.md
* @typedef {Object} HtmxExtension
* @see https://htmx.org/extensions/#defining
* @property {(api: any) => void} init
* @property {(name: string, event: Event|CustomEvent) => boolean} onEvent
* @property {(text: string, xhr: XMLHttpRequest, elt: Element) => string} transformResponse
* @property {(swapStyle: HtmxSwapStyle) => boolean} isInlineSwap
* @property {(swapStyle: HtmxSwapStyle, target: Element, fragment: Node, settleInfo: HtmxSettleInfo) => boolean} handleSwap
* @property {(xhr: XMLHttpRequest, parameters: FormData, elt: Element) => *|string|null} encodeParameters
* @property {(swapStyle: HtmxSwapStyle, target: Node, fragment: Node, settleInfo: HtmxSettleInfo) => boolean|Node[]} handleSwap
* @property {(xhr: XMLHttpRequest, parameters: FormData, elt: Node) => *|string|null} encodeParameters
* @property {() => string[]|null} getSelectors
*/