18 - Add text mode to InlineEditInput
This commit is contained in:
parent
24de58982a
commit
81bd3880ec
1 changed files with 49 additions and 27 deletions
|
|
@ -1,26 +1,34 @@
|
||||||
import { useEffect, useRef, useState } from 'react';
|
import { useEffect, useRef, useState } from 'react';
|
||||||
|
|
||||||
interface InlineEditInputProps {
|
interface BaseProps {
|
||||||
|
className?: string;
|
||||||
|
disabled?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface NumberProps extends BaseProps {
|
||||||
|
type?: 'number';
|
||||||
value: number;
|
value: number;
|
||||||
onSave: (value: number) => Promise<void>;
|
onSave: (value: number) => Promise<void>;
|
||||||
formatDisplay?: (value: number) => string;
|
formatDisplay?: (value: number) => string;
|
||||||
min?: number;
|
min?: number;
|
||||||
step?: string;
|
step?: string;
|
||||||
className?: string;
|
|
||||||
disabled?: boolean;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface TextProps extends BaseProps {
|
||||||
|
type: 'text';
|
||||||
|
value: string;
|
||||||
|
onSave: (value: string) => Promise<void>;
|
||||||
|
formatDisplay?: (value: string) => string;
|
||||||
|
}
|
||||||
|
|
||||||
|
type InlineEditInputProps = NumberProps | TextProps;
|
||||||
|
|
||||||
type Status = 'idle' | 'editing' | 'saving' | 'success' | 'error';
|
type Status = 'idle' | 'editing' | 'saving' | 'success' | 'error';
|
||||||
|
|
||||||
export default function InlineEditInput({
|
export default function InlineEditInput(props: InlineEditInputProps) {
|
||||||
value,
|
const { className = '', disabled = false } = props;
|
||||||
onSave,
|
const isText = props.type === 'text';
|
||||||
formatDisplay = (v) => String(v),
|
|
||||||
min,
|
|
||||||
step,
|
|
||||||
className = '',
|
|
||||||
disabled = false,
|
|
||||||
}: InlineEditInputProps) {
|
|
||||||
const [status, setStatus] = useState<Status>('idle');
|
const [status, setStatus] = useState<Status>('idle');
|
||||||
const [editValue, setEditValue] = useState('');
|
const [editValue, setEditValue] = useState('');
|
||||||
const inputRef = useRef<HTMLInputElement>(null);
|
const inputRef = useRef<HTMLInputElement>(null);
|
||||||
|
|
@ -35,7 +43,7 @@ export default function InlineEditInput({
|
||||||
|
|
||||||
const startEditing = () => {
|
const startEditing = () => {
|
||||||
if (disabled) return;
|
if (disabled) return;
|
||||||
setEditValue(String(value));
|
setEditValue(String(props.value));
|
||||||
setStatus('editing');
|
setStatus('editing');
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -46,20 +54,30 @@ export default function InlineEditInput({
|
||||||
const save = async () => {
|
const save = async () => {
|
||||||
if (savingRef.current) return;
|
if (savingRef.current) return;
|
||||||
|
|
||||||
const parsed = Number(editValue);
|
let parsedValue: string | number;
|
||||||
if (isNaN(parsed)) {
|
if (isText) {
|
||||||
setStatus('idle');
|
const trimmed = editValue.trim();
|
||||||
return;
|
if (trimmed === '' || trimmed === props.value) {
|
||||||
}
|
setStatus('idle');
|
||||||
if (parsed === value) {
|
return;
|
||||||
setStatus('idle');
|
}
|
||||||
return;
|
parsedValue = trimmed;
|
||||||
|
} else {
|
||||||
|
const parsed = Number(editValue);
|
||||||
|
if (isNaN(parsed) || parsed === props.value) {
|
||||||
|
setStatus('idle');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
parsedValue = parsed;
|
||||||
}
|
}
|
||||||
|
|
||||||
savingRef.current = true;
|
savingRef.current = true;
|
||||||
setStatus('saving');
|
setStatus('saving');
|
||||||
try {
|
try {
|
||||||
await onSave(parsed);
|
// Type assertion needed: TS can't correlate isText with the union branch
|
||||||
|
await (props.onSave as (v: typeof parsedValue) => Promise<void>)(
|
||||||
|
parsedValue,
|
||||||
|
);
|
||||||
setStatus('success');
|
setStatus('success');
|
||||||
setTimeout(() => setStatus('idle'), 1500);
|
setTimeout(() => setStatus('idle'), 1500);
|
||||||
} catch {
|
} catch {
|
||||||
|
|
@ -79,19 +97,23 @@ export default function InlineEditInput({
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const displayValue = isText
|
||||||
|
? (props.formatDisplay ?? String)(props.value)
|
||||||
|
: (props.formatDisplay ?? ((v: number) => String(v)))(props.value);
|
||||||
|
|
||||||
if (status === 'editing' || status === 'saving') {
|
if (status === 'editing' || status === 'saving') {
|
||||||
return (
|
return (
|
||||||
<input
|
<input
|
||||||
ref={inputRef}
|
ref={inputRef}
|
||||||
type="number"
|
type={isText ? 'text' : 'number'}
|
||||||
value={editValue}
|
value={editValue}
|
||||||
onChange={(e) => setEditValue(e.target.value)}
|
onChange={(e) => setEditValue(e.target.value)}
|
||||||
onBlur={save}
|
onBlur={save}
|
||||||
onKeyDown={handleKeyDown}
|
onKeyDown={handleKeyDown}
|
||||||
min={min}
|
min={props.type !== 'text' ? props.min : undefined}
|
||||||
step={step}
|
step={props.type !== 'text' ? props.step : undefined}
|
||||||
disabled={status === 'saving'}
|
disabled={status === 'saving'}
|
||||||
className={`w-24 rounded border border-blue-300 bg-white px-2 py-0.5 text-sm text-gray-900 outline-none focus:ring-2 focus:ring-blue-500 ${className}`}
|
className={`rounded border border-blue-300 bg-white px-2 py-0.5 text-sm text-gray-900 outline-none focus:ring-2 focus:ring-blue-500 ${isText ? 'w-48' : 'w-24'} ${className}`}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
@ -105,7 +127,7 @@ export default function InlineEditInput({
|
||||||
: 'cursor-pointer rounded px-1 py-0.5 hover:bg-blue-50 hover:text-blue-700'
|
: 'cursor-pointer rounded px-1 py-0.5 hover:bg-blue-50 hover:text-blue-700'
|
||||||
} ${className}`}
|
} ${className}`}
|
||||||
>
|
>
|
||||||
{formatDisplay(value)}
|
{displayValue}
|
||||||
{status === 'success' && <span className="text-green-600">✓</span>}
|
{status === 'success' && <span className="text-green-600">✓</span>}
|
||||||
{status === 'error' && <span className="text-red-600">✗</span>}
|
{status === 'error' && <span className="text-red-600">✗</span>}
|
||||||
</span>
|
</span>
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue