Skip to content

Commit

Permalink
build(libs/form-builder): lazy load devtools (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
hpierre74 authored Feb 24, 2022
1 parent f27205b commit a078351
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 7 deletions.
4 changes: 3 additions & 1 deletion libs/form-builder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"name": "@bedrockstreaming/form-builder",
"version": "0.8.2",
"dependencies": {
"@hookform/devtools": "4.0.2",
"react-hook-form": "7.27.0"
},
"peerDependencies": {
Expand All @@ -11,5 +10,8 @@
"devDependencies": {
"@testing-library/react": "11.2.6",
"@testing-library/react-hooks": "7.0.2"
},
"optionalDependencies": {
"@hookform/devtools": "4.0.2"
}
}
8 changes: 5 additions & 3 deletions libs/form-builder/src/lib/formBuilder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
useForm,
ValidationMode,
} from 'react-hook-form';
import { DevTool } from '@hookform/devtools';

import { Dictionary, ExtraValidation, FormSchema } from './types';
import { useAutoFocus } from './hooks/useAutoFocus.hook';
Expand All @@ -24,6 +23,7 @@ import { SubmitField } from './components/submitField.component';
import { getFieldRules, FieldRules } from './utils/validation.utils';
import { filterDependentsFieldsById } from './utils/conditionalFields.utils';
import { isEmpty } from './utils/object.utils';
import { useDevtools } from './hooks/useDevtools.hook';

const EMPTY_OBJECT = {} as const;
const NOOP = () => null;
Expand All @@ -39,7 +39,7 @@ export interface FormBuilderProps {
extraValidation?: ExtraValidation;
isLastStep?: boolean;
currentStepIndex?: number;
formProps?: { [key: string]: any };
formProps?: { [key: string]: unknown };
debug?: boolean;
}

Expand Down Expand Up @@ -71,6 +71,8 @@ export function FormBuilder({
defaultValues,
});

const Debug = useDevtools(debug);

const isPreFilled = defaultValues && typeof defaultValues === 'object' && Object.keys(defaultValues).length > 0;

const typesAllowed = React.useMemo(() => Object.keys(dictionary || EMPTY_OBJECT), [dictionary]);
Expand Down Expand Up @@ -181,7 +183,7 @@ export function FormBuilder({
onNextStep={onNextStep}
/>
</form>
{debug && <DevTool control={control} />}
{debug && <Debug control={control} />}
</>
);
}
18 changes: 18 additions & 0 deletions libs/form-builder/src/lib/hooks/__tests__/useDevtools.hook.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { renderHook } from '@testing-library/react-hooks';
import { useDevtools, BaseDebug } from '../useDevtools.hook';

describe('useDebug', () => {
it('should load Devtools', async () => {
const { result, waitForNextUpdate } = renderHook(() => useDevtools(true));

await waitForNextUpdate();

expect(result.current).not.toBe(BaseDebug);
});

it('should not load Devtools', async () => {
const { result } = renderHook(() => useDevtools(false));

expect(result.current).toBe(BaseDebug);
});
});
22 changes: 22 additions & 0 deletions libs/form-builder/src/lib/hooks/useDevtools.hook.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import * as React from 'react';

export const BaseDebug = (): JSX.Element | null => null;

export const useDevtools = (debug: boolean) => {
const [, setLoaded] = React.useState(false);
const Component = React.useRef<React.ElementType>(BaseDebug);

React.useEffect(() => {
if (debug && Component.current === BaseDebug) {
// eslint-disable-next-line import/no-extraneous-dependencies
import('@hookform/devtools')
.then(({ DevTool }) => {
Component.current = DevTool as React.ElementType;
setLoaded(true);
})
.catch();
}
}, [debug, setLoaded]);

return Component.current;
};
1 change: 0 additions & 1 deletion libs/form-context/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"peerDependencies": {
"react": "17.0.2",
"@bedrockstreaming/form-builder": "0.8.1",
"@hookform/devtools": "4.0.2",
"react-hook-form": "7.27.0"
},
"devDependencies": {
Expand Down
3 changes: 1 addition & 2 deletions libs/form-validation-rule-list/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"peerDependencies": {
"react": "17.0.2",
"@bedrockstreaming/form-builder": "0.8.1",
"react-hook-form": "7.27.0",
"@hookform/devtools": "4.0.2"
"react-hook-form": "7.27.0"
}
}

0 comments on commit a078351

Please sign in to comment.