@oneblink/apps-react 13.2.0-beta.2 → 13.3.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,8 +1,6 @@
1
- import * as React from 'react';
2
1
  import { FormTypes } from '@oneblink/types';
3
2
  type Props = {
4
3
  element: FormTypes.HtmlElement;
5
4
  };
6
- declare function FormElementHTML({ element }: Props): import("react/jsx-runtime").JSX.Element;
7
- declare const _default: React.MemoExoticComponent<typeof FormElementHTML>;
5
+ declare const _default: import("react").MemoExoticComponent<({ element }: Props) => import("react/jsx-runtime").JSX.Element>;
8
6
  export default _default;
@@ -1,10 +1,48 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
- import * as React from 'react';
2
+ import { memo, useEffect, useState } from 'react';
3
3
  import useReplaceableText from '../hooks/useReplaceableText';
4
4
  import QuillHTML from '../components/QuillHTML';
5
- function FormElementHTML({ element }) {
5
+ import useIsPageVisible from '../hooks/useIsPageVisible';
6
+ import useIsSectionExpanded from '../hooks/useIsSectionExpanded';
7
+ // Long enough for screen readers to pick up the alert after it is rendered,
8
+ // short enough that it is unlikely to still be active when another is shown
9
+ const SCREEN_READER_ALERT_DURATION_MS = 2000;
10
+ /**
11
+ * Only apply the alert role for a short time after the element becomes visible.
12
+ * Alert regions left in the DOM can be announced again by some screen readers
13
+ * (e.g. NVDA) when another alert is shown, which reads out every visible alert
14
+ * instead of only the new one.
15
+ *
16
+ * Elements on inactive pages and in collapsed sections stay mounted while
17
+ * hidden, so the alert is (re)activated whenever the element becomes visible
18
+ * rather than when it is mounted.
19
+ */
20
+ const useIsScreenReaderAlertActive = (screenReaderAlert) => {
21
+ const isPageVisible = useIsPageVisible();
22
+ const isSectionExpanded = useIsSectionExpanded();
23
+ const isVisibleAlert = screenReaderAlert && isPageVisible && isSectionExpanded;
24
+ const [isActive, setIsActive] = useState(isVisibleAlert);
25
+ const [previousIsVisibleAlert, setPreviousIsVisibleAlert] = useState(isVisibleAlert);
26
+ // Updating state during render (instead of in an effect) makes React discard
27
+ // this render and re-render immediately, so the alert role is committed to
28
+ // the DOM at the same time the element becomes visible
29
+ if (isVisibleAlert !== previousIsVisibleAlert) {
30
+ setPreviousIsVisibleAlert(isVisibleAlert);
31
+ setIsActive(isVisibleAlert);
32
+ }
33
+ useEffect(() => {
34
+ if (!isActive) {
35
+ return;
36
+ }
37
+ const timeoutId = setTimeout(() => setIsActive(false), SCREEN_READER_ALERT_DURATION_MS);
38
+ return () => clearTimeout(timeoutId);
39
+ }, [isActive]);
40
+ return isActive;
41
+ };
42
+ const FormElementHTML = ({ element }) => {
6
43
  const html = useReplaceableText(element.defaultValue);
7
- return (_jsx("div", { className: "cypress-html-element", children: _jsx("div", { className: "ob-form__element ob-information cypress-html-element", role: element.screenReaderAlert ? 'alert' : undefined, children: _jsx(QuillHTML, { html: html, className: "cypress-html-element-content ob-information__content" }) }) }));
8
- }
9
- export default React.memo(FormElementHTML);
44
+ const isScreenReaderAlertActive = useIsScreenReaderAlertActive(!!element.screenReaderAlert);
45
+ return (_jsx("div", { className: "cypress-html-element", children: _jsx("div", { className: "ob-form__element ob-information cypress-html-element", role: isScreenReaderAlertActive ? 'alert' : undefined, children: _jsx(QuillHTML, { html: html, className: "cypress-html-element-content ob-information__content" }) }) }));
46
+ };
47
+ export default memo(FormElementHTML);
10
48
  //# sourceMappingURL=FormElementHTML.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"FormElementHTML.js","sourceRoot":"","sources":["../../src/form-elements/FormElementHTML.tsx"],"names":[],"mappings":";AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAE9B,OAAO,kBAAkB,MAAM,6BAA6B,CAAA;AAC5D,OAAO,SAAS,MAAM,yBAAyB,CAAA;AAM/C,SAAS,eAAe,CAAC,EAAE,OAAO,EAAS;IACzC,MAAM,IAAI,GAAG,kBAAkB,CAAC,OAAO,CAAC,YAAY,CAAC,CAAA;IAErD,OAAO,CACL,cAAK,SAAS,EAAC,sBAAsB,YACnC,cACE,SAAS,EAAC,sDAAsD,EAChE,IAAI,EAAE,OAAO,CAAC,iBAAiB,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,SAAS,YAErD,KAAC,SAAS,IACR,IAAI,EAAE,IAAI,EACV,SAAS,EAAC,sDAAsD,GAChE,GACE,GACF,CACP,CAAA;AACH,CAAC;AAED,eAAe,KAAK,CAAC,IAAI,CAAC,eAAe,CAAC,CAAA","sourcesContent":["import * as React from 'react'\nimport { FormTypes } from '@oneblink/types'\nimport useReplaceableText from '../hooks/useReplaceableText'\nimport QuillHTML from '../components/QuillHTML'\n\ntype Props = {\n element: FormTypes.HtmlElement\n}\n\nfunction FormElementHTML({ element }: Props) {\n const html = useReplaceableText(element.defaultValue)\n\n return (\n <div className=\"cypress-html-element\">\n <div\n className=\"ob-form__element ob-information cypress-html-element\"\n role={element.screenReaderAlert ? 'alert' : undefined}\n >\n <QuillHTML\n html={html}\n className=\"cypress-html-element-content ob-information__content\"\n />\n </div>\n </div>\n )\n}\n\nexport default React.memo(FormElementHTML)\n"]}
