From 5bb73df804a08b7def6bf643ec59387b4e92f495 Mon Sep 17 00:00:00 2001 From: Divlo Date: Sun, 2 Apr 2023 22:08:32 +0200 Subject: [PATCH] fix: rename `value` to `message` in `HandleUseFormCallback` return type BREAKING CHANGE: Migrate your onSubmit handlers to return a `message` instead of `value` --- README.md | 4 ++-- example/components/FormExample.tsx | 4 ++-- example/cypress/e2e/Form.cy.ts | 2 +- src/hooks/useForm.ts | 8 ++++---- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 2f02c55..6c57860 100644 --- a/README.md +++ b/README.md @@ -89,13 +89,13 @@ export const Example = () => { formData, formElement ) => { - console.log(formData) // { inputName: 'value of the input validated' } + console.log(formData) // { inputName: 'value of the input validated and type-safe' } formElement.reset() // The return can be either `null` or an object with a global message of type `'error' | 'success'`. return { type: 'success', - value: 'Success: Form submitted' + message: 'Success: Form submitted' } } diff --git a/example/components/FormExample.tsx b/example/components/FormExample.tsx index e7d3f7c..8aefb87 100644 --- a/example/components/FormExample.tsx +++ b/example/components/FormExample.tsx @@ -23,12 +23,12 @@ export const FormExample: React.FC = () => { formData, formElement ) => { - await simulateServerRequest(2000) + await simulateServerRequest(2_000) console.log('onSubmit:', formData) formElement.reset() return { type: 'success', - value: 'common:success-message' + message: 'common:success-message' } } diff --git a/example/cypress/e2e/Form.cy.ts b/example/cypress/e2e/Form.cy.ts index 6ff3725..8fee940 100644 --- a/example/cypress/e2e/Form.cy.ts +++ b/example/cypress/e2e/Form.cy.ts @@ -3,7 +3,7 @@ describe('Form', () => { cy.visit('/') }) - it('suceeds, reset input values and display the global success message', () => { + it('succeeds, reset input values and display the global success message', () => { cy.get('[data-cy=input-name]').type('John') cy.get('[data-cy=input-email]').type('john@john.com') cy.get('#error-name').should('not.exist') diff --git a/src/hooks/useForm.ts b/src/hooks/useForm.ts index 2af2a7d..1aa6f22 100644 --- a/src/hooks/useForm.ts +++ b/src/hooks/useForm.ts @@ -38,13 +38,13 @@ export type HandleUseForm = ( export interface GlobalMessage { type: 'error' | 'success' - value?: string + message?: string properties?: undefined } export interface PropertiesMessage { type: 'error' - value?: string + message?: string properties: { [key in keyof Partial]: string } } @@ -129,8 +129,8 @@ export const useForm = ( formElement ) if (message != null) { - const { value, type, properties } = message - setMessage(value) + const { message: messageValue, type, properties } = message + setMessage(messageValue) setFetchState(type) if (type === 'error') { const propertiesErrors: ErrorsObject =