Skip to content

Commit

Permalink
Merge pull request #7894 from marmelab/ts-strict-null-checks
Browse files Browse the repository at this point in the history
[TypeScript] Fix missing null checks in form helpers
  • Loading branch information
djhi authored Jun 24, 2022
2 parents 8e6e259 + 99735ca commit 105c14c
Show file tree
Hide file tree
Showing 10 changed files with 17 additions and 13 deletions.
2 changes: 1 addition & 1 deletion packages/ra-core/src/form/FormDataConsumer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const FormDataConsumerView = (props: Props) => {
let ret;

// If we have an index, we are in an iterator like component (such as the SimpleFormIterator)
if (typeof index !== 'undefined') {
if (typeof index !== 'undefined' && source) {
scopedFormData = get(formData, source);
getSource = (scopedSource: string) => {
getSourceHasBeenCalled = true;
Expand Down
6 changes: 3 additions & 3 deletions packages/ra-core/src/form/FormGroupsContext.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { createContext } from 'react';

export const FormGroupsContext = createContext<FormGroupsContextValue>(
undefined
);
export const FormGroupsContext = createContext<
FormGroupsContextValue | undefined
>(undefined);

export type FormGroupSubscriber = () => void;

Expand Down
4 changes: 3 additions & 1 deletion packages/ra-core/src/form/choices/ChoicesContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import { FilterPayload, RaRecord, SortPayload } from '../../types';
*
* Use the useChoicesContext() hook to read the context.
*/
export const ChoicesContext = createContext<ChoicesContextValue>(undefined);
export const ChoicesContext = createContext<ChoicesContextValue | undefined>(
undefined
);

export type ChoicesContextValue<RecordType extends RaRecord = any> = {
allChoices: RecordType[];
Expand Down
2 changes: 1 addition & 1 deletion packages/ra-core/src/form/getFormInitialValues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { RaRecord } from '../types';

export default function getFormInitialValues(
defaultValues: DefaultValue,
record: Partial<RaRecord>
record?: Partial<RaRecord>
) {
const finalInitialValues = merge(
{},
Expand Down
2 changes: 1 addition & 1 deletion packages/ra-core/src/form/getSimpleValidationResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ValidationErrorMessageWithArgs } from './validate';

// Flattening an object into path keys:
// https://github.com/lodash/lodash/issues/2240#issuecomment-418820848
export const flattenErrors = (obj, path = []) =>
export const flattenErrors = (obj, path: string[] = []) =>
!_.isObject(obj)
? { [path.join('.')]: obj }
: _.reduce(
Expand Down
2 changes: 1 addition & 1 deletion packages/ra-core/src/form/setSubmissionErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const setSubmissionErrors = (
});
});
};
const setErrorFromObject = (errors: any, rootPath: string) => {
const setErrorFromObject = (errors: FieldValues, rootPath: string) => {
Object.entries(errors).forEach(([name, error]) => {
if (typeof error === 'object') {
setErrorFromObject(error, `${rootPath}${name}.`);
Expand Down
4 changes: 3 additions & 1 deletion packages/ra-core/src/form/useApplyInputDefaultValues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import { InputProps } from './useInput';
* This hook updates the input default value whenever the record changes
* It applies either the record value if it has one or the defaultValue if it was specified
*/
export const useApplyInputDefaultValues = (props: Partial<InputProps>) => {
export const useApplyInputDefaultValues = (
props: Partial<InputProps> & { source: string }
) => {
const { defaultValue, source } = props;
const record = useRecordContext(props);
const { getValues, resetField } = useFormContext();
Expand Down
2 changes: 1 addition & 1 deletion packages/ra-core/src/form/useAugmentedForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export const useAugmentedForm = (props: UseAugmentedFormProps) => {

// warn when unsaved change
useWarnWhenUnsavedChanges(
warnWhenUnsavedChanges,
Boolean(warnWhenUnsavedChanges),
formRootPathname,
form.control
);
Expand Down
4 changes: 2 additions & 2 deletions packages/ra-core/src/form/useFormGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type FieldState = {
};

type FormGroupState = {
errors: object;
errors?: object;
isDirty: boolean;
isTouched: boolean;
isValid: boolean;
Expand Down Expand Up @@ -128,7 +128,7 @@ export const useFormGroup = (name: string): FormGroupState => {
export const getFormGroupState = (
fieldStates: FieldState[]
): FormGroupState => {
return fieldStates.reduce(
return fieldStates.reduce<FormGroupState>(
(acc, fieldState) => {
let errors = acc.errors || {};

Expand Down
2 changes: 1 addition & 1 deletion packages/ra-core/src/form/useSuggestions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ export const getSuggestionsFactory = ({
getChoiceText: (choice: any) => string | ReactElement;
getChoiceValue: (choice: any) => string;
}) => filter => {
let suggestions = [];
let suggestions: any[] = [];
// if an item is selected and matches the filter
if (
selectedItem &&
Expand Down

0 comments on commit 105c14c

Please sign in to comment.