feat(ui): inline-edit dropdowns auto-open + auto-exit on dismiss
When a user clicks an inline-edit affordance for country / timezone / subdivision, the field flipped to its combobox trigger but the popover didn't open — they had to click again. And if they dismissed the popover without picking, the field stayed in edit mode showing a "Select country…" trigger they couldn't get out of. Combobox primitives (country / timezone / subdivision) now accept: - defaultOpen — open on first render - onOpenChange — fired on every open/close transition InlineCountryField / InlineTimezoneField / and the country + subdivision fields inside addresses-editor pass defaultOpen=true and use onOpenChange to auto-exit edit mode when the popover closes without a selection. A pickedRef gate prevents the close-handler from racing the commit() exit when the user does pick a value. Bonus: addresses-editor now renders a flag emoji next to the country name in the read-only state (regional-indicator pair from the ISO code). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
'use client';
|
||||
|
||||
import { useState } from 'react';
|
||||
import { useRef, useState } from 'react';
|
||||
import { Loader2, Pencil } from 'lucide-react';
|
||||
import { toast } from 'sonner';
|
||||
|
||||
@@ -31,8 +31,12 @@ export function InlineCountryField({
|
||||
}: InlineCountryFieldProps) {
|
||||
const [editing, setEditing] = useState(false);
|
||||
const [saving, setSaving] = useState(false);
|
||||
// Set true when the user picks a value from the dropdown, so the
|
||||
// popover-close handler knows commit() will exit edit mode itself.
|
||||
const pickedRef = useRef(false);
|
||||
|
||||
async function commit(next: CountryCode | null) {
|
||||
pickedRef.current = true;
|
||||
if (next === (value ?? null)) {
|
||||
setEditing(false);
|
||||
return;
|
||||
@@ -51,7 +55,23 @@ export function InlineCountryField({
|
||||
if (editing) {
|
||||
return (
|
||||
<div className={cn('flex items-center gap-1', className)}>
|
||||
<CountryCombobox value={value} onChange={(iso) => void commit(iso)} data-testid={testId} />
|
||||
<CountryCombobox
|
||||
value={value}
|
||||
onChange={(iso) => void commit(iso)}
|
||||
data-testid={testId}
|
||||
defaultOpen
|
||||
onOpenChange={(open) => {
|
||||
// When the dropdown closes without a selection, leave edit mode
|
||||
// so the user isn't stuck staring at the trigger button. If a
|
||||
// pick happened, commit() handles the exit (and may need to keep
|
||||
// edit mode briefly to show the saving spinner).
|
||||
if (!open && !pickedRef.current) {
|
||||
setEditing(false);
|
||||
}
|
||||
// Reset for the next open cycle.
|
||||
if (open) pickedRef.current = false;
|
||||
}}
|
||||
/>
|
||||
{saving && <Loader2 className="h-3 w-3 animate-spin text-muted-foreground" />}
|
||||
</div>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user