Discord bot
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Form.tsx 1005 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import * as React from 'react'
  2. import {useForm} from 'react-hook-form'
  3. import Method from './Method'
  4. type Props = {
  5. method: Method,
  6. action: string,
  7. href?: string,
  8. headers?: Record<string, string>,
  9. onResponse?: (...args: unknown[]) => unknown,
  10. onError?: (...args: unknown[]) => unknown,
  11. onSubmit?: (...args: unknown[]) => unknown,
  12. children: (...args: unknown[]) => unknown,
  13. }
  14. const Form: React.FC<Props> = ({
  15. method,
  16. action,
  17. href,
  18. children,
  19. onResponse,
  20. onError,
  21. headers,
  22. onSubmit,
  23. }) => {
  24. const onFormSubmit = async (body) => {
  25. const response = await fetch(href, {
  26. method,
  27. body,
  28. headers,
  29. })
  30. const data = await response.json()
  31. if (typeof (onResponse as unknown) === 'function') {
  32. onResponse(data)
  33. }
  34. }
  35. const {register, handleSubmit} = useForm()
  36. return (
  37. <form
  38. method={method === Method.GET ? 'get' : 'post'}
  39. action={action}
  40. onSubmit={handleSubmit(onSubmit || onFormSubmit, onError)}
  41. >
  42. {children({ register })}
  43. </form>
  44. )
  45. }
  46. export default Form