Skip to content

Commit 681f469

Browse files
committed
save sendTo input to model on blur or on enter
1 parent 13a0e1a commit 681f469

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

src/components/notification-picker.tsx

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ export function NotificationPicker({
3535
const [enableNotification, setEnableNotification] = useState<boolean>(
3636
model.notification?.enableNotification ?? true
3737
);
38-
const sendToString = model.notification?.sendTo?.join(', ') || '';
38+
const [sendToInput, setSendToInput] = useState<string>(
39+
model.notification?.sendTo?.join(', ') || ''
40+
);
3941

4042
const enableNotificationChange = (e: React.ChangeEvent<HTMLInputElement>) => {
4143
const updatedEnableNotification = e.target.checked;
@@ -67,17 +69,27 @@ export function NotificationPicker({
6769
[selectedEvents, model, modelChange]
6870
);
6971

70-
const sendToChange = useCallback(
71-
(e: React.ChangeEvent<HTMLInputElement>) => {
72-
const { value } = e.target;
73-
const sendToArray = value.split(',').map(sendToStr => sendToStr.trim());
74-
const updatedNotification = {
75-
...model.notification,
76-
sendTo: sendToArray
77-
};
78-
modelChange({ ...model, notification: updatedNotification });
72+
const sendToChange = useCallback((e: React.ChangeEvent<HTMLInputElement>) => {
73+
setSendToInput(e.target.value);
74+
}, []);
75+
76+
const handleBlur = useCallback(() => {
77+
const emailArray = sendToInput
78+
.split(',')
79+
.map(email => email.trim())
80+
.filter(email => email);
81+
const updatedNotification = { ...model.notification, sendTo: emailArray };
82+
modelChange({ ...model, notification: updatedNotification });
83+
}, [sendToInput, model, modelChange]);
84+
85+
const handleKeyDown = useCallback(
86+
(e: React.KeyboardEvent) => {
87+
if (e.key === 'Enter') {
88+
e.preventDefault();
89+
handleBlur();
90+
}
7991
},
80-
[model, modelChange]
92+
[handleBlur]
8193
);
8294

8395
const deleteSelectedEvent = useCallback(
@@ -113,10 +125,12 @@ export function NotificationPicker({
113125

114126
<TextField
115127
label={trans.__('Send To')}
116-
value={sendToString}
128+
value={sendToInput}
117129
name="sendTo"
118130
variant="outlined"
119131
onChange={sendToChange}
132+
onBlur={handleBlur}
133+
onKeyDown={handleKeyDown}
120134
disabled={!enableNotification}
121135
/>
122136
<NotificationEventsSelect

0 commit comments

Comments
 (0)