Skip to content

Commit

Permalink
feat: upgrade to ui@5 (TECH-387) (#476)
Browse files Browse the repository at this point in the history
Implements TECH-387
Upgrades ui-core dep to ui@5 (which has a lot of breaking changes)

Relates to dhis2/data-visualizer-app#1068 and dhis2/dashboard-app#891

Major topics:

* PeriodSelector refactored to use name again
* DynamicDimension refactored, note: won't work with pagination!
* New stories for Filter, PeriodDimension and DynamicDimension
* Any component using Transfer, SingleSelect or MultiSelect should've been updated
  • Loading branch information
martinkrulltott authored Jun 11, 2020
1 parent 24b60a0 commit 8045a6e
Show file tree
Hide file tree
Showing 45 changed files with 579 additions and 474 deletions.
7 changes: 5 additions & 2 deletions 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-05-22T11:34:10.538Z\n"
"PO-Revision-Date: 2020-05-22T11:34:10.538Z\n"
"POT-Creation-Date: 2020-06-03T21:43:03.758Z\n"
"PO-Revision-Date: 2020-06-03T21:43:03.758Z\n"

msgid "Data Type"
msgstr ""
Expand Down Expand Up @@ -59,6 +59,9 @@ msgstr ""
msgid "Dimension recommended with selected data"
msgstr ""

msgid "No items selected"
msgstr ""

msgid "Search"
msgstr ""

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
},
"dependencies": {
"@dhis2/d2-ui-org-unit-dialog": "^7.0.4",
"@dhis2/ui-core": "^4.21.1",
"@dhis2/ui": "^5.0.3",
"@material-ui/core": "^3.9.3",
"@material-ui/icons": "^3.0.2",
"classnames": "^2.2.6",
Expand Down
55 changes: 55 additions & 0 deletions src/__demo__/DynamicDimension.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import React from 'react'
import { storiesOf } from '@storybook/react'

import ItemSelector from '../components/DynamicDimension/ItemSelector'

const items = [
{ id: '1', name: 'One' },
{ id: '2', name: 'Two' },
{ id: '3', name: 'Three' },
{ id: '4', name: 'Four' },
{ id: '5', name: 'Five' },
]

storiesOf('DynamicDimension', module).add('ItemSelector none selected', () => {
return (
<ItemSelector
onSelect={selected => console.log(selected)}
allItems={items}
/>
)
})

storiesOf('DynamicDimension', module).add('ItemSelector one selected', () => {
return (
<ItemSelector
onSelect={selected => console.log(selected)}
allItems={items}
initialSelectedItemIds={[items[2].id]}
/>
)
})

storiesOf('DynamicDimension', module).add(
'ItemSelector one selected not in options',
() => {
return (
<>
<ItemSelector
onSelect={selected => console.log(selected)}
allItems={items}
initialSelectedItemIds={['6']}
/>
<p>
Note: This currently does not work as ui (currently @5.0.1)
does not yet support selected items that are not part of the
options array{' '}
<a href="https://jira.dhis2.org/browse/TECH-380">
https://jira.dhis2.org/browse/TECH-380
</a>
</p>
</>
// TODO: fix the issue above
)
}
)
25 changes: 25 additions & 0 deletions src/__demo__/Filter.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import React, { useState } from 'react'
import { storiesOf } from '@storybook/react'

import Filter from '../components/Filter/Filter'

function FilterWithState() {
const [text, setText] = useState(null)

const onTextChange = value => setText(value)
const onClearFilter = () => setText(null)
return (
<Filter
placeholder="Filter dimensions"
text={text}
onChange={onTextChange}
onClear={onClearFilter}
disableUnderline={true}
type="search"
/>
)
}

storiesOf('Filter', module).add('default', () => {
return <FilterWithState />
})
15 changes: 13 additions & 2 deletions src/__demo__/PeriodDimension.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@ import { storiesOf } from '@storybook/react'

import PeriodDimension from '../components/PeriodDimension/PeriodDimension'

storiesOf('PeriodDimension', module).add('default', () => {
return <PeriodDimension />
const selectedPeriods = [{ id: 'LAST_12_MONTHS', name: 'Last 12 months' }]

storiesOf('PeriodDimension', module).add('None selected', () => {
return <PeriodDimension onSelect={selected => console.log(selected)} />
})

storiesOf('PeriodDimension', module).add('One selected', () => {
return (
<PeriodDimension
selectedPeriods={selectedPeriods}
onSelect={selected => console.log(selected)}
/>
)
})
7 changes: 2 additions & 5 deletions src/components/DataDimension/DataTypesSelector.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react'
import PropTypes from 'prop-types'
import i18n from '@dhis2/d2-i18n'
import { SingleSelectField, SingleSelectOption } from '@dhis2/ui-core'
import { SingleSelectField, SingleSelectOption } from '@dhis2/ui'

import { dataTypes } from '../../modules/dataTypes'
import styles from './styles/DataTypesSelector.style'
Expand All @@ -10,10 +10,7 @@ export const DataTypes = ({ currentDataType, onChange }) => (
<div className="container">
<SingleSelectField
label={i18n.t('Data Type')}
selected={{
value: dataTypes[currentDataType]?.id,
label: dataTypes[currentDataType]?.getName(),
}}
selected={dataTypes[currentDataType]?.id}
onChange={ref => onChange(ref.selected.value)}
dense
>
Expand Down
12 changes: 2 additions & 10 deletions src/components/DataDimension/Detail.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react'
import PropTypes from 'prop-types'
import i18n from '@dhis2/d2-i18n'
import { SingleSelectField, SingleSelectOption } from '@dhis2/ui-core'
import { SingleSelectField, SingleSelectOption } from '@dhis2/ui'
import { TOTALS, DETAIL } from '../../modules/dataTypes'

import styles from './styles/Detail.style'
Expand All @@ -13,19 +13,11 @@ const getOptions = () => ({

export const Detail = ({ currentValue, onChange }) => {
const options = getOptions()
const currentLabel = options[currentValue]
return (
<div className="detail-container">
<SingleSelectField
label={i18n.t('Detail')}
selected={
currentLabel
? {
value: currentValue,
label: currentLabel,
}
: null
}
selected={currentValue}
onChange={ref => onChange(ref.selected.value)}
dense
>
Expand Down
10 changes: 2 additions & 8 deletions src/components/DataDimension/Groups.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react'
import PropTypes from 'prop-types'
import { SingleSelectField, SingleSelectOption } from '@dhis2/ui-core'
import { SingleSelectField, SingleSelectOption } from '@dhis2/ui'

import { Detail } from './Detail'
import { dataTypes } from '../../modules/dataTypes'
Expand All @@ -23,19 +23,13 @@ export const Groups = ({

const groupDetail = dataTypes[dataType].groupDetail

const selected = optionItems.find(item => item.id === groupId) || {}

return (
<div className="container">
<style jsx>{styles}</style>
<div className="group-container">
<SingleSelectField
label={dataTypes[dataType].getGroupLabel()}
selected={
selected.id && selected.name
? { value: selected.id, label: selected.name }
: {}
}
selected={groupId}
placeholder={
!groupId && dataTypes[dataType].getPlaceholder
? dataTypes[dataType].getPlaceholder()
Expand Down
2 changes: 1 addition & 1 deletion src/components/DataDimension/styles/Groups.style.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import css from 'styled-jsx/css'
import { colors } from '@dhis2/ui-core'
import { colors } from '@dhis2/ui'

export default css`
.container {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { colors, theme } from '@dhis2/ui-core'
import { colors, theme } from '@dhis2/ui'

export const styles = {
labelWrapper: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { colors, spacers } from '@dhis2/ui-core'
import { colors, spacers } from '@dhis2/ui'

// Fix for vertical flex scrolling in Firefox/Safari:
// Wrap the list in a div with position:relative (and flex:1 instead of on the list)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { colors, theme } from '@dhis2/ui-core'
import { colors, theme } from '@dhis2/ui'

export const styles = {
toolTip: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { colors } from '@dhis2/ui-core'
import { colors } from '@dhis2/ui'

export const styles = {
divContainer: {
Expand Down
24 changes: 15 additions & 9 deletions src/components/DynamicDimension/DynamicDimension.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ export const DynamicDimension = ({
}, [])

const getItems = async () =>
setItems(await apiFetchItemsByDimension(context, dimensionId))
setItems(await apiFetchItemsByDimension(context, dimensionId)) // TODO: refactor to use the data engine instead
// TODO: *** Once pagination is in use, check if there are items that are in selectedItems that needs to be added to the items list
// TODO: This needs to be refactored to use a loading spinner once Transfer supports it: https://jira.dhis2.org/browse/TECH-379

const selectItems = items => {
const formattedItems = items.map(item => ({
id: item.value,
name: item.label,
}))
const onSelectItems = selectedItemIds => {
const formattedItems = selectedItemIds.map(id => ({
id,
name: items.find(item => item.id === id).name, // TODO: Re: *** above, this won't work with pagination
})) // TODO: fetch the name from somewhere else, as not all content in selectedItems might be present in the items list
onSelect({
dimensionId: dimensionId,
items: formattedItems,
Expand All @@ -36,9 +37,9 @@ export const DynamicDimension = ({

return (
<ItemSelector
onSelect={selectItems}
onSelect={onSelectItems}
allItems={items}
initialSelectedItems={selectedItems}
initialSelectedItemIds={selectedItems.map(item => item.id)}
rightFooter={rightFooter}
// TODO: Pass in a func prop to fetch items, instead of fetching them on this level, to enable the loading spinner?
/>
Expand All @@ -48,7 +49,12 @@ export const DynamicDimension = ({
DynamicDimension.propTypes = {
context: PropTypes.object.isRequired,
dimensionId: PropTypes.string.isRequired,
selectedItems: PropTypes.array.isRequired,
selectedItems: PropTypes.arrayOf(
PropTypes.shape({
id: PropTypes.string,
name: PropTypes.string,
})
).isRequired,
onSelect: PropTypes.func.isRequired,
rightFooter: PropTypes.node,
}
Expand Down
45 changes: 22 additions & 23 deletions src/components/DynamicDimension/ItemSelector.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useState } from 'react'
import PropTypes from 'prop-types'
import { Transfer } from '@dhis2/ui-core'
import { Transfer } from '@dhis2/ui'
import i18n from '@dhis2/d2-i18n'

import styles from '../styles/DimensionSelector.style'
Expand All @@ -15,16 +15,12 @@ import {
const ItemSelector = ({
allItems,
onSelect,
initialSelectedItems,
initialSelectedItemIds,
leftHeader,
rightFooter,
}) => {
const [selectedItems, setSelectedItems] = useState(
initialSelectedItems.map(item => ({
label: item.name,
value: item.id,
key: item.id,
}))
const [selectedItemIds, setSelectedItemIds] = useState(
initialSelectedItemIds
)

const renderEmptySelection = () => (
Expand All @@ -37,10 +33,10 @@ const ItemSelector = ({
return (
<Transfer
onChange={({ selected }) => {
setSelectedItems(selected)
setSelectedItemIds(selected)
onSelect(selected)
}}
selected={selectedItems}
selected={selectedItemIds}
leftHeader={leftHeader}
filterable
enableOrderChange
Expand All @@ -49,31 +45,34 @@ const ItemSelector = ({
selectedWidth={TRANSFER_SELECTED_WIDTH}
selectedEmptyComponent={renderEmptySelection()}
rightFooter={rightFooter}
options={allItems.map(({ id, name }) => ({
label: name,
value: id,
}))}
renderOption={props => (
<TransferOption {...props} icon={GenericIcon} />
)}
// TODO: Add a filter placeholer once the Transfer component supports this (https://github.com/dhis2/ui/issues/131)
// TODO: Add rightHeader "Selected Periods" once the Transfer component supports this (https://github.com/dhis2/ui-core/issues/885)
>
{allItems.map(item => (
<TransferOption
label={item.name}
value={item.id}
key={item.id}
icon={GenericIcon}
/>
))}
</Transfer>
/>
)
}

ItemSelector.propTypes = {
allItems: PropTypes.arrayOf(PropTypes.object).isRequired,
allItems: PropTypes.arrayOf(
PropTypes.shape({
id: PropTypes.string,
name: PropTypes.string,
})
).isRequired,
onSelect: PropTypes.func.isRequired,
initialSelectedItems: PropTypes.arrayOf(PropTypes.object),
initialSelectedItemIds: PropTypes.arrayOf(PropTypes.string),
leftHeader: PropTypes.node,
rightFooter: PropTypes.node,
}

ItemSelector.defaultProps = {
initialSelectedItems: [],
initialSelectedItemIds: [],
}

export default ItemSelector
2 changes: 1 addition & 1 deletion src/components/Filter/Filter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react'
import PropTypes from 'prop-types'
import { InputField } from '@dhis2/ui-core'
import { InputField } from '@dhis2/ui'

export const Filter = ({ text, onChange, onClear, placeholder, type }) => (
<InputField
Expand Down
2 changes: 1 addition & 1 deletion src/components/Filter/__tests__/Filter.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react'
import { shallow } from 'enzyme'
import { InputField } from '@dhis2/ui-core'
import { InputField } from '@dhis2/ui'

import { Filter } from '../Filter'

Expand Down
Loading

0 comments on commit 8045a6e

Please sign in to comment.