Use forms with or without client-side JavaScript--no code duplication required!
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.
 
 
 

95 lignes
2.2 KiB

  1. import * as React from 'react';
  2. import {
  3. ENCTYPE_APPLICATION_JSON,
  4. ENCTYPE_MULTIPART_FORM_DATA,
  5. } from '../../common';
  6. import {
  7. DEFAULT_ENCTYPE_SERIALIZERS,
  8. EncTypeSerializerMap,
  9. SerializerOptions,
  10. } from '../../utils/serialization';
  11. import {
  12. AllowedClientMethod,
  13. AllowedServerMethod,
  14. FormDerivedElement,
  15. FormDerivedElementComponent,
  16. } from '../common';
  17. import { useFormFetch } from '../hooks/useFormFetch';
  18. export interface FormProps extends Omit<React.HTMLProps<FormDerivedElement>, 'action' | 'method'> {
  19. action?: string;
  20. method?: AllowedServerMethod;
  21. clientAction?: string;
  22. clientHeaders?: HeadersInit;
  23. clientMethod?: AllowedClientMethod;
  24. invalidate?: (...args: unknown[]) => unknown;
  25. refresh?: (response: Response) => void;
  26. encTypeSerializers?: EncTypeSerializerMap;
  27. responseEncType?: string;
  28. serializerOptions?: SerializerOptions;
  29. disableFetch?: boolean;
  30. }
  31. export const Form = React.forwardRef<FormDerivedElement, FormProps>(({
  32. children,
  33. onSubmit,
  34. action,
  35. method = 'get' as const,
  36. clientAction = action,
  37. clientMethod = method,
  38. clientHeaders,
  39. encType = ENCTYPE_MULTIPART_FORM_DATA,
  40. invalidate,
  41. refresh,
  42. encTypeSerializers = DEFAULT_ENCTYPE_SERIALIZERS,
  43. responseEncType = ENCTYPE_APPLICATION_JSON,
  44. serializerOptions,
  45. disableFetch = false,
  46. ...etcProps
  47. }, forwardedRef) => {
  48. const { handleSubmit } = useFormFetch({
  49. clientAction,
  50. onSubmit,
  51. clientMethod,
  52. clientHeaders,
  53. encType,
  54. invalidate,
  55. refresh,
  56. encTypeSerializers,
  57. responseEncType,
  58. serializerOptions,
  59. });
  60. const serverMethodRaw = method.toLowerCase();
  61. const serverMethod = serverMethodRaw === 'get' ? 'get' : 'post';
  62. return (
  63. <FormDerivedElementComponent
  64. {...etcProps}
  65. ref={forwardedRef}
  66. onSubmit={disableFetch ? onSubmit : handleSubmit}
  67. action={action}
  68. method={serverMethod}
  69. encType={ENCTYPE_MULTIPART_FORM_DATA}
  70. >
  71. {children}
  72. </FormDerivedElementComponent>
  73. );
  74. });
  75. Form.displayName = 'Form' as const;
  76. Form.defaultProps = {
  77. action: undefined,
  78. clientAction: undefined,
  79. clientHeaders: undefined,
  80. clientMethod: undefined,
  81. encTypeSerializers: DEFAULT_ENCTYPE_SERIALIZERS,
  82. disableFetch: false as const,
  83. invalidate: undefined,
  84. method: 'get' as const,
  85. refresh: undefined,
  86. responseEncType: ENCTYPE_APPLICATION_JSON,
  87. serializerOptions: undefined,
  88. };