Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(libs/form-builder): add capacity to change the default behavior 'onChange' #102

Closed
wants to merge 13 commits into from
Closed
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ Run `yarn nx dep-graph` to see a diagram of the dependencies of your projects.

Visit the [Nx Documentation](https://nx.dev) to learn more.

## Release or Pre-release a new version

Use the actions tabs in github!

## Contributors

This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification.
Expand Down
2 changes: 1 addition & 1 deletion libs/form-builder/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bedrockstreaming/form-builder",
"version": "0.9.2",
"version": "0.9.3-fix-toomuch-calls.2",
"dependencies": {
"react-hook-form": "7.27.0"
},
Expand Down
61 changes: 58 additions & 3 deletions libs/form-builder/src/lib/__tests__/formBuilder.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* eslint-disable */
import '@testing-library/jest-dom';
import { act, render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event'
import { FormBuilder } from '../formBuilder';

const makeStep = ({ fieldsById, stepId, label }) => ({
Expand Down Expand Up @@ -60,12 +62,18 @@ describe('<FormBuilder />', () => {
steps: { ...stepOne, ...stepTwo },
stepsById: [stepOneId, stepTwoId],
};

// aria-checked="true"
const CORRECT_DICTIONARY = {
text: ({ label, ...props }) => (
text: ({ label, errors, id, ...props }) => (
<fieldset>
<label>{label}</label>
<input type="text" placeholder="Test" />
<label for={id}>{label}</label>
<input id={id} type="text" placeholder="Test" />
{
!!errors ? (
<span>{errors.message}</span>
) : null
}
</fieldset>
),
checkbox: ({ label, value = false, ...props }) => (
Expand All @@ -77,6 +85,53 @@ describe('<FormBuilder />', () => {
submit: ({ label }) => <button type="submit">{label}</button>,
};

describe('onChangeTriggerByField behavior', () => {
it.only('should trigger validate only changed field', async () => {
await getWrapper({
schema: {
fields: {
[fieldOneId]: {
id: fieldOneId,
type: 'text',
meta: {
label: 'bar',
},
validation: {
required: {
key: 'required',
message: 'field is required',
value: true,
},
}
},
[fieldTwoId]: {
id: fieldTwoId,
type: 'text',
meta: {
label: 'foo',
},
validation: {
required: {
key: 'required',
message: 'field is required',
value: true,
},
}
},
},
steps: { ...stepOne },
stepsById: [stepOneId, stepTwoId],
},
dictionary: CORRECT_DICTIONARY,
onSubmit,
});

await userEvent.type(screen.getByLabelText('bar'), 'tesTyping');

expect(screen.queryByText('field is required')).not.toBeInTheDocument();
})
})

describe('with bad props', () => {
it('should not render if we pass no dictionary', async () => {
await getWrapper({
Expand Down
3 changes: 2 additions & 1 deletion libs/form-builder/src/lib/components/formField.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export interface FormFieldProps {
propRef?: Ref;
disabled?: boolean;
label?: string;
onClick?: (event: any) => void;
onClick?: (event: React.BaseSyntheticEvent) => void;
onChange?: (event: React.BaseSyntheticEvent) => void;
isValidating?: boolean;
formId?: string;
shouldDisplayRequiredHint?: boolean;
Expand Down
13 changes: 10 additions & 3 deletions libs/form-builder/src/lib/formBuilder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export interface FormBuilderProps {
onSubmit: SubmitHandler<FieldValues>;
onNextStep?: (value: UnpackNestedValue<FieldValues>) => void;
defaultValues?: DefaultValues<FieldValues>;
behavior?: keyof ValidationMode;
behavior?: keyof ValidationMode | 'onChangeTriggerByField';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to add this new possible value in docs? 👀

extraValidation?: ExtraValidation;
isLastStep?: boolean;
currentStepIndex?: number;
Expand Down Expand Up @@ -66,7 +66,7 @@ export function FormBuilder({
trigger,
setFocus,
} = useForm<FieldValues>({
mode: behavior,
mode: behavior === 'onChangeTriggerByField' ? 'onSubmit' : behavior,
criteriaMode: 'all',
defaultValues,
});
Expand Down Expand Up @@ -149,7 +149,8 @@ export function FormBuilder({
defaultValue={defaultValue}
rules={validationRulesById[fieldId]}
render={({ field }) => {
const { ref, ...fieldRest } = field;
const { ref, onChange, ...fieldRest } = field;

return (
<FormField
id={id}
Expand All @@ -164,6 +165,12 @@ export function FormBuilder({
{...formMeta}
{...meta}
{...fieldRest}
onChange={(event) => {
onChange(event);
if (behavior === 'onChangeTriggerByField') {
trigger(id);
}
}}
/>
);
}}
Expand Down
2 changes: 1 addition & 1 deletion libs/form-context/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bedrockstreaming/form-context",
"version": "0.9.2",
"version": "0.9.3-fix-toomuch-calls.2",
"peerDependencies": {
"react": "17.0.2 || ^18.0.0",
"@bedrockstreaming/form-builder": "0.8.1",
Expand Down
2 changes: 1 addition & 1 deletion libs/form-editor/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bedrockstreaming/form-editor",
"version": "0.9.2",
"version": "0.9.3-fix-toomuch-calls.2",
"dependencies": {
"@bedrockstreaming/form-builder": "0.8.1",
"@bedrockstreaming/form-redux": "0.8.1",
Expand Down
2 changes: 1 addition & 1 deletion libs/form-redux/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bedrockstreaming/form-redux",
"version": "0.9.2",
"version": "0.9.3-fix-toomuch-calls.2",
"peerDependencies": {
"@bedrockstreaming/form-builder": "0.8.1",
"@hookform/devtools": "^4.0.2",
Expand Down
2 changes: 1 addition & 1 deletion libs/form-validation-rule-list/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bedrockstreaming/form-validation-rule-list",
"version": "0.9.2",
"version": "0.9.3-fix-toomuch-calls.2",
"dependencies": {},
"peerDependencies": {
"react": "17.0.2 || ^18.0.0",
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "forms",
"version": "0.9.2",
"version": "0.9.3-fix-toomuch-calls.2",
"license": "MIT",
"keywords": [
"react-hook-form",
Expand Down Expand Up @@ -99,6 +99,8 @@
"@nx-plus/docusaurus": "12.2.0",
"@testing-library/react": "11.2.6",
"@testing-library/react-hooks": "7.0.2",
"@testing-library/user-event": "14.5.2",
"@testing-library/jest-dom": "6.4.6",
"@types/debug": "4.1.7",
"@types/deep-freeze": "0.1.2",
"@types/jest": "26.0.24",
Expand Down
Loading
Loading