Skip to content

Commit 105c14c

Browse files
authored
Merge pull request #7894 from marmelab/ts-strict-null-checks
[TypeScript] Fix missing null checks in form helpers
2 parents 8e6e259 + 99735ca commit 105c14c

10 files changed

+17
-13
lines changed

packages/ra-core/src/form/FormDataConsumer.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export const FormDataConsumerView = (props: Props) => {
5656
let ret;
5757

5858
// If we have an index, we are in an iterator like component (such as the SimpleFormIterator)
59-
if (typeof index !== 'undefined') {
59+
if (typeof index !== 'undefined' && source) {
6060
scopedFormData = get(formData, source);
6161
getSource = (scopedSource: string) => {
6262
getSourceHasBeenCalled = true;

packages/ra-core/src/form/FormGroupsContext.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { createContext } from 'react';
22

3-
export const FormGroupsContext = createContext<FormGroupsContextValue>(
4-
undefined
5-
);
3+
export const FormGroupsContext = createContext<
4+
FormGroupsContextValue | undefined
5+
>(undefined);
66

77
export type FormGroupSubscriber = () => void;
88

packages/ra-core/src/form/choices/ChoicesContext.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import { FilterPayload, RaRecord, SortPayload } from '../../types';
66
*
77
* Use the useChoicesContext() hook to read the context.
88
*/
9-
export const ChoicesContext = createContext<ChoicesContextValue>(undefined);
9+
export const ChoicesContext = createContext<ChoicesContextValue | undefined>(
10+
undefined
11+
);
1012

1113
export type ChoicesContextValue<RecordType extends RaRecord = any> = {
1214
allChoices: RecordType[];

packages/ra-core/src/form/getFormInitialValues.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { RaRecord } from '../types';
33

44
export default function getFormInitialValues(
55
defaultValues: DefaultValue,
6-
record: Partial<RaRecord>
6+
record?: Partial<RaRecord>
77
) {
88
const finalInitialValues = merge(
99
{},

packages/ra-core/src/form/getSimpleValidationResolver.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { ValidationErrorMessageWithArgs } from './validate';
44

55
// Flattening an object into path keys:
66
// https://github.com/lodash/lodash/issues/2240#issuecomment-418820848
7-
export const flattenErrors = (obj, path = []) =>
7+
export const flattenErrors = (obj, path: string[] = []) =>
88
!_.isObject(obj)
99
? { [path.join('.')]: obj }
1010
: _.reduce(

packages/ra-core/src/form/setSubmissionErrors.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export const setSubmissionErrors = (
3939
});
4040
});
4141
};
42-
const setErrorFromObject = (errors: any, rootPath: string) => {
42+
const setErrorFromObject = (errors: FieldValues, rootPath: string) => {
4343
Object.entries(errors).forEach(([name, error]) => {
4444
if (typeof error === 'object') {
4545
setErrorFromObject(error, `${rootPath}${name}.`);

packages/ra-core/src/form/useApplyInputDefaultValues.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import { InputProps } from './useInput';
88
* This hook updates the input default value whenever the record changes
99
* It applies either the record value if it has one or the defaultValue if it was specified
1010
*/
11-
export const useApplyInputDefaultValues = (props: Partial<InputProps>) => {
11+
export const useApplyInputDefaultValues = (
12+
props: Partial<InputProps> & { source: string }
13+
) => {
1214
const { defaultValue, source } = props;
1315
const record = useRecordContext(props);
1416
const { getValues, resetField } = useFormContext();

packages/ra-core/src/form/useAugmentedForm.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export const useAugmentedForm = (props: UseAugmentedFormProps) => {
9898

9999
// warn when unsaved change
100100
useWarnWhenUnsavedChanges(
101-
warnWhenUnsavedChanges,
101+
Boolean(warnWhenUnsavedChanges),
102102
formRootPathname,
103103
form.control
104104
);

packages/ra-core/src/form/useFormGroup.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ type FieldState = {
1313
};
1414

1515
type FormGroupState = {
16-
errors: object;
16+
errors?: object;
1717
isDirty: boolean;
1818
isTouched: boolean;
1919
isValid: boolean;
@@ -128,7 +128,7 @@ export const useFormGroup = (name: string): FormGroupState => {
128128
export const getFormGroupState = (
129129
fieldStates: FieldState[]
130130
): FormGroupState => {
131-
return fieldStates.reduce(
131+
return fieldStates.reduce<FormGroupState>(
132132
(acc, fieldState) => {
133133
let errors = acc.errors || {};
134134

packages/ra-core/src/form/useSuggestions.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ export const getSuggestionsFactory = ({
174174
getChoiceText: (choice: any) => string | ReactElement;
175175
getChoiceValue: (choice: any) => string;
176176
}) => filter => {
177-
let suggestions = [];
177+
let suggestions: any[] = [];
178178
// if an item is selected and matches the filter
179179
if (
180180
selectedItem &&

0 commit comments

Comments
 (0)