From d133810650a3916a2b6fa1abd11ff515966e60f2 Mon Sep 17 00:00:00 2001 From: Chris Villa Date: Mon, 9 Oct 2023 18:16:02 +0100 Subject: [PATCH 1/9] refactor: split InputOrGroup into smaller files --- .../InputOrGroup/fields/ArrayField/index.tsx | 90 ++++++ .../fields/DefaultField/index.tsx | 42 +++ .../fields/ExternalField/index.tsx | 28 ++ .../InputOrGroup/fields/RadioField/index.tsx | 63 +++++ .../InputOrGroup/fields/SelectField/index.tsx | 52 ++++ .../fields/TextareaField/index.tsx | 34 +++ .../components/InputOrGroup/fields/index.tsx | 6 + .../core/components/InputOrGroup/index.tsx | 256 ++---------------- 8 files changed, 334 insertions(+), 237 deletions(-) create mode 100644 packages/core/components/InputOrGroup/fields/ArrayField/index.tsx create mode 100644 packages/core/components/InputOrGroup/fields/DefaultField/index.tsx create mode 100644 packages/core/components/InputOrGroup/fields/ExternalField/index.tsx create mode 100644 packages/core/components/InputOrGroup/fields/RadioField/index.tsx create mode 100644 packages/core/components/InputOrGroup/fields/SelectField/index.tsx create mode 100644 packages/core/components/InputOrGroup/fields/TextareaField/index.tsx create mode 100644 packages/core/components/InputOrGroup/fields/index.tsx diff --git a/packages/core/components/InputOrGroup/fields/ArrayField/index.tsx b/packages/core/components/InputOrGroup/fields/ArrayField/index.tsx new file mode 100644 index 000000000..7c713496a --- /dev/null +++ b/packages/core/components/InputOrGroup/fields/ArrayField/index.tsx @@ -0,0 +1,90 @@ +import getClassNameFactory from "../../../../lib/get-class-name-factory"; +import styles from "../../styles.module.css"; +import { List, Trash } from "react-feather"; +import { InputOrGroup, type InputProps } from "../.."; +import { IconButton } from "../../../IconButton"; +import { replace } from "../../../../lib"; + +const getClassName = getClassNameFactory("Input", styles); + +export const ArrayField = ({ + field, + onChange, + value, + name, + label, +}: InputProps) => { + if (!field.arrayFields) { + return null; + } + + return ( +
+ +
+ +
+ {label || name} +
+
+ {Array.isArray(value) ? ( + value.map((item, i) => ( +
+ + {field.getItemSummary + ? field.getItemSummary(item, i) + : `Item #${i}`} + +
+ { + const existingValue = value || []; + + existingValue.splice(i, 1); + onChange(existingValue); + }} + title="Delete" + > + + +
+
+
+ {Object.keys(field.arrayFields!).map((fieldName) => { + const subField = field.arrayFields![fieldName]; + + return ( + + onChange( + replace(value, i, { ...item, [fieldName]: val }) + ) + } + /> + ); + })} +
+
+ )) + ) : ( +
+ )} + + +
+
+ ); +}; diff --git a/packages/core/components/InputOrGroup/fields/DefaultField/index.tsx b/packages/core/components/InputOrGroup/fields/DefaultField/index.tsx new file mode 100644 index 000000000..dfb9f2c90 --- /dev/null +++ b/packages/core/components/InputOrGroup/fields/DefaultField/index.tsx @@ -0,0 +1,42 @@ +import getClassNameFactory from "../../../../lib/get-class-name-factory"; +import styles from "../../styles.module.css"; +import { Hash, Type } from "react-feather"; +import type { InputProps } from "../.."; + +const getClassName = getClassNameFactory("Input", styles); + +export const DefaultField = ({ + field, + onChange, + readOnly, + value, + name, + label, +}: InputProps) => { + return ( + + ); +}; diff --git a/packages/core/components/InputOrGroup/fields/ExternalField/index.tsx b/packages/core/components/InputOrGroup/fields/ExternalField/index.tsx new file mode 100644 index 000000000..672d6e63e --- /dev/null +++ b/packages/core/components/InputOrGroup/fields/ExternalField/index.tsx @@ -0,0 +1,28 @@ +import getClassNameFactory from "../../../../lib/get-class-name-factory"; +import styles from "../../styles.module.css"; +import type { InputProps } from "../.."; +import { ExternalInput } from "../../../ExternalInput"; + +const getClassName = getClassNameFactory("Input", styles); + +export const ExternalField = ({ + field, + onChange, + readOnly, + value, + name, + label, +}: InputProps) => { + if (!field.adaptor) { + return null; + } + + return ( +
+
+ {name === "_data" ? "External content" : label || name} +
+ +
+ ); +}; diff --git a/packages/core/components/InputOrGroup/fields/RadioField/index.tsx b/packages/core/components/InputOrGroup/fields/RadioField/index.tsx new file mode 100644 index 000000000..3dc630b56 --- /dev/null +++ b/packages/core/components/InputOrGroup/fields/RadioField/index.tsx @@ -0,0 +1,63 @@ +import getClassNameFactory from "../../../../lib/get-class-name-factory"; +import styles from "../../styles.module.css"; +import { CheckCircle } from "react-feather"; +import type { InputProps } from "../.."; + +const getClassName = getClassNameFactory("Input", styles); + +export const RadioField = ({ + field, + onChange, + readOnly, + value, + name, +}: InputProps) => { + if (!field.options) { + return null; + } + + return ( +
+
+
+
+ +
+ {field.label || name} +
+ +
+ {field.options.map((option) => ( + + ))} +
+
+
+ ); +}; diff --git a/packages/core/components/InputOrGroup/fields/SelectField/index.tsx b/packages/core/components/InputOrGroup/fields/SelectField/index.tsx new file mode 100644 index 000000000..f98ef53ca --- /dev/null +++ b/packages/core/components/InputOrGroup/fields/SelectField/index.tsx @@ -0,0 +1,52 @@ +import getClassNameFactory from "../../../../lib/get-class-name-factory"; +import styles from "../../styles.module.css"; +import { ChevronDown } from "react-feather"; +import type { InputProps } from "../.."; + +const getClassName = getClassNameFactory("Input", styles); + +export const SelectField = ({ + field, + onChange, + label, + value, + name, +}: InputProps) => { + if (!field.options) { + return null; + } + + return ( + + ); +}; diff --git a/packages/core/components/InputOrGroup/fields/TextareaField/index.tsx b/packages/core/components/InputOrGroup/fields/TextareaField/index.tsx new file mode 100644 index 000000000..8fae006c7 --- /dev/null +++ b/packages/core/components/InputOrGroup/fields/TextareaField/index.tsx @@ -0,0 +1,34 @@ +import getClassNameFactory from "../../../../lib/get-class-name-factory"; +import styles from "../../styles.module.css"; +import { Type } from "react-feather"; +import type { InputProps } from "../.."; + +const getClassName = getClassNameFactory("Input", styles); + +export const TextareaField = ({ + onChange, + readOnly, + value, + name, + label, +}: InputProps) => { + return ( +