SHARE
TWEET

Untitled

a guest Aug 23rd, 2019 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const App: React.FC = () => {
  2.   const [isDirty, setIsDirty] = React.useState(false);
  3.   let formRef = Maybe.nothing<Formik<FormValues>>();
  4.  
  5.   const initFormState = () =>
  6.     formRef.match<void>({
  7.       Just: node => {
  8.         const { dirty } = node.getFormikComputedProps();
  9.         setIsDirty(dirty);
  10.       },
  11.       Nothing: () => null
  12.     });
  13.  
  14.   const setFormRef = (ref: Formik<FormValues>) => {
  15.     formRef = Maybe.of(ref);
  16.     initFormState();
  17.   };
  18.  
  19.   const onSubmit = (values: FormValues) => console.log(values);
  20.  
  21.   const submitForm = () =>
  22.     formRef.match({
  23.       Just: justCurrent => justCurrent.submitForm(),
  24.       Nothing: () => null
  25.     });
  26.  
  27.   const clearForm = () =>
  28.     formRef.match({
  29.       Just: node => node.resetForm(),
  30.       Nothing: () => null
  31.     });
  32.  
  33.   React.useEffect(initFormState, []);
  34.   return (
  35.     <div className="App">
  36.       <SimpleForm
  37.         ref={setFormRef}
  38.         onSubmit={onSubmit}
  39.         onDirtyChanged={setIsDirty}
  40.       />
  41.       <button type="submit" onClick={submitForm}>
  42.         Submit
  43.       </button>
  44.       {isDirty && (
  45.         <button type="button" onClick={clearForm}>
  46.           Clear
  47.         </button>
  48.       )}
  49.     </div>
  50.   );
  51. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top