Skip to content

Commit 3ec29b9

Browse files
authored
fix: Bring back validation to custom selector in token menu (#5576)
1 parent 7859ede commit 3ec29b9

File tree

1 file changed

+54
-22
lines changed

1 file changed

+54
-22
lines changed

apps/builder/app/builder/features/style-panel/style-source/style-source-menu.tsx

Lines changed: 54 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
DropdownMenuSeparator,
1010
DropdownMenuTrigger,
1111
Flex,
12+
InputErrorsTooltip,
1213
rawTheme,
1314
styled,
1415
Text,
@@ -18,6 +19,7 @@ import { CheckMarkIcon, ChevronDownIcon, DotIcon } from "@webstudio-is/icons";
1819
import {
1920
pseudoClassDescriptions,
2021
pseudoElementDescriptions,
22+
validateSelector,
2123
} from "@webstudio-is/css-data";
2224
import {
2325
menuCssVars,
@@ -163,31 +165,61 @@ const SelectorCombobox = ({
163165
onSelect: (selector: string) => void;
164166
}) => {
165167
const [value, setValue] = useState("");
168+
const [error, setError] = useState<string>();
169+
170+
const handleSubmit = (selector: string) => {
171+
const validation = validateSelector(selector);
172+
if (validation.success === false) {
173+
setError(validation.error);
174+
return;
175+
}
176+
setError(undefined);
177+
onSelect(selector);
178+
setValue("");
179+
};
180+
181+
const availableItems = allSelectors.filter((selector) =>
182+
existingSelectors.every((s) => s !== selector)
183+
);
166184

167185
return (
168-
<Box onKeyDown={(event) => event.stopPropagation()}>
169-
<Combobox<string>
170-
autoFocus={false}
171-
placeholder="::before"
172-
suffix={<span />}
173-
getItems={() =>
174-
allSelectors.filter((selector) =>
175-
existingSelectors.every((s) => s !== selector)
176-
)
186+
<form
187+
onKeyDown={(event) => event.stopPropagation()}
188+
onSubmit={(event) => {
189+
event.preventDefault();
190+
if (value.trim()) {
191+
handleSubmit(value.trim());
177192
}
178-
value={value}
179-
itemToString={(item) => item ?? ""}
180-
defaultHighlightedIndex={0}
181-
getDescription={getSelectorDescription}
182-
onItemSelect={(item) => {
183-
if (item) {
184-
onSelect(item);
185-
setValue("");
186-
}
187-
}}
188-
onChange={(newValue) => setValue(newValue ?? "")}
189-
/>
190-
</Box>
193+
}}
194+
>
195+
<InputErrorsTooltip
196+
variant="wrapped"
197+
errors={error ? [error] : undefined}
198+
>
199+
<Combobox<string>
200+
autoFocus={false}
201+
placeholder="::before"
202+
suffix={<span />}
203+
color={error ? "error" : undefined}
204+
getItems={() => availableItems}
205+
value={value}
206+
itemToString={(item) => item ?? ""}
207+
defaultHighlightedIndex={0}
208+
getDescription={getSelectorDescription}
209+
onItemSelect={(item) => {
210+
if (item) {
211+
handleSubmit(item);
212+
}
213+
}}
214+
onChange={(newValue) => {
215+
setValue(newValue ?? "");
216+
if (error) {
217+
setError(undefined);
218+
}
219+
}}
220+
/>
221+
</InputErrorsTooltip>
222+
</form>
191223
);
192224
};
193225

0 commit comments

Comments
 (0)