fix: rename value to message in HandleUseFormCallback return type

BREAKING CHANGE: Migrate your onSubmit handlers to return a `message` instead of `value`
This commit is contained in:
Divlo 2023-04-02 22:08:32 +02:00
parent 69f12002c7
commit 5bb73df804
No known key found for this signature in database
GPG Key ID: 8F9478F220CE65E9
4 changed files with 9 additions and 9 deletions

View File

@ -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'
}
}

View File

@ -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'
}
}

View File

@ -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')

View File

@ -38,13 +38,13 @@ export type HandleUseForm<K extends Schema> = (
export interface GlobalMessage {
type: 'error' | 'success'
value?: string
message?: string
properties?: undefined
}
export interface PropertiesMessage<K extends Schema> {
type: 'error'
value?: string
message?: string
properties: { [key in keyof Partial<K>]: string }
}
@ -129,8 +129,8 @@ export const useForm = <K extends Schema>(
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<typeof validationSchema> =