Skip to content

Commit

Permalink
Move logics to useElement.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
epi-qang2 committed Dec 28, 2023
1 parent 1a47db5 commit 186358b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import React, { useMemo } from "react";
import { FormContainer, ResetButton } from "@episerver/forms-sdk";
import { ResetButton } from "@episerver/forms-sdk";
import { useElement } from "../../hooks/useElement";
import { useForms } from "../../context/store";
import { htmlDecodeEntities } from "@episerver/forms-sdk/src/helpers/dependencyHelper";

interface ResetButtonElementBlockProps {
element: ResetButton
Expand All @@ -13,25 +11,14 @@ export const ResetButtonElementBlock = (props: ResetButtonElementBlockProps) =>
const { elementContext, handleReset } = useElement(element);
const { extraAttr } = elementContext;

const formContext = useForms();
const form = formContext?.formContainer ?? {} as FormContainer;



const handleResetButton = () => {
if (confirm(htmlDecodeEntities(form.properties.resetConfirmationMessage))) {
handleReset()
}
}

return useMemo(() => (
<>
<input
type="reset"
className="Form__Element FormResetButton Form__Element--NonData"
{...extraAttr}
value={element.properties.label}
onClick={handleResetButton}
onClick={handleReset}
/>
</>
), []);
Expand Down
6 changes: 5 additions & 1 deletion src/@episerver/forms-react/src/hooks/useElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
FormSubmission,
FormDependConditions,
FormValidationResult,
htmlDecodeEntities,
} from "@episerver/forms-sdk";
import { DispatchFunctions } from "../context/dispatchFunctions";

Expand Down Expand Up @@ -156,7 +157,10 @@ export const useElement = (element: FormElementBase) => {
}

const handleReset = () => {
dispatchFuncs.resetForm(formContext?.formContainer ?? {} as FormContainer);
const form = formContext?.formContainer ?? {} as FormContainer
if (confirm(htmlDecodeEntities(form.properties.resetConfirmationMessage))) {
dispatchFuncs.resetForm(form);
}
}

return {
Expand Down

0 comments on commit 186358b

Please sign in to comment.