Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions packages/ui-components/src/lib/hooks/useFormContext.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { useContext } from 'react';
import { FormikContext, FormikContextType } from 'formik';

// Note: Using context this way instead of useFormikContext suppresses the error if it is not within Formik
// TODO: #106 Fix React 18 TypeScript errors @jaredhill4
// @ts-ignore
export const useFormContext = <T>() => useContext<FormikContextType<T>>(FormikContext); // Using context this way instead of useFormikContext suppresses the error if it is not within Formik
// This hook safely gets the Formik context if available, or returns null if not
export const useFormContext = <T>() => {
try {
// Using context this way instead of useFormikContext suppresses the error if it is not within Formik
const context = useContext<FormikContextType<T>>(FormikContext);
return context;
} catch (error) {
// Return null if context is not available or there's an error
return null;
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const InputRadio: FC<InputRadioProps> = ({
}) => {
return (
<FormControlLabel
control={<Radio {...props} color={color} />}
control={<Radio {...props} color={color} inputProps={{ 'aria-label': typeof label === 'string' ? label : undefined }} />}
className={classNames('lc-input lc-input-radio', className)}
label={label}
labelPlacement={labelPlacement}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const InputRadioGroup: FC<InputRadioGroupProps> = ({ className, formikPro
const formContext = useFormContext();
if (!formikProps && formContext) formikProps = formContext;

const fieldProps = formikProps?.getFieldProps(props.name);
const fieldProps = formikProps?.getFieldProps?.(props.name);
const fieldValue = props.value ?? fieldProps?.value;

const handleChange = (event: ChangeEvent<HTMLInputElement>) => {
Expand All @@ -28,7 +28,12 @@ export const InputRadioGroup: FC<InputRadioGroupProps> = ({ className, formikPro
return (
<FormControl className={classNames('lc-input-radio-group', className)} component="fieldset">
{label && <FormLabel component="legend">{label}</FormLabel>}
<RadioGroup aria-label={props.name} onChange={handleChange} value={fieldValue} {...props} />
<RadioGroup
aria-label={props.name}
onChange={handleChange}
value={fieldValue}
{...props}
/>
</FormControl>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ export const InputSwitch: FC<InputSwitchProps> = ({
name={name}
type="checkbox"
className="lc-input-switch-input"
role="switch"
aria-checked={fieldValue}
{...fieldProps}
{...props}
checked={fieldValue}
Expand Down
Loading