TaskRenameDialog.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import { useRef } from "react";
  2. import type { RefObject } from "react";
  3. import { Button } from "@/components/ui/button.js";
  4. import {
  5. Dialog,
  6. DialogContent,
  7. DialogFooter,
  8. DialogHeader,
  9. DialogTitle,
  10. } from "@/components/ui/dialog.js";
  11. import { Input } from "@/components/ui/input.js";
  12. import { isImeComposingKeyEvent } from "@/lib/imeComposition.js";
  13. import { logger } from "@/logger.js";
  14. export function TaskRenameDialog({
  15. open,
  16. value,
  17. inputRef,
  18. intl,
  19. /** 覆盖标题/占位文案;缺省回退任务重命名文案(workspace 别名等复用同一对话框)。 */
  20. title = intl.formatMessage({ id: "taskList.rename" }),
  21. placeholder = intl.formatMessage({ id: "taskList.renamePlaceholder" }),
  22. onOpenChange,
  23. onChange,
  24. onCancel,
  25. onConfirm,
  26. }: {
  27. open: boolean;
  28. value: string;
  29. inputRef: RefObject<HTMLInputElement | null>;
  30. intl: {
  31. formatMessage: (desc: { id: string }, values?: Record<string, string>) => string;
  32. };
  33. title?: string;
  34. placeholder?: string;
  35. onOpenChange: (open: boolean) => void;
  36. onChange: (value: string) => void;
  37. onCancel: () => void;
  38. onConfirm: () => void;
  39. }) {
  40. const compositionActiveRef = useRef(false);
  41. return (
  42. <Dialog open={open} onOpenChange={onOpenChange}>
  43. <DialogContent className="max-w-xl overflow-hidden rounded-2xl p-0">
  44. <div className="flex min-w-0 flex-col gap-6 p-6">
  45. <DialogHeader className="space-y-2">
  46. <DialogTitle>{title}</DialogTitle>
  47. </DialogHeader>
  48. <div className="flex min-w-0 flex-col space-y-4">
  49. <Input
  50. ref={inputRef}
  51. value={value}
  52. size="lg"
  53. autoFocus
  54. placeholder={placeholder}
  55. onChange={(event) => {
  56. onChange(event.target.value);
  57. }}
  58. onCompositionEnd={() => {
  59. compositionActiveRef.current = false;
  60. }}
  61. onCompositionStart={() => {
  62. compositionActiveRef.current = true;
  63. }}
  64. onKeyDown={(event) => {
  65. if (event.key === "Enter") {
  66. if (
  67. isImeComposingKeyEvent({
  68. compositionActive: compositionActiveRef.current,
  69. nativeEvent: event.nativeEvent,
  70. })
  71. ) {
  72. // task 重命名里中文输入法 Enter 是候选确认,不是确认按钮。
  73. // 同时读取本地 composition 状态,避免平台事件时序让 isComposing 提前变 false。
  74. logger.debug("[TaskRenameDialog] ignore rename enter during IME", {
  75. valueLength: value.length,
  76. });
  77. return;
  78. }
  79. event.preventDefault();
  80. logger.info("[TaskRenameDialog] rename confirm via enter", {
  81. valueLength: value.length,
  82. trimmedLength: value.trim().length,
  83. });
  84. onConfirm();
  85. }
  86. }}
  87. />
  88. </div>
  89. <DialogFooter className="flex items-center justify-end gap-3">
  90. <Button
  91. type="button"
  92. variant="secondary"
  93. size="lg"
  94. className="h-10 min-w-0 px-5"
  95. onClick={onCancel}
  96. >
  97. {intl.formatMessage({ id: "common.cancel" })}
  98. </Button>
  99. <Button
  100. type="button"
  101. size="lg"
  102. className="h-10 min-w-0 px-5"
  103. onClick={() => {
  104. logger.info("[TaskRenameDialog] rename confirm via button", {
  105. valueLength: value.length,
  106. trimmedLength: value.trim().length,
  107. });
  108. onConfirm();
  109. }}
  110. >
  111. {intl.formatMessage({ id: "common.confirm" })}
  112. </Button>
  113. </DialogFooter>
  114. </div>
  115. </DialogContent>
  116. </Dialog>
  117. );
  118. }