Skip to content

Commit

Permalink
null safe value access
Browse files Browse the repository at this point in the history
  • Loading branch information
PawelSuwinski committed May 27, 2024
1 parent 0cdf153 commit 4dc1f9b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/useOnSubmit.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ test.each([
name: 'Book name 2',
authors: ['Author 1', 'Author 2'],
},
{
id: '3',
name: 'Book name 3',
authors: ['Author 1', 'Author 2'],
cover: null,
},
])('Call update without file inputs ($name)', async (values: RaRecord) => {
let save;
const Dummy = () => {
Expand Down
6 changes: 4 additions & 2 deletions src/useOnSubmit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ import { useCallback } from 'react';
import { useCreate, useNotify, useRedirect, useUpdate } from 'react-admin';
import type { HttpError, RaRecord } from 'react-admin';
import { useParams } from 'react-router-dom';
import lodashIsPlainObject from 'lodash.isplainobject';
import getIdentifierValue from './getIdentifierValue.js';
import type { SubmissionErrors, UseOnSubmitProps } from './types.js';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const findFile = (values: any[]): object | undefined =>
const findFile = (values: any[]): Blob | undefined =>
values.find((value) =>
Array.isArray(value)
? findFile(value)
: typeof value === 'object' && value.rawFile instanceof File,
: lodashIsPlainObject(value) &&
Object.values(value).find((val) => val instanceof File),
);

const useOnSubmit = ({
Expand Down

0 comments on commit 4dc1f9b

Please sign in to comment.