feat(app): sheet draft editor route, modes, recovery offer and conflict dialog
This commit is contained in:
1 parent
b14509f221
commit
6aba9711f0
44 files changed
+3143
-13
No files matched your search
@@ -0,0 +1,9 @@
|
||||
import Button, { type ButtonProps } from '@/components/ui/Button'
|
||||
|
||||
/** An action of a panel: absent from the page while the panel is read-only. */
|
||||
export default function ActionButton({
|
||||
readOnly,
|
||||
...props
|
||||
}: ButtonProps & { readOnly: boolean }) {
|
||||
return readOnly ? null : <Button type="button" {...props} />
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
/** A label and its value as text: what a field shows when it cannot be edited. */
|
||||
export default function ReadOnlyValue({
|
||||
id,
|
||||
label,
|
||||
value,
|
||||
multiline = false,
|
||||
}: {
|
||||
id: string
|
||||
label: string
|
||||
value: string
|
||||
multiline?: boolean
|
||||
}) {
|
||||
return (
|
||||
<div className="space-y-1">
|
||||
<p
|
||||
id={`${id}-label`}
|
||||
className="text-sm font-medium text-[var(--color-ink)]"
|
||||
>
|
||||
{label}
|
||||
</p>
|
||||
<p
|
||||
id={id}
|
||||
aria-labelledby={`${id}-label`}
|
||||
className={`text-sm text-[var(--color-ink-muted)] ${multiline ? 'whitespace-pre-wrap' : ''}`}
|
||||
>
|
||||
{value === '' ? '—' : value}
|
||||
</p>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
import FormField, { fieldDescribedBy } from '@/components/ui/FormField'
|
||||
import Select from '@/components/ui/Select'
|
||||
import ReadOnlyValue from './ReadOnlyValue'
|
||||
|
||||
export interface SelectOption {
|
||||
value: string
|
||||
label: string
|
||||
}
|
||||
|
||||
export interface SelectFieldProps {
|
||||
id: string
|
||||
label: string
|
||||
value: string
|
||||
options: readonly SelectOption[]
|
||||
/** A choice is a confirmation: it commits at once. */
|
||||
onCommit: (value: string) => void
|
||||
readOnly?: boolean
|
||||
hint?: string
|
||||
error?: string
|
||||
}
|
||||
|
||||
/** A choice among fixed options; read-only, the chosen option's label as text. */
|
||||
export default function SelectField({
|
||||
id,
|
||||
label,
|
||||
value,
|
||||
options,
|
||||
onCommit,
|
||||
readOnly,
|
||||
hint,
|
||||
error,
|
||||
}: SelectFieldProps) {
|
||||
if (readOnly) {
|
||||
const chosen = options.find((option) => option.value === value)
|
||||
return <ReadOnlyValue id={id} label={label} value={chosen?.label ?? ''} />
|
||||
}
|
||||
return (
|
||||
<FormField id={id} label={label} hint={hint} error={error}>
|
||||
<Select
|
||||
id={id}
|
||||
value={value}
|
||||
onChange={(e) => onCommit(e.target.value)}
|
||||
aria-invalid={Boolean(error)}
|
||||
aria-describedby={fieldDescribedBy(id, hint, error)}
|
||||
>
|
||||
{options.map((option) => (
|
||||
<option key={option.value} value={option.value}>
|
||||
{option.label}
|
||||
</option>
|
||||
))}
|
||||
</Select>
|
||||
</FormField>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
import { useEffect, useRef, useState, type KeyboardEvent } from 'react'
|
||||
import FormField, { fieldDescribedBy } from '@/components/ui/FormField'
|
||||
import Input from '@/components/ui/Input'
|
||||
import Textarea from '@/components/ui/Textarea'
|
||||
import ReadOnlyValue from './ReadOnlyValue'
|
||||
|
||||
export interface TextFieldProps {
|
||||
id: string
|
||||
label: string
|
||||
/** The committed value (the document's). */
|
||||
value: string
|
||||
/** One commit per change: blur with a changed value, Enter (single line), unmount. */
|
||||
onCommit: (value: string) => void
|
||||
/** Shows the value as text, no control (every mode that cannot edit). */
|
||||
readOnly?: boolean
|
||||
multiline?: boolean
|
||||
maxLength?: number
|
||||
hint?: string
|
||||
/** The refusal phrased by the caller, shown next to the control. */
|
||||
error?: string
|
||||
}
|
||||
|
||||
/**
|
||||
* A text field that commits once per change, never per keystroke (BDR-050): typing stays
|
||||
* local, blur or Enter commits a changed value, and a value still pending when the field
|
||||
* unmounts (a mode switch) is committed by the cleanup. Read-only, it is plain text.
|
||||
*/
|
||||
export default function TextField(props: TextFieldProps) {
|
||||
if (props.readOnly) {
|
||||
return (
|
||||
<ReadOnlyValue
|
||||
id={props.id}
|
||||
label={props.label}
|
||||
value={props.value}
|
||||
multiline={props.multiline}
|
||||
/>
|
||||
)
|
||||
}
|
||||
return <EditableText {...props} />
|
||||
}
|
||||
|
||||
function EditableText({
|
||||
id,
|
||||
label,
|
||||
value,
|
||||
onCommit,
|
||||
multiline,
|
||||
maxLength,
|
||||
hint,
|
||||
error,
|
||||
}: TextFieldProps) {
|
||||
const [draft, setDraft] = useState(value)
|
||||
const [seen, setSeen] = useState(value)
|
||||
// A new committed value (undo, reload, restore) replaces what was typed.
|
||||
if (seen !== value) {
|
||||
setSeen(value)
|
||||
setDraft(value)
|
||||
}
|
||||
const latest = useRef({ draft, value, onCommit })
|
||||
useEffect(() => {
|
||||
latest.current = { draft, value, onCommit }
|
||||
})
|
||||
useEffect(
|
||||
() => () => {
|
||||
const pending = latest.current
|
||||
if (pending.draft !== pending.value) pending.onCommit(pending.draft)
|
||||
},
|
||||
[],
|
||||
)
|
||||
|
||||
const commit = () => {
|
||||
if (draft !== value) onCommit(draft)
|
||||
}
|
||||
const onKeyDown = (event: KeyboardEvent<HTMLInputElement>) => {
|
||||
if (event.key === 'Enter' && !event.nativeEvent.isComposing) commit()
|
||||
}
|
||||
const shared = {
|
||||
id,
|
||||
value: draft,
|
||||
maxLength,
|
||||
onChange: (e: { target: { value: string } }) => setDraft(e.target.value),
|
||||
onBlur: commit,
|
||||
'aria-invalid': Boolean(error),
|
||||
'aria-describedby': fieldDescribedBy(id, hint, error),
|
||||
}
|
||||
return (
|
||||
<FormField id={id} label={label} hint={hint} error={error}>
|
||||
{multiline ? (
|
||||
<Textarea {...shared} />
|
||||
) : (
|
||||
<Input {...shared} onKeyDown={onKeyDown} />
|
||||
)}
|
||||
</FormField>
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user