|
| 1 | +import * as React from 'react'; |
| 2 | +import { useIntl } from 'react-intl'; |
| 3 | + |
| 4 | +import { DropdownMenu, TriggerButton } from '@box/blueprint-web'; |
| 5 | +import { ApprovalTask } from '@box/blueprint-web-assets/icons/Fill'; |
| 6 | +import { Tasks } from '@box/blueprint-web-assets/icons/MediumFilled'; |
| 7 | +import messages from './messages'; |
| 8 | +import { TASK_TYPE_APPROVAL, TASK_TYPE_GENERAL } from '../../constants'; |
| 9 | +import type { TaskType } from '../../common/types/tasks'; |
| 10 | + |
| 11 | +import './AddTaskMenuV2.scss'; |
| 12 | + |
| 13 | +export interface AddTaskMenuV2Props { |
| 14 | + isDisabled: boolean; |
| 15 | + onMenuItemClick: (taskType: TaskType) => void; |
| 16 | + setAddTaskButtonRef?: (element: HTMLButtonElement | null) => void; |
| 17 | +} |
| 18 | + |
| 19 | +const AddTaskMenuV2: React.FC<AddTaskMenuV2Props> = ({ isDisabled, onMenuItemClick, setAddTaskButtonRef }) => { |
| 20 | + const { formatMessage } = useIntl(); |
| 21 | + |
| 22 | + const [isOpen, setIsOpen] = React.useState(false); |
| 23 | + |
| 24 | + const handleMenuItemClick = React.useCallback( |
| 25 | + (taskType: TaskType) => { |
| 26 | + // Open the modal first |
| 27 | + onMenuItemClick(taskType); |
| 28 | + // Then close the dropdown. We rely on onCloseAutoFocus to prevent focus restoration. |
| 29 | + setIsOpen(false); |
| 30 | + }, |
| 31 | + [onMenuItemClick], |
| 32 | + ); |
| 33 | + |
| 34 | + return ( |
| 35 | + <DropdownMenu.Root open={isOpen} onOpenChange={setIsOpen}> |
| 36 | + <DropdownMenu.Trigger> |
| 37 | + <TriggerButton |
| 38 | + variant="secondary" |
| 39 | + disabled={isDisabled} |
| 40 | + ref={setAddTaskButtonRef} |
| 41 | + caretDirection={isOpen ? 'up' : 'down'} |
| 42 | + label={formatMessage(messages.tasksAddTask)} |
| 43 | + /> |
| 44 | + </DropdownMenu.Trigger> |
| 45 | + |
| 46 | + <DropdownMenu.Content |
| 47 | + align="end" |
| 48 | + className="bcs-AddTaskMenu-v-two" |
| 49 | + onCloseAutoFocus={(event: Event) => { |
| 50 | + // Prevent focus from returning to the trigger button when the menu closes. |
| 51 | + // This allows the Modal (which was just opened) to keep focus on its input field |
| 52 | + // without having it stolen back, which would trigger a blur validation error. |
| 53 | + event.preventDefault(); |
| 54 | + }} |
| 55 | + > |
| 56 | + <DropdownMenu.Item onClick={() => handleMenuItemClick(TASK_TYPE_GENERAL)}> |
| 57 | + <div className="bcs-AddTaskMenu-v-two-menuItem"> |
| 58 | + <div className="bcs-AddTaskMenu-v-two-icon"> |
| 59 | + <Tasks color="black" width={20} height={20} /> |
| 60 | + </div> |
| 61 | + <div> |
| 62 | + <div className="bcs-AddTaskMenu-v-two-title"> |
| 63 | + {formatMessage(messages.taskAddTaskGeneral)} |
| 64 | + </div> |
| 65 | + <div className="bcs-AddTaskMenu-v-two-description"> |
| 66 | + {formatMessage(messages.taskAddTaskGeneralDescription)} |
| 67 | + </div> |
| 68 | + </div> |
| 69 | + </div> |
| 70 | + </DropdownMenu.Item> |
| 71 | + <DropdownMenu.Item onClick={() => handleMenuItemClick(TASK_TYPE_APPROVAL)}> |
| 72 | + <div className="bcs-AddTaskMenu-v-two-menuItem"> |
| 73 | + <div className="bcs-AddTaskMenu-v-two-icon"> |
| 74 | + {/* Should be replaced by icons/MediumFilled/ApprovalTask after it will be availabel */} |
| 75 | + <ApprovalTask color="black" width={20} height={20} /> |
| 76 | + </div> |
| 77 | + <div> |
| 78 | + <div className="bcs-AddTaskMenu-v-two-title"> |
| 79 | + {formatMessage(messages.taskAddTaskApproval)} |
| 80 | + </div> |
| 81 | + <div className="bcs-AddTaskMenu-v-two-description"> |
| 82 | + {formatMessage(messages.taskAddTaskApprovalDescription)} |
| 83 | + </div> |
| 84 | + </div> |
| 85 | + </div> |
| 86 | + </DropdownMenu.Item> |
| 87 | + </DropdownMenu.Content> |
| 88 | + </DropdownMenu.Root> |
| 89 | + ); |
| 90 | +}; |
| 91 | + |
| 92 | +export default AddTaskMenuV2; |
0 commit comments