From 4dc1f9bf8b59fd9b4cfd0aba77c83c0694da8110 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Suwi=C5=84ski?= Date: Mon, 27 May 2024 16:43:54 +0200 Subject: [PATCH] null safe value access --- src/useOnSubmit.test.tsx | 6 ++++++ src/useOnSubmit.ts | 6 ++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/useOnSubmit.test.tsx b/src/useOnSubmit.test.tsx index 3d1343e8..34ac313e 100644 --- a/src/useOnSubmit.test.tsx +++ b/src/useOnSubmit.test.tsx @@ -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 = () => { diff --git a/src/useOnSubmit.ts b/src/useOnSubmit.ts index 0313b44b..fcbbc49b 100644 --- a/src/useOnSubmit.ts +++ b/src/useOnSubmit.ts @@ -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 = ({