1
+ {"version":3,"file":"FormElementHTML.js","sourceRoot":"","sources":["../../src/form-elements/FormElementHTML.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAA;AAEjD,OAAO,kBAAkB,MAAM,6BAA6B,CAAA;AAC5D,OAAO,SAAS,MAAM,yBAAyB,CAAA;AAC/C,OAAO,gBAAgB,MAAM,2BAA2B,CAAA;AACxD,OAAO,oBAAoB,MAAM,+BAA+B,CAAA;AAEhE,4EAA4E;AAC5E,4EAA4E;AAC5E,MAAM,+BAA+B,GAAG,IAAI,CAAA;AAE5C;;;;;;;;;GASG;AACH,MAAM,4BAA4B,GAAG,CAAC,iBAA0B,EAAE,EAAE;IAClE,MAAM,aAAa,GAAG,gBAAgB,EAAE,CAAA;IACxC,MAAM,iBAAiB,GAAG,oBAAoB,EAAE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AAA;AAED,eAAe,IAAI,CAAC,eAAe,CAAC,CAAA","sourcesContent":["import { memo, useEffect, useState } from 'react'\nimport { FormTypes } from '@oneblink/types'\nimport useReplaceableText from '../hooks/useReplaceableText'\nimport QuillHTML from '../components/QuillHTML'\nimport useIsPageVisible from '../hooks/useIsPageVisible'\nimport useIsSectionExpanded from '../hooks/useIsSectionExpanded'\n\n// Long enough for screen readers to pick up the alert after it is rendered,\n// short enough that it is unlikely to still be active when another is shown\nconst SCREEN_READER_ALERT_DURATION_MS = 2000\n\n/**\n * Only apply the alert role for a short time after the element becomes visible.\n * Alert regions left in the DOM can be announced again by some screen readers\n * (e.g. NVDA) when another alert is shown, which reads out every visible alert\n * instead of only the new one.\n *\n * Elements on inactive pages and in collapsed sections stay mounted while\n * hidden, so the alert is (re)activated whenever the element becomes visible\n * rather than when it is mounted.\n */\nconst useIsScreenReaderAlertActive = (screenReaderAlert: boolean) => {\n const isPageVisible = useIsPageVisible()\n const isSectionExpanded = useIsSectionExpanded()\n const isVisibleAlert = screenReaderAlert && isPageVisible && isSectionExpanded\n\n const [isActive, setIsActive] = useState(isVisibleAlert)\n const [previousIsVisibleAlert, setPreviousIsVisibleAlert] =\n useState(isVisibleAlert)\n // Updating state during render (instead of in an effect) makes React discard\n // this render and re-render immediately, so the alert role is committed to\n // the DOM at the same time the element becomes visible\n if (isVisibleAlert !== previousIsVisibleAlert) {\n setPreviousIsVisibleAlert(isVisibleAlert)\n setIsActive(isVisibleAlert)\n }\n\n useEffect(() => {\n if (!isActive) {\n return\n }\n const timeoutId = setTimeout(\n () => setIsActive(false),\n SCREEN_READER_ALERT_DURATION_MS,\n )\n return () => clearTimeout(timeoutId)\n }, [isActive])\n\n return isActive\n}\n\ntype Props = {\n element: FormTypes.HtmlElement\n}\n\nconst FormElementHTML = ({ element }: Props) => {\n const html = useReplaceableText(element.defaultValue)\n const isScreenReaderAlertActive = useIsScreenReaderAlertActive(\n !!element.screenReaderAlert,\n )\n\n return (\n <div className=\"cypress-html-element\">\n <div\n className=\"ob-form__element ob-information cypress-html-element\"\n role={isScreenReaderAlertActive ? 'alert' : undefined}\n >\n <QuillHTML\n html={html}\n className=\"cypress-html-element-content ob-information__content\"\n />\n </div>\n </div>\n )\n}\n\nexport default memo(FormElementHTML)\n"]}
@@ -9,6 +9,7 @@ import { checkSectionValidity } from '../services/form-validation/validators';
9
9
  import { HintBelowLabel, HintTooltip, } from '../components/renderer/FormElementLabelContainer';
10
10
  import useValidationClass from '../hooks/useValidationClass';
11
11
  import MaterialIcon from '../components/MaterialIcon';
12
+ import { IsSectionExpandedProvider } from '../hooks/useIsSectionExpanded';
12
13
  function sectionStateToBoolean(state) {
13
14
  if (state === 'COLLAPSED') {
14
15
  return true;
@@ -112,7 +113,7 @@ function FormElementSection({ element, childrenReadOnly, onLookup, displayValida
112
113
  root: 'ob-section__content',
113
114
  entered: 'ob-section__expanded',
114
115
  hidden: 'ob-section__collapsed',
115
- }, children: _jsx(SectionElementsWrapper, { element: element, onCollapse: handleClickBottomCollapseButton, children: _jsx(OneBlinkFormElements, { ...props, readOnly: childrenReadOnly, displayValidationMessages: displayValidationMessage, onLookup: handleLookup, elements: element.elements, onUpdateFormElements: handleUpdateNestedFormElements, sectionState: sectionState }) }) })] }));
116
+ }, children: _jsx(SectionElementsWrapper, { element: element, onCollapse: handleClickBottomCollapseButton, children: _jsx(IsSectionExpandedProvider, { isExpanded: !isCollapsed, children: _jsx(OneBlinkFormElements, { ...props, readOnly: childrenReadOnly, displayValidationMessages: displayValidationMessage, onLookup: handleLookup, elements: element.elements, onUpdateFormElements: handleUpdateNestedFormElements, sectionState: sectionState }) }) }) })] }));
116
117
  }
