@zag-js/form-utils
Advanced tools
Comparing version 0.2.0 to 0.2.1
declare type DescriptorOptions = { | ||
type: "HTMLInputElement" | "HTMLTextAreaElement" | "HTMLSelectElement"; | ||
property: "value" | "checked"; | ||
property?: "value" | "checked"; | ||
}; | ||
declare function setElementValue(el: HTMLElement, value: string, option: DescriptorOptions): void; | ||
declare function dispatchInputValueEvent(el: HTMLElement | null, value: string | number): void; | ||
declare function dispatchInputCheckedEvent(el: HTMLElement | null, checked: boolean): void; | ||
declare function trackInputPropertyMutation(el: HTMLInputElement | null, options: DescriptorOptions & { | ||
fn?: (value: string) => void; | ||
}): (() => void) | undefined; | ||
@@ -15,3 +13,8 @@ declare function getClosestForm(el: HTMLElement): HTMLFormElement | null; | ||
declare function isNativeDisabled(el: HTMLElement): boolean; | ||
declare type FormControlOptions = { | ||
onFieldsetDisabled: () => void; | ||
onFormReset: () => void; | ||
}; | ||
declare function trackFormControl(el: HTMLElement | null, options: FormControlOptions): (() => void) | undefined; | ||
export { dispatchInputCheckedEvent, dispatchInputValueEvent, getClosestForm, isNativeDisabled, trackFieldsetDisabled, trackFormReset, trackInputPropertyMutation }; | ||
export { FormControlOptions, dispatchInputCheckedEvent, dispatchInputValueEvent, getClosestForm, isNativeDisabled, setElementValue, trackFieldsetDisabled, trackFormControl, trackFormReset }; |
@@ -27,5 +27,6 @@ "use strict"; | ||
isNativeDisabled: () => isNativeDisabled, | ||
setElementValue: () => setElementValue, | ||
trackFieldsetDisabled: () => trackFieldsetDisabled, | ||
trackFormReset: () => trackFormReset, | ||
trackInputPropertyMutation: () => trackInputPropertyMutation | ||
trackFormControl: () => trackFormControl, | ||
trackFormReset: () => trackFormReset | ||
}); | ||
@@ -58,6 +59,11 @@ module.exports = __toCommonJS(src_exports); | ||
var _a; | ||
const { type, property } = options; | ||
const { type, property = "value" } = options; | ||
const proto = getWindow(el)[type].prototype; | ||
return (_a = Object.getOwnPropertyDescriptor(proto, property)) != null ? _a : {}; | ||
} | ||
function setElementValue(el, value, option) { | ||
var _a; | ||
const descriptor = getDescriptor(el, option); | ||
(_a = descriptor.set) == null ? void 0 : _a.call(el, value); | ||
} | ||
function dispatchInputValueEvent(el, value) { | ||
@@ -87,22 +93,2 @@ var _a; | ||
} | ||
function trackInputPropertyMutation(el, options) { | ||
const { fn, property, type } = options; | ||
if (!fn || !el) | ||
return; | ||
const { get, set } = getDescriptor(el, { property, type }); | ||
let run = true; | ||
Object.defineProperty(el, property, { | ||
get() { | ||
return get == null ? void 0 : get.call(this); | ||
}, | ||
set(value) { | ||
if (run) | ||
fn(value); | ||
return set == null ? void 0 : set.call(this, value); | ||
} | ||
}); | ||
return () => { | ||
run = false; | ||
}; | ||
} | ||
@@ -138,2 +124,17 @@ // src/form.ts | ||
} | ||
function trackFormControl(el, options) { | ||
if (!el) | ||
return; | ||
const { onFieldsetDisabled, onFormReset } = options; | ||
const cleanups = [ | ||
trackFormReset(el, onFormReset), | ||
trackFieldsetDisabled(el, (disabled) => { | ||
if (disabled) | ||
onFieldsetDisabled(); | ||
}) | ||
]; | ||
return () => { | ||
cleanups.forEach((cleanup) => cleanup == null ? void 0 : cleanup()); | ||
}; | ||
} | ||
// Annotate the CommonJS export names for ESM import in node: | ||
@@ -145,5 +146,6 @@ 0 && (module.exports = { | ||
isNativeDisabled, | ||
setElementValue, | ||
trackFieldsetDisabled, | ||
trackFormReset, | ||
trackInputPropertyMutation | ||
trackFormControl, | ||
trackFormReset | ||
}); |
{ | ||
"name": "@zag-js/form-utils", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "", | ||
@@ -22,3 +22,3 @@ "main": "dist/index.js", | ||
"devDependencies": { | ||
"@zag-js/dom-utils": "0.2.0" | ||
"@zag-js/dom-utils": "0.2.1" | ||
}, | ||
@@ -25,0 +25,0 @@ "publishConfig": { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11762
269