Newer
Older
import React from 'react'
import PropTypes from 'prop-types'
import { withStyles } from '@material-ui/core/styles'
import Chip from '@material-ui/core/Chip'
import Tooltip from '@material-ui/core/Tooltip'
import { ISOStringToYear } from './FacetHelpers'
const styles = theme => ({
root: {
display: 'flex',
justifyContent: 'center',
},
chip: {
margin: theme.spacing(0.5)
}
})
/**
* A component for displaying the active facet selections an array of Material-UI's Chips.
* Redux is used for keeping track of the selections.
*/
const ChipsArray = props => {
const handleDelete = item => () => {
if (!props.someFacetIsFetching && !props.fetchingResultCount) {
switch (item.filterType) {
facetID: item.facetID,
option: item.filterType,
value: item.value
facetID: item.facetID,
option: item.filterType,
value: null
facetID: item.facetID,
option: item.filterType,
value: null
facetID: item.facetID
})
const generateLabel = (facetLabel, valueLabel, filterType) => {
return filterType !== 'timespanFilter' &&
filterType !== 'integerFilter' &&
valueLabel.length > 18
: `${facetLabel}: ${valueLabel}`
const { classes, data } = props
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
return (
<div className={classes.root}>
{data !== null && data.map(item => {
const icon = null
let key = null
let valueLabel = null
if (item.filterType === 'uriFilter') {
key = item.value.node.id
valueLabel = item.value.node.prefLabel
}
if (item.filterType === 'textFilter') {
key = item.value
valueLabel = item.value
}
if (item.filterType === 'timespanFilter') {
key = item.facetID
valueLabel = `${ISOStringToYear(item.value.start)} to
${ISOStringToYear(item.value.end)}`
}
if (item.filterType === 'integerFilter') {
const { start, end } = item.value
key = item.facetID
// valueLabel = `${item.value.start} to ${item.value.end}`;
valueLabel = `
${start !== '' ? start : '-'}
to ${end !== '' ? end : '-'}`
}
return (
<Tooltip key={key} title={`${item.facetLabel}: ${valueLabel}`}>
<Chip
key={key}
icon={icon}
label={generateLabel(item.facetLabel, valueLabel, item.filterType)}
className={classes.chip}
disabled={props.someFacetIsFetching || props.fetchingResultCount}
onDelete={handleDelete(item)}
color='primary'
/>
</Tooltip>
)
})}
</div>
)
}
ChipsArray.propTypes = {
classes: PropTypes.object.isRequired,
data: PropTypes.array.isRequired,
facetClass: PropTypes.string.isRequired,
updateFacetOption: PropTypes.func.isRequired,
someFacetIsFetching: PropTypes.bool.isRequired,
fetchingResultCount: PropTypes.bool.isRequired,
fetchFacet: PropTypes.func.isRequired
export default withStyles(styles)(ChipsArray)