Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions demo/FormExamples.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ export function FormExamples() {
});
}}
transform={(formData, _, update) => {
console.log('transform >> formData:', formData);

formData.__v = formData.__v ? formData.__v + 1 : 1;

update(formData);
Expand All @@ -141,14 +143,17 @@ export function FormExamples() {
validations={validations}
customActions={
useCustomActions
? (isSubmiting, { onCancel, resetForm }) => (
? (isSubmiting, { onCancel, resetForm, clearForm }) => (
<div>
<button type="submit" className="btn btn-success">
<button type="submit" className="btn btn-success me-2">
Custom save
</button>
<button type="button" className="btn btn-secondary" onClick={() => resetForm()}>
<button type="button" className="btn btn-secondary me-2" onClick={() => resetForm()}>
Custom reset
</button>
<button type="button" className="btn btn-info" onClick={() => clearForm()}>
Custom clear
</button>
</div>
)
: undefined
Expand Down Expand Up @@ -757,6 +762,7 @@ export function FormExamples() {
/>

<ResetForm />
<ClearForm />
</Form>
);
}
Expand All @@ -765,7 +771,7 @@ const ResetForm = () => {
const formControl = useFormControl();

const reset = () => {
console.log('reset form');
console.log('reset form - resets to initial state');

formControl.resetFormData();
};
Expand All @@ -777,6 +783,22 @@ const ResetForm = () => {
);
};

const ClearForm = () => {
const formControl = useFormControl();

const clear = () => {
console.log('clear form - sets empty state');

formControl.clearFormData();
};

return (
<button type="button" className="btn btn-outline-info ms-2 mb-3" onClick={clear}>
Clear form
</button>
);
};

const FormSwitchExample = () => {
const autocompleteField1FormControl = useFormControl('autocompleteField1');

Expand Down
21 changes: 20 additions & 1 deletion src/forms/Form.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export function Form({
const formRef = useRef(null);
const [isSubmiting, setIsSubmiting] = useState(false);

/* Resets to initial values/state */
function resetForm() {
if (formRef.current && formRef.current.classList) {
formRef.current.classList.remove('was-validated');
Expand All @@ -30,6 +31,14 @@ export function Form({
formState.reset();
}

function clearForm() {

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

repliquei lógica do reset

if (formRef.current && formRef.current.classList) {
formRef.current.classList.remove('was-validated');
}

formState.clear();
}

function handleSubmit(e) {
e.preventDefault();
e.stopPropagation();
Expand Down Expand Up @@ -76,7 +85,17 @@ export function Form({
<form {...formProps}>
<FormContext.Provider value={formState}>{children}</FormContext.Provider>

<FormActions {...{ submitLabel, cancelLabel, onCancel: handleCancel, isSubmiting, customActions, resetForm }} />
<FormActions
{...{
submitLabel,
cancelLabel,
onCancel: handleCancel,
isSubmiting,
customActions,
resetForm,
clearForm,
}}
/>
</form>
);
}
Expand Down
5 changes: 3 additions & 2 deletions src/forms/FormActions.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import React from 'react';
import PropTypes from 'prop-types';
import { isFunction } from 'js-var-type';

export function FormActions({ submitLabel, cancelLabel, onCancel, isSubmiting, customActions, resetForm }) {
export function FormActions({ submitLabel, cancelLabel, clearForm, onCancel, isSubmiting, customActions, resetForm }) {
if (customActions) {
return isFunction(customActions) ? customActions(isSubmiting, { onCancel, resetForm }) : customActions;
return isFunction(customActions) ? customActions(isSubmiting, { clearForm, onCancel, resetForm }) : customActions;
}

return (
Expand All @@ -22,6 +22,7 @@ export function FormActions({ submitLabel, cancelLabel, onCancel, isSubmiting, c
FormActions.propTypes = {
submitLabel: PropTypes.oneOfType([PropTypes.func, PropTypes.node]),
cancelLabel: PropTypes.oneOfType([PropTypes.func, PropTypes.node]),
clearForm: PropTypes.func,
onCancel: PropTypes.func.isRequired,
isSubmiting: PropTypes.bool,
customActions: PropTypes.oneOfType([PropTypes.func, PropTypes.node, PropTypes.arrayOf(PropTypes.node)]),
Expand Down
20 changes: 19 additions & 1 deletion src/forms/helpers/useForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,30 @@ function useFormState(initialState, { onChange, transform }) {
return nextFormState;
});
},
/* Resets to initial values/state */
resetState() {
setIsDirty(false);
setFormState(initialState);
},
/* Sets empty state */
clearState() {
setIsDirty(true);

setFormState(() => {

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

repliquei lógica do update, mas com state vazio

const nextFormState = {};

setTimeout(() => {
transformRef.current(nextFormState);
});

return nextFormState;
});
},
};
}

export function useForm(initialState, { validations, onChange, transform }) {
const { getState, updateState, resetState } = useFormState(initialState, { onChange, transform });
const { getState, updateState, resetState, clearState } = useFormState(initialState, { onChange, transform });
const [submitAttempted, setSubmitAttempted] = useState(false);
const [, setRefreshForm] = useState(false);
const { getAllKeys: getElementNames, get: getElementRefs, push: registerElementRef } = useArrayValueMap();
Expand Down Expand Up @@ -125,6 +140,9 @@ export function useForm(initialState, { validations, onChange, transform }) {
getValue(name) {
return getValueByPath(formState, name);
},
clear() {
clearState();
},
reset() {
resetState();
setSubmitAttempted(false);
Expand Down
3 changes: 2 additions & 1 deletion src/forms/helpers/useFormControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ export function useFormControl(name, type) {
handleOnChange,
register,
getFormData: () => formState.getFormData(),
resetFormData: () => formState.reset(),
resetFormData: () => formState.reset() /* Resets to initial values/state */,
clearFormData: () => formState.clear() /* Sets empty state */,
isValid: () => formState.getValidationMessage(name) === '',
getFormSubmitedAttempted: () => formState.getSubmitedAttempted(),
};
Expand Down