@talend/react-faceted-search
Version:
82 lines (81 loc) • 2.72 kB
JavaScript
import { useMemo } from 'react';
import { format } from 'date-fns/format';
import PropTypes from 'prop-types';
import { createOperatorsDict, operatorNames } from '../../../dictionary/operator.dictionary';
import { operatorPropTypes, operatorsPropTypes } from '../../facetedSearch.propTypes';
import { BadgeFaceted } from '../BadgeFaceted';
import { BadgeDateForm } from './BadgeDateForm.component';
import { Badge } from "@talend/react-components";
import { jsx as _jsx } from "react/jsx-runtime";
const DATE_FORMAT = 'yyyy-MM-dd';
const defaultOperatorsName = [operatorNames.notEquals, operatorNames.equals, operatorNames.greaterThan, operatorNames.greaterThanOrEquals, operatorNames.lessThan, operatorNames.lessThanOrEquals];
const createDefaultOperators = t => Object.values(createOperatorsDict(t)).filter(operator => defaultOperatorsName.includes(operator.name));
const BadgeDate = ({
id,
initialOperatorOpened,
initialValueOpened,
label,
operator,
operators,
size,
t,
value,
category,
readOnly,
removable,
displayType,
...rest
}) => {
const currentOperators = useMemo(() => operators || createDefaultOperators(t), [operators, t]);
const currentOperator = operator || currentOperators[0];
const badgeDateId = `${id}-badge-date`;
return /*#__PURE__*/_jsx(BadgeFaceted, {
badgeId: id,
displayType: displayType,
id: badgeDateId,
initialOperatorOpened: initialOperatorOpened,
initialValueOpened: initialValueOpened,
labelCategory: label,
labelValue: value ? format(value, DATE_FORMAT) : t('FACETED_SEARCH_VALUE_ALL', {
defaultValue: 'All'
}),
operator: currentOperator,
operators: currentOperators,
readOnly: readOnly,
removable: removable,
size: size,
t: t,
value: value || '',
children: ({
onSubmitBadge,
onChangeValue,
badgeValue
}) => /*#__PURE__*/_jsx(BadgeDateForm, {
id: badgeDateId,
onChange: onChangeValue,
onSubmit: onSubmitBadge,
value: badgeValue,
feature: category || label,
t: t,
...rest
})
});
};
BadgeDate.propTypes = {
id: PropTypes.string.isRequired,
initialOperatorOpened: PropTypes.bool,
initialValueOpened: PropTypes.bool,
label: PropTypes.string.isRequired,
operator: operatorPropTypes,
operators: operatorsPropTypes,
size: PropTypes.oneOf(Object.values(Badge.SIZES)),
t: PropTypes.func.isRequired,
value: PropTypes.number,
category: PropTypes.string,
readOnly: PropTypes.bool,
removable: PropTypes.bool,
displayType: PropTypes.oneOf(Object.values(Badge.TYPES))
};
// eslint-disable-next-line import/prefer-default-export
export { BadgeDate };
//# sourceMappingURL=BadgeDate.component.js.map