@@ -18,7 +18,7 @@ import { action } from '@storybook/addon-actions';
1818import { Meta } from '@storybook/react' ;
1919import React , { useState } from 'react' ;
2020import * as yup from 'yup' ;
21- import { RruForm , RruSelectInput , RruTextInput } from '../src/index' ;
21+ import { RruForm , RruSelectInput , RruTextInput , useRruForm } from '../src/index' ;
2222import colorsOptions from './data/colorsOptions' ;
2323
2424const storyMeta : Meta = {
@@ -251,3 +251,78 @@ export const NestedFields = (args) => {
251251 </ RruForm >
252252 ) ;
253253} ;
254+
255+ export const SetValueProgrammatically = ( args ) => {
256+ const rruFormContext1 = useRruForm ( ) ;
257+ const rruFormContext2 = useRruForm ( ) ;
258+
259+ const initialValues = {
260+ email : 'sample@test.com' ,
261+ } ;
262+
263+ const yupValidationSchema = yup . object ( ) . shape ( {
264+ email : yup
265+ . string ( )
266+ . nullable ( )
267+ . required ( 'Email is required' )
268+ . matches ( / ^ [ A - Z a - z 0 - 9 . _ % + - ] + @ [ A - Z a - z 0 - 9 . - ] + \. [ A - Z a - z ] { 2 , 7 } $ / , 'Email is incorrect' ) ,
269+ } ) ;
270+
271+ const onSubmit1 = ( form ) => {
272+ action ( 'submitting the form 1' ) ( form ) ;
273+ } ;
274+
275+ const onSubmit2 = ( form ) => {
276+ action ( 'submitting the form 2' ) ( form ) ;
277+ } ;
278+
279+ const triggerManualAccess1 = ( ) => {
280+ action ( 'trigger manual access 1' ) ( ) ;
281+ action ( '>>> 1' ) ( rruFormContext1 . getFieldValue ( 'email' ) ) ;
282+ action ( '<<< 1' ) ( '1111@form.test' ) ;
283+ rruFormContext1 . setFieldValue ( 'email' , '1111@form.test' ) ;
284+ } ;
285+
286+ const triggerManualAccess2 = ( ) => {
287+ action ( 'trigger manual access 2' ) ( ) ;
288+ action ( '>>> 2' ) ( rruFormContext2 . getFieldValue ( 'email' ) ) ;
289+ action ( '<<< 2' ) ( '2222@form.test' ) ;
290+ rruFormContext2 . setFieldValue ( 'email' , '2222@form.test' ) ;
291+ } ;
292+
293+ return (
294+ < div >
295+ < RruForm
296+ context = { rruFormContext1 }
297+ initialValues = { initialValues }
298+ yupValidationSchema = { yupValidationSchema }
299+ onSubmit = { onSubmit1 } >
300+ < RruTextInput name = 'email' label = 'Email' requiredAsterisk autoComplete = 'email' />
301+
302+ < button className = 'btn btn-primary mt-4 me-4' onClick = { triggerManualAccess1 } >
303+ Trigger manual access
304+ </ button >
305+
306+ < button type = 'submit' className = 'btn btn-primary mt-4' >
307+ Submit
308+ </ button >
309+ </ RruForm >
310+
311+ < RruForm
312+ context = { rruFormContext2 }
313+ initialValues = { initialValues }
314+ yupValidationSchema = { yupValidationSchema }
315+ onSubmit = { onSubmit2 } >
316+ < RruTextInput name = 'email' label = 'Email' requiredAsterisk autoComplete = 'email' />
317+
318+ < button className = 'btn btn-primary mt-4 me-4' onClick = { triggerManualAccess2 } >
319+ Trigger manual access
320+ </ button >
321+
322+ < button type = 'submit' className = 'btn btn-primary mt-4' >
323+ Submit
324+ </ button >
325+ </ RruForm >
326+ </ div >
327+ ) ;
328+ } ;
0 commit comments