-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(faceted/badge): BadgePeriod (#5102)
- Loading branch information
Showing
14 changed files
with
596 additions
and
55 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@talend/react-faceted-search': minor | ||
'@talend/react-components': patch | ||
--- | ||
|
||
feat: add BadgePeriod in faceted search |
41 changes: 19 additions & 22 deletions
41
packages/components/src/DateTimePickers/shared/error-messages.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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,4 +47,15 @@ | |
} | ||
} | ||
} | ||
|
||
|
||
&:global(.period) { | ||
.tc-badge-faceted-overlay { | ||
& > button { | ||
> span > span { | ||
max-width: none; | ||
} | ||
} | ||
} | ||
} | ||
} |
124 changes: 124 additions & 0 deletions
124
packages/faceted-search/src/components/Badges/BadgePeriod/BadgePeriod.component.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,124 @@ | ||
import { useMemo } from 'react'; | ||
|
||
import { format, isValid } from 'date-fns'; | ||
import isEmpty from 'lodash/isEmpty'; | ||
import PropTypes from 'prop-types'; | ||
|
||
import Badge from '@talend/react-components/lib/Badge'; | ||
|
||
import { operatorPropTypes, operatorsPropTypes } from '../../facetedSearch.propTypes'; | ||
import { BadgeFaceted } from '../BadgeFaceted'; | ||
import { BadgePeriodForm } from './BadgePeriodForm.component'; | ||
|
||
const DATE_FORMAT_YYYY_DD_MM = 'yyyy-MM-dd'; | ||
|
||
function isDateRange(value) { | ||
return value.id === 'CUSTOM'; | ||
} | ||
|
||
function formatDate(date) { | ||
return format(date, DATE_FORMAT_YYYY_DD_MM); | ||
} | ||
|
||
function getRangeLabel(startDateTime, endDateTime, t) { | ||
return `${formatDate(startDateTime)} ${t('TO', { defaultValue: 'to' })} ${formatDate( | ||
endDateTime, | ||
)}`; | ||
} | ||
|
||
const getSelectBadgeLabel = (value, t) => { | ||
const labelAll = t('FACETED_SEARCH_VALUE_ALL', { defaultValue: 'All' }); | ||
|
||
if (isEmpty(value)) return labelAll; | ||
|
||
if (isDateRange(value) && isValid(value.startDateTime) && isValid(value.endDateTime)) { | ||
return getRangeLabel(value.startDateTime, value.endDateTime, t); | ||
} | ||
|
||
return value.label || labelAll; | ||
}; | ||
|
||
// eslint-disable-next-line import/prefer-default-export | ||
export const BadgePeriod = ({ | ||
displayType, | ||
filterBarPlaceholder, | ||
id, | ||
initialOperatorOpened, | ||
initialValueOpened, | ||
label, | ||
readOnly, | ||
removable, | ||
operator, | ||
operators, | ||
size, | ||
value, | ||
values, | ||
t, | ||
...rest | ||
}) => { | ||
const currentOperator = operator || operators?.[0]; | ||
const badgePeriodId = `${id}-badge-period`; | ||
const badgeLabel = useMemo(() => getSelectBadgeLabel(value, t), [value, t]); | ||
return ( | ||
<BadgeFaceted | ||
badgeId={id} | ||
displayType={displayType} | ||
id={badgePeriodId} | ||
initialOperatorOpened={initialOperatorOpened} | ||
initialValueOpened={initialValueOpened} | ||
labelCategory={label} | ||
labelValue={badgeLabel} | ||
operator={currentOperator} | ||
operators={operators} | ||
readOnly={readOnly} | ||
removable={removable} | ||
size={size} | ||
t={t} | ||
value={value || {}} | ||
type="period" | ||
> | ||
{({ onSubmitBadge, onChangeValue, badgeValue }) => ( | ||
<BadgePeriodForm | ||
id={badgePeriodId} | ||
onChange={onChangeValue} | ||
onSubmit={onSubmitBadge} | ||
value={badgeValue} | ||
values={values} | ||
t={t} | ||
{...rest} | ||
/> | ||
)} | ||
</BadgeFaceted> | ||
); | ||
}; | ||
|
||
BadgePeriod.propTypes = { | ||
displayType: PropTypes.oneOf(Object.values(Badge.TYPES)), | ||
filterBarPlaceholder: PropTypes.string, | ||
id: PropTypes.string.isRequired, | ||
readOnly: PropTypes.bool, | ||
removable: PropTypes.bool, | ||
label: PropTypes.string, | ||
initialOperatorOpened: PropTypes.bool, | ||
initialValueOpened: PropTypes.bool, | ||
operator: operatorPropTypes, | ||
operators: operatorsPropTypes, | ||
size: PropTypes.oneOf(Object.values(Badge.SIZES)), | ||
value: PropTypes.oneOfType([ | ||
PropTypes.string, | ||
PropTypes.shape({ | ||
checked: PropTypes.bool, | ||
id: PropTypes.string.isRequired, | ||
label: PropTypes.string.isRequired, | ||
}), | ||
]), | ||
values: PropTypes.array, | ||
t: PropTypes.func.isRequired, | ||
}; | ||
|
||
BadgePeriod.defaultProps = { | ||
readOnly: false, | ||
removable: true, | ||
initialOperatorOpened: false, | ||
initialValueOpened: false, | ||
}; |
83 changes: 83 additions & 0 deletions
83
packages/faceted-search/src/components/Badges/BadgePeriod/BadgePeriod.component.test.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,83 @@ | ||
import { render, within } from '@testing-library/react'; | ||
|
||
import getDefaultT from '../../../translate'; | ||
import { BadgeFacetedProvider } from '../../context/badgeFaceted.context'; | ||
import { BadgePeriod } from './BadgePeriod.component'; | ||
|
||
const t = getDefaultT(); | ||
|
||
const operator = { | ||
label: 'My Operator', | ||
name: 'my-operator', | ||
}; | ||
|
||
const badgeFacetedContextValue = { | ||
onDeleteBadge: jest.fn(), | ||
onHideOperator: jest.fn(), | ||
onSubmitBadge: jest.fn(), | ||
dispatch: jest.fn(), | ||
}; | ||
|
||
const BadgeWithContext = props => ( | ||
<BadgeFacetedProvider value={badgeFacetedContextValue}> | ||
<BadgePeriod {...props} /> | ||
</BadgeFacetedProvider> | ||
); | ||
describe('BadgePeriod', () => { | ||
it('should return "All" when there is no value', () => { | ||
// Given | ||
const props = { | ||
id: 'myId', | ||
label: 'My Label', | ||
operator, | ||
operators: [{ id: 'in' }], | ||
t, | ||
}; | ||
// When | ||
render(<BadgeWithContext {...props} />); | ||
// Then | ||
expect( | ||
within(document.querySelector('#tc-badge-select-myId-badge-period')).getByText('All'), | ||
).toBeVisible(); | ||
}); | ||
it('should return "All" when value is empty', () => { | ||
// Given | ||
const props = { | ||
id: 'myId', | ||
label: 'My Label', | ||
operator, | ||
operators: [{ id: 'in' }], | ||
t, | ||
value: {}, | ||
}; | ||
// When | ||
render(<BadgeWithContext {...props} />); | ||
// Then | ||
expect( | ||
within(document.querySelector('#tc-badge-select-myId-badge-period')).getByText('All'), | ||
).toBeVisible(); | ||
}); | ||
it('should display range when custom period is selected', () => { | ||
// Given | ||
const props = { | ||
id: 'myId', | ||
label: 'My Label', | ||
operator, | ||
operators: [{ id: 'in' }], | ||
t, | ||
value: { | ||
id: 'CUSTOM', | ||
startDateTime: new Date('2021-01-01'), | ||
endDateTime: new Date('2021-01-02'), | ||
}, | ||
}; | ||
// When | ||
render(<BadgeWithContext {...props} />); | ||
// Then | ||
expect( | ||
within(document.querySelector('#tc-badge-select-myId-badge-period')).getByText( | ||
'2021-01-01 to 2021-01-02', | ||
), | ||
).toBeVisible(); | ||
}); | ||
}); |
Oops, something went wrong.