From dc6222b1ea140f06b3857c311e7d8d012622439e Mon Sep 17 00:00:00 2001 From: Kai Vandivier <49666798+KaiVandivier@users.noreply.github.com> Date: Fri, 15 Dec 2023 17:48:51 +0100 Subject: [PATCH] fix: avoid breaking selects (v39) [DHIS2-16264] (#1965) * fix: loading logic * fix: hide stateful selects to preserve state * chore: format * chore: update snapshots --- .../ElementSchemes/DataElementIdScheme.js | 2 +- .../ElementSchemes/OrgUnitIdScheme.js | 2 +- src/components/MoreOptions/MoreOptions.js | 18 +++++++++++++++--- .../MoreOptions/MoreOptions.module.css | 4 ++++ .../__snapshots__/MoreOptions.test.js.snap | 6 +----- .../Page/__snapshots__/Page.test.js.snap | 6 +----- 6 files changed, 23 insertions(+), 15 deletions(-) diff --git a/src/components/ElementSchemes/DataElementIdScheme.js b/src/components/ElementSchemes/DataElementIdScheme.js index c2c7e2777..51b7c13f6 100644 --- a/src/components/ElementSchemes/DataElementIdScheme.js +++ b/src/components/ElementSchemes/DataElementIdScheme.js @@ -22,7 +22,7 @@ const DataElementIdScheme = ({ fetchAttributes(`${baseUrl}/api/`, 'dataElementAttribute') .then((attributes) => setSchemes(attributes)) .catch((error) => setError(error)) - setLoading(false) + .finally(() => setLoading(false)) }, []) const validationText = diff --git a/src/components/ElementSchemes/OrgUnitIdScheme.js b/src/components/ElementSchemes/OrgUnitIdScheme.js index 1d5c373a3..47511c650 100644 --- a/src/components/ElementSchemes/OrgUnitIdScheme.js +++ b/src/components/ElementSchemes/OrgUnitIdScheme.js @@ -17,7 +17,7 @@ const OrgUnitIdScheme = ({ name, label, orgUnitIdSchemeOptions, dataTest }) => { fetchAttributes(`${baseUrl}/api/`, 'organisationUnitAttribute') .then((attributes) => setSchemes(attributes)) .catch((error) => setError(error)) - setLoading(false) + .finally(() => setLoading(false)) }, []) const validationText = diff --git a/src/components/MoreOptions/MoreOptions.js b/src/components/MoreOptions/MoreOptions.js index 9b6e3bbfe..d1ac59935 100644 --- a/src/components/MoreOptions/MoreOptions.js +++ b/src/components/MoreOptions/MoreOptions.js @@ -5,6 +5,11 @@ import PropTypes from 'prop-types' import React, { useState } from 'react' import styles from './MoreOptions.module.css' +/** + * If not initially visible, doesn't render children. After opening the first + * time, however, children are instead hidden by CSS on toggle to preserve + * state + */ const MoreOptions = ({ children, initiallyVisible, @@ -13,8 +18,12 @@ const MoreOptions = ({ dataTest = 'interaction-more-options', }) => { const [hidden, setHidden] = useState(!initiallyVisible) + const [hasOpened, setHasOpened] = useState(initiallyVisible) const onToggle = () => { + if (!hasOpened) { + setHasOpened(true) + } setHidden(!hidden) } @@ -38,9 +47,12 @@ const MoreOptions = ({