Skip to content

Commit 27f17a0

Browse files
committed
Merge branch 'master' of github.com:NHSDigital/nhsuk-react-components
2 parents 40ce3c8 + 1c27533 commit 27f17a0

File tree

5 files changed

+12
-6
lines changed

5 files changed

+12
-6
lines changed

src/__tests__/index.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ describe('Index', () => {
2020
'Fieldset',
2121
'Footer',
2222
'Form',
23+
'useFormContext',
2324
'Header',
2425
'Hero',
2526
'Hint',

src/components/form/FormContext.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
import { createContext } from 'react';
1+
import { createContext, useContext } from 'react';
22

33
export interface IFormContext {
44
setError: (name: string, hasError: boolean) => void;
55
isForm: boolean;
66
}
77

8-
export default createContext<IFormContext>({
8+
const FormContext = createContext<IFormContext>({
99
setError: () => {},
1010
isForm: false,
1111
});
12+
13+
export const useFormContext = (): IFormContext => useContext<IFormContext>(FormContext);
14+
15+
export default FormContext;

src/components/form/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
import Form from './Form';
2+
export { useFormContext } from './FormContext';
23

34
export default Form;

src/components/input/Input.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import React, { HTMLProps, useContext, useState, useEffect } from 'react';
1+
import React, { HTMLProps, useState, useEffect } from 'react';
22
import classNames from 'classnames';
33
import Hint from '../hint';
4-
import FormContext, { IFormContext } from '../form/FormContext';
4+
import { useFormContext } from '../form/FormContext';
55
import ErrorMessage from '../error-message';
66

77
interface InputProps extends HTMLProps<HTMLInputElement> {
@@ -22,7 +22,7 @@ const Input: React.FC<InputProps> = ({
2222
error,
2323
...rest
2424
}) => {
25-
const { isForm, setError } = useContext<IFormContext>(FormContext);
25+
const { isForm, setError } = useFormContext();
2626
const [name] = useState<string>(
2727
rest.name || `input_${(Math.random() + 1).toString(36).substring(2, 7)}`,
2828
);

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export { default as ErrorMessage } from './components/error-message';
1212
export { default as ErrorSummary } from './components/error-summary';
1313
export { default as Fieldset } from './components/fieldset';
1414
export { default as Footer } from './components/footer';
15-
export { default as Form } from './components/form';
15+
export { default as Form, useFormContext } from './components/form';
1616
export { default as Header } from './components/header';
1717
export { default as Hero } from './components/hero';
1818
export { default as Hint } from './components/hint';

0 commit comments

Comments
 (0)