Skip to content

Commit

Permalink
fix: avoid breaking selects (v39) [DHIS2-16264] (#1965)
Browse files Browse the repository at this point in the history
* fix: loading logic

* fix: hide stateful selects to preserve state

* chore: format

* chore: update snapshots
  • Loading branch information
KaiVandivier authored Dec 15, 2023
1 parent 9fc9128 commit dc6222b
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/components/ElementSchemes/DataElementIdScheme.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const DataElementIdScheme = ({
fetchAttributes(`${baseUrl}/api/`, 'dataElementAttribute')
.then((attributes) => setSchemes(attributes))
.catch((error) => setError(error))
setLoading(false)
.finally(() => setLoading(false))
}, [])

Check warning on line 26 in src/components/ElementSchemes/DataElementIdScheme.js

View workflow job for this annotation

GitHub Actions / lint

React Hook useEffect has a missing dependency: 'baseUrl'. Either include it or remove the dependency array

const validationText =
Expand Down
2 changes: 1 addition & 1 deletion src/components/ElementSchemes/OrgUnitIdScheme.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}, [])

Check warning on line 21 in src/components/ElementSchemes/OrgUnitIdScheme.js

View workflow job for this annotation

GitHub Actions / lint

React Hook useEffect has a missing dependency: 'baseUrl'. Either include it or remove the dependency array

const validationText =
Expand Down
18 changes: 15 additions & 3 deletions src/components/MoreOptions/MoreOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
}

Expand All @@ -38,9 +47,12 @@ const MoreOptions = ({
</div>
<h2 className={styles.label}>{label}</h2>
</header>
<Divider />
<div className={styles.children} data-test={`${dataTest}-children`}>
{!hidden && children}
{!hidden && <Divider />}
<div
className={cx(styles.children, { [styles.hidden]: hidden })}
data-test={`${dataTest}-children`}
>
{hasOpened && children}
</div>
</section>
)
Expand Down
4 changes: 4 additions & 0 deletions src/components/MoreOptions/MoreOptions.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
margin-top: var(--spacers-dp4);
}

.hidden {
display: none;
}

.header svg {
margin-left: -3px;
margin-right: 5px;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@ exports[`matches snapshot when not toggled 1`] = `
</h2>
</header>
<div
class="jsx-1555285825 jsx-876534464 "
data-test="dhis2-uicore-divider"
/>
<div
class="children"
class="children hidden"
data-test="more-options-children"
/>
</section>
Expand Down
6 changes: 1 addition & 5 deletions src/components/Page/__snapshots__/Page.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -563,11 +563,7 @@ exports[`matches snapshot when not loading and a mini summary task 1`] = `
</h2>
</header>
<div
class="jsx-1555285825 jsx-876534464 "
data-test="dhis2-uicore-divider"
/>
<div
class="children"
class="children hidden"
data-test="mini-job-summary-container-MoreOptions-children"
/>
</section>
Expand Down

0 comments on commit dc6222b

Please sign in to comment.