|
| 1 | +import { useMemo } from 'react'; |
| 2 | +import { isEmpty } from 'lodash'; |
| 3 | +import PropTypes from 'prop-types'; |
| 4 | +import Badge from '@talend/react-components/lib/Badge'; |
| 5 | + |
| 6 | +import { BadgeFaceted } from '../BadgeFaceted'; |
| 7 | +import { BadgeMenuForm } from './BadgeMenuForm.component'; |
| 8 | +import { operatorPropTypes, operatorsPropTypes } from '../../facetedSearch.propTypes'; |
| 9 | + |
| 10 | +const getSelectBadgeLabel = (value, t) => { |
| 11 | + const labelAll = t('FACETED_SEARCH_VALUE_ALL', { defaultValue: 'All' }); |
| 12 | + if (!isEmpty(value)) { |
| 13 | + return value.label; |
| 14 | + } |
| 15 | + return labelAll; |
| 16 | +}; |
| 17 | + |
| 18 | +// eslint-disable-next-line import/prefer-default-export |
| 19 | +export const BadgeMenu = ({ |
| 20 | + id, |
| 21 | + readOnly, |
| 22 | + removable, |
| 23 | + label, |
| 24 | + initialOperatorOpened, |
| 25 | + initialValueOpened, |
| 26 | + operator, |
| 27 | + operators, |
| 28 | + size, |
| 29 | + value, |
| 30 | + values, |
| 31 | + displayType, |
| 32 | + filterBarPlaceholder, |
| 33 | + t, |
| 34 | + ...rest |
| 35 | +}) => { |
| 36 | + const currentOperators = useMemo(() => operators, [operators]); |
| 37 | + const currentOperator = operator || (currentOperators && currentOperators[0]); |
| 38 | + const badgeMenuId = `${id}-badge-menu`; |
| 39 | + const badgeLabel = useMemo(() => getSelectBadgeLabel(value, t), [value, t]); |
| 40 | + return ( |
| 41 | + <BadgeFaceted |
| 42 | + badgeId={id} |
| 43 | + displayType={displayType} |
| 44 | + id={badgeMenuId} |
| 45 | + initialOperatorOpened={initialOperatorOpened} |
| 46 | + initialValueOpened={initialValueOpened} |
| 47 | + labelCategory={label} |
| 48 | + labelValue={badgeLabel} |
| 49 | + operator={currentOperator} |
| 50 | + operators={currentOperators} |
| 51 | + readOnly={readOnly} |
| 52 | + removable={removable} |
| 53 | + size={size} |
| 54 | + t={t} |
| 55 | + value={value || {}} |
| 56 | + > |
| 57 | + {({ onSubmitBadge, onChangeValue, badgeValue }) => ( |
| 58 | + <BadgeMenuForm |
| 59 | + id={badgeMenuId} |
| 60 | + onChange={onChangeValue} |
| 61 | + onSubmit={onSubmitBadge} |
| 62 | + value={badgeValue} |
| 63 | + values={values} |
| 64 | + filterBarPlaceholder={filterBarPlaceholder} |
| 65 | + t={t} |
| 66 | + {...rest} |
| 67 | + /> |
| 68 | + )} |
| 69 | + </BadgeFaceted> |
| 70 | + ); |
| 71 | +}; |
| 72 | + |
| 73 | +BadgeMenu.propTypes = { |
| 74 | + label: PropTypes.string.isRequired, |
| 75 | + id: PropTypes.string.isRequired, |
| 76 | + initialOperatorOpened: PropTypes.bool, |
| 77 | + initialValueOpened: PropTypes.bool, |
| 78 | + operator: operatorPropTypes, |
| 79 | + operators: operatorsPropTypes, |
| 80 | + size: PropTypes.oneOf(Object.values(Badge.SIZES)), |
| 81 | + value: PropTypes.oneOfType([ |
| 82 | + PropTypes.string, |
| 83 | + PropTypes.shape({ |
| 84 | + checked: PropTypes.bool, |
| 85 | + id: PropTypes.string.isRequired, |
| 86 | + label: PropTypes.string.isRequired, |
| 87 | + }), |
| 88 | + ]), |
| 89 | + readOnly: PropTypes.bool, |
| 90 | + removable: PropTypes.bool, |
| 91 | + values: PropTypes.array, |
| 92 | + t: PropTypes.func.isRequired, |
| 93 | + displayType: PropTypes.oneOf(Object.values(Badge.TYPES)), |
| 94 | + filterBarPlaceholder: PropTypes.string, |
| 95 | +}; |
0 commit comments