Skip to content

Commit 649ce40

Browse files
authored
feat: add handle filter tokens on blur (#1837)
feat: add tokens on blur Signed-off-by: Adam Setch <[email protected]>
1 parent 36ee11b commit 649ce40

File tree

1 file changed

+41
-31
lines changed

1 file changed

+41
-31
lines changed

src/renderer/components/filters/UserHandleFilter.tsx

+41-31
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,24 @@ export const UserHandleFilter: FC = () => {
4848
mapValuesToTokens(settings.filterIncludeHandles),
4949
);
5050

51+
const addIncludeHandlesToken = (
52+
event:
53+
| React.KeyboardEvent<HTMLInputElement>
54+
| React.FocusEvent<HTMLInputElement>,
55+
) => {
56+
const value = (event.target as HTMLInputElement).value.trim();
57+
58+
if (value.length > 0 && !includeHandles.some((v) => v.text === value)) {
59+
setIncludeHandles([
60+
...includeHandles,
61+
{ id: includeHandles.length, text: value },
62+
]);
63+
updateFilter('filterIncludeHandles', value as UserHandle, true);
64+
65+
(event.target as HTMLInputElement).value = '';
66+
}
67+
};
68+
5169
const removeIncludeHandleToken = (tokenId: string | number) => {
5270
const value = includeHandles.find((v) => v.id === tokenId)?.text || '';
5371
updateFilter('filterIncludeHandles', value as UserHandle, false);
@@ -58,29 +76,33 @@ export const UserHandleFilter: FC = () => {
5876
const includeHandlesKeyDown = (
5977
event: React.KeyboardEvent<HTMLInputElement>,
6078
) => {
61-
const value = (event.target as HTMLInputElement).value.trim();
79+
if (tokenEvents.includes(event.key)) {
80+
addIncludeHandlesToken(event);
81+
}
82+
};
6283

63-
if (
64-
tokenEvents.includes(event.key) &&
65-
!includeHandles.some((v) => v.text === value) &&
66-
value.length > 0
67-
) {
68-
event.preventDefault();
84+
const [excludeHandles, setExcludeHandles] = useState<InputToken[]>(
85+
mapValuesToTokens(settings.filterExcludeHandles),
86+
);
6987

70-
setIncludeHandles([
71-
...includeHandles,
72-
{ id: includeHandles.length, text: value },
88+
const addExcludeHandlesToken = (
89+
event:
90+
| React.KeyboardEvent<HTMLInputElement>
91+
| React.FocusEvent<HTMLInputElement>,
92+
) => {
93+
const value = (event.target as HTMLInputElement).value.trim();
94+
95+
if (value.length > 0 && !excludeHandles.some((v) => v.text === value)) {
96+
setExcludeHandles([
97+
...excludeHandles,
98+
{ id: excludeHandles.length, text: value },
7399
]);
74-
updateFilter('filterIncludeHandles', value as UserHandle, true);
100+
updateFilter('filterExcludeHandles', value as UserHandle, true);
75101

76102
(event.target as HTMLInputElement).value = '';
77103
}
78104
};
79105

80-
const [excludeHandles, setExcludeHandles] = useState<InputToken[]>(
81-
mapValuesToTokens(settings.filterExcludeHandles),
82-
);
83-
84106
const removeExcludeHandleToken = (tokenId: string | number) => {
85107
const value = excludeHandles.find((v) => v.id === tokenId)?.text || '';
86108
updateFilter('filterExcludeHandles', value as UserHandle, false);
@@ -91,22 +113,8 @@ export const UserHandleFilter: FC = () => {
91113
const excludeHandlesKeyDown = (
92114
event: React.KeyboardEvent<HTMLInputElement>,
93115
) => {
94-
const value = (event.target as HTMLInputElement).value.trim();
95-
96-
if (
97-
tokenEvents.includes(event.key) &&
98-
!excludeHandles.some((v) => v.text === value) &&
99-
value.length > 0
100-
) {
101-
event.preventDefault();
102-
103-
setExcludeHandles([
104-
...excludeHandles,
105-
{ id: excludeHandles.length, text: value },
106-
]);
107-
updateFilter('filterExcludeHandles', value as UserHandle, true);
108-
109-
(event.target as HTMLInputElement).value = '';
116+
if (tokenEvents.includes(event.key)) {
117+
addExcludeHandlesToken(event);
110118
}
111119
};
112120

@@ -146,6 +154,7 @@ export const UserHandleFilter: FC = () => {
146154
tokens={includeHandles}
147155
onTokenRemove={removeIncludeHandleToken}
148156
onKeyDown={includeHandlesKeyDown}
157+
onBlur={addIncludeHandlesToken}
149158
size="small"
150159
disabled={
151160
!settings.detailedNotifications ||
@@ -172,6 +181,7 @@ export const UserHandleFilter: FC = () => {
172181
tokens={excludeHandles}
173182
onTokenRemove={removeExcludeHandleToken}
174183
onKeyDown={excludeHandlesKeyDown}
184+
onBlur={addExcludeHandlesToken}
175185
size="small"
176186
disabled={
177187
!settings.detailedNotifications ||

0 commit comments

Comments
 (0)