-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
324 additions
and
284 deletions.
There are no files selected for viewing
173 changes: 173 additions & 0 deletions
173
plugins/lime-plugin-mesh-wide-config/src/components/FormEdit.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
import { Trans, t } from "@lingui/macro"; | ||
import { StateUpdater, useEffect, useState } from "preact/hooks"; | ||
import { Controller, useFormContext } from "react-hook-form"; | ||
import { v4 as uuidv4 } from "uuid"; | ||
|
||
import { useDisclosure } from "components/Modal/useDisclosure"; | ||
import { Button, ButtonProps } from "components/buttons/button"; | ||
import InputField from "components/inputs/InputField"; | ||
import { useToast } from "components/toast/toastProvider"; | ||
|
||
import { EditOrDelete } from "plugins/lime-plugin-mesh-wide-config/src/components/Components"; | ||
import { | ||
AddNewSectionFormProps, | ||
AddNewSectionModal, | ||
} from "plugins/lime-plugin-mesh-wide-config/src/components/modals"; | ||
import { IMeshWideConfig } from "plugins/lime-plugin-mesh-wide-config/src/meshConfigTypes"; | ||
|
||
export const EditableField = ({ | ||
isList, | ||
name, | ||
setIsEditing, | ||
}: { | ||
isList: boolean; | ||
name: string; | ||
setIsEditing?: StateUpdater<boolean>; | ||
}) => { | ||
const { control, setValue, watch, getValues } = useFormContext(); | ||
|
||
const value = watch(name); | ||
const [initialState] = useState(value); | ||
// Hack to force re-render when the list changes | ||
const [uniqueKeys, setUniqueKeys] = useState( | ||
isList ? value.map(() => uuidv4()) : "" | ||
); | ||
|
||
const removeListItem = (index) => { | ||
const updatedValues = value.filter((_, i) => i !== index); | ||
setValue(name, updatedValues); // Update form values | ||
setUniqueKeys((keys) => keys.filter((_, i) => i !== index)); // Update keys to match the new array | ||
}; | ||
|
||
const addListItem = () => { | ||
setValue(name, [...value, ""]); // Update form values | ||
setUniqueKeys((keys) => [...keys, uuidv4()]); // Add a new unique key | ||
}; | ||
|
||
// Ensure the list has at least one item at the start | ||
useEffect(() => { | ||
if (isList && value.length === 0) { | ||
setValue(name, [""]); | ||
setUniqueKeys([uuidv4()]); // Reset keys for new list | ||
} | ||
}, [isList, value, name, setValue]); | ||
|
||
if (isList) { | ||
return ( | ||
<div key={name} className={"flex flex-col gap-6"}> | ||
{uniqueKeys.map((item, index) => ( | ||
<Controller | ||
key={uniqueKeys[index]} // Use the unique key | ||
control={control} | ||
name={`${name}[${index}]`} | ||
rules={{ | ||
minLength: { | ||
value: 1, | ||
message: t`Minimum length is 1`, | ||
}, | ||
required: t`This field cannot be empty`, | ||
}} | ||
render={({ | ||
field: { value, ...rest }, | ||
fieldState: { error }, | ||
}) => { | ||
return ( | ||
<div | ||
className={ | ||
"flex flex-row justify-center align-items-center gap-4" | ||
} | ||
> | ||
<InputField | ||
id={`${name}[${index}]`} | ||
className="w-100" | ||
value={value} | ||
error={error?.message} | ||
{...rest} | ||
/> | ||
<EditOrDelete | ||
onDelete={() => removeListItem(index)} | ||
/> | ||
</div> | ||
); | ||
}} | ||
/> | ||
))} | ||
<AddElementButton onClick={addListItem} /> | ||
</div> | ||
); | ||
} | ||
|
||
return ( | ||
<Controller | ||
name={name} | ||
control={control} | ||
rules={{ | ||
minLength: { | ||
value: 1, | ||
message: t`Minimum length is 1`, | ||
}, | ||
required: t`This field cannot be empty`, | ||
}} | ||
render={({ field, fieldState: { error } }) => ( | ||
<InputField | ||
id={name} | ||
label={<Trans>Value</Trans>} | ||
className="w-100" | ||
error={error?.message} | ||
{...field} | ||
/> | ||
)} | ||
/> | ||
); | ||
}; | ||
|
||
export const AddNewConfigSection = ({ | ||
sectionName, | ||
}: { | ||
sectionName?: string; | ||
}) => { | ||
const { open, onOpen, onClose } = useDisclosure(); | ||
const { showToast } = useToast(); | ||
|
||
const { watch, setValue } = useFormContext<IMeshWideConfig>(); | ||
const section = watch(sectionName); | ||
|
||
const onSuccess = (data: AddNewSectionFormProps) => { | ||
if (!sectionName) { | ||
setValue(data.name, {}); | ||
} else { | ||
let value: string | string[] = data.value; | ||
if (data.isList) { | ||
value = data.values; | ||
} | ||
setValue(sectionName, { | ||
...section, | ||
[data.name]: value, | ||
}); | ||
} | ||
onClose(); | ||
showToast({ | ||
text: <Trans>Added section {data.name}</Trans>, | ||
}); | ||
}; | ||
|
||
return ( | ||
<> | ||
<AddElementButton onClick={onOpen} /> | ||
<AddNewSectionModal | ||
sectionName={sectionName} | ||
isOpen={open} | ||
onSuccess={onSuccess} | ||
onClose={onClose} | ||
/> | ||
</> | ||
); | ||
}; | ||
|
||
export const AddElementButton = (props: ButtonProps) => { | ||
return ( | ||
<Button color={"info"} {...props}> | ||
<Trans>Add new section</Trans> | ||
</Button> | ||
); | ||
}; |
124 changes: 124 additions & 0 deletions
124
plugins/lime-plugin-mesh-wide-config/src/components/FormOption.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import { Trans } from "@lingui/macro"; | ||
import { useState } from "preact/hooks"; | ||
import { FormProvider, useForm, useFormContext } from "react-hook-form"; | ||
|
||
import { useDisclosure } from "components/Modal/useDisclosure"; | ||
import { Button } from "components/buttons/button"; | ||
import Divider from "components/divider"; | ||
import { useToast } from "components/toast/toastProvider"; | ||
|
||
import { EditOrDelete } from "plugins/lime-plugin-mesh-wide-config/src/components/Components"; | ||
import { EditableField } from "plugins/lime-plugin-mesh-wide-config/src/components/FormEdit"; | ||
import { DeletePropModal } from "plugins/lime-plugin-mesh-wide-config/src/components/modals"; | ||
import { IMeshWideConfig } from "plugins/lime-plugin-mesh-wide-config/src/meshConfigTypes"; | ||
|
||
type OptionContainerProps = { | ||
sectionName: string; | ||
keyString: string; | ||
}; | ||
|
||
export const OptionContainer = ({ | ||
keyString, | ||
sectionName, | ||
}: OptionContainerProps) => { | ||
const { watch, setValue } = useFormContext(); | ||
const [isEditing, setIsEditing] = useState(false); | ||
const { | ||
open: isDeleteModalOpen, | ||
onOpen: openDeleteModal, | ||
onClose: onCloseDeleteModal, | ||
} = useDisclosure(); | ||
|
||
const { showToast } = useToast(); | ||
|
||
const onDelete = async () => { | ||
const newValues = { ...section }; | ||
delete newValues[keyString]; | ||
setValue(sectionName, newValues); | ||
onCloseDeleteModal(); | ||
showToast({ | ||
text: <Trans>Deleted {keyString}</Trans>, | ||
onAction: () => { | ||
setValue(sectionName, section); | ||
}, | ||
}); | ||
}; | ||
|
||
const name = `${sectionName}[${keyString}]`; | ||
const value = watch(name); | ||
const section = watch(sectionName); | ||
|
||
let _value = value; | ||
const isList = Array.isArray(value); | ||
if (isList) { | ||
_value = value.join(", "); | ||
} | ||
|
||
const fmethods = useForm<IMeshWideConfig>({ | ||
defaultValues: { [sectionName]: { [keyString]: value } }, | ||
}); | ||
|
||
const onSubmit = (data: IMeshWideConfig) => { | ||
const newSectionValues = { ...section, ...data[sectionName] }; | ||
setValue(sectionName, newSectionValues); | ||
setIsEditing(false); | ||
}; | ||
|
||
return ( | ||
<div class={"px-4"}> | ||
<div className={"flex justify-center"}> | ||
<Divider color={"white"} /> | ||
</div> | ||
<div className="pl-6 pr-4 py-6 flex flex-col gap-4"> | ||
<div className={"flex flex-row items-center justify-between"}> | ||
<div> | ||
{isList && <Trans>(List)</Trans>} {keyString} | ||
</div> | ||
<EditOrDelete | ||
onEdit={() => setIsEditing((prev) => !prev)} | ||
onDelete={openDeleteModal} | ||
/> | ||
</div> | ||
{!isEditing && <div>{_value}</div>} | ||
{isEditing && ( | ||
<FormProvider {...fmethods}> | ||
<form> | ||
<EditableField | ||
isList={isList} | ||
name={name} | ||
setIsEditing={setIsEditing} | ||
/> | ||
<div className={"flex flex-row gap-4"}> | ||
<Button | ||
onClick={(e) => { | ||
e.preventDefault(); | ||
fmethods.handleSubmit(onSubmit)(); | ||
}} | ||
outline={true} | ||
> | ||
<Trans>Done</Trans> | ||
</Button> | ||
<Button | ||
color={"danger"} | ||
onClick={() => { | ||
fmethods.reset(); | ||
setIsEditing(false); | ||
}} | ||
outline={true} | ||
> | ||
<Trans>Cancel</Trans> | ||
</Button> | ||
</div> | ||
</form> | ||
</FormProvider> | ||
)} | ||
</div> | ||
<DeletePropModal | ||
prop={keyString} | ||
isOpen={isDeleteModalOpen} | ||
onDelete={onDelete} | ||
onClose={onCloseDeleteModal} | ||
/> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.