Skip to content

Commit 9e9ef24

Browse files
authored
fix: avoid empty field validatation (#1019)
avoid empty field validations
1 parent e5e9ede commit 9e9ef24

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

.changeset/heavy-coins-greet.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@ensembleui/react-runtime": patch
3+
---
4+
5+
Fix: avoid empty field to validate

packages/runtime/src/widgets/Form/TextInput.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ export const TextInput: React.FC<TextInputProps> = (props) => {
196196
if (regex) {
197197
rulesArray.push({
198198
validator: (_, inputValue?: string) => {
199-
if (new RegExp(regex).test(inputValue || "")) {
199+
if (!inputValue || new RegExp(regex).test(inputValue || "")) {
200200
return Promise.resolve();
201201
}
202202
return Promise.reject(

packages/runtime/src/widgets/Form/__tests__/Form.test.tsx

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,5 +290,63 @@ describe("Form", () => {
290290
expect(screen.getByText("Please enter a value")).toBeInTheDocument();
291291
});
292292
});
293+
294+
test("validate only if field is not empty", async () => {
295+
render(
296+
<Form
297+
children={[
298+
{
299+
name: "TextInput",
300+
properties: {
301+
label: "Regex test",
302+
validator: {
303+
regex: /^\d{5}$/,
304+
regexError: "Please enter a valid 5-digit zip code",
305+
},
306+
},
307+
},
308+
{
309+
name: "Button",
310+
properties: {
311+
label: "Submit",
312+
submitForm: true,
313+
},
314+
},
315+
]}
316+
id="form"
317+
/>,
318+
{
319+
wrapper: FormTestWrapper,
320+
},
321+
);
322+
323+
const validateBtn = screen.getByText("Submit");
324+
fireEvent.click(validateBtn);
325+
326+
await waitFor(() => {
327+
expect(
328+
screen.queryByText("Please enter a valid 5-digit zip code"),
329+
).not.toBeInTheDocument();
330+
});
331+
332+
const input = screen.getByLabelText("Regex test");
333+
fireEvent.change(input, { target: { value: "12" } });
334+
fireEvent.click(validateBtn);
335+
336+
await waitFor(() => {
337+
expect(
338+
screen.queryByText("Please enter a valid 5-digit zip code"),
339+
).toBeInTheDocument();
340+
});
341+
342+
fireEvent.change(input, { target: { value: "" } });
343+
fireEvent.click(validateBtn);
344+
345+
await waitFor(() => {
346+
expect(
347+
screen.queryByText("Please enter a valid 5-digit zip code"),
348+
).not.toBeInTheDocument();
349+
});
350+
});
293351
});
294352
/* eslint-enable react/no-children-prop */

0 commit comments

Comments
 (0)