117
118
  export default React.memo(FormElementSection);
118
119
  const SectionElementsWrapper = ({ children, element, onCollapse, }) => {
@@ -1 +1 @@
1
- {"version":3,"file":"FormElementSection.js","sourceRoot":"","sources":["../../src/form-elements/FormElementSection.tsx"],"names":[],"mappings":";AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;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;AAE7C,MAAM,sBAAsB,GAAG,CAAC,EAC9B,QAAQ,EACR,OAAO,EACP,UAAU,GAKX,EAAE,EAAE;IACH,OAAO,OAAO,CAAC,qBAAqB,CAAC,CAAC,CAAC,CACrC,eAAK,SAAS,EAAC,2CAA2C,aACvD,QAAQ,EAET,kBACE,IAAI,EAAC,QAAQ,EACb,SAAS,EAAC,+DAA+D,EACzE,OAAO,EAAE,UAAU,aAEnB,eAAM,SAAS,EAAC,MAAM,YACpB,KAAC,YAAY,8BAA2B,GACnC,EACP,sCAAqB,IACd,IACL,CACP,CAAC,CAAC,CAAC,CACF,4BAAG,QAAQ,GAAI,CAChB,CAAA;AACH,CAAC,CAAA","sourcesContent":["import * as React from 'react'\nimport clsx from 'clsx'\nimport { Collapse } from '@mui/material'\nimport Tooltip from '../components/renderer/Tooltip'\nimport { FormTypes } from '@oneblink/types'\nimport useBooleanState from '../hooks/useBooleanState'\nimport OneBlinkFormElements, {\n Props,\n} from '../components/renderer/OneBlinkFormElements'\nimport { checkSectionValidity } from '../services/form-validation/validators'\nimport {\n FormElementLookupHandler,\n UpdateFormElementsHandler,\n ExecutedLookups,\n SectionState,\n} from '../types/form'\nimport {\n HintBelowLabel,\n HintTooltip,\n} from '../components/renderer/FormElementLabelContainer'\nimport useValidationClass from '../hooks/useValidationClass'\nimport MaterialIcon from '../components/MaterialIcon'\n\nfunction sectionStateToBoolean(state: 'COLLAPSED' | 'EXPANDED') {\n if (state === 'COLLAPSED') {\n return true\n }\n return false\n}\n\nfunction FormElementSection<T extends FormTypes._NestedElementsElement>({\n element,\n childrenReadOnly,\n onLookup,\n displayValidationMessages,\n onUpdateFormElements,\n sectionHeaderId,\n sectionState = [],\n ...props\n}: Omit<Props<T>, 'elements' | 'readOnly'> & {\n element: FormTypes.SectionElement\n /**\n * Lock to apply to nested fields. True when the whole form is read-only or a\n * parent is read-only. A child included in `editableFormElementIds` can stay\n * editable.\n */\n childrenReadOnly: boolean\n sectionHeaderId: string\n sectionState?: SectionState\n}) {\n const sectionStateEntry = sectionState.find(\n (section) => section.id === `${props['idPrefix']}${element.id}`,\n )\n const isCollapsedFromState = sectionStateEntry?.state\n ? sectionStateToBoolean(sectionStateEntry.state)\n : element.isCollapsed\n\n const [isCollapsed, , , toggle] = useBooleanState(isCollapsedFromState)\n\n const handleToggle = React.useCallback(() => {\n // trigger onChange to update the sectionState\n props.onChange(\n element,\n {\n executedLookups: undefined,\n sectionState,\n },\n props['idPrefix'],\n )\n toggle()\n }, [element, props, sectionState, toggle])\n\n const [isDisplayingError, setIsDisplayingError] = React.useState(isCollapsed)\n const headerRef = React.useRef<HTMLDivElement>(null)\n const id = `${props['idPrefix']}${element.id}`\n\n React.useEffect(() => {\n if (isCollapsed && !isDisplayingError) {\n setIsDisplayingError(true)\n }\n }, [isCollapsed, isDisplayingError])\n\n const displayValidationMessage =\n displayValidationMessages || isDisplayingError\n\n const isValid = React.useMemo(\n () => !checkSectionValidity(element, props.formElementsValidation),\n [element, props.formElementsValidation],\n )\n\n const { validationClassName, valid } = useValidationClass({\n formElementsValid: isValid,\n displayInvalidClassName: displayValidationMessage,\n validClassName: 'ob-section__valid',\n invalidClassName: 'ob-section__invalid',\n })\n\n const handleLookup = React.useCallback<FormElementLookupHandler>(\n (mergeLookupResults, options) => {\n onLookup((currentFormSubmission) => {\n let model = currentFormSubmission.submission\n let newExecutedLookups: ExecutedLookups = {}\n const elements = currentFormSubmission.elements.map((formElement) => {\n if (formElement.type === 'section' && formElement.id === element.id) {\n const { elements, submission, executedLookups } =\n mergeLookupResults({\n elements: formElement.elements,\n submission: currentFormSubmission.submission,\n lastElementUpdated: currentFormSubmission.lastElementUpdated,\n executedLookups: currentFormSubmission.executedLookups,\n sectionState: currentFormSubmission.sectionState,\n })\n model = submission\n newExecutedLookups = executedLookups\n return {\n ...formElement,\n elements,\n }\n }\n return formElement\n })\n\n return {\n elements,\n submission: model,\n lastElementUpdated: currentFormSubmission.lastElementUpdated,\n executedLookups: newExecutedLookups,\n sectionState: currentFormSubmission.sectionState,\n }\n }, options)\n },\n [element.id, onLookup],\n )\n\n const handleUpdateNestedFormElements =\n React.useCallback<UpdateFormElementsHandler>(\n (setter) => {\n onUpdateFormElements((formElements) => {\n return formElements.map((formElement) => {\n if (\n formElement.id === element.id &&\n formElement.type === 'section'\n ) {\n return {\n ...formElement,\n elements: setter(formElement.elements),\n }\n }\n return formElement\n })\n })\n },\n [element.id, onUpdateFormElements],\n )\n\n const handleClickBottomCollapseButton = React.useCallback(() => {\n handleToggle()\n headerRef.current?.scrollIntoView({\n behavior: 'smooth',\n block: 'center',\n inline: 'nearest',\n })\n }, [handleToggle])\n\n return (\n <div className={clsx('ob-section', validationClassName)}>\n <div\n className={clsx('ob-section__header cypress-section-header', {\n 'ob-section__header-filled': element.canCollapseFromBottom,\n })}\n onClick={handleToggle}\n tabIndex={0}\n onKeyDown={(e) => {\n if (e.key === 'Enter') {\n handleToggle()\n }\n }}\n ref={headerRef}\n >\n <h3 className=\"ob-section__header-text title is-3\" id={sectionHeaderId}>\n {element.label}\n {element.hint &&\n (element.hintPosition === 'TOOLTIP' || !element.hintPosition) && (\n <HintTooltip hint={element.hint} inputId={id} />\n )}\n </h3>\n <div className=\"ob-section__header-icon-container\">\n {!valid && displayValidationMessage && (\n <Tooltip title=\"Section has errors\">\n <MaterialIcon className=\"has-text-danger cypress-section-invalid-icon section-invalid-icon fade-in\">\n warning\n </MaterialIcon>\n </Tooltip>\n )}\n <MaterialIcon\n className={clsx('ob-section__header-icon', {\n 'is-rotated': !isCollapsed,\n })}\n >\n expand_more\n </MaterialIcon>\n </div>\n {element.hint && element.hintPosition === 'BELOW_LABEL' && (\n <div className=\"ob-section__hint-text-container\">\n <HintBelowLabel hint={element.hint} inputId={id} />\n </div>\n )}\n </div>\n {!element.canCollapseFromBottom && <hr className=\"ob-section__divider\" />}\n <Collapse\n in={!isCollapsed}\n classes={{\n root: 'ob-section__content',\n entered: 'ob-section__expanded',\n hidden: 'ob-section__collapsed',\n }}\n >\n <SectionElementsWrapper\n element={element}\n onCollapse={handleClickBottomCollapseButton}\n >\n <OneBlinkFormElements\n {...props}\n readOnly={childrenReadOnly}\n displayValidationMessages={displayValidationMessage}\n onLookup={handleLookup}\n elements={element.elements}\n onUpdateFormElements={handleUpdateNestedFormElements}\n sectionState={sectionState}\n />\n </SectionElementsWrapper>\n </Collapse>\n </div>\n )\n}\n\nexport default React.memo(FormElementSection)\n\nconst SectionElementsWrapper = ({\n children,\n element,\n onCollapse,\n}: {\n children: React.ReactNode\n element: FormTypes.SectionElement\n onCollapse: () => void\n}) => {\n return element.canCollapseFromBottom ? (\n <div className=\"ob-section__collapsible-content-container\">\n {children}\n\n <button\n type=\"button\"\n className=\"button is-rounded is-light ob-section__bottom-collapse-button\"\n onClick={onCollapse}\n >\n <span className=\"icon\">\n <MaterialIcon>expand_less</MaterialIcon>\n </span>\n <span>Collapse</span>\n </button>\n </div>\n ) : (\n <>{children}</>\n )\n}\n"]}
1
+ {"version":3,"file":"FormElementSection.js","sourceRoot":"","sources":["../../src/form-elements/FormElementSection.tsx"],"names":[],"mappings":";AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAC9B,OAAO,IAAI,MAAM,MAAM,CAAA;AACvB,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAA;AACxC,OAAO,OAAO,MAAM,gCAAgC,CAAA;AAEpD,OAAO,eAAe,MAAM,0BAA0B,CAAA;AACtD,OAAO,oBAEN,MAAM,6CAA6C,CAAA;AACpD,OAAO,EAAE,oBAAoB,EAAE,MAAM,wCAAwC,CAAA;AAO7E,OAAO,EACL,cAAc,EACd,WAAW,GACZ,MAAM,kDAAkD,CAAA;AACzD,OAAO,kBAAkB,MAAM,6BAA6B,CAAA;AAC5D,OAAO,YAAY,MAAM,4BAA4B,CAAA;AACrD,OAAO,EAAE,yBAAyB,EAAE,MAAM,+BAA+B,CAAA;AAEzE,SAAS,qBAAqB,CAAC,KAA+B;IAC5D,IAAI,KAAK,KAAK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B,CAAA;AACH,CAAC,CAAA","sourcesContent":["import * as React from 'react'\nimport clsx from 'clsx'\nimport { Collapse } from '@mui/material'\nimport Tooltip from '../components/renderer/Tooltip'\nimport { FormTypes } from '@oneblink/types'\nimport useBooleanState from '../hooks/useBooleanState'\nimport OneBlinkFormElements, {\n Props,\n} from '../components/renderer/OneBlinkFormElements'\nimport { checkSectionValidity } from '../services/form-validation/validators'\nimport {\n FormElementLookupHandler,\n UpdateFormElementsHandler,\n ExecutedLookups,\n SectionState,\n} from '../types/form'\nimport {\n HintBelowLabel,\n HintTooltip,\n} from '../components/renderer/FormElementLabelContainer'\nimport useValidationClass from '../hooks/useValidationClass'\nimport MaterialIcon from '../components/MaterialIcon'\nimport { IsSectionExpandedProvider } from '../hooks/useIsSectionExpanded'\n\nfunction sectionStateToBoolean(state: 'COLLAPSED' | 'EXPANDED') {\n if (state === 'COLLAPSED') {\n return true\n }\n return false\n}\n\nfunction FormElementSection<T extends FormTypes._NestedElementsElement>({\n element,\n childrenReadOnly,\n onLookup,\n displayValidationMessages,\n onUpdateFormElements,\n sectionHeaderId,\n sectionState = [],\n ...props\n}: Omit<Props<T>, 'elements' | 'readOnly'> & {\n element: FormTypes.SectionElement\n /**\n * Lock to apply to nested fields. True when the whole form is read-only or a\n * parent is read-only. A child included in `editableFormElementIds` can stay\n * editable.\n */\n childrenReadOnly: boolean\n sectionHeaderId: string\n sectionState?: SectionState\n}) {\n const sectionStateEntry = sectionState.find(\n (section) => section.id === `${props['idPrefix']}${element.id}`,\n )\n const isCollapsedFromState = sectionStateEntry?.state\n ? sectionStateToBoolean(sectionStateEntry.state)\n : element.isCollapsed\n\n const [isCollapsed, , , toggle] = useBooleanState(isCollapsedFromState)\n\n const handleToggle = React.useCallback(() => {\n // trigger onChange to update the sectionState\n props.onChange(\n element,\n {\n executedLookups: undefined,\n sectionState,\n },\n props['idPrefix'],\n )\n toggle()\n }, [element, props, sectionState, toggle])\n\n const [isDisplayingError, setIsDisplayingError] = React.useState(isCollapsed)\n const headerRef = React.useRef<HTMLDivElement>(null)\n const id = `${props['idPrefix']}${element.id}`\n\n React.useEffect(() => {\n if (isCollapsed && !isDisplayingError) {\n setIsDisplayingError(true)\n }\n }, [isCollapsed, isDisplayingError])\n\n const displayValidationMessage =\n displayValidationMessages || isDisplayingError\n\n const isValid = React.useMemo(\n () => !checkSectionValidity(element, props.formElementsValidation),\n [element, props.formElementsValidation],\n )\n\n const { validationClassName, valid } = useValidationClass({\n formElementsValid: isValid,\n displayInvalidClassName: displayValidationMessage,\n validClassName: 'ob-section__valid',\n invalidClassName: 'ob-section__invalid',\n })\n\n const handleLookup = React.useCallback<FormElementLookupHandler>(\n (mergeLookupResults, options) => {\n onLookup((currentFormSubmission) => {\n let model = currentFormSubmission.submission\n let newExecutedLookups: ExecutedLookups = {}\n const elements = currentFormSubmission.elements.map((formElement) => {\n if (formElement.type === 'section' && formElement.id === element.id) {\n const { elements, submission, executedLookups } =\n mergeLookupResults({\n elements: formElement.elements,\n submission: currentFormSubmission.submission,\n lastElementUpdated: currentFormSubmission.lastElementUpdated,\n executedLookups: currentFormSubmission.executedLookups,\n sectionState: currentFormSubmission.sectionState,\n })\n model = submission\n newExecutedLookups = executedLookups\n return {\n ...formElement,\n elements,\n }\n }\n return formElement\n })\n\n return {\n elements,\n submission: model,\n lastElementUpdated: currentFormSubmission.lastElementUpdated,\n executedLookups: newExecutedLookups,\n sectionState: currentFormSubmission.sectionState,\n }\n }, options)\n },\n [element.id, onLookup],\n )\n\n const handleUpdateNestedFormElements =\n React.useCallback<UpdateFormElementsHandler>(\n (setter) => {\n onUpdateFormElements((formElements) => {\n return formElements.map((formElement) => {\n if (\n formElement.id === element.id &&\n formElement.type === 'section'\n ) {\n return {\n ...formElement,\n elements: setter(formElement.elements),\n }\n }\n return formElement\n })\n })\n },\n [element.id, onUpdateFormElements],\n )\n\n const handleClickBottomCollapseButton = React.useCallback(() => {\n handleToggle()\n headerRef.current?.scrollIntoView({\n behavior: 'smooth',\n block: 'center',\n inline: 'nearest',\n })\n }, [handleToggle])\n\n return (\n <div className={clsx('ob-section', validationClassName)}>\n <div\n className={clsx('ob-section__header cypress-section-header', {\n 'ob-section__header-filled': element.canCollapseFromBottom,\n })}\n onClick={handleToggle}\n tabIndex={0}\n onKeyDown={(e) => {\n if (e.key === 'Enter') {\n handleToggle()\n }\n }}\n ref={headerRef}\n >\n <h3 className=\"ob-section__header-text title is-3\" id={sectionHeaderId}>\n {element.label}\n {element.hint &&\n (element.hintPosition === 'TOOLTIP' || !element.hintPosition) && (\n <HintTooltip hint={element.hint} inputId={id} />\n )}\n </h3>\n <div className=\"ob-section__header-icon-container\">\n {!valid && displayValidationMessage && (\n <Tooltip title=\"Section has errors\">\n <MaterialIcon className=\"has-text-danger cypress-section-invalid-icon section-invalid-icon fade-in\">\n warning\n </MaterialIcon>\n </Tooltip>\n )}\n <MaterialIcon\n className={clsx('ob-section__header-icon', {\n 'is-rotated': !isCollapsed,\n })}\n >\n expand_more\n </MaterialIcon>\n </div>\n {element.hint && element.hintPosition === 'BELOW_LABEL' && (\n <div className=\"ob-section__hint-text-container\">\n <HintBelowLabel hint={element.hint} inputId={id} />\n </div>\n )}\n </div>\n {!element.canCollapseFromBottom && <hr className=\"ob-section__divider\" />}\n <Collapse\n in={!isCollapsed}\n classes={{\n root: 'ob-section__content',\n entered: 'ob-section__expanded',\n hidden: 'ob-section__collapsed',\n }}\n >\n <SectionElementsWrapper\n element={element}\n onCollapse={handleClickBottomCollapseButton}\n >\n <IsSectionExpandedProvider isExpanded={!isCollapsed}>\n <OneBlinkFormElements\n {...props}\n readOnly={childrenReadOnly}\n displayValidationMessages={displayValidationMessage}\n onLookup={handleLookup}\n elements={element.elements}\n onUpdateFormElements={handleUpdateNestedFormElements}\n sectionState={sectionState}\n />\n </IsSectionExpandedProvider>\n </SectionElementsWrapper>\n </Collapse>\n </div>\n )\n}\n\nexport default React.memo(FormElementSection)\n\nconst SectionElementsWrapper = ({\n children,\n element,\n onCollapse,\n}: {\n children: React.ReactNode\n element: FormTypes.SectionElement\n onCollapse: () => void\n}) => {\n return element.canCollapseFromBottom ? (\n <div className=\"ob-section__collapsible-content-container\">\n {children}\n\n <button\n type=\"button\"\n className=\"button is-rounded is-light ob-section__bottom-collapse-button\"\n onClick={onCollapse}\n >\n <span className=\"icon\">\n <MaterialIcon>expand_less</MaterialIcon>\n </span>\n <span>Collapse</span>\n </button>\n </div>\n ) : (\n <>{children}</>\n )\n}\n"]}
@@ -0,0 +1,14 @@
1
+ import { ReactNode } from 'react';
2
+ type IsSectionExpandedContextValue = boolean;
3
+ type Props = {
4
+ isExpanded: IsSectionExpandedContextValue;
5
+ children: ReactNode;
6
+ };
7
+ /**
8
+ * Collapsed sections keep their elements mounted but hidden, so this allows
9
+ * elements to know if they are visible. A section is only considered expanded
10
+ * if all of its ancestor sections are also expanded.
11
+ */
12
+ export declare const IsSectionExpandedProvider: ({ isExpanded, children }: Props) => import("react/jsx-runtime").JSX.Element;
13
+ declare const useIsSectionExpanded: () => boolean;
14
+ export default useIsSectionExpanded;
@@ -0,0 +1,18 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { createContext, useContext } from 'react';
3
+ // Elements that are not in a section are always expanded
4
+ const IsSectionExpandedContext = createContext(true);
5
+ /**
6
+ * Collapsed sections keep their elements mounted but hidden, so this allows
7
+ * elements to know if they are visible. A section is only considered expanded
8
+ * if all of its ancestor sections are also expanded.
9
+ */
10
+ export const IsSectionExpandedProvider = ({ isExpanded, children }) => {
11
+ const isParentSectionExpanded = useContext(IsSectionExpandedContext);
12
+ return (_jsx(IsSectionExpandedContext.Provider, { value: isParentSectionExpanded && isExpanded, children: children }));
13
+ };
14
+ const useIsSectionExpanded = () => {
15
+ return useContext(IsSectionExpandedContext);
16
+ };
17
+ export default useIsSectionExpanded;
18
+ //# sourceMappingURL=useIsSectionExpanded.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useIsSectionExpanded.js","sourceRoot":"","sources":["../../src/hooks/useIsSectionExpanded.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAE,aAAa,EAAa,UAAU,EAAE,MAAM,OAAO,CAAA;AAS5D,yDAAyD;AACzD,MAAM,wBAAwB,GAC5B,aAAa,CAAgC,IAAI,CAAC,CAAA;AAEpD;;;;GAIG;AACH,MAAM,CAAC,MAAM,yBAAyB,GAAG,CAAC,EAAE,UAAU,EAAE,QAAQ,EAAS,EAAE,EAAE;IAC3E,MAAM,uBAAuB,GAAG,UAAU,CAAC,wBAAwB,CAAC,CAAA;IACpE,OAAO,CACL,KAAC,wBAAwB,CAAC,QAAQ,IAChC,KAAK,EAAE,uBAAuB,IAAI,UAAU,YAE3C,QAAQ,GACyB,CACrC,CAAA;AACH,CAAC,CAAA;AAED,MAAM,oBAAoB,GAAG,GAAG,EAAE;IAChC,OAAO,UAAU,CAAC,wBAAwB,CAAC,CAAA;AAC7C,CAAC,CAAA;AAED,eAAe,oBAAoB,CAAA","sourcesContent":["import { createContext, ReactNode, useContext } from 'react'\n\ntype IsSectionExpandedContextValue = boolean\n\ntype Props = {\n isExpanded: IsSectionExpandedContextValue\n children: ReactNode\n}\n\n// Elements that are not in a section are always expanded\nconst IsSectionExpandedContext =\n createContext<IsSectionExpandedContextValue>(true)\n\n/**\n * Collapsed sections keep their elements mounted but hidden, so this allows\n * elements to know if they are visible. A section is only considered expanded\n * if all of its ancestor sections are also expanded.\n */\nexport const IsSectionExpandedProvider = ({ isExpanded, children }: Props) => {\n const isParentSectionExpanded = useContext(IsSectionExpandedContext)\n return (\n <IsSectionExpandedContext.Provider\n value={isParentSectionExpanded && isExpanded}\n >\n {children}\n </IsSectionExpandedContext.Provider>\n )\n}\n\nconst useIsSectionExpanded = () => {\n return useContext(IsSectionExpandedContext)\n}\n\nexport default useIsSectionExpanded\n"]}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@oneblink/apps-react",
3
3
  "description": "Helper functions for OneBlink apps in ReactJS.",
4
- "version": "13.2.0-beta.2",
4
+ "version": "13.3.0-beta.1",
5
5
  "author": "OneBlink <developers@oneblink.io> (https://oneblink.io)",
6
6
  "bugs": {
7
7
  "url": "https://github.com/oneblink/apps-react/issues"