import React, { useRef } from 'react'; import PropTypes from 'prop-types'; import { isFunction, get, findIndex } from 'lodash'; import Dropdown from 'antd/lib/dropdown'; import Icon from 'antd/lib/icon'; import Menu from 'antd/lib/menu'; import Typography from 'antd/lib/typography'; import { DynamicDateType } from '@/services/parameters/DateParameter'; import { DynamicDateRangeType } from '@/services/parameters/DateRangeParameter'; import './DynamicButton.less'; const { Text } = Typography; function DynamicButton({ options, selectedDynamicValue, onSelect, enabled }) { const menu = ( onSelect(get(options, key, 'static'))} selectedKeys={[`${findIndex(options, { value: selectedDynamicValue })}`]} data-test="DynamicButtonMenu" > {options.map((option, index) => ( // eslint-disable-next-line react/no-array-index-key {option.name} {option.label && ( {isFunction(option.label) ? option.label() : option.label} )} ))} {enabled && } {enabled && ( Back to Static Value )} ); const containerRef = useRef(null); return (
e.stopPropagation()}> )} getPopupContainer={() => containerRef.current} data-test="DynamicButton" />
); } DynamicButton.propTypes = { options: PropTypes.arrayOf(PropTypes.object), // eslint-disable-line react/forbid-prop-types selectedDynamicValue: PropTypes.oneOfType([DynamicDateType, DynamicDateRangeType]), onSelect: PropTypes.func, enabled: PropTypes.bool, }; DynamicButton.defaultProps = { options: [], selectedDynamicValue: null, onSelect: () => {}, enabled: false, }; export default DynamicButton;