From 2a23326dad7c1ae6c27b61eb63afbe18d0eb91e7 Mon Sep 17 00:00:00 2001 From: Matthias Nannt Date: Wed, 23 Nov 2022 10:10:52 +0100 Subject: [PATCH] refactor radio buttons, add checkbox input type to react lib --- packages/react/src/components/Inputs.tsx | 1 + .../react/src/components/inputs/Button.tsx | 1 - .../react/src/components/inputs/Checkbox.tsx | 90 +++++++++++++++++++ .../react/src/components/inputs/Radio.tsx | 27 +++--- .../react/src/components/shared/Fieldset.tsx | 16 ++++ .../react/src/components/shared/Legend.tsx | 11 +++ .../react/src/components/shared/Option.tsx | 11 +++ .../react/src/components/shared/Options.tsx | 11 +++ .../react/src/components/shared/Outer.tsx | 6 -- packages/tsconfig/react-library.json | 4 +- 10 files changed, 159 insertions(+), 19 deletions(-) create mode 100644 packages/react/src/components/inputs/Checkbox.tsx create mode 100644 packages/react/src/components/shared/Fieldset.tsx create mode 100644 packages/react/src/components/shared/Legend.tsx create mode 100644 packages/react/src/components/shared/Option.tsx create mode 100644 packages/react/src/components/shared/Options.tsx diff --git a/packages/react/src/components/Inputs.tsx b/packages/react/src/components/Inputs.tsx index 34b657ea49..760e166d09 100644 --- a/packages/react/src/components/Inputs.tsx +++ b/packages/react/src/components/Inputs.tsx @@ -1,4 +1,5 @@ export * from "./inputs/Button"; +export * from "./inputs/Checkbox"; export * from "./inputs/Radio"; export * from "./inputs/Submit"; export * from "./inputs/Text"; diff --git a/packages/react/src/components/inputs/Button.tsx b/packages/react/src/components/inputs/Button.tsx index 00cb7b90e7..b46cf428bf 100644 --- a/packages/react/src/components/inputs/Button.tsx +++ b/packages/react/src/components/inputs/Button.tsx @@ -1,4 +1,3 @@ -import clsx from "clsx"; import React, { useMemo } from "react"; import { getElementId } from "../../lib/element"; import { useEffectUpdateSchema } from "../../lib/schema"; diff --git a/packages/react/src/components/inputs/Checkbox.tsx b/packages/react/src/components/inputs/Checkbox.tsx new file mode 100644 index 0000000000..e93415b2f3 --- /dev/null +++ b/packages/react/src/components/inputs/Checkbox.tsx @@ -0,0 +1,90 @@ +import clsx from "clsx"; +import React, { useMemo } from "react"; +import { useFormContext } from "react-hook-form"; +import { getElementId } from "../../lib/element"; +import { normalizeOptions } from "../../lib/options"; +import { useEffectUpdateSchema } from "../../lib/schema"; +import { getValidationRules } from "../../lib/validation"; +import { NameRequired, OptionsArray, OptionsObjectArray, UniversalInputProps } from "../../types"; +import { Fieldset } from "../shared/Fieldset"; +import { Help } from "../shared/Help"; +import { Inner } from "../shared/Inner"; +import { Label } from "../shared/Label"; +import { Legend } from "../shared/Legend"; +import { Messages } from "../shared/Messages"; +import { Option } from "../shared/Option"; +import { Options } from "../shared/Options"; +import { Outer } from "../shared/Outer"; +import { Wrapper } from "../shared/Wrapper"; + +interface CheckboxInputUniqueProps { + options?: OptionsArray | OptionsObjectArray; + fieldsetClassName?: string; + legendClassName?: string; + optionsClassName?: string; + optionClassName?: string; +} + +type FormbricksProps = CheckboxInputUniqueProps & UniversalInputProps & NameRequired; + +const inputType = "checkbox"; + +export function Checkbox(props: FormbricksProps) { + const elemId = useMemo(() => getElementId(props.id, props.name), [props.id, props.name]); + const options = useMemo(() => normalizeOptions(props.options), [props.options]); + useEffectUpdateSchema(props, inputType); + + const { register } = useFormContext(); + const validationRules = getValidationRules(props.validation); + + if (!options || options.length === 0) { + return ( + + + + + + + + + + ); + } + + return ( + +
+ {props.label} + + + {options.map((option) => ( + + ))} + +
+ +
+ ); +} diff --git a/packages/react/src/components/inputs/Radio.tsx b/packages/react/src/components/inputs/Radio.tsx index 1903c445a6..5d9e0d3405 100644 --- a/packages/react/src/components/inputs/Radio.tsx +++ b/packages/react/src/components/inputs/Radio.tsx @@ -6,15 +6,23 @@ import { normalizeOptions } from "../../lib/options"; import { useEffectUpdateSchema } from "../../lib/schema"; import { getValidationRules } from "../../lib/validation"; import { NameRequired, OptionsArray, OptionsObjectArray, UniversalInputProps } from "../../types"; +import { Fieldset } from "../shared/Fieldset"; import { Help } from "../shared/Help"; import { Inner } from "../shared/Inner"; import { Label } from "../shared/Label"; +import { Legend } from "../shared/Legend"; import { Messages } from "../shared/Messages"; +import { Option } from "../shared/Option"; +import { Options } from "../shared/Options"; import { Outer } from "../shared/Outer"; import { Wrapper } from "../shared/Wrapper"; interface RadioInputUniqueProps { options?: OptionsArray | OptionsObjectArray; + fieldsetClassName?: string; + legendClassName?: string; + optionsClassName?: string; + optionClassName?: string; } type FormbricksProps = RadioInputUniqueProps & UniversalInputProps & NameRequired; @@ -26,10 +34,7 @@ export function Radio(props: FormbricksProps) { const options = useMemo(() => normalizeOptions(props.options), [props.options]); useEffectUpdateSchema(props, inputType); - const { - register, - formState: { errors }, - } = useFormContext(); + const { register } = useFormContext(); const validationRules = getValidationRules(props.validation); if (!options || options.length === 0) { @@ -56,12 +61,12 @@ export function Radio(props: FormbricksProps) { return ( -
- {props.label} +
+ {props.label} -
+ {options.map((option) => ( -
+
+ ))} -
-
+ +
); diff --git a/packages/react/src/components/shared/Fieldset.tsx b/packages/react/src/components/shared/Fieldset.tsx new file mode 100644 index 0000000000..43c9e14ff3 --- /dev/null +++ b/packages/react/src/components/shared/Fieldset.tsx @@ -0,0 +1,16 @@ +import clsx from "clsx"; +import React from "react"; + +interface FieldsetProps { + name: string; + fieldsetClassName?: string; + children: React.ReactNode; +} + +export function Fieldset({ name, fieldsetClassName, children }: FieldsetProps) { + return ( +
+ {children} +
+ ); +} diff --git a/packages/react/src/components/shared/Legend.tsx b/packages/react/src/components/shared/Legend.tsx new file mode 100644 index 0000000000..35ebe11edd --- /dev/null +++ b/packages/react/src/components/shared/Legend.tsx @@ -0,0 +1,11 @@ +import clsx from "clsx"; +import React from "react"; + +interface LegendProps { + legendClassName?: string; + children: React.ReactNode; +} + +export function Legend({ legendClassName, children }: LegendProps) { + return {children}; +} diff --git a/packages/react/src/components/shared/Option.tsx b/packages/react/src/components/shared/Option.tsx new file mode 100644 index 0000000000..928080bcf3 --- /dev/null +++ b/packages/react/src/components/shared/Option.tsx @@ -0,0 +1,11 @@ +import clsx from "clsx"; +import React from "react"; + +interface OptionProps { + optionClassName?: string; + children: React.ReactNode; +} + +export function Option({ optionClassName, children }: OptionProps) { + return
{children}
; +} diff --git a/packages/react/src/components/shared/Options.tsx b/packages/react/src/components/shared/Options.tsx new file mode 100644 index 0000000000..5ca887d908 --- /dev/null +++ b/packages/react/src/components/shared/Options.tsx @@ -0,0 +1,11 @@ +import clsx from "clsx"; +import React from "react"; + +interface OptionsProps { + optionsClassName?: string; + children: React.ReactNode; +} + +export function Options({ optionsClassName, children }: OptionsProps) { + return
{children}
; +} diff --git a/packages/react/src/components/shared/Outer.tsx b/packages/react/src/components/shared/Outer.tsx index ba6b3e7f46..02507942ad 100644 --- a/packages/react/src/components/shared/Outer.tsx +++ b/packages/react/src/components/shared/Outer.tsx @@ -1,8 +1,5 @@ import clsx from "clsx"; import React from "react"; -import { FieldErrorsImpl, useFormContext } from "react-hook-form"; -import { Help } from "./Help"; -import { Messages } from "./Messages"; interface OuterProps { inputType: string; @@ -11,9 +8,6 @@ interface OuterProps { } export function Outer({ inputType, outerClassName, children }: OuterProps) { - const { - formState: { errors }, - } = useFormContext(); return (
{children} diff --git a/packages/tsconfig/react-library.json b/packages/tsconfig/react-library.json index a30a5f7bb0..1b2ad99e1d 100644 --- a/packages/tsconfig/react-library.json +++ b/packages/tsconfig/react-library.json @@ -6,6 +6,8 @@ "lib": ["ES2015", "DOM"], "module": "ESNext", "target": "ES6", - "jsx": "react" + "jsx": "react", + "noUnusedLocals": true, + "noUnusedParameters": true } }