Skip to content

Commit e58c300

Browse files
committed
storybook: added SetValueProgrammaticallyAllTypes
1 parent 62b09c9 commit e58c300

File tree

1 file changed

+82
-1
lines changed

1 file changed

+82
-1
lines changed

stories/Form.RruForm.stories.tsx

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,20 @@ import { action } from '@storybook/addon-actions';
1818
import { Meta } from '@storybook/react';
1919
import React, { useState } from 'react';
2020
import * as yup from 'yup';
21-
import { RruForm, RruSelectInput, RruTextInput, RruTextareaInput, useRruForm } from '../src/index';
21+
import {
22+
RruCheckboxInput,
23+
RruDateTimeInput,
24+
RruFileInput,
25+
RruForm,
26+
RruMultiCheckboxInput,
27+
RruMultiSelectInput,
28+
RruRadioInput,
29+
RruSelectInput,
30+
RruTextInput,
31+
RruTextareaInput,
32+
useRruForm,
33+
} from '../src/index';
34+
import animalsOptions from './data/animalsOptions';
2235
import colorsOptions from './data/colorsOptions';
2336

2437
const storyMeta: Meta = {
@@ -422,3 +435,71 @@ export const SetValueProgrammaticallyNested = (args) => {
422435
</RruForm>
423436
);
424437
};
438+
439+
export const SetValueProgrammaticallyAllTypes = (args) => {
440+
const rruFormContext = useRruForm();
441+
442+
const initialValues = {
443+
email: 'sample@test.com',
444+
notes: 'old notes',
445+
color: 'RED',
446+
colors: ['RED'],
447+
animal: 'LION',
448+
animals: ['LION'],
449+
agree: false,
450+
birthDate: '1990-05-20',
451+
image: { name: 'cat.png', foo: '111', bar: 'test' },
452+
};
453+
454+
const yupValidationSchema = yup.object().shape({
455+
email: yup
456+
.string()
457+
.nullable()
458+
.required('Email is required')
459+
.matches(/^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,7}$/, 'Email is incorrect'),
460+
notes: yup.string().nullable().required('This field is required'),
461+
});
462+
463+
const onSubmit = (form) => {
464+
action('submitting the form')(form);
465+
};
466+
467+
const triggerManualAccess = () => {
468+
action('trigger manual access')();
469+
rruFormContext.setFieldValue('email', 'new@form.test');
470+
rruFormContext.setFieldValue('notes', 'some new notes');
471+
rruFormContext.setFieldValue('color', 'BLUE');
472+
rruFormContext.setFieldValue('colors', ['BLUE', 'ORANGE']);
473+
rruFormContext.setFieldValue('animal', 'CAT');
474+
rruFormContext.setFieldValue('animals', ['CAT', 'DOG']);
475+
rruFormContext.setFieldValue('agree', true);
476+
rruFormContext.setFieldValue('birthDate', '2020-01-23');
477+
rruFormContext.setFieldValue('image', { name: 'lion.png', conf: '222' });
478+
};
479+
480+
return (
481+
<RruForm
482+
context={rruFormContext}
483+
initialValues={initialValues}
484+
yupValidationSchema={yupValidationSchema}
485+
onSubmit={onSubmit}>
486+
<RruTextInput name='email' label='Email' requiredAsterisk autoComplete='email' />
487+
<RruTextareaInput name='notes' label='Notes' />
488+
<RruSelectInput name='color' label='Color' options={colorsOptions} />
489+
<RruMultiSelectInput name='colors' label='Colors' options={colorsOptions} />
490+
<RruRadioInput name='animal' label='Animal' options={animalsOptions} />
491+
<RruMultiCheckboxInput name='animals' label='Animals' options={animalsOptions} />
492+
<RruCheckboxInput name='agree' label='Agree' />
493+
<RruDateTimeInput mode='date' name='birthDate' label='Birth Date' />
494+
<RruFileInput name='image' label='Image' />
495+
496+
<button className='btn btn-primary mt-4 me-4' onClick={triggerManualAccess}>
497+
Trigger manual access
498+
</button>
499+
500+
<button type='submit' className='btn btn-primary mt-4'>
501+
Submit
502+
</button>
503+
</RruForm>
504+
);
505+
};

0 commit comments

Comments
 (0)