Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TypeScript] Fix missing null checks in form helpers #7894

Merged
merged 1 commit into from
Jun 24, 2022
Merged
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
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