-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: refactoring of form functions
- Loading branch information
Showing
7 changed files
with
153 additions
and
151 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import React, { useRef } from 'react'; | ||
// const fileInputRef = useRef<HTMLInputElement>(null); | ||
|
||
function handleInputChange( | ||
e, | ||
books, | ||
setBooks: React.Dispatch<React.SetStateAction<any>>, | ||
) { | ||
const { name, value } = e.target; | ||
|
||
// Si el campo es 'author', dividimos los nombres por comas en un array | ||
if (name === 'authors') { | ||
const authorNames = value.split(','); | ||
setBooks({ | ||
...books, | ||
[name]: authorNames, // Guardamos un array de nombres de autores | ||
}); | ||
} else { | ||
setBooks({ | ||
...books, | ||
[name]: value, | ||
}); | ||
} | ||
} | ||
|
||
function handleCategory( | ||
selectedOptions, | ||
setBooks: React.Dispatch<React.SetStateAction<any>>, | ||
) { | ||
// Verificar si se seleccionaron opciones | ||
if (selectedOptions && selectedOptions.length > 0) { | ||
// Obtener los valores de las opciones seleccionadas | ||
const selectedValues = selectedOptions.map((option) => option.value); | ||
|
||
// Actualizar el estado de 'books' con los valores seleccionados | ||
setBooks((prevBooks) => ({ | ||
...prevBooks, | ||
category: selectedValues, | ||
})); | ||
} else { | ||
// Si no se seleccionaron opciones, establecer el estado de 'category' como un array vacío | ||
setBooks((prevBooks) => ({ | ||
...prevBooks, | ||
category: [], | ||
})); | ||
} | ||
} | ||
|
||
function handleField( | ||
fieldName, | ||
newValue, | ||
setBooks: React.Dispatch<React.SetStateAction<any>>, | ||
) { | ||
setBooks((books) => ({ | ||
...books, | ||
[fieldName]: newValue, | ||
})); | ||
} | ||
|
||
function useFileInputRef() { | ||
const fileInputRef = useRef<HTMLInputElement>(null); | ||
|
||
function handleButtonClick() { | ||
if (fileInputRef.current) { | ||
fileInputRef.current.click(); | ||
} | ||
} | ||
|
||
return { fileInputRef, handleButtonClick }; | ||
} | ||
|
||
export { handleInputChange, handleCategory, handleField, useFileInputRef }; |
Oops, something went wrong.