Skip to content

Commit

Permalink
(feat) O3-3112: Add form collapse toggle to the esm-form-engine-app (
Browse files Browse the repository at this point in the history
…#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
usamaidrsk and denniskigen authored Apr 29, 2024
1 parent 47c79a0 commit ef47592
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 1 deletion.
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;
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;
}
9 changes: 8 additions & 1 deletion packages/esm-form-engine-app/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { defineConfigSchema, getAsyncLifecycle } from '@openmrs/esm-framework';
import { registerWorkspace } from '@openmrs/esm-patient-common-lib';
import { configSchema } from './config-schema';

const moduleName = '@openmrs/esm-form-engine-app';
Expand All @@ -16,3 +15,11 @@ export function startupApp() {
}

export const formRenderer = getAsyncLifecycle(() => import('./form-renderer/form-renderer.component'), options);

export const formCollapseToggle = getAsyncLifecycle(
() => import('./form-collapse-toggle/form-collapse-toggle.component'),
{
featureName: 'rfe-form-collapse-toggle',
moduleName,
},
);
5 changes: 5 additions & 0 deletions packages/esm-form-engine-app/src/routes.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
"name": "react-form-engine-widget",
"component": "formRenderer",
"slot": "form-widget-slot"
},
{
"name": "rfe-form-collapse-toggle",
"component": "formCollapseToggle",
"slot": "patient-chart-workspace-header-slot"
}
],
"pages": []
Expand Down
3 changes: 3 additions & 0 deletions packages/esm-form-engine-app/translations/en.json
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"
}

0 comments on commit ef47592

Please sign in to comment.