|
| 1 | +/********************************************************************************** |
| 2 | + * Copyright (c) 2025 EclipseSource and others. |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the MIT License as outlined in the LICENSE file. |
| 6 | + **********************************************************************************/ |
| 7 | + |
| 8 | +import '../../../../../style/tree/editable-string-cell.css'; |
| 9 | + |
| 10 | +import { Checkbox, Input, Select } from 'antd'; |
| 11 | +import type { CheckboxChangeEvent } from 'antd/lib/checkbox'; |
| 12 | +import React, { useCallback, useEffect, useRef } from 'react'; |
| 13 | +import { CDTTreeItem, CDTTreeItemResource, EditableCDTTreeTableStringColumn } from '../../../common'; |
| 14 | +import LabelCell from './LabelCell'; |
| 15 | + |
| 16 | +interface EditableLabelCellProps<T extends CDTTreeItemResource> { |
| 17 | + column: EditableCDTTreeTableStringColumn; |
| 18 | + record: CDTTreeItem<T>; |
| 19 | + editing: boolean; |
| 20 | + autoFocus: boolean; |
| 21 | + onSubmit: (newValue: string) => void; |
| 22 | + onCancel: () => void; |
| 23 | + onEdit?: (edit: boolean) => void; |
| 24 | +} |
| 25 | + |
| 26 | +const EditableLabelCell = <T extends CDTTreeItemResource>({ |
| 27 | + column, |
| 28 | + record, |
| 29 | + editing, |
| 30 | + autoFocus, |
| 31 | + onSubmit, |
| 32 | + onCancel, |
| 33 | + onEdit |
| 34 | +}: EditableLabelCellProps<T>) => { |
| 35 | + const containerRef = useRef<HTMLDivElement>(null); |
| 36 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 37 | + const editorRef = useRef<any>(null); |
| 38 | + |
| 39 | + const commitEdit = useCallback( |
| 40 | + (newValue: string, event?: { stopPropagation: () => void; preventDefault: () => void }) => { |
| 41 | + event?.stopPropagation(); |
| 42 | + event?.preventDefault(); |
| 43 | + onSubmit(newValue); |
| 44 | + onEdit?.(false); |
| 45 | + }, |
| 46 | + [onSubmit] |
| 47 | + ); |
| 48 | + |
| 49 | + const cancelEdit = useCallback(() => { |
| 50 | + onCancel(); |
| 51 | + onEdit?.(false); |
| 52 | + }, [column.edit.value, onCancel, onEdit]); |
| 53 | + |
| 54 | + // Cancel the edit only if focus leaves the entire container. |
| 55 | + const handleBlur = useCallback(() => { |
| 56 | + setTimeout(() => { |
| 57 | + if (containerRef.current && document.activeElement && !containerRef.current.contains(document.activeElement)) { |
| 58 | + cancelEdit(); |
| 59 | + } |
| 60 | + }, 0); |
| 61 | + }, [cancelEdit]); |
| 62 | + |
| 63 | + const handleKeyDown = useCallback( |
| 64 | + (e: React.KeyboardEvent) => { |
| 65 | + if (e.key === 'Escape') { |
| 66 | + cancelEdit(); |
| 67 | + } |
| 68 | + e.stopPropagation(); |
| 69 | + }, |
| 70 | + [cancelEdit] |
| 71 | + ); |
| 72 | + |
| 73 | + // Consume the double-click event so no other handler is triggered. |
| 74 | + const handleDoubleClick = useCallback( |
| 75 | + (e: React.MouseEvent) => { |
| 76 | + e.preventDefault(); |
| 77 | + e.stopPropagation(); |
| 78 | + onEdit?.(true); |
| 79 | + }, |
| 80 | + [column, onEdit] |
| 81 | + ); |
| 82 | + |
| 83 | + // Focus the editor when entering edit mode. |
| 84 | + useEffect(() => { |
| 85 | + if (editing && editorRef.current && autoFocus) { |
| 86 | + editorRef.current.focus(); |
| 87 | + } |
| 88 | + }, [editing, autoFocus]); |
| 89 | + |
| 90 | + if (editing) { |
| 91 | + return ( |
| 92 | + <div className='edit-field-container' ref={containerRef}> |
| 93 | + {(() => { |
| 94 | + switch (column.edit.type) { |
| 95 | + case 'text': |
| 96 | + return ( |
| 97 | + <Input |
| 98 | + ref={editorRef} |
| 99 | + className={'text-field-cell'} |
| 100 | + defaultValue={column.edit.value} |
| 101 | + onPressEnter={e => commitEdit(e.currentTarget.value, e)} |
| 102 | + onBlur={handleBlur} |
| 103 | + onClick={e => e.stopPropagation()} |
| 104 | + onKeyDown={handleKeyDown} |
| 105 | + /> |
| 106 | + ); |
| 107 | + case 'boolean': { |
| 108 | + const checked = column.edit.value === '1'; |
| 109 | + return ( |
| 110 | + <Checkbox |
| 111 | + ref={editorRef} |
| 112 | + checked={checked} |
| 113 | + onChange={(e: CheckboxChangeEvent) => commitEdit(e.target.checked ? '1' : '0', e)} |
| 114 | + onBlur={handleBlur} |
| 115 | + onClick={e => e.stopPropagation()} |
| 116 | + onKeyDown={handleKeyDown} |
| 117 | + /> |
| 118 | + ); |
| 119 | + } |
| 120 | + case 'enum': { |
| 121 | + return ( |
| 122 | + <Select |
| 123 | + ref={editorRef} |
| 124 | + className={'enum-field-cell'} |
| 125 | + placeholder={column.label} |
| 126 | + value={column.label} // we want to use 'Write Only' as value even if it is not an option |
| 127 | + onChange={newValue => commitEdit(newValue)} |
| 128 | + onBlur={handleBlur} |
| 129 | + onClick={e => e.stopPropagation()} |
| 130 | + onKeyDown={handleKeyDown} |
| 131 | + > |
| 132 | + {column.edit.options.map(opt => { |
| 133 | + return ( |
| 134 | + <Select.Option key={opt.value} value={opt.value}> |
| 135 | + {opt.label} |
| 136 | + </Select.Option> |
| 137 | + ); |
| 138 | + })} |
| 139 | + </Select> |
| 140 | + ); |
| 141 | + } |
| 142 | + default: |
| 143 | + return null; |
| 144 | + } |
| 145 | + })()} |
| 146 | + </div> |
| 147 | + ); |
| 148 | + } |
| 149 | + |
| 150 | + return ( |
| 151 | + <div className='editable-string-cell' onDoubleClick={handleDoubleClick}> |
| 152 | + <LabelCell record={record} column={column} /> |
| 153 | + </div> |
| 154 | + ); |
| 155 | +}; |
| 156 | + |
| 157 | +export default React.memo(EditableLabelCell); |
0 commit comments