Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Decimals and Steps options only allow positive values (DHIS2-9002, DHIS2-9194) #1161

Merged
merged 11 commits into from
Aug 14, 2020
Merged
8 changes: 5 additions & 3 deletions packages/app/i18n/en.pot
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ msgstr ""
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1)\n"
"POT-Creation-Date: 2020-07-28T10:55:58.947Z\n"
"PO-Revision-Date: 2020-07-28T10:55:58.947Z\n"
"POT-Creation-Date: 2020-07-28T11:59:27.684Z\n"
"PO-Revision-Date: 2020-07-28T11:59:27.684Z\n"

msgid "Rename successful"
msgstr ""
Expand Down Expand Up @@ -453,7 +453,9 @@ msgstr ""
msgid "Auto"
msgstr ""

msgid "The number of axis steps between the min and max values"
msgid ""
"Number of axis tick steps, including the min and max. A value of 2 or lower "
"will be ignored."
msgstr ""

msgid "Steps"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
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,
}) => (
<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)
}}
janhenrikoverland marked this conversation as resolved.
Show resolved Hide resolved
name={option.name}
value={value || value === 0 ? value.toString() : ''}
helpText={helpText}
placeholder={placeholder}
inputWidth={width}
dense
/>
</div>
</div>
)

PositiveNumberBaseType.propTypes = {
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)
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import React from 'react'

import i18n from '@dhis2/d2-i18n'

import TextBaseOption from './TextBaseOption'
import PositiveNumberBaseType from './PositiveNumberBaseType'

const RangeAxisDecimals = () => (
<TextBaseOption
type="number"
<PositiveNumberBaseType
width="96px"
label={i18n.t('Decimals')}
placeholder={i18n.t('Auto')}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ import React from 'react'

import i18n from '@dhis2/d2-i18n'

import TextBaseOption from './TextBaseOption'
import PositiveNumberBaseType from './PositiveNumberBaseType'

export const RangeAxisSteps = () => (
<TextBaseOption
type="number"
<PositiveNumberBaseType
width="96px"
helpText={i18n.t(
'The number of axis steps between the min and max values'
'Number of axis tick steps, including the min and max. A value of 2 or lower will be ignored.'
)}
label={i18n.t('Steps')}
placeholder={i18n.t('Auto')}
Expand Down