Discord bot
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

52 linhas
1005 B

  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