devnonla-ui 0.6.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/index.ts +2 -0
- package/src/input/EditableInput.tsx +408 -0
- package/src/lib/sizes.ts +5 -2
- package/src/table/Table.tsx +140 -16
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -4,6 +4,8 @@ export type { ButtonColor, ButtonGroupProps, ButtonProps, ButtonSize, ButtonType
|
|
|
4
4
|
export { Button } from "./button/Button";
|
|
5
5
|
export type { ButtonCopyProps } from "./button/ButtonCopy";
|
|
6
6
|
export { ButtonCopy } from "./button/ButtonCopy";
|
|
7
|
+
export type { EditableInputProps, EditableKeyProps } from "./input/EditableInput";
|
|
8
|
+
export { EditableInput } from "./input/EditableInput";
|
|
7
9
|
export type { InputNumberProps, InputProps, InputSize, PasswordProps, TextAreaProps, TextAreaRef } from "./input/Input";
|
|
8
10
|
export { Input, InputNumber, TextArea } from "./input/Input";
|
|
9
11
|
export type { SearchInputProps } from "./input/SearchInput";
|
|
@@ -0,0 +1,408 @@
|
|
|
1
|
+
import { Pencil } from "lucide-react";
|
|
2
|
+
import { type CSSProperties, cloneElement, isValidElement, type KeyboardEvent, type ReactElement, type ReactNode, type Ref, useEffect, useLayoutEffect, useRef, useState } from "react";
|
|
3
|
+
import { createPortal } from "react-dom";
|
|
4
|
+
import { usePopupContainer } from "../app/context";
|
|
5
|
+
import { Button } from "../button/Button";
|
|
6
|
+
import { cn } from "../lib/cn";
|
|
7
|
+
import { type CanonicalSize, type ControlSize, controlHeightVar, controlRadiusVar, getSizeTokens, useControlSize } from "../lib/sizes";
|
|
8
|
+
import { message } from "../message/message";
|
|
9
|
+
|
|
10
|
+
const POPUP_PAD: Record<CanonicalSize, number> = { small: 12, default: 16, large: 20 };
|
|
11
|
+
const FIELD_INSET: Record<CanonicalSize, string> = { small: "-4px", default: "-6px", large: "-8px" };
|
|
12
|
+
const MULTILINE_MIN: Record<CanonicalSize, number> = { small: 56, default: 72, large: 96 };
|
|
13
|
+
const GLASS_BORDER = 1;
|
|
14
|
+
|
|
15
|
+
const fieldClass = (saving: boolean) => cn("relative z-1 m-0 w-full border-0 bg-transparent text-foreground placeholder:text-muted-foreground/50 focus:outline-none", saving && "opacity-60");
|
|
16
|
+
|
|
17
|
+
type Lock = {
|
|
18
|
+
top: number;
|
|
19
|
+
left: number;
|
|
20
|
+
width: number;
|
|
21
|
+
height: number;
|
|
22
|
+
padTop: number;
|
|
23
|
+
padBottom: number;
|
|
24
|
+
font: string;
|
|
25
|
+
letterSpacing: string;
|
|
26
|
+
color: string;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
function glyphEl(el: HTMLElement): HTMLElement {
|
|
30
|
+
let node = el;
|
|
31
|
+
while (node.childElementCount === 1 && node.firstElementChild instanceof HTMLElement) {
|
|
32
|
+
node = node.firstElementChild;
|
|
33
|
+
}
|
|
34
|
+
return node;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function containingOffset(container: HTMLElement) {
|
|
38
|
+
const cs = getComputedStyle(container);
|
|
39
|
+
const fixedContaining = cs.transform !== "none" || cs.perspective !== "none" || cs.filter !== "none" || cs.backdropFilter !== "none" || cs.willChange.includes("transform");
|
|
40
|
+
if (!fixedContaining) return { top: 0, left: 0 };
|
|
41
|
+
const r = container.getBoundingClientRect();
|
|
42
|
+
return { top: r.top, left: r.left };
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function readLock(textEl: HTMLElement, rowEl: HTMLElement, minWidth: number | undefined): Lock {
|
|
46
|
+
const glyph = glyphEl(textEl);
|
|
47
|
+
const g = glyph.getBoundingClientRect();
|
|
48
|
+
const row = rowEl.getBoundingClientRect();
|
|
49
|
+
const cs = getComputedStyle(glyph);
|
|
50
|
+
const width = Math.max(g.width, row.right - g.left, minWidth ?? 0);
|
|
51
|
+
return {
|
|
52
|
+
top: row.top,
|
|
53
|
+
left: g.left,
|
|
54
|
+
width,
|
|
55
|
+
height: row.height,
|
|
56
|
+
padTop: g.top - row.top,
|
|
57
|
+
padBottom: row.bottom - g.bottom,
|
|
58
|
+
font: cs.font,
|
|
59
|
+
letterSpacing: cs.letterSpacing,
|
|
60
|
+
color: cs.color,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function EditorPopover({
|
|
65
|
+
textRef,
|
|
66
|
+
rowRef,
|
|
67
|
+
inputRef,
|
|
68
|
+
size,
|
|
69
|
+
multiline,
|
|
70
|
+
minWidth,
|
|
71
|
+
saving,
|
|
72
|
+
saveDisabled,
|
|
73
|
+
selectOnFocus,
|
|
74
|
+
onSave,
|
|
75
|
+
onCancel,
|
|
76
|
+
children,
|
|
77
|
+
}: {
|
|
78
|
+
textRef: Ref<HTMLDivElement>;
|
|
79
|
+
rowRef: Ref<HTMLDivElement>;
|
|
80
|
+
inputRef: { current: HTMLInputElement | HTMLTextAreaElement | null };
|
|
81
|
+
size: CanonicalSize;
|
|
82
|
+
multiline: boolean;
|
|
83
|
+
minWidth?: number;
|
|
84
|
+
saving: boolean;
|
|
85
|
+
saveDisabled?: boolean;
|
|
86
|
+
selectOnFocus?: boolean;
|
|
87
|
+
onSave: () => void;
|
|
88
|
+
onCancel: () => void;
|
|
89
|
+
children: ReactElement<{ className?: string; style?: CSSProperties; ref?: Ref<HTMLInputElement | HTMLTextAreaElement> }>;
|
|
90
|
+
}) {
|
|
91
|
+
const getContainer = usePopupContainer();
|
|
92
|
+
const [lock, setLock] = useState<Lock | null>(null);
|
|
93
|
+
const [correction, setCorrection] = useState({ x: 0, y: 0 });
|
|
94
|
+
const attempts = useRef(0);
|
|
95
|
+
|
|
96
|
+
useLayoutEffect(() => {
|
|
97
|
+
const text = typeof textRef === "object" && textRef ? textRef.current : null;
|
|
98
|
+
const row = typeof rowRef === "object" && rowRef ? rowRef.current : null;
|
|
99
|
+
if (!text || !row) return;
|
|
100
|
+
const sync = () => setLock((prev) => {
|
|
101
|
+
const next = readLock(text, row, minWidth);
|
|
102
|
+
if (
|
|
103
|
+
prev &&
|
|
104
|
+
Math.abs(prev.top - next.top) < 0.25 &&
|
|
105
|
+
Math.abs(prev.left - next.left) < 0.25 &&
|
|
106
|
+
Math.abs(prev.width - next.width) < 0.25 &&
|
|
107
|
+
Math.abs(prev.height - next.height) < 0.25 &&
|
|
108
|
+
Math.abs(prev.padTop - next.padTop) < 0.25 &&
|
|
109
|
+
Math.abs(prev.padBottom - next.padBottom) < 0.25 &&
|
|
110
|
+
prev.font === next.font &&
|
|
111
|
+
prev.letterSpacing === next.letterSpacing &&
|
|
112
|
+
prev.color === next.color
|
|
113
|
+
) {
|
|
114
|
+
return prev;
|
|
115
|
+
}
|
|
116
|
+
return next;
|
|
117
|
+
});
|
|
118
|
+
sync();
|
|
119
|
+
const ro = new ResizeObserver(sync);
|
|
120
|
+
ro.observe(text);
|
|
121
|
+
ro.observe(row);
|
|
122
|
+
window.addEventListener("scroll", sync, true);
|
|
123
|
+
window.addEventListener("resize", sync);
|
|
124
|
+
return () => {
|
|
125
|
+
ro.disconnect();
|
|
126
|
+
window.removeEventListener("scroll", sync, true);
|
|
127
|
+
window.removeEventListener("resize", sync);
|
|
128
|
+
};
|
|
129
|
+
}, [textRef, rowRef, minWidth]);
|
|
130
|
+
|
|
131
|
+
useLayoutEffect(() => {
|
|
132
|
+
attempts.current = 0;
|
|
133
|
+
}, [lock?.top, lock?.left, lock?.padTop]);
|
|
134
|
+
|
|
135
|
+
useLayoutEffect(() => {
|
|
136
|
+
const text = typeof textRef === "object" && textRef ? textRef.current : null;
|
|
137
|
+
const input = inputRef.current;
|
|
138
|
+
if (!lock || !text || !input) return;
|
|
139
|
+
const g = glyphEl(text).getBoundingClientRect();
|
|
140
|
+
const box = input.getBoundingClientRect();
|
|
141
|
+
const cs = getComputedStyle(input);
|
|
142
|
+
const dx = g.left - (box.left + parseFloat(cs.paddingLeft));
|
|
143
|
+
const dy = g.top - (box.top + parseFloat(cs.paddingTop));
|
|
144
|
+
if (Math.abs(dx) < 0.25 && Math.abs(dy) < 0.25) {
|
|
145
|
+
attempts.current = 0;
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
if (attempts.current >= 3) return;
|
|
149
|
+
attempts.current += 1;
|
|
150
|
+
setCorrection((current) => ({ x: current.x + dx, y: current.y + dy }));
|
|
151
|
+
}, [lock, textRef, inputRef, correction]);
|
|
152
|
+
|
|
153
|
+
useEffect(() => {
|
|
154
|
+
if (!lock) return;
|
|
155
|
+
const timer = setTimeout(() => {
|
|
156
|
+
const el = inputRef.current;
|
|
157
|
+
el?.focus();
|
|
158
|
+
if (selectOnFocus && el instanceof HTMLInputElement) el.select();
|
|
159
|
+
}, 0);
|
|
160
|
+
return () => clearTimeout(timer);
|
|
161
|
+
}, [lock != null, selectOnFocus, inputRef]);
|
|
162
|
+
|
|
163
|
+
if (!lock || !isValidElement(children)) return null;
|
|
164
|
+
|
|
165
|
+
const pad = POPUP_PAD[size];
|
|
166
|
+
const container = getContainer();
|
|
167
|
+
const origin = containingOffset(container);
|
|
168
|
+
const field = cloneElement(children, {
|
|
169
|
+
ref: inputRef,
|
|
170
|
+
style: {
|
|
171
|
+
...children.props.style,
|
|
172
|
+
boxSizing: "border-box",
|
|
173
|
+
width: "100%",
|
|
174
|
+
height: multiline ? undefined : lock.height,
|
|
175
|
+
minHeight: multiline ? Math.max(MULTILINE_MIN[size], lock.height) : undefined,
|
|
176
|
+
margin: 0,
|
|
177
|
+
border: 0,
|
|
178
|
+
paddingTop: lock.padTop,
|
|
179
|
+
paddingRight: 0,
|
|
180
|
+
paddingBottom: multiline ? 8 : lock.padBottom,
|
|
181
|
+
paddingLeft: 0,
|
|
182
|
+
font: lock.font,
|
|
183
|
+
letterSpacing: lock.letterSpacing,
|
|
184
|
+
color: lock.color,
|
|
185
|
+
},
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
return createPortal(
|
|
189
|
+
<div
|
|
190
|
+
className="nonla-popup-layer nonla-glass outline-none"
|
|
191
|
+
style={{
|
|
192
|
+
position: "fixed",
|
|
193
|
+
zIndex: "var(--nonla-z-popup, 1050)",
|
|
194
|
+
borderRadius: 10,
|
|
195
|
+
top: lock.top - origin.top - pad - GLASS_BORDER + correction.y,
|
|
196
|
+
left: lock.left - origin.left - pad - GLASS_BORDER + correction.x,
|
|
197
|
+
width: lock.width + (pad + GLASS_BORDER) * 2,
|
|
198
|
+
padding: pad,
|
|
199
|
+
}}
|
|
200
|
+
>
|
|
201
|
+
<div className="relative">
|
|
202
|
+
<span aria-hidden className="pointer-events-none absolute top-0 bottom-0" style={{ left: FIELD_INSET[size], right: FIELD_INSET[size], borderRadius: controlRadiusVar(size), background: "color-mix(in srgb, var(--foreground) 6%, transparent)" }} />
|
|
203
|
+
{field}
|
|
204
|
+
</div>
|
|
205
|
+
<div className="mt-3 flex items-center gap-2">
|
|
206
|
+
<Button type="primary" size={size} loading={saving} disabled={saveDisabled} onClick={onSave}>
|
|
207
|
+
Save
|
|
208
|
+
</Button>
|
|
209
|
+
<Button type="text" size={size} disabled={saving} onClick={onCancel}>
|
|
210
|
+
Cancel
|
|
211
|
+
</Button>
|
|
212
|
+
</div>
|
|
213
|
+
</div>,
|
|
214
|
+
container,
|
|
215
|
+
);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function EditButton({ label, size, onClick }: { label: string; size: CanonicalSize; onClick: () => void }) {
|
|
219
|
+
const tok = getSizeTokens(size);
|
|
220
|
+
const box = tok.icon + 8;
|
|
221
|
+
return (
|
|
222
|
+
<button
|
|
223
|
+
type="button"
|
|
224
|
+
aria-label={label}
|
|
225
|
+
title={label}
|
|
226
|
+
onClick={onClick}
|
|
227
|
+
className="inline-flex shrink-0 items-center justify-center rounded text-muted-foreground opacity-0 transition-opacity hover:bg-muted hover:text-foreground group-hover/edit:opacity-100 focus-visible:opacity-100"
|
|
228
|
+
style={{ width: box, height: box }}
|
|
229
|
+
>
|
|
230
|
+
<Pencil size={tok.icon} />
|
|
231
|
+
</button>
|
|
232
|
+
);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
export type EditableInputProps = {
|
|
236
|
+
display: ReactNode;
|
|
237
|
+
editing: boolean;
|
|
238
|
+
onStartEdit: () => void;
|
|
239
|
+
onCancelEdit: () => void;
|
|
240
|
+
initialValue: string;
|
|
241
|
+
placeholder?: string;
|
|
242
|
+
type?: "text" | "password" | "textarea";
|
|
243
|
+
editLabel?: string;
|
|
244
|
+
minWidth?: number;
|
|
245
|
+
size?: ControlSize;
|
|
246
|
+
/** Datatable cells may be cleared. KV/secrets stay required. */
|
|
247
|
+
allowEmpty?: boolean;
|
|
248
|
+
onSave: (value: string) => Promise<void>;
|
|
249
|
+
};
|
|
250
|
+
|
|
251
|
+
const KEY_RE = /^[A-Z][A-Z0-9_]*$/;
|
|
252
|
+
|
|
253
|
+
export type EditableKeyProps = {
|
|
254
|
+
value: string;
|
|
255
|
+
editing: boolean;
|
|
256
|
+
onStartEdit: () => void;
|
|
257
|
+
onCancelEdit: () => void;
|
|
258
|
+
onSave: (key: string) => Promise<void>;
|
|
259
|
+
placeholder?: string;
|
|
260
|
+
editLabel?: string;
|
|
261
|
+
size?: ControlSize;
|
|
262
|
+
};
|
|
263
|
+
|
|
264
|
+
function EditableKey({ value, editing, onStartEdit, onCancelEdit, onSave, placeholder = "VARIABLE_NAME", editLabel = "Edit key", size }: EditableKeyProps) {
|
|
265
|
+
const resolved = useControlSize(size);
|
|
266
|
+
const tok = getSizeTokens(resolved);
|
|
267
|
+
const [draft, setDraft] = useState(value);
|
|
268
|
+
const [saving, setSaving] = useState(false);
|
|
269
|
+
const textRef = useRef<HTMLDivElement>(null);
|
|
270
|
+
const rowRef = useRef<HTMLDivElement>(null);
|
|
271
|
+
const inputRef = useRef<HTMLInputElement>(null);
|
|
272
|
+
|
|
273
|
+
useEffect(() => {
|
|
274
|
+
if (!editing) return;
|
|
275
|
+
setDraft(value);
|
|
276
|
+
}, [editing, value]);
|
|
277
|
+
|
|
278
|
+
const handleCancel = () => {
|
|
279
|
+
setDraft(value);
|
|
280
|
+
onCancelEdit();
|
|
281
|
+
};
|
|
282
|
+
|
|
283
|
+
const handleSave = async () => {
|
|
284
|
+
const next = draft.trim().toUpperCase();
|
|
285
|
+
if (!next) {
|
|
286
|
+
message.error("Key is required");
|
|
287
|
+
return;
|
|
288
|
+
}
|
|
289
|
+
if (!KEY_RE.test(next)) {
|
|
290
|
+
message.error("Key must match [A-Z][A-Z0-9_]* (e.g. API_TOKEN)");
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
if (next === value) {
|
|
294
|
+
onCancelEdit();
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
setSaving(true);
|
|
298
|
+
try {
|
|
299
|
+
await onSave(next);
|
|
300
|
+
onCancelEdit();
|
|
301
|
+
} catch {
|
|
302
|
+
// Caller shows the error; keep the draft open.
|
|
303
|
+
} finally {
|
|
304
|
+
setSaving(false);
|
|
305
|
+
}
|
|
306
|
+
};
|
|
307
|
+
|
|
308
|
+
const handleKeyDown = (e: KeyboardEvent<HTMLInputElement>) => {
|
|
309
|
+
if (e.key === "Enter") {
|
|
310
|
+
e.preventDefault();
|
|
311
|
+
void handleSave();
|
|
312
|
+
} else if (e.key === "Escape") {
|
|
313
|
+
e.preventDefault();
|
|
314
|
+
handleCancel();
|
|
315
|
+
}
|
|
316
|
+
};
|
|
317
|
+
|
|
318
|
+
return (
|
|
319
|
+
<div ref={rowRef} className="flex w-full min-w-0 items-center" style={{ minHeight: controlHeightVar(resolved) }}>
|
|
320
|
+
<div className="group/edit inline-flex max-w-full items-center gap-1">
|
|
321
|
+
<div ref={textRef} className={cn("min-w-0", editing && "invisible")} style={{ fontSize: tok.fontSize, lineHeight: `${tok.lineHeight}px`, minHeight: tok.lineHeight }}>
|
|
322
|
+
<code className="select-text font-mono font-semibold tracking-wide text-foreground">{value}</code>
|
|
323
|
+
</div>
|
|
324
|
+
{editing ? null : <EditButton label={editLabel} size={resolved} onClick={onStartEdit} />}
|
|
325
|
+
</div>
|
|
326
|
+
{editing ? (
|
|
327
|
+
<EditorPopover textRef={textRef} rowRef={rowRef} inputRef={inputRef} size={resolved} multiline={false} saving={saving} selectOnFocus onSave={() => void handleSave()} onCancel={handleCancel}>
|
|
328
|
+
<input type="text" value={draft} onChange={(e) => setDraft(e.target.value.toUpperCase())} onKeyDown={handleKeyDown} placeholder={placeholder} disabled={saving} className={fieldClass(saving)} />
|
|
329
|
+
</EditorPopover>
|
|
330
|
+
) : null}
|
|
331
|
+
</div>
|
|
332
|
+
);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
function EditableInputRoot({ display, editing, onStartEdit, onCancelEdit, initialValue, placeholder, type = "text", editLabel = "Edit value", minWidth, size, allowEmpty = false, onSave }: EditableInputProps) {
|
|
336
|
+
const resolved = useControlSize(size);
|
|
337
|
+
const tok = getSizeTokens(resolved);
|
|
338
|
+
const [draft, setDraft] = useState(initialValue);
|
|
339
|
+
const [saving, setSaving] = useState(false);
|
|
340
|
+
const textRef = useRef<HTMLDivElement>(null);
|
|
341
|
+
const rowRef = useRef<HTMLDivElement>(null);
|
|
342
|
+
const inputRef = useRef<HTMLInputElement | HTMLTextAreaElement>(null);
|
|
343
|
+
const multiline = type === "textarea";
|
|
344
|
+
|
|
345
|
+
useEffect(() => {
|
|
346
|
+
if (!editing) return;
|
|
347
|
+
setDraft(initialValue);
|
|
348
|
+
}, [editing, initialValue]);
|
|
349
|
+
|
|
350
|
+
const handleCancel = () => {
|
|
351
|
+
setDraft(initialValue);
|
|
352
|
+
onCancelEdit();
|
|
353
|
+
};
|
|
354
|
+
|
|
355
|
+
const handleSave = async () => {
|
|
356
|
+
if (!allowEmpty && !draft) {
|
|
357
|
+
message.error("Value is required");
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
if (draft === initialValue) {
|
|
361
|
+
onCancelEdit();
|
|
362
|
+
return;
|
|
363
|
+
}
|
|
364
|
+
setSaving(true);
|
|
365
|
+
try {
|
|
366
|
+
await onSave(draft);
|
|
367
|
+
onCancelEdit();
|
|
368
|
+
} catch {
|
|
369
|
+
// Caller shows the error; keep the draft open.
|
|
370
|
+
} finally {
|
|
371
|
+
setSaving(false);
|
|
372
|
+
}
|
|
373
|
+
};
|
|
374
|
+
|
|
375
|
+
const handleKeyDown = (e: KeyboardEvent<HTMLInputElement | HTMLTextAreaElement>) => {
|
|
376
|
+
if (e.key === "Escape") {
|
|
377
|
+
e.preventDefault();
|
|
378
|
+
handleCancel();
|
|
379
|
+
return;
|
|
380
|
+
}
|
|
381
|
+
if (e.key === "Enter" && (!multiline || e.metaKey || e.ctrlKey)) {
|
|
382
|
+
e.preventDefault();
|
|
383
|
+
void handleSave();
|
|
384
|
+
}
|
|
385
|
+
};
|
|
386
|
+
|
|
387
|
+
const field = multiline ? (
|
|
388
|
+
<textarea value={draft} rows={3} onChange={(e) => setDraft(e.target.value)} onKeyDown={handleKeyDown} placeholder={placeholder} disabled={saving} className={fieldClass(saving)} />
|
|
389
|
+
) : (
|
|
390
|
+
<input type={type} value={draft} onChange={(e) => setDraft(e.target.value)} onKeyDown={handleKeyDown} placeholder={placeholder} disabled={saving} className={fieldClass(saving)} />
|
|
391
|
+
);
|
|
392
|
+
|
|
393
|
+
return (
|
|
394
|
+
<div ref={rowRef} className="group/edit flex w-full min-w-0 items-center" style={{ minHeight: controlHeightVar(resolved) }}>
|
|
395
|
+
<div ref={textRef} className={cn("min-w-0 truncate", editing && "invisible")} style={{ fontSize: tok.fontSize, lineHeight: `${tok.lineHeight}px`, minHeight: tok.lineHeight }}>
|
|
396
|
+
{display}
|
|
397
|
+
</div>
|
|
398
|
+
{editing ? null : <EditButton label={editLabel} size={resolved} onClick={onStartEdit} />}
|
|
399
|
+
{editing ? (
|
|
400
|
+
<EditorPopover textRef={textRef} rowRef={rowRef} inputRef={inputRef} size={resolved} multiline={multiline} minWidth={minWidth} saving={saving} saveDisabled={!allowEmpty && !draft} onSave={() => void handleSave()} onCancel={handleCancel}>
|
|
401
|
+
{field}
|
|
402
|
+
</EditorPopover>
|
|
403
|
+
) : null}
|
|
404
|
+
</div>
|
|
405
|
+
);
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
export const EditableInput = Object.assign(EditableInputRoot, { Key: EditableKey });
|
package/src/lib/sizes.ts
CHANGED
|
@@ -93,9 +93,12 @@ export function controlStatusClass(status?: "error" | "warning"): string {
|
|
|
93
93
|
/** Focus surface fade — Input / Select / DatePicker / TimePicker. */
|
|
94
94
|
export const controlFieldTransition = "transition-[background-color,border-color] duration-[var(--nonla-dur-fast,150ms)] ease-[var(--nonla-ease-out,cubic-bezier(0.16,1,0.3,1))] motion-reduce:transition-none";
|
|
95
95
|
|
|
96
|
-
/**
|
|
96
|
+
/**
|
|
97
|
+
* Outlined field. Border is `--input` (ink on page bg), not glass.
|
|
98
|
+
* Glass border is white frost — it disappears on white.
|
|
99
|
+
*/
|
|
97
100
|
export const controlFieldSurface =
|
|
98
|
-
"border border-solid border-
|
|
101
|
+
"border border-solid border-input bg-glass-bar text-foreground shadow-[inset_0_1px_0_var(--glass-highlight)] backdrop-blur-xl";
|
|
99
102
|
|
|
100
103
|
/** Focus / open border — brand (Button primary), keep fields outlined. */
|
|
101
104
|
export const controlFieldFocusBorder = "focus:border-brand focus-within:border-brand data-[state=open]:border-brand aria-expanded:border-brand";
|
package/src/table/Table.tsx
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type CSSProperties, type Key, type ReactNode, useMemo, useState } from "react";
|
|
1
|
+
import { type CSSProperties, type Key, type ReactNode, useLayoutEffect, useMemo, useRef, useState } from "react";
|
|
2
2
|
import { Checkbox } from "../checkbox/Checkbox";
|
|
3
3
|
import { Empty } from "../empty/Empty";
|
|
4
4
|
import { cn } from "../lib/cn";
|
|
@@ -18,6 +18,11 @@ export type ColumnType<T> = {
|
|
|
18
18
|
/** Custom cell — `(value, record, index) => ReactNode`. */
|
|
19
19
|
render?: (value: any, record: T, index: number) => ReactNode;
|
|
20
20
|
className?: string;
|
|
21
|
+
/**
|
|
22
|
+
* Take leftover container width (measured). `true` shares equally with other flex columns.
|
|
23
|
+
* Text wraps inside that width so the table does not scroll horizontally.
|
|
24
|
+
*/
|
|
25
|
+
flex?: boolean | number;
|
|
21
26
|
ellipsis?: boolean;
|
|
22
27
|
fixed?: "left" | "right";
|
|
23
28
|
/** `true` = default compare on `dataIndex`; or pass a compare fn. */
|
|
@@ -142,6 +147,23 @@ function SortIcon({ order }: { order: SortOrder | undefined }) {
|
|
|
142
147
|
);
|
|
143
148
|
}
|
|
144
149
|
|
|
150
|
+
type FlexLayout = { selection?: number; cols: Record<string, number> };
|
|
151
|
+
|
|
152
|
+
function flexWeight<T>(col: ColumnType<T>): number {
|
|
153
|
+
if (col.width != null) return 0;
|
|
154
|
+
if (col.flex === true) return 1;
|
|
155
|
+
if (typeof col.flex === "number" && col.flex > 0) return col.flex;
|
|
156
|
+
return 0;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function layoutsClose(prev: FlexLayout | null, next: FlexLayout): boolean {
|
|
160
|
+
if (!prev) return false;
|
|
161
|
+
if (Math.abs((prev.selection ?? 0) - (next.selection ?? 0)) > 0.5) return false;
|
|
162
|
+
const keys = Object.keys(next.cols);
|
|
163
|
+
if (keys.length !== Object.keys(prev.cols).length) return false;
|
|
164
|
+
return keys.every((key) => Math.abs((prev.cols[key] ?? -1) - next.cols[key]) <= 0.5);
|
|
165
|
+
}
|
|
166
|
+
|
|
145
167
|
function sizeClasses(s: CanonicalSize) {
|
|
146
168
|
if (s === "small") {
|
|
147
169
|
return {
|
|
@@ -152,15 +174,15 @@ function sizeClasses(s: CanonicalSize) {
|
|
|
152
174
|
}
|
|
153
175
|
if (s === "large") {
|
|
154
176
|
return {
|
|
155
|
-
head: "h-12 px-3 text-
|
|
156
|
-
cell: "px-3 py-3 text-
|
|
177
|
+
head: "h-12 px-3 text-base",
|
|
178
|
+
cell: "px-3 py-3 text-base",
|
|
157
179
|
check: "w-12 px-3",
|
|
158
180
|
};
|
|
159
181
|
}
|
|
160
|
-
// default —
|
|
182
|
+
// default — text-base is 14px (`--text-sm` in this theme is 13px)
|
|
161
183
|
return {
|
|
162
|
-
head: "h-10 px-2 text-
|
|
163
|
-
cell: "p-2 text-
|
|
184
|
+
head: "h-10 px-2 text-base",
|
|
185
|
+
cell: "p-2 text-base",
|
|
164
186
|
check: "w-10 px-2",
|
|
165
187
|
};
|
|
166
188
|
}
|
|
@@ -264,19 +286,101 @@ export function Table<T extends object = Record<string, unknown>>({ columns = []
|
|
|
264
286
|
|
|
265
287
|
const alignClass = (align?: "left" | "center" | "right") => (align === "center" ? "text-center" : align === "right" ? "text-right" : "text-left");
|
|
266
288
|
|
|
289
|
+
const containerRef = useRef<HTMLDivElement>(null);
|
|
290
|
+
const columnsRef = useRef(columns);
|
|
291
|
+
columnsRef.current = columns;
|
|
292
|
+
const [flexLayout, setFlexLayout] = useState<FlexLayout | null>(null);
|
|
293
|
+
const flexSig = `${scroll?.x ?? ""}|${rowSelection ? 1 : 0}|${showHeader ? 1 : 0}|${dataSource.length}|${columns.map((col, i) => `${getColumnKey(col, i)}:${col.flex ?? ""}:${col.width ?? ""}`).join(",")}`;
|
|
294
|
+
const flexSigRef = useRef(flexSig);
|
|
295
|
+
if (flexSigRef.current !== flexSig) {
|
|
296
|
+
flexSigRef.current = flexSig;
|
|
297
|
+
if (flexLayout) setFlexLayout(null);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
useLayoutEffect(() => {
|
|
301
|
+
const container = containerRef.current;
|
|
302
|
+
const cols = columnsRef.current;
|
|
303
|
+
const wantsFlex = scroll?.x == null && cols.some((col) => flexWeight(col) > 0);
|
|
304
|
+
if (!container || !wantsFlex) {
|
|
305
|
+
setFlexLayout((prev) => (prev ? null : prev));
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
let locked: { selection?: number; fixed: Record<string, number>; weights: { key: string; weight: number }[] } | null = null;
|
|
310
|
+
|
|
311
|
+
const apply = () => {
|
|
312
|
+
const client = container.clientWidth;
|
|
313
|
+
if (client <= 0) return;
|
|
314
|
+
if (!locked) {
|
|
315
|
+
const table = container.querySelector("table");
|
|
316
|
+
const row = table?.querySelector("thead > tr") ?? table?.querySelector("tbody > tr");
|
|
317
|
+
if (!row) return;
|
|
318
|
+
const cells = [...row.children] as HTMLElement[];
|
|
319
|
+
const start = rowSelection ? 1 : 0;
|
|
320
|
+
const fixed: Record<string, number> = {};
|
|
321
|
+
const weights: { key: string; weight: number }[] = [];
|
|
322
|
+
cols.forEach((col, i) => {
|
|
323
|
+
const key = getColumnKey(col, i);
|
|
324
|
+
const weight = flexWeight(col);
|
|
325
|
+
if (weight > 0) weights.push({ key, weight });
|
|
326
|
+
else fixed[key] = cells[start + i]?.getBoundingClientRect().width ?? 0;
|
|
327
|
+
});
|
|
328
|
+
if (!weights.length) return;
|
|
329
|
+
locked = {
|
|
330
|
+
selection: rowSelection ? cells[0]?.getBoundingClientRect().width : undefined,
|
|
331
|
+
fixed,
|
|
332
|
+
weights,
|
|
333
|
+
};
|
|
334
|
+
}
|
|
335
|
+
const used = (locked.selection ?? 0) + Object.values(locked.fixed).reduce((sum, n) => sum + n, 0);
|
|
336
|
+
if (used > client + 1) {
|
|
337
|
+
setFlexLayout((prev) => (prev ? null : prev));
|
|
338
|
+
return;
|
|
339
|
+
}
|
|
340
|
+
const leftover = Math.max(0, client - used);
|
|
341
|
+
const weightSum = locked.weights.reduce((sum, item) => sum + item.weight, 0);
|
|
342
|
+
const colsWidth = { ...locked.fixed };
|
|
343
|
+
for (const item of locked.weights) colsWidth[item.key] = (leftover * item.weight) / weightSum;
|
|
344
|
+
const next: FlexLayout = { selection: locked.selection, cols: colsWidth };
|
|
345
|
+
setFlexLayout((prev) => (layoutsClose(prev, next) ? prev : next));
|
|
346
|
+
};
|
|
347
|
+
|
|
348
|
+
apply();
|
|
349
|
+
const observer = new ResizeObserver(apply);
|
|
350
|
+
observer.observe(container);
|
|
351
|
+
return () => observer.disconnect();
|
|
352
|
+
}, [flexSig, rowSelection, scroll?.x, showHeader]);
|
|
353
|
+
|
|
354
|
+
const wantsFlex = scroll?.x == null && columns.some((col) => flexWeight(col) > 0);
|
|
355
|
+
|
|
356
|
+
const colStyle = (col: ColumnType<T>, key: string): CSSProperties => {
|
|
357
|
+
const measured = flexLayout?.cols[key];
|
|
358
|
+
if (measured != null) return { width: measured, maxWidth: measured, minWidth: 0 };
|
|
359
|
+
return { width: col.width, minWidth: col.minWidth };
|
|
360
|
+
};
|
|
361
|
+
|
|
267
362
|
return (
|
|
268
363
|
<div className={cn("relative w-full", className)}>
|
|
269
364
|
{titleNode != null ? <div className="mb-3 text-sm font-medium text-foreground">{titleNode}</div> : null}
|
|
270
365
|
|
|
271
366
|
<Spin spinning={Boolean(loading)}>
|
|
272
|
-
<div data-slot="table-container" className={cn("relative w-full overflow-x-auto", bordered && "rounded-md border border-border")} style={scroll?.x != null ? { overflowX: "auto" } : undefined}>
|
|
367
|
+
<div ref={containerRef} data-slot="table-container" className={cn("relative w-full", flexLayout ? "overflow-x-hidden" : "overflow-x-auto", bordered && "rounded-md border border-border")} style={scroll?.x != null ? { overflowX: "auto" } : undefined}>
|
|
273
368
|
<div style={scroll?.y != null ? { maxHeight: scroll.y, overflow: "auto" } : undefined}>
|
|
274
|
-
<table data-slot="table" className="
|
|
369
|
+
<table data-slot="table" className={cn("caption-bottom border-collapse text-base", flexLayout ? "w-full table-fixed" : wantsFlex ? "w-max" : "w-full")}>
|
|
370
|
+
{flexLayout ? (
|
|
371
|
+
<colgroup>
|
|
372
|
+
{rowSelection ? <col style={{ width: flexLayout.selection }} /> : null}
|
|
373
|
+
{columns.map((col, i) => {
|
|
374
|
+
const key = getColumnKey(col, i);
|
|
375
|
+
return <col key={key} style={{ width: flexLayout.cols[key] }} />;
|
|
376
|
+
})}
|
|
377
|
+
</colgroup>
|
|
378
|
+
) : null}
|
|
275
379
|
{showHeader ? (
|
|
276
380
|
<thead data-slot="table-header" className="[&_tr]:border-b [&_tr]:border-border">
|
|
277
381
|
<tr data-slot="table-row" className="border-b border-border transition-colors hover:bg-transparent">
|
|
278
382
|
{rowSelection ? (
|
|
279
|
-
<th data-slot="table-head" className={cn(sz.head, sz.check, "align-middle font-medium text-foreground", scroll?.y && "sticky top-0 z-10
|
|
383
|
+
<th data-slot="table-head" className={cn(sz.head, sz.check, "align-middle font-medium text-foreground bg-foreground/4", scroll?.y && "sticky top-0 z-10", bordered && "border-b border-border")} style={selectionColWidth != null ? { width: selectionColWidth } : undefined}>
|
|
280
384
|
<div className="flex items-center justify-center">
|
|
281
385
|
{rowSelection.type === "radio" ? null : (
|
|
282
386
|
<Checkbox
|
|
@@ -301,10 +405,9 @@ export function Table<T extends object = Record<string, unknown>>({ columns = []
|
|
|
301
405
|
<th
|
|
302
406
|
key={key}
|
|
303
407
|
data-slot="table-head"
|
|
304
|
-
className={cn(sz.head, "align-middle font-medium
|
|
408
|
+
className={cn(sz.head, "align-middle font-medium text-foreground bg-foreground/4", flexLayout && flexWeight(col) > 0 ? "min-w-0" : "whitespace-nowrap", alignClass(col.align), scroll?.y && "sticky top-0 z-10", bordered && "border-b border-border", sortable && "cursor-pointer select-none", col.className, headerExtra?.className)}
|
|
305
409
|
style={{
|
|
306
|
-
|
|
307
|
-
minWidth: col.minWidth,
|
|
410
|
+
...colStyle(col, key),
|
|
308
411
|
...headerExtra?.style,
|
|
309
412
|
}}
|
|
310
413
|
onClick={sortable ? () => toggleSort(col, i) : undefined}
|
|
@@ -334,10 +437,31 @@ export function Table<T extends object = Record<string, unknown>>({ columns = []
|
|
|
334
437
|
const rowProps = onRow?.(record, absoluteIndex);
|
|
335
438
|
const extraClass = typeof rowClassName === "function" ? rowClassName(record, absoluteIndex) : rowClassName;
|
|
336
439
|
const selected = selectedKeys.includes(key);
|
|
440
|
+
const inSelectionMode = Boolean(rowSelection) && selectedKeys.length > 0;
|
|
441
|
+
const rowDisabled = Boolean(rowSelection?.getCheckboxProps?.(record)?.disabled);
|
|
337
442
|
return (
|
|
338
|
-
<tr
|
|
443
|
+
<tr
|
|
444
|
+
key={key}
|
|
445
|
+
data-slot="table-row"
|
|
446
|
+
data-state={selected ? "selected" : undefined}
|
|
447
|
+
className={cn("border-b border-border transition-colors hover:bg-muted/50 data-[state=selected]:bg-muted", inSelectionMode && !rowDisabled && "cursor-pointer", rowProps?.className, extraClass)}
|
|
448
|
+
style={rowProps?.style}
|
|
449
|
+
onClick={inSelectionMode ? undefined : rowProps?.onClick}
|
|
450
|
+
onClickCapture={
|
|
451
|
+
inSelectionMode && !rowDisabled
|
|
452
|
+
? (e) => {
|
|
453
|
+
if (e.target instanceof Element && e.target.closest("[data-row-select]")) return;
|
|
454
|
+
e.preventDefault();
|
|
455
|
+
e.stopPropagation();
|
|
456
|
+
if (rowSelection?.type === "radio") emitSelection([key]);
|
|
457
|
+
else if (selected) emitSelection(selectedKeys.filter((k) => k !== key));
|
|
458
|
+
else emitSelection([...selectedKeys, key]);
|
|
459
|
+
}
|
|
460
|
+
: undefined
|
|
461
|
+
}
|
|
462
|
+
>
|
|
339
463
|
{rowSelection ? (
|
|
340
|
-
<td data-slot="table-cell" className={cn(sz.cell, sz.check, "align-middle")} style={selectionColWidth != null ? { width: selectionColWidth } : undefined} onClick={(e) => e.stopPropagation()}>
|
|
464
|
+
<td data-row-select data-slot="table-cell" className={cn(sz.cell, sz.check, "align-middle")} style={selectionColWidth != null ? { width: selectionColWidth } : undefined} onClick={(e) => e.stopPropagation()}>
|
|
341
465
|
<div className="flex items-center justify-center">
|
|
342
466
|
{rowSelection.type === "radio" ? (
|
|
343
467
|
<input type="radio" name="nonla-table-row-select" checked={selected} disabled={rowSelection.getCheckboxProps?.(record)?.disabled} aria-label="Select row" className="size-3.5 accent-foreground" onChange={() => emitSelection([key])} />
|
|
@@ -362,8 +486,8 @@ export function Table<T extends object = Record<string, unknown>>({ columns = []
|
|
|
362
486
|
<td
|
|
363
487
|
key={getColumnKey(col, i)}
|
|
364
488
|
data-slot="table-cell"
|
|
365
|
-
className={cn(sz.cell, "align-middle", alignClass(col.align), col.ellipsis ? "max-w-0 truncate" : "whitespace-nowrap", col.className)}
|
|
366
|
-
style={
|
|
489
|
+
className={cn(sz.cell, "align-middle", alignClass(col.align), flexLayout && flexWeight(col) > 0 ? (col.ellipsis ? "max-w-0 truncate" : "min-w-0 whitespace-normal wrap-break-word") : col.ellipsis ? "max-w-0 truncate" : "whitespace-nowrap", col.className)}
|
|
490
|
+
style={colStyle(col, getColumnKey(col, i))}
|
|
367
491
|
title={col.ellipsis && (typeof content === "string" || typeof content === "number") ? String(content) : undefined}
|
|
368
492
|
>
|
|
369
493
|
{content}
|