Repository navigation
Expand file tree
/
Copy pathpoll-editor.tsx
More file actions
194 lines (179 loc) · 7.82 KB
/
Copy pathpoll-editor.tsx
File metadata and controls
194 lines (179 loc) · 7.82 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import { ChevronDown, Plus, X } from 'lucide-react';
import InputError from '@/components/input-error';
import { MentionTextarea } from '@/components/mention-textarea';
import { Button } from '@/components/ui/button';
import { cn } from '@/lib/utils';
export type PollDraft = {
options: string[];
duration: number;
};
export const emptyPollDraft: PollDraft = { options: ['', ''], duration: 1440 };
const maxOptionLength = 50;
const maxOptions = 4;
const durations = [
{ minutes: 60, label: '1 hour' },
{ minutes: 1440, label: '1 day' },
{ minutes: 4320, label: '3 days' },
{ minutes: 10080, label: '7 days' },
];
export function PollEditor({
id,
poll,
errors,
onChange,
onRemove,
className,
}: {
id: string;
poll: PollDraft;
errors: Record<string, string | undefined>;
onChange: (poll: PollDraft) => void;
onRemove: () => void;
className?: string;
}) {
const updateOption = (index: number, value: string) =>
onChange({
...poll,
options: poll.options.map((option, current) =>
current === index ? value : option,
),
});
const removeOption = (index: number) => {
onChange({
...poll,
options: poll.options.filter((_, current) => current !== index),
});
requestAnimationFrame(() =>
document.getElementById(`${id}-option-${index - 1}`)?.focus(),
);
};
const addOption = () => {
onChange({ ...poll, options: [...poll.options, ''] });
requestAnimationFrame(() =>
document
.getElementById(`${id}-option-${poll.options.length}`)
?.focus(),
);
};
const pollError =
errors['poll.options'] ?? errors['poll.duration'] ?? errors.poll;
return (
<fieldset
className={cn('rounded-lg border p-3', className)}
data-test="poll-editor"
>
<legend className="sr-only">Poll</legend>
<div className="space-y-2">
{poll.options.map((option, index) => {
const error = errors[`poll.options.${index}`];
return (
<div key={index}>
<div className="flex items-center gap-2">
<label
htmlFor={`${id}-option-${index}`}
className="sr-only"
>
Option {index + 1}
</label>
<MentionTextarea
id={`${id}-option-${index}`}
name="poll[options][]"
value={option}
onValueChange={(value) =>
updateOption(
index,
value.replace(/[\r\n]+/g, ' '),
)
}
onKeyDown={(event) => {
if (event.key === 'Enter') {
event.preventDefault();
}
}}
suggestsMentions={false}
rows={1}
wrap="off"
maxLength={maxOptionLength}
placeholder={`Option ${index + 1}`}
autoComplete="off"
aria-invalid={error ? true : undefined}
className={cn(
'h-9 w-full min-w-0 resize-none overflow-hidden rounded-md border bg-transparent px-3 py-1.5 text-[15px] leading-[22px] transition-[color,box-shadow] outline-none placeholder:text-muted-foreground focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50',
'aria-invalid:border-destructive aria-invalid:ring-destructive/20',
)}
/>
{index >= 2 && (
<button
type="button"
onClick={() => removeOption(index)}
aria-label={`Remove option ${index + 1}`}
title="Remove option"
className="shrink-0 rounded-md p-1.5 text-muted-foreground transition-colors hover:bg-accent hover:text-foreground focus-visible:ring-[3px] focus-visible:ring-ring/50 focus-visible:outline-none"
>
<X className="size-4" />
</button>
)}
</div>
<InputError message={error} className="mt-1" />
</div>
);
})}
</div>
{poll.options.length < maxOptions && (
<Button
type="button"
variant="ghost"
size="sm"
onClick={addOption}
className="mt-2 -ml-2 text-primary hover:text-primary"
data-test="add-poll-option-button"
>
<Plus />
Add option
</Button>
)}
<div className="mt-2 flex flex-wrap items-center justify-between gap-2 border-t pt-3">
<label className="flex items-center gap-2 text-sm text-muted-foreground">
Poll length
<span className="relative inline-flex">
<select
id="poll-duration"
name="poll[duration]"
value={poll.duration}
onChange={(event) =>
onChange({
...poll,
duration: Number(event.currentTarget.value),
})
}
className="h-8 appearance-none rounded-md border bg-background pr-8 pl-2.5 text-sm text-foreground outline-none focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50"
>
{durations.map((duration) => (
<option
key={duration.minutes}
value={duration.minutes}
>
{duration.label}
</option>
))}
</select>
<ChevronDown
aria-hidden="true"
className="pointer-events-none absolute top-1/2 right-2.5 size-4 -translate-y-1/2 text-muted-foreground"
/>
</span>
</label>
<Button
type="button"
variant="ghost"
size="sm"
onClick={onRemove}
className="text-muted-foreground hover:text-destructive"
>
Remove poll
</Button>
</div>
<InputError message={pollError} className="mt-2" />
</fieldset>
);
}