🐛 fix(data-grid): 修复日期时间类型字段编辑交互问题并中文化日期选择器

- 修复"此刻"按钮点击后自动提交的问题,改为自定义按钮仅填值、需点击"确定"才保存
- 修复 datetime 编辑态点击外部后不退出的问题,通过 onBlur + pickerOpenRef 兜底
- 全局配置 dayjs 中文 locale,日期选择器月份/星期等文本显示为中文
- 为 time/date/year 类型 picker 添加 onBlur 兜底,确保焦点离开后退出编辑
- save 函数增加 editing 守卫和 catch 兜底,防止重复保存或异常时卡死编辑态
- refs #289
This commit is contained in:
Syngnat
2026-04-01 14:49:28 +08:00
parent 15f72c013d
commit e464c2cce1
2 changed files with 31 additions and 2 deletions

View File

@@ -571,6 +571,7 @@ const EditableCell: React.FC<EditableCellProps> = React.memo(({
}) => {
const [editing, setEditing] = useState(false);
const inputRef = useRef<any>(null);
const pickerOpenRef = useRef(false);
const form = useContext(EditableContext);
const cellContextMenuContext = useContext(CellContextMenuContext);
@@ -596,7 +597,7 @@ const EditableCell: React.FC<EditableCellProps> = React.memo(({
const save = async () => {
try {
if (!form) return;
if (!form || !editing) return;
const fieldName = getCellFieldName(record, dataIndex);
await form.validateFields([fieldName]);
let nextValue = form.getFieldValue(fieldName);
@@ -617,6 +618,8 @@ const EditableCell: React.FC<EditableCellProps> = React.memo(({
}
} catch (errInfo) {
console.log('Save failed:', errInfo);
// 日期时间类型保存失败时兜底退出编辑,避免 DatePicker 卡在编辑态
if (isDateTimeField && editing) setEditing(false);
}
};
@@ -642,6 +645,7 @@ const EditableCell: React.FC<EditableCellProps> = React.memo(({
style={{ width: '100%' }}
format={TEMPORAL_FORMATS[pickerType]}
onChange={() => setTimeout(save, 0)}
onBlur={() => setTimeout(save, 0)}
needConfirm={false}
/>
) : pickerType === 'datetime' ? (
@@ -649,12 +653,30 @@ const EditableCell: React.FC<EditableCellProps> = React.memo(({
ref={inputRef}
style={{ width: '100%' }}
showTime
showNow={false}
format={TEMPORAL_FORMATS[pickerType]}
renderExtraFooter={() => (
<a
style={{ padding: '0 2px' }}
onClick={() => {
// 自定义"此刻":仅将当前时间填入表单字段,面板保持打开。
// 用户需点击"确定"才真正保存,替代内置 showNow 的自动提交行为。
const fieldName = getCellFieldName(record, dataIndex);
setCellFieldValue(form, fieldName, dayjs());
}}
></a>
)}
onOk={() => setTimeout(save, 0)}
onOpenChange={(open) => {
// 面板关闭(点击外部)且非通过"确定"按钮触发时退出编辑,不保存
pickerOpenRef.current = open;
// 面板关闭(点击外部)时退出编辑,不保存;仅"确定"按钮onOk触发保存
if (!open) setTimeout(() => { if (editing) toggleEdit(); }, 0);
}}
onBlur={() => {
// 兜底:面板未打开或已关闭时,点击外部通过 blur 退出编辑。
// 延迟检查面板状态,避免点击自定义"此刻"按钮时误退出(此时面板仍打开)。
setTimeout(() => { if (editing && !pickerOpenRef.current) setEditing(false); }, 150);
}}
needConfirm
/>
) : (
@@ -664,6 +686,7 @@ const EditableCell: React.FC<EditableCellProps> = React.memo(({
format={TEMPORAL_FORMATS[pickerType]}
picker={pickerType as any}
onChange={() => setTimeout(save, 0)}
onBlur={() => setTimeout(save, 0)}
needConfirm={false}
/>
)

View File

@@ -3,6 +3,12 @@ import ReactDOM from 'react-dom/client'
import App from './App'
// import './index.css' // Optional global styles
// 全局配置 dayjs 使用中文 locale使 Ant Design 的 DatePicker/TimePicker 等组件
// 的月份、星期等文本显示为中文。必须在 Ant Design 组件渲染前完成配置。
import dayjs from 'dayjs'
import 'dayjs/locale/zh-cn'
dayjs.locale('zh-cn')
// 全局配置 Monaco Editor 使用本地打包的文件,避免从 CDN (jsdelivr) 加载。
// Windows WebView2 环境下访问外部 CDN 可能失败,导致编辑器一直显示 Loading。
// 中文语言包必须在 monaco-editor 主包之前导入,否则右键菜单等 UI 仍为英文。