From 6f45998710ca2d228b91b248758fe1be03f4692f Mon Sep 17 00:00:00 2001 From: semantic-release-bot Date: Fri, 24 Mar 2023 16:55:45 +0000 Subject: [PATCH 1/5] chore(release): 2.0.11 [skip ci] MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## [2.0.11](https://github.com/aviemet/useInertiaForm/compare/v2.0.10...v2.0.11) (2023-03-24) ### Bug Fixes * 🐛 Adds errors to dependency array in Form component ([f342147](https://github.com/aviemet/useInertiaForm/commit/f342147b10a29ab1ee1e0e7067d5ed0c33d21b02)) --- CHANGELOG.md | 7 +++++++ package.json | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5792ed7..8906257 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +## [2.0.11](https://github.com/aviemet/useInertiaForm/compare/v2.0.10...v2.0.11) (2023-03-24) + + +### Bug Fixes + +* 🐛 Adds errors to dependency array in Form component ([f342147](https://github.com/aviemet/useInertiaForm/commit/f342147b10a29ab1ee1e0e7067d5ed0c33d21b02)) + ## [2.0.10](https://github.com/aviemet/useInertiaForm/compare/v2.0.9...v2.0.10) (2023-03-24) diff --git a/package.json b/package.json index 19051de..63dc4e6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "use-inertia-form", - "version": "2.0.10", + "version": "2.0.11", "description": "Extra functionality for Inertia.js useForm hook", "main": "dist/useInertiaForm.js", "module": "dist/useInertiaForm.esm.js", From 3f970c704d4e0b438e5836b4a550c551e4ffd85a Mon Sep 17 00:00:00 2001 From: Avram Walden Date: Fri, 24 Mar 2023 14:46:02 -0700 Subject: [PATCH 2/5] =?UTF-8?q?fix:=20=F0=9F=90=9B=20Only=20resets=20form?= =?UTF-8?q?=20data=20after=20success=20if=20async?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Form/index.tsx | 25 +++++++++++++------------ src/Inputs/Submit.tsx | 4 ++-- src/useInertiaForm.ts | 21 +++++++++++++-------- tsconfig.json | 2 +- 4 files changed, 29 insertions(+), 23 deletions(-) diff --git a/src/Form/index.tsx b/src/Form/index.tsx index 1dcfb6d..a18a33d 100644 --- a/src/Form/index.tsx +++ b/src/Form/index.tsx @@ -1,6 +1,6 @@ import React, { useCallback, useEffect } from 'react' import axios from 'axios' -import { type Visit } from '@inertiajs/core' +import { type VisitOptions } from '@inertiajs/core' import useInertiaForm, { NestedObject } from '../useInertiaForm' import { useForm, type UseFormProps, type HTTPVerb, FormProvider } from './FormProvider' import FormMetaWrapper, { useFormMeta, type FormMetaValue } from './FormMetaWrapper' @@ -13,6 +13,7 @@ export interface FormProps extends PartialHTMLForm { method?: HTTPVerb to?: string async?: boolean + resetAfterSubmit?: boolean remember?: boolean railsAttributes?: boolean onSubmit?: (form: UseFormProps) => boolean|void @@ -28,6 +29,7 @@ const Form = ({ method = 'post', to, async = false, + resetAfterSubmit, remember = true, onSubmit, onChange, @@ -35,18 +37,17 @@ const Form = ({ onError, ...props }: Omit, 'railsAttributes'>) => { - const form = remember && (model || to) ? useInertiaForm(`${method}/${model || to}`, data) : useInertiaForm(data) const contextValueObject = useCallback((): UseFormProps => ( { ...form, model, method, to, submit } - ), [form.data, form.errors]) + ), [data, form.data, form.errors]) /** * Submits the form. If async prop is true, submits using axios, * otherwise submits using Inertia's `useForm.submit` method */ - const submit = async (options?: Partial) => { + const submit = async (options?: Partial) => { let shouldSubmit = to && onSubmit && onSubmit(contextValueObject()) === false ? false : true if(shouldSubmit) { @@ -62,7 +63,14 @@ const Form = ({ e.preventDefault() e.stopPropagation() - submit() + submit({ + onSuccess: () => { + if(resetAfterSubmit || (resetAfterSubmit !== false && async === true)) { + form.reset() + } + if(onSuccess) onSuccess(contextValueObject()) + }, + }) } // Set values from url search params. Allows for prefilling form data from a link @@ -82,13 +90,6 @@ const Form = ({ if(onError) onError(contextValueObject()) }, [form.errors]) - useEffect(() => { - if(!form.wasSuccessful) return - - form.reset() - if(onSuccess) onSuccess(contextValueObject()) - }, [form.wasSuccessful]) - return (
diff --git a/src/Inputs/Submit.tsx b/src/Inputs/Submit.tsx index ebac77a..9318877 100644 --- a/src/Inputs/Submit.tsx +++ b/src/Inputs/Submit.tsx @@ -9,9 +9,9 @@ const Submit = React.forwardRef(( { children, type = 'submit', disabled, component = 'button', ...props }, ref, ) => { - const { processing } = useForm() + const { processing, isDirty } = useForm() - const finalProps = { children, type, disabled: disabled || processing, ref, ...props } + const finalProps = { children, type, disabled: disabled || processing || !isDirty, ref, ...props } if(typeof component === 'string') { return React.createElement(component, finalProps) diff --git a/src/useInertiaForm.ts b/src/useInertiaForm.ts index 8dfc022..aa3e003 100644 --- a/src/useInertiaForm.ts +++ b/src/useInertiaForm.ts @@ -57,15 +57,20 @@ export default function useInertiaForm( const isMounted = useRef() // Data - let rememberKey = null - let transformedData = rememberKeyOrInitialValues - if(typeof rememberKeyOrInitialValues === 'string') { - rememberKey = rememberKeyOrInitialValues - transformedData = maybeInitialValues - } + const getFormArguments = useCallback((): [string, TForm] => { + let rememberKey: string = null + let transformedData = rememberKeyOrInitialValues + if(typeof rememberKeyOrInitialValues === 'string') { + rememberKey = rememberKeyOrInitialValues + transformedData = maybeInitialValues + } + return [rememberKey, fillEmptyValues(transformedData as TForm)] + }, [rememberKeyOrInitialValues, maybeInitialValues]) + + const [rememberKey, transformedData] = getFormArguments() - const [defaults, setDefaults] = useState((fillEmptyValues(transformedData) || {}) as TForm) - const [data, setData] = rememberKey ? useRemember(defaults, `${rememberKey}:data`) : useState(defaults) + const [defaults, setDefaults] = useState(transformedData || {} as TForm) + const [data, setData] = rememberKey ? useRemember(transformedData, `${rememberKey}:data`) : useState(transformedData) // Errors const [errors, setErrors] = rememberKey diff --git a/tsconfig.json b/tsconfig.json index b624224..bf1f7ad 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,7 +2,7 @@ "compilerOptions": { "target": "ESNext", "module": "esnext", - "lib": ["dom", "esnext"], + "lib": ["dom", "esnext", "dom.iterable"], "jsx": "react", "declaration": true, "sourceMap": true, From f24cdef3ef66a4b04f595708d90cbc394d74824d Mon Sep 17 00:00:00 2001 From: semantic-release-bot Date: Fri, 24 Mar 2023 21:48:17 +0000 Subject: [PATCH 3/5] chore(release): 2.0.12 [skip ci] MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## [2.0.12](https://github.com/aviemet/useInertiaForm/compare/v2.0.11...v2.0.12) (2023-03-24) ### Bug Fixes * 🐛 Only resets form data after success if async ([3f970c7](https://github.com/aviemet/useInertiaForm/commit/3f970c704d4e0b438e5836b4a550c551e4ffd85a)) --- CHANGELOG.md | 7 +++++++ package.json | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8906257..43ef31f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +## [2.0.12](https://github.com/aviemet/useInertiaForm/compare/v2.0.11...v2.0.12) (2023-03-24) + + +### Bug Fixes + +* 🐛 Only resets form data after success if async ([3f970c7](https://github.com/aviemet/useInertiaForm/commit/3f970c704d4e0b438e5836b4a550c551e4ffd85a)) + ## [2.0.11](https://github.com/aviemet/useInertiaForm/compare/v2.0.10...v2.0.11) (2023-03-24) diff --git a/package.json b/package.json index 63dc4e6..2931376 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "use-inertia-form", - "version": "2.0.11", + "version": "2.0.12", "description": "Extra functionality for Inertia.js useForm hook", "main": "dist/useInertiaForm.js", "module": "dist/useInertiaForm.esm.js", From 403270a3e6ee48e74c9703722df2d396762b5bb8 Mon Sep 17 00:00:00 2001 From: Avram Walden Date: Fri, 24 Mar 2023 14:52:38 -0700 Subject: [PATCH 4/5] =?UTF-8?q?fix:=20=F0=9F=90=9B=20Removes=20disabled=20?= =?UTF-8?q?conditions=20from=20Submit=20button?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Inputs/Submit.tsx | 5 +---- tsconfig.json | 2 +- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/src/Inputs/Submit.tsx b/src/Inputs/Submit.tsx index 9318877..c6256b3 100644 --- a/src/Inputs/Submit.tsx +++ b/src/Inputs/Submit.tsx @@ -1,5 +1,4 @@ import React from 'react' -import { useForm } from '../Form' interface ButtonProps extends React.ButtonHTMLAttributes { component?: string | JSX.Element @@ -9,9 +8,7 @@ const Submit = React.forwardRef(( { children, type = 'submit', disabled, component = 'button', ...props }, ref, ) => { - const { processing, isDirty } = useForm() - - const finalProps = { children, type, disabled: disabled || processing || !isDirty, ref, ...props } + const finalProps = { children, type, disabled: disabled, ref, ...props } if(typeof component === 'string') { return React.createElement(component, finalProps) diff --git a/tsconfig.json b/tsconfig.json index bf1f7ad..b624224 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,7 +2,7 @@ "compilerOptions": { "target": "ESNext", "module": "esnext", - "lib": ["dom", "esnext", "dom.iterable"], + "lib": ["dom", "esnext"], "jsx": "react", "declaration": true, "sourceMap": true, From 1f468ea20fd52aefc5284d0ceb4aaba2ff3df66c Mon Sep 17 00:00:00 2001 From: semantic-release-bot Date: Fri, 24 Mar 2023 21:55:05 +0000 Subject: [PATCH 5/5] chore(release): 2.0.13 [skip ci] MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## [2.0.13](https://github.com/aviemet/useInertiaForm/compare/v2.0.12...v2.0.13) (2023-03-24) ### Bug Fixes * 🐛 Removes disabled conditions from Submit button ([403270a](https://github.com/aviemet/useInertiaForm/commit/403270a3e6ee48e74c9703722df2d396762b5bb8)) --- CHANGELOG.md | 7 +++++++ package.json | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 43ef31f..fbd848e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +## [2.0.13](https://github.com/aviemet/useInertiaForm/compare/v2.0.12...v2.0.13) (2023-03-24) + + +### Bug Fixes + +* 🐛 Removes disabled conditions from Submit button ([403270a](https://github.com/aviemet/useInertiaForm/commit/403270a3e6ee48e74c9703722df2d396762b5bb8)) + ## [2.0.12](https://github.com/aviemet/useInertiaForm/compare/v2.0.11...v2.0.12) (2023-03-24) diff --git a/package.json b/package.json index 2931376..9641dd9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "use-inertia-form", - "version": "2.0.12", + "version": "2.0.13", "description": "Extra functionality for Inertia.js useForm hook", "main": "dist/useInertiaForm.js", "module": "dist/useInertiaForm.esm.js",