-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Decimals and Steps options only allow positive values (DHIS2-900…
…2, DHIS2-9194) (#1161) * New base type PositiveNumberBaseType added that only allows positive whole numbers * Math.round prevents decimal numbers and returns Number instead of String, which fixes DHIS2-9194 * parseInt(value, 10) to allow for clearing the value with backspace * Applies to both typing manually and by using the browser's built-in "stepper arrows". * New help text for Steps, as per Slack discussion, Number of axis tick steps, including the min and max. A value of 2 or lower will be ignored.
- Loading branch information
1 parent
6809675
commit f29e13c
Showing
4 changed files
with
75 additions
and
8 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
67 changes: 67 additions & 0 deletions
67
packages/app/src/components/VisualizationOptions/Options/PositiveNumberBaseType.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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import React from 'react' | ||
import PropTypes from 'prop-types' | ||
import { connect } from 'react-redux' | ||
import { InputField } from '@dhis2/ui' | ||
|
||
import { sGetUiOptions } from '../../../reducers/ui' | ||
import { acSetUiOptions } from '../../../actions/ui' | ||
import { tabSectionOption } from '../styles/VisualizationOptions.style.js' | ||
|
||
export const PositiveNumberBaseType = ({ | ||
label, | ||
placeholder, | ||
helpText, | ||
width, | ||
option, | ||
value, | ||
onChange, | ||
disabled, | ||
}) => ( | ||
<div className={tabSectionOption.className}> | ||
<div> | ||
<InputField | ||
type="number" | ||
label={label} | ||
onChange={({ value }) => { | ||
const parsedValue = parseInt(value, 10) | ||
parsedValue >= 0 | ||
? onChange(Math.round(value)) | ||
: onChange(parsedValue ? 0 : null) | ||
}} | ||
name={option.name} | ||
value={value || value === 0 ? value.toString() : ''} | ||
helpText={helpText} | ||
placeholder={placeholder} | ||
inputWidth={width} | ||
dense | ||
disabled={disabled} | ||
/> | ||
</div> | ||
</div> | ||
) | ||
|
||
PositiveNumberBaseType.propTypes = { | ||
disabled: PropTypes.bool, | ||
helpText: PropTypes.string, | ||
label: PropTypes.string, | ||
option: PropTypes.object, | ||
placeholder: PropTypes.string, | ||
value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]), | ||
width: PropTypes.string, | ||
onChange: PropTypes.func, | ||
} | ||
|
||
const mapStateToProps = (state, ownProps) => ({ | ||
value: sGetUiOptions(state)[ownProps.option.name], | ||
enabled: sGetUiOptions(state)[ownProps.option.name] !== undefined, | ||
}) | ||
|
||
const mapDispatchToProps = (dispatch, ownProps) => ({ | ||
onChange: value => | ||
dispatch(acSetUiOptions({ [ownProps.option.name]: value })), | ||
}) | ||
|
||
export default connect( | ||
mapStateToProps, | ||
mapDispatchToProps | ||
)(PositiveNumberBaseType) |
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