import React, { useCallback, useState } from 'react' import { Container, FormAutoSaveIndicator, Typography } from '@toptal/picasso' import { SPACING_6, SPACING_4 } from '@toptal/picasso-utils' import type { ChangedFields } from '@toptal/picasso-forms' import { FormNonCompound as Form, useFormAutoSave, Input, RichTextEditor, SubmitButton, } from '@toptal/picasso-forms' // the emulation of the api call const saveWithDelay = async () => new Promise(resolve => setTimeout(() => resolve('success'), 2000)) interface FormData { 'autoSave-firstName'?: string 'autoSave-lastName'?: string 'autoSave-about'?: string 'autoSave-bio'?: string } const autoSaveSubscribedFields: (keyof FormData)[] = [ 'autoSave-about', 'autoSave-bio', ] const Example = () => { const [autoSaveValues, setAutoSaveValues] = useState({ 'autoSave-firstName': undefined, 'autoSave-lastName': undefined, 'autoSave-about': undefined, 'autoSave-bio': undefined, }) const handleFormValuesChange = useCallback( async (changedFields: ChangedFields, values: FormData) => { await saveWithDelay() setAutoSaveValues(values) }, [] ) const { autoSaveDecorator, savingFields } = useFormAutoSave({ subscribedFields: autoSaveSubscribedFields, onFormValuesChange: handleFormValuesChange, }) return ( onSubmit={values => window.alert(JSON.stringify(values, undefined, 2))} decorators={[autoSaveDecorator]} > } /> } /> Values should be updated only after subscribed fields changes.
            Saved values: {JSON.stringify(autoSaveValues, undefined, 2)}
          
{(savingFields?.['autoSave-bio'] || savingFields?.['autoSave-about']) && ( Saving... )}
Submit ) } export default Example