This repository has been archived by the owner on Jun 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: manually trigger native event on clear (#106)
* fix: manually trigger native event on clear * release
- Loading branch information
Jaeho Lee
authored
Dec 14, 2022
1 parent
295a4f8
commit 9914a6b
Showing
4 changed files
with
38 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@greenlabs/formula-components": patch | ||
--- | ||
|
||
fix: manually trigger native event on clear |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* inspired by https://stackoverflow.com/a/71340077 | ||
* triggerNativeEventFor(inputRef.current, { event: 'input', value: '' }); | ||
* triggerNativeEventFor(checkBoxRef.current, { event: 'input', checked: false }); | ||
*/ | ||
export function triggerNativeEventFor<T>( | ||
element: T, | ||
{ | ||
event, | ||
...valueObj | ||
}: { event: keyof HTMLElementEventMap; [key: string]: string } | ||
) { | ||
if (!(element instanceof Element)) { | ||
throw new Error(`Expected an Element but received ${element} instead!`) | ||
} | ||
|
||
const [prop, value] = Object.entries(valueObj)[0] ?? [] | ||
const prototype = Object.getPrototypeOf(element) | ||
const desc = Object.getOwnPropertyDescriptor(prototype, prop) | ||
|
||
desc?.set?.call(element, value) | ||
element.dispatchEvent(new Event(event as string, { bubbles: true })) | ||
} |