All files / src/form SaveButtonGroup.tsx

72.72% Statements 8/11
50% Branches 7/14
33.33% Functions 1/3
72.72% Lines 8/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58                      66x 4x   4x       4x         4x             4x   4x 4x                                                
import React, { ReactElement, useState } from "react";
import { Menu, Space, Dropdown, Button } from "antd";
import { CaretDownOutlined } from '@ant-design/icons';
 
type SaveButtonGroupProps = {
  saveAndCloseButton: ReactElement | null;
  saveAndEditButton: ReactElement | null;
  saveAndContinueButton: ReactElement | null;
  switchModeButton: ReactElement | null;
};
 
const SaveButtonGroup: React.FC<SaveButtonGroupProps> = (props) => {
  const [dropdownVisible, setDropdownVisible] = useState(false);
 
  const handleDropdownVisibleChange = (visible: boolean): void => {
    setDropdownVisible(visible);
  };
 
  const handleMenuClick = (): void => {
    setDropdownVisible(true);
  };
 
  const dropdownContent: ReactElement = (
    <Menu onClick={handleMenuClick}>
      {props.saveAndEditButton && <Menu.Item key="saveEdit">{props.saveAndEditButton}</Menu.Item>}
      {props.saveAndContinueButton && <Menu.Item key="saveContinue">{props.saveAndContinueButton}</Menu.Item>}
      {props.switchModeButton && <Menu.Item key="switchMode">{props.switchModeButton}</Menu.Item>}
    </Menu>
  );
 
  const hasButtons = props.saveAndCloseButton || props.saveAndEditButton || props.saveAndContinueButton;
 
  Eif (!hasButtons) {
    return null;
  }
 
  return (
    <Space size={2}>
      {props.saveAndCloseButton}
      {(props.saveAndEditButton || props.saveAndContinueButton) && (
        <Dropdown
          overlay={dropdownContent}
          trigger={['click']}
          overlayClassName={"save-button-group-overlay"}
          visible={dropdownVisible}
          onVisibleChange={handleDropdownVisibleChange}
        >
          <Button size={"small"} title={"Advanced operations"}>
            <CaretDownOutlined />
          </Button>
        </Dropdown>
      )}
    </Space>
  );
};
 
export default SaveButtonGroup;