forked from ohcnetwork/care_fe
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'ohcnetwork:develop' into Issues/ohcnetwork#9055/RemoveC…
…onsultationButton
- Loading branch information
Showing
28 changed files
with
511 additions
and
1,422 deletions.
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
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
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,55 @@ | ||
import { useEffect, useState } from "react"; | ||
|
||
/** | ||
* A custom React hook that observes changes to a specific attribute of a DOM element | ||
* and returns a new value when the attribute changes. It is primarily useful | ||
* for detecting updates to the value of a custom CUI component through layers where | ||
* the component's state cannot be determined or mutated (example. CARE Scribe). | ||
* | ||
* @template T | ||
* @param {Object} options - Configuration options for the observer. | ||
* @param {HTMLElement | null} options.targetElement - The DOM element to observe for attribute changes. | ||
* @param {string} [options.attribute="value"] - The name of the attribute to observe (default is "value"). | ||
* | ||
* @example | ||
* const targetElement = document.getElementById('my-input'); | ||
* const DOMValue = useValueInjectionObserver({ | ||
* targetElement: targetElement, | ||
* attribute: 'value', | ||
* }); | ||
* | ||
* @returns {unknown} This hook returns the current value of the attribute. | ||
*/ | ||
export function useValueInjectionObserver<T = unknown>(options: { | ||
targetElement: HTMLElement | null; | ||
attribute?: string; | ||
}) { | ||
const { targetElement, attribute = "value" } = options; | ||
const [value, setValue] = useState<T>(); | ||
|
||
useEffect(() => { | ||
const observer = new MutationObserver((mutationsList) => { | ||
mutationsList.forEach((mutation) => { | ||
if ( | ||
mutation.type === "attributes" && | ||
mutation.attributeName === attribute | ||
) { | ||
const newValue = JSON.parse( | ||
targetElement?.getAttribute(attribute) || '""', | ||
); | ||
setValue(newValue); | ||
} | ||
}); | ||
}); | ||
|
||
const config = { | ||
attributes: true, | ||
attributeFilter: [attribute], | ||
}; | ||
|
||
targetElement && observer.observe(targetElement, config); | ||
return () => observer.disconnect(); | ||
}, [targetElement]); | ||
|
||
return value; | ||
} |
Oops, something went wrong.