import React, { useRef } from "react"; import PropTypes from "prop-types"; import { isFunction, get, findIndex } from "lodash"; import Dropdown from "antd/lib/dropdown"; 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 ArrowLeftOutlinedIcon from "@ant-design/icons/ArrowLeftOutlined"; import ThunderboltTwoToneIcon from "@ant-design/icons/ThunderboltTwoTone"; import ThunderboltOutlinedIcon from "@ant-design/icons/ThunderboltOutlined"; import "./DynamicButton.less"; const { Text } = Typography; function DynamicButton({ options, selectedDynamicValue, onSelect, enabled, staticValueLabel }) { 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 && ( {staticValueLabel} )} ); 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, staticValueLabel: PropTypes.string, }; DynamicButton.defaultProps = { options: [], selectedDynamicValue: null, onSelect: () => {}, enabled: false, staticValueLabel: "Back to Static Value", }; export default DynamicButton;