diff --git a/editor.planx.uk/src/@planx/components/Checklist/Checklist.stories.tsx b/editor.planx.uk/src/@planx/components/Checklist/Checklist.stories.tsx index daafdc024c..bc4d495353 100644 --- a/editor.planx.uk/src/@planx/components/Checklist/Checklist.stories.tsx +++ b/editor.planx.uk/src/@planx/components/Checklist/Checklist.stories.tsx @@ -1,6 +1,6 @@ import { Meta, StoryObj } from "@storybook/react"; -import Checklist from "./Public"; +import Checklist from "./Public/Public"; const meta = { title: "PlanX Components/Checklist", diff --git a/editor.planx.uk/src/@planx/components/Checklist/Editor.tsx b/editor.planx.uk/src/@planx/components/Checklist/Editor.tsx deleted file mode 100644 index 0afc79ade7..0000000000 --- a/editor.planx.uk/src/@planx/components/Checklist/Editor.tsx +++ /dev/null @@ -1,429 +0,0 @@ -import Delete from "@mui/icons-material/Delete"; -import Box from "@mui/material/Box"; -import Button from "@mui/material/Button"; -import IconButton from "@mui/material/IconButton"; -import { ComponentType as TYPES } from "@opensystemslab/planx-core/types"; -import { FormikErrors, FormikValues, useFormik } from "formik"; -import adjust from "ramda/src/adjust"; -import compose from "ramda/src/compose"; -import remove from "ramda/src/remove"; -import React, { useEffect, useRef } from "react"; -import { FormikHookReturn } from "types"; -import ImgInput from "ui/editor/ImgInput/ImgInput"; -import InputGroup from "ui/editor/InputGroup"; -import ListManager from "ui/editor/ListManager/ListManager"; -import { ModalFooter } from "ui/editor/ModalFooter"; -import ModalSection from "ui/editor/ModalSection"; -import ModalSectionContent from "ui/editor/ModalSectionContent"; -import RichTextInput from "ui/editor/RichTextInput/RichTextInput"; -import SimpleMenu from "ui/editor/SimpleMenu"; -import Input from "ui/shared/Input/Input"; -import InputRow from "ui/shared/InputRow"; -import InputRowItem from "ui/shared/InputRowItem"; -import { Switch } from "ui/shared/Switch"; - -import { Option, parseBaseNodeData } from "../shared"; -import { FlagsSelect } from "../shared/FlagsSelect"; -import { ICONS } from "../shared/icons"; -import type { Checklist, Group } from "./model"; -import { toggleExpandableChecklist } from "./model"; -import { ChecklistProps, OptionEditorProps } from "./types"; - -const OptionEditor: React.FC = (props) => { - return ( -
- - {props.value.id ? ( - - ) : null} - - { - props.onChange({ - ...props.value, - data: { - ...props.value.data, - text: ev.target.value, - }, - }); - }} - placeholder="Option" - /> - - - { - props.onChange({ - ...props.value, - data: { - ...props.value.data, - img, - }, - }); - }} - /> - - {typeof props.index !== "undefined" && - props.groups && - props.onMoveToGroup && ( - ({ - label: `Move to ${group || `group ${groupIndex}`}`, - onClick: () => { - props.onMoveToGroup && - typeof props.index === "number" && - props.onMoveToGroup(props.index, groupIndex); - }, - disabled: groupIndex === props.groupIndex, - }))} - /> - )} - - - {props.showValueField && ( - - { - props.onChange({ - ...props.value, - data: { - ...props.value.data, - val: ev.target.value, - }, - }); - }} - /> - - )} - - { - props.onChange({ - ...props.value, - data: { - ...props.value.data, - flag: ev, - }, - }); - }} - /> -
- ); -}; - -const Options: React.FC<{ formik: FormikHookReturn }> = ({ formik }) => { - return ( - - {formik.values.groupedOptions ? ( - - {formik.values.groupedOptions.map( - (groupedOption: Group - ) : ( - { - formik.setFieldValue("options", newOptions); - }} - newValueLabel="add new option" - newValue={() => - ({ - data: { - text: "", - description: "", - val: "", - }, - }) as Option - } - Editor={OptionEditor} - editorExtraProps={{ showValueField: !!formik.values.fn }} - /> - )} - - ); -}; - -export const ChecklistComponent: React.FC = (props) => { - const type = TYPES.Checklist; - - const formik = useFormik({ - initialValues: { - allRequired: props.node?.data?.allRequired || false, - neverAutoAnswer: props.node?.data?.neverAutoAnswer || false, - description: props.node?.data?.description || "", - fn: props.node?.data?.fn || "", - groupedOptions: props.groupedOptions, - img: props.node?.data?.img || "", - options: props.options, - text: props.node?.data?.text || "", - ...parseBaseNodeData(props.node?.data), - }, - onSubmit: ({ options, groupedOptions, ...values }) => { - const sourceOptions = options?.length - ? options - : groupedOptions?.flatMap((group) => group.children); - - const filteredOptions = (sourceOptions || []).filter( - (option) => option.data.text, - ); - - const processedOptions = filteredOptions.map((option) => ({ - ...option, - id: option.id || undefined, - type: TYPES.Answer, - })); - - if (props.handleSubmit) { - props.handleSubmit( - { - type, - data: { - ...values, - ...(groupedOptions - ? { - categories: groupedOptions.map((group) => ({ - title: group.title, - count: group.children.length, - })), - } - : { - categories: undefined, - }), - }, - }, - processedOptions, - ); - } else { - alert(JSON.stringify({ type, ...values, options }, null, 2)); - } - }, - validate: ({ options, ...values }) => { - const errors: FormikErrors = {}; - if (values.fn && !options?.some((option) => option.data.val)) { - errors.fn = - "At least one option must set a data value when the checklist has a data field"; - } - return errors; - }, - }); - - const focusRef = useRef(null); - - // horrible hack to remove focus from Rich Text Editor - useEffect(() => { - setTimeout(() => { - (document.activeElement as any).blur(); - focusRef.current?.focus(); - }, 50); - }, []); - - return ( -