From 1e4b69d7f9596f3dd63ecdf7ed6ad0de7fcfaca2 Mon Sep 17 00:00:00 2001 From: Matthias Nannt Date: Tue, 15 Nov 2022 20:17:17 +0100 Subject: [PATCH] add simple validation to react lib, minLength, maxLength --- .changeset/perfect-walls-switch.md | 5 -- packages/react/package.json | 2 +- packages/react/src/components/Formbricks.tsx | 69 +++++++++++++++---- packages/react/src/components/inputs/Text.tsx | 24 +++++-- .../react/src/components/inputs/Textarea.tsx | 24 +++++-- packages/react/src/lib/validation.ts | 12 ++++ packages/tailwind-config/package.json | 4 +- 7 files changed, 104 insertions(+), 36 deletions(-) delete mode 100644 .changeset/perfect-walls-switch.md create mode 100644 packages/react/src/lib/validation.ts diff --git a/.changeset/perfect-walls-switch.md b/.changeset/perfect-walls-switch.md deleted file mode 100644 index 0ac8940d6c..0000000000 --- a/.changeset/perfect-walls-switch.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -"@formbricks/react": patch ---- - -provide basic form functionality with text, textarea and submit types diff --git a/packages/react/package.json b/packages/react/package.json index a0e9776d75..5e07ca6b91 100644 --- a/packages/react/package.json +++ b/packages/react/package.json @@ -1,6 +1,6 @@ { "name": "@formbricks/react", - "version": "0.0.0", + "version": "0.0.1", "author": "Formbricks ", "description": "Building React forms has never been quicker.", "homepage": "https://formbricks.com", diff --git a/packages/react/src/components/Formbricks.tsx b/packages/react/src/components/Formbricks.tsx index f6b77abab6..40a34b0203 100644 --- a/packages/react/src/components/Formbricks.tsx +++ b/packages/react/src/components/Formbricks.tsx @@ -1,17 +1,13 @@ import React, { useContext, useEffect, useMemo } from "react"; import { generateId } from "../lib/utils"; +import { getValidationRules } from "../lib/validation"; import { SchemaContext } from "./Form"; -import { Text } from "./inputs/Text"; +import { Text, TextInputUniqueProps } from "./inputs/Text"; import { Textarea } from "./inputs/Textarea"; import { Help } from "./shared/Help"; -interface BasicTypeProps { - id?: string; - name: string; - label?: string; - placeholder?: string; +interface FormbricksInputProps { type: "text" | "textarea"; - help?: string; } interface SubmitTypeProps { @@ -21,20 +17,51 @@ interface SubmitTypeProps { placeholder?: string; type: "submit"; help?: string; + validation?: string; } -type FormbricksProps = BasicTypeProps | SubmitTypeProps; +export interface UniversalInputProps { + id?: string; + help?: string; + name: string; + label?: string; + elemId: string; + validation: string[]; +} -export function Formbricks({ id, name, label, placeholder, help, type }: FormbricksProps) { +type FormbricksProps = FormbricksInputProps & UniversalInputProps & SubmitTypeProps & TextInputUniqueProps; + +export function Formbricks({ + id, + name, + label, + placeholder, + help, + type, + validation, + minLength, + maxLength, +}: FormbricksProps) { const elemId = useMemo(() => (typeof id !== "undefined" ? id : `${name}=${generateId(3)}`), [id]); - const { schema, setSchema } = useContext(SchemaContext); + const { setSchema } = useContext(SchemaContext); + const validationRules = getValidationRules(validation); useEffect(() => { setSchema((schema: any) => { const newSchema = JSON.parse(JSON.stringify(schema)); let elementIdx = newSchema.findIndex((e: any) => e.name === name); if (elementIdx === -1) { - newSchema.push({ name, type, label, help }); + newSchema.push({ + id, + name, + label, + placeholder, + help, + type, + validation, + minLength, + maxLength, + }); elementIdx = newSchema.length - 1; } /* if (["checkbox", "radio"].includes(type)) { @@ -48,9 +75,25 @@ export function Formbricks({ id, name, label, placeholder, help, type }: Formbri
{type === "text" ? ( - + ) : type === "textarea" ? ( -