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

chore: PeriodTypeSelect as functional component #2926

Merged
merged 2 commits into from
Aug 8, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion src/components/periods/PeriodTypeSelect.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,58 @@
import i18n from '@dhis2/d2-i18n'
import PropTypes from 'prop-types'
import React, { Component } from 'react'
import React, { useEffect } from 'react'
import { RELATIVE_PERIODS } from '../../constants/periods.js'
import { getPeriodTypes, getRelativePeriods } from '../../util/periods.js'
import { SelectField } from '../core/index.js'

const PeriodTypeSelect = ({
onChange,
className,
errorText,
hiddenPeriods,
period,
value,
}) => {
useEffect(() => {
const relativePeriodType = {
id: RELATIVE_PERIODS,
name: i18n.t('Relative'),
}

if (!value && period) {
if (getRelativePeriods().find((p) => p.id === period.id)) {
// false will not clear the period dropdown
onChange(relativePeriodType, false)
}
} else if (!value) {
// set relativePeriods as default
onChange(relativePeriodType)
}
}, [value, period, onChange])

return (
<SelectField
label={i18n.t('Period type')}
items={getPeriodTypes(hiddenPeriods)}
value={value}
onChange={onChange}
className={className}
errorText={!value && errorText ? errorText : null}
dataTest="periodtypeselect"
/>
)
}

PeriodTypeSelect.propTypes = {
onChange: PropTypes.func.isRequired,
className: PropTypes.string,
errorText: PropTypes.string,
hiddenPeriods: PropTypes.array,
period: PropTypes.object,
value: PropTypes.string,
}

/*
class PeriodTypeSelect extends Component {
turban marked this conversation as resolved.
Show resolved Hide resolved
static propTypes = {
onChange: PropTypes.func.isRequired,
Expand Down Expand Up @@ -50,5 +98,6 @@ class PeriodTypeSelect extends Component {
)
}
}
*/

export default PeriodTypeSelect