File size: 5,763 Bytes
45ad5e2 03b24dc 45ad5e2 | 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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 | import React, { useEffect, useState } from 'react';
import { cn } from '@/lib/utils';
const baseInput =
'w-full min-w-0 rounded-md border border-transparent bg-transparent px-1.5 py-0.5 text-sm text-slate-800 ' +
'hover:border-slate-200 focus:border-violet-300 focus:bg-white focus:outline-none focus:ring-1 focus:ring-violet-200';
/**
* Inline text / email / number field: blur or Enter commits if the value changed.
*/
export function EditableCell({
value,
onCommit,
className = '',
inputClassName = '',
type = 'text',
multiline = false,
disabled = false,
}) {
const str = value == null || value === undefined ? '' : String(value);
const [local, setLocal] = useState(str);
useEffect(() => {
setLocal(str);
}, [str]);
const commit = () => {
if (disabled) return;
const next = multiline ? local : local.trim();
const prev = multiline ? str : str.trim();
if (next !== prev) onCommit(next);
};
const stop = (e) => e.stopPropagation();
if (multiline) {
return (
<textarea
className={cn(baseInput, 'resize-y min-h-[2.5rem] max-w-full', className, inputClassName)}
value={local}
onChange={(e) => setLocal(e.target.value)}
onBlur={commit}
onKeyDown={stop}
disabled={disabled}
rows={2}
/>
);
}
return (
<input
type={type}
className={cn(baseInput, className, inputClassName)}
value={local}
onChange={(e) => setLocal(e.target.value)}
onBlur={commit}
onKeyDown={(e) => {
stop(e);
if (e.key === 'Enter') {
e.preventDefault();
e.currentTarget.blur();
}
}}
disabled={disabled}
/>
);
}
function usdWholeString(value) {
if (value == null || value === '') return '';
const n = Math.round(Number(value));
return Number.isFinite(n) ? String(n) : '';
}
function formatUsdWholeDisplay(value) {
const s = usdWholeString(value);
if (s === '') return '';
return new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
maximumFractionDigits: 0,
minimumFractionDigits: 0,
}).format(Number(s));
}
function parseUsdWholeInput(raw) {
const cleaned = String(raw ?? '')
.replace(/[$,\s]/g, '')
.trim();
if (cleaned === '') return null;
const n = Math.round(Number(cleaned));
return Number.isFinite(n) ? n : NaN;
}
/**
* USD whole dollars: shows $1,234 when blurred; plain digits while focused (no number spinners).
* onCommit receives '' to clear, or a digits string for the integer amount.
*/
export function EditableCurrencyCell({
value,
onCommit,
className = '',
inputClassName = '',
disabled = false,
}) {
const canonical = usdWholeString(value);
const [focused, setFocused] = useState(false);
const [local, setLocal] = useState(() =>
canonical ? formatUsdWholeDisplay(canonical) : ''
);
useEffect(() => {
if (focused) return;
setLocal(canonical ? formatUsdWholeDisplay(canonical) : '');
}, [canonical, focused]);
const commit = () => {
if (disabled) return;
const parsed = parseUsdWholeInput(local);
if (parsed === null) {
if (canonical !== '') onCommit('');
setLocal('');
setFocused(false);
return;
}
if (Number.isNaN(parsed)) {
setLocal(canonical ? formatUsdWholeDisplay(canonical) : '');
setFocused(false);
return;
}
const next = String(parsed);
if (next !== canonical) onCommit(next);
setLocal(formatUsdWholeDisplay(next));
setFocused(false);
};
const handleFocus = () => {
if (disabled) return;
setFocused(true);
setLocal(canonical);
};
const handleBlur = () => {
commit();
};
return (
<input
type="text"
inputMode="decimal"
autoComplete="off"
className={cn(baseInput, className, inputClassName)}
value={local}
onChange={(e) => setLocal(e.target.value)}
onFocus={handleFocus}
onBlur={handleBlur}
onKeyDown={(e) => {
e.stopPropagation();
if (e.key === 'Enter') {
e.preventDefault();
e.currentTarget.blur();
}
}}
disabled={disabled}
/>
);
}
function toDateInputValue(iso) {
if (!iso) return '';
const d = new Date(iso);
if (Number.isNaN(d.getTime())) return '';
return d.toISOString().slice(0, 10);
}
/**
* ISO datetime from API → date input; onCommit receives YYYY-MM-DD or empty string for clear.
*/
export function EditableDateCell({ value, onCommit, className = '', disabled = false }) {
const [local, setLocal] = useState(() => toDateInputValue(value));
useEffect(() => {
setLocal(toDateInputValue(value));
}, [value]);
const commit = () => {
if (disabled) return;
const prev = toDateInputValue(value);
if (local !== prev) onCommit(local);
};
return (
<input
type="date"
className={cn(baseInput, 'max-w-[11rem]', className)}
value={local}
onChange={(e) => setLocal(e.target.value)}
onBlur={commit}
onKeyDown={(e) => e.stopPropagation()}
disabled={disabled}
/>
);
}
|