-
Notifications
You must be signed in to change notification settings - Fork 227
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(feat) O3-3112: Add form collapse toggle to the
esm-form-engine-app
(…
…#1814) * feat: form collapse * refactor: rename formCollapseToggle * refactor: remove redundant add lister * refactor: use usecallback for handleFormEmbedded --------- Co-authored-by: Dennis Kigen <[email protected]>
- Loading branch information
1 parent
47c79a0
commit ef47592
Showing
5 changed files
with
68 additions
and
1 deletion.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
packages/esm-form-engine-app/src/form-collapse-toggle/form-collapse-toggle.component.tsx
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,46 @@ | ||
import React, { useCallback, useEffect, useState } from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { Toggle } from '@carbon/react'; | ||
import styles from './styles.scss'; | ||
|
||
const FormCollapseToggle = () => { | ||
const { t } = useTranslation(); | ||
const [isFormEmbedded, setIsFormEmbedded] = useState<boolean>(false); | ||
|
||
const handleFormEmbedded = useCallback((event) => { | ||
setIsFormEmbedded(event?.detail?.value || false); | ||
}, []); | ||
|
||
useEffect(() => { | ||
window.addEventListener('openmrs:form-view-embedded', handleFormEmbedded); | ||
|
||
return () => { | ||
window.removeEventListener('openmrs:form-view-embedded', handleFormEmbedded); | ||
}; | ||
}, [handleFormEmbedded]); | ||
|
||
const handleOnToggle = (value: boolean) => { | ||
const FormCollapseToggleEvent = new CustomEvent('openmrs:form-collapse-toggle', { detail: { value } }); | ||
window.dispatchEvent(FormCollapseToggleEvent); | ||
}; | ||
|
||
if (!isFormEmbedded) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<div className={styles.toggleContainer}> | ||
<Toggle | ||
size="sm" | ||
aria-label={t('toggleCollapseOrExpand', 'Toggle collapse or expand')} | ||
defaultToggled | ||
id="collapsable-toggle" | ||
labelA={t('expandAll', 'Expand all')} | ||
labelB={t('collapseAll', 'Collapse all')} | ||
onToggle={handleOnToggle} | ||
/> | ||
</div> | ||
); | ||
}; | ||
|
||
export default FormCollapseToggle; |
6 changes: 6 additions & 0 deletions
6
packages/esm-form-engine-app/src/form-collapse-toggle/styles.scss
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,6 @@ | ||
.toggleContainer { | ||
display: flex; | ||
align-items: center; | ||
height: var(--workspace-header-height); | ||
margin-right: 0.5rem; | ||
} |
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 |
---|---|---|
@@ -1,8 +1,11 @@ | ||
{ | ||
"closeThisPanel": "Close this panel", | ||
"collapseAll": "Collapse all", | ||
"errorTitle": "There was an error with this form", | ||
"expandAll": "Expand all", | ||
"loading": "Loading", | ||
"or": "or", | ||
"thisList": "this list", | ||
"toggleCollapseOrExpand": "Toggle collapse or expand", | ||
"tryAgainMessage": "Try opening another form from" | ||
} |