-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNameDialog.tsx
More file actions
66 lines (62 loc) · 1.78 KB
/
Copy pathNameDialog.tsx
File metadata and controls
66 lines (62 loc) · 1.78 KB
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
/**
* A modal, in-app prompt for one name.
*
* §REQ:TWO-PNL-NVGT-OOX-070 the create-folder command asks the user for the new
* folder's name before creating anything.
* §REQ:TWO-PNL-NVGT-OOX-080 the rename command asks for the new name, offering
* the current one as the starting point.
*/
import { useEffect, useId, useRef, useState } from 'react'
export interface NameDialogProps {
label: string
fieldLabel: string
submitLabel: string
cancelLabel?: string
/** §REQ:TWO-PNL-NVGT-OOX-080 the value the field starts with (the current name). */
initialValue?: string
onSubmit: (name: string) => void
onCancel: () => void
}
export function NameDialog({
label,
fieldLabel,
submitLabel,
cancelLabel = 'Cancel',
initialValue = '',
onSubmit,
onCancel,
}: NameDialogProps) {
const [name, setName] = useState(initialValue)
const fieldId = useId()
const fieldRef = useRef<HTMLInputElement>(null)
useEffect(() => {
fieldRef.current?.focus()
fieldRef.current?.select()
}, [])
return (
<div className="pc-dialog" role="dialog" aria-modal="true" aria-label={label}>
<form
onSubmit={(event) => {
event.preventDefault()
// §REQ:TWO-PNL-NVGT-OOX-070 an empty name is not a command
if (name.trim() !== '') onSubmit(name.trim())
}}
>
<label htmlFor={fieldId}>{fieldLabel}</label>
<input
id={fieldId}
ref={fieldRef}
type="text"
value={name}
onChange={(event) => setName(event.target.value)}
/>
<div className="pc-dialog__actions">
<button type="submit">{submitLabel}</button>
<button type="button" onClick={onCancel}>
{cancelLabel}
</button>
</div>
</form>
</div>
)
}