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
39 changes: 39 additions & 0 deletions demo/FormExamples.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
export function FormExamples() {
const [bootstrapFormValidation, setBootstrapFormValidation] = useState(false);
const [changeCustomValidation, setChangeCustomValidation] = useState(false);
const [useCustomActions, setUseCustomActions] = useState(false);

const validations = useMemo(
() => ({
Expand Down Expand Up @@ -138,6 +139,20 @@ export function FormExamples() {
}}
customValidation={bootstrapFormValidation}
validations={validations}
customActions={
useCustomActions
? (isSubmiting, { onCancel, resetForm }) => (
<div>
<button type="submit" className="btn btn-success">
Custom save
</button>
<button type="button" className="btn btn-secondary" onClick={() => resetForm()}>
Custom reset
</button>
</div>
)
: undefined
}
>
<h5>Form configuration:</h5>
<FormGroupSwitch
Expand All @@ -152,6 +167,12 @@ export function FormExamples() {
label="Change validation for variable custom validation?"
afterChange={(value) => setChangeCustomValidation(value)}
/>
<FormGroupSwitch
id="useCustomActions"
name="useCustomActions"
label="Use form custom actions?"
afterChange={(value) => setUseCustomActions(value)}
/>
<hr />
<div className="row">
<div className="col">
Expand Down Expand Up @@ -734,10 +755,28 @@ export function FormExamples() {
includeEmptyItem={false}
menuClassName="p-4 w-100"
/>

<ResetForm />
</Form>
);
}

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

const reset = () => {
console.log('reset form');

formControl.resetFormData();
};

return (
<button type="button" className="btn btn-outline-secondary mb-3" onClick={reset}>
Reset form
</button>
);
};

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

Expand Down
4 changes: 2 additions & 2 deletions src/forms/Form.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export function Form({
<form {...formProps}>
<FormContext.Provider value={formState}>{children}</FormContext.Provider>

<FormActions {...{ submitLabel, cancelLabel, onCancel: handleCancel, isSubmiting, customActions }} />
<FormActions {...{ submitLabel, cancelLabel, onCancel: handleCancel, isSubmiting, customActions, resetForm }} />
</form>
);
}
Expand All @@ -92,7 +92,7 @@ Form.defaultProps = {
Form.propTypes = {
cancelLabel: PropTypes.string,
children: PropTypes.oneOfType([PropTypes.node, PropTypes.arrayOf(PropTypes.node)]),
customActions: PropTypes.oneOfType([PropTypes.node, PropTypes.arrayOf(PropTypes.node)]),
customActions: PropTypes.oneOfType([PropTypes.func, PropTypes.node, PropTypes.arrayOf(PropTypes.node)]),
customValidation: PropTypes.bool,
initialValues: PropTypes.object,
onCancel: PropTypes.func,
Expand Down
7 changes: 4 additions & 3 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 }) {
export function FormActions({ submitLabel, cancelLabel, onCancel, isSubmiting, customActions, resetForm }) {
if (customActions) {
return isFunction(customActions) ? customActions(isSubmiting) : customActions;
return isFunction(customActions) ? customActions(isSubmiting, { onCancel, resetForm }) : customActions;
}

return (
Expand All @@ -24,5 +24,6 @@ FormActions.propTypes = {
cancelLabel: PropTypes.oneOfType([PropTypes.func, PropTypes.node]),
onCancel: PropTypes.func.isRequired,
isSubmiting: PropTypes.bool,
customActions: PropTypes.oneOfType([PropTypes.func, PropTypes.node]),
customActions: PropTypes.oneOfType([PropTypes.func, PropTypes.node, PropTypes.arrayOf(PropTypes.node)]),
resetForm: PropTypes.func,
};
1 change: 1 addition & 0 deletions src/forms/helpers/useFormControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export function useFormControl(name, type) {
handleOnChange,
register,
getFormData: () => formState.getFormData(),
resetFormData: () => formState.reset(),
isValid: () => formState.getValidationMessage(name) === '',
getFormSubmitedAttempted: () => formState.getSubmitedAttempted(),
};
Expand Down