-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: options with numeric values (sortOrder) (#564)
Fixes the issue of options with numerical values not working correctly. Specifically, sortOrder, which has been also refactored to remove the None value in the select, since the checkbox already can tell if there is no sort order (option off). The select then has only the 2 values for "low to high" and "high to low". When the option is enabled via the checkboxes, "low to high" is preselected effectively changing the option value in the ui.
- Loading branch information
Showing
4 changed files
with
87 additions
and
25 deletions.
There are no files selected for viewing
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
100 changes: 81 additions & 19 deletions
100
packages/app/src/components/VisualizationOptions/Options/SortOrder.js
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,27 +1,89 @@ | ||
import React from 'react' | ||
import React, { useState } from 'react' | ||
import PropTypes from 'prop-types' | ||
import { connect } from 'react-redux' | ||
|
||
import i18n from '@dhis2/d2-i18n' | ||
|
||
import SelectBaseOption from './SelectBaseOption' | ||
import { Checkbox, SingleSelectField, SingleSelectOption } from '@dhis2/ui-core' | ||
|
||
import { sGetUiOptions } from '../../../reducers/ui' | ||
import { acSetUiOptions } from '../../../actions/ui' | ||
|
||
import { | ||
tabSectionOptionItem, | ||
tabSectionOptionToggleable, | ||
} from '../styles/VisualizationOptions.style.js' | ||
|
||
import { options } from '../../../modules/options' | ||
|
||
const optionName = 'sortOrder' | ||
const defaultValue = options[optionName].defaultValue | ||
|
||
const SortOrder = () => ( | ||
<SelectBaseOption | ||
toggleable={true} | ||
label={i18n.t('Custom sort order')} | ||
option={{ | ||
name: optionName, | ||
defaultValue: defaultValue, | ||
items: [ | ||
{ id: '-1', label: i18n.t('Low to high') }, | ||
{ id: '1', label: i18n.t('High to low') }, | ||
{ id: '0', label: i18n.t('None') }, | ||
], | ||
}} | ||
/> | ||
) | ||
|
||
export default SortOrder | ||
const SortOrder = ({ value, onChange }) => { | ||
const [enabled, setEnabled] = useState(Boolean(value)) | ||
|
||
const options = [ | ||
{ value: -1, label: i18n.t('Low to high') }, | ||
{ value: 1, label: i18n.t('High to low') }, | ||
] | ||
|
||
const selected = options.find(option => option.value === value) || {} | ||
|
||
const onToggle = checked => { | ||
setEnabled(checked) | ||
|
||
onChange({ | ||
sortOrder: checked ? -1 : defaultValue, | ||
}) | ||
} | ||
|
||
return ( | ||
<div className={enabled ? '' : tabSectionOptionItem.className}> | ||
<Checkbox | ||
checked={enabled} | ||
label={i18n.t('Custom sort order')} | ||
name="sortOrder-toggle" | ||
onChange={({ checked }) => onToggle(checked)} | ||
dense | ||
/> | ||
{enabled ? ( | ||
<div className={tabSectionOptionToggleable.className}> | ||
<SingleSelectField | ||
name="sortOrder-select" | ||
selected={selected} | ||
onChange={({ selected }) => | ||
onChange({ | ||
sortOrder: selected.value, | ||
}) | ||
} | ||
inputWidth="280px" | ||
dense | ||
> | ||
{options.map(({ value, label }) => ( | ||
<SingleSelectOption | ||
key={value} | ||
value={value} | ||
label={label} | ||
/> | ||
))} | ||
</SingleSelectField> | ||
</div> | ||
) : null} | ||
</div> | ||
) | ||
} | ||
|
||
SortOrder.propTypes = { | ||
value: PropTypes.object.isRequired, | ||
onChange: PropTypes.func.isRequired, | ||
} | ||
|
||
const mapStateToProps = state => ({ | ||
value: sGetUiOptions(state)[optionName] || defaultValue, | ||
}) | ||
|
||
const mapDispatchToProps = dispatch => ({ | ||
onChange: value => dispatch(acSetUiOptions(value)), | ||
}) | ||
|
||
export default connect(mapStateToProps, mapDispatchToProps)(SortOrder) |
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