From 1d6b66310939666265a6108a098a056263d605e8 Mon Sep 17 00:00:00 2001 From: Wojciech Maj Date: Wed, 5 Jun 2024 22:52:08 +0200 Subject: [PATCH] Fix issues found by Biome --- .../react-date-picker/src/DateInput.spec.tsx | 6 ++-- packages/react-date-picker/src/DateInput.tsx | 14 ++++---- .../react-date-picker/src/DateInput/Input.tsx | 2 +- .../src/DateInput/MonthSelect.spec.tsx | 32 +++++++++---------- packages/react-date-picker/src/DatePicker.tsx | 6 ++-- .../src/shared/utils.spec.ts | 4 +-- 6 files changed, 31 insertions(+), 33 deletions(-) diff --git a/packages/react-date-picker/src/DateInput.spec.tsx b/packages/react-date-picker/src/DateInput.spec.tsx index fa14d60f..126acd99 100644 --- a/packages/react-date-picker/src/DateInput.spec.tsx +++ b/packages/react-date-picker/src/DateInput.spec.tsx @@ -499,11 +499,11 @@ describe('DateInput', () => { const { container } = render(); - const customInputs = container.querySelectorAll('input[data-input]'); + const customInputs = Array.from(container.querySelectorAll('input[data-input]')); - customInputs.forEach((customInput) => { + for (const customInput of customInputs) { fireEvent.change(customInput, { target: { value: '' } }); - }); + } expect(onChange).toHaveBeenCalledTimes(1); expect(onChange).toHaveBeenCalledWith(null, false); diff --git a/packages/react-date-picker/src/DateInput.tsx b/packages/react-date-picker/src/DateInput.tsx index 9302bbb1..329a490e 100644 --- a/packages/react-date-picker/src/DateInput.tsx +++ b/packages/react-date-picker/src/DateInput.tsx @@ -54,7 +54,7 @@ function getValue( const valueDate = toDate(rawValue); - if (isNaN(valueDate.getTime())) { + if (Number.isNaN(valueDate.getTime())) { throw new Error(`Invalid date: ${value}`); } @@ -147,7 +147,7 @@ function renderCustomInputs( {element} ); arr.push(divider); - const currentMatch = matches && matches[index]; + const currentMatch = matches?.[index]; if (currentMatch) { const renderFunction = @@ -405,7 +405,7 @@ export default function DateInput({ return; } - const isNumberKey = !isNaN(Number(key)); + const isNumberKey = !Number.isNaN(Number(key)); if (!isNumberKey) { return; @@ -455,12 +455,10 @@ export default function DateInput({ ].filter(filterBoolean); const values: Record = {}; - formElements.forEach((formElement) => { + for (const formElement of formElements) { values[formElement.name] = - 'valueAsNumber' in formElement - ? formElement.valueAsNumber - : Number((formElement as unknown as HTMLInputElement).value); - }); + 'valueAsNumber' in formElement ? formElement.valueAsNumber : Number(formElement.value); + } const isEveryValueEmpty = formElements.every((formElement) => !formElement.value); diff --git a/packages/react-date-picker/src/DateInput/Input.tsx b/packages/react-date-picker/src/DateInput/Input.tsx index c60c9e7c..471ee1b7 100644 --- a/packages/react-date-picker/src/DateInput/Input.tsx +++ b/packages/react-date-picker/src/DateInput/Input.tsx @@ -95,7 +95,7 @@ function getSelectionString(input: HTMLInputElement) { if ('getSelection' in window) { const selection = window.getSelection(); - return selection && selection.toString(); + return selection?.toString(); } return null; diff --git a/packages/react-date-picker/src/DateInput/MonthSelect.spec.tsx b/packages/react-date-picker/src/DateInput/MonthSelect.spec.tsx index fdd58666..02dd6b89 100644 --- a/packages/react-date-picker/src/DateInput/MonthSelect.spec.tsx +++ b/packages/react-date-picker/src/DateInput/MonthSelect.spec.tsx @@ -125,11 +125,11 @@ describe('MonthSelect', () => { const { container } = render(); const select = container.querySelector('select') as HTMLSelectElement; - const options = select.querySelectorAll('option'); + const options = Array.from(select.querySelectorAll('option')); - options.forEach((option) => { + for (const option of options) { expect(option).not.toBeDisabled(); - }); + } }); it('has all options enabled given minDate in a past year', () => { @@ -138,11 +138,11 @@ describe('MonthSelect', () => { ); const select = container.querySelector('select') as HTMLSelectElement; - const options = select.querySelectorAll('option[value]'); + const options = Array.from(select.querySelectorAll('option[value]')); - options.forEach((option) => { + for (const option of options) { expect(option).not.toBeDisabled(); - }); + } }); it('has first (month in minDate) options disabled given minDate in a current year', () => { @@ -154,13 +154,13 @@ describe('MonthSelect', () => { const options = Array.from(select.querySelectorAll('option')).slice(1); // Getting rid of "--" option // January - June - options.slice(0, 6).forEach((option) => { + for (const option of options.slice(0, 6)) { expect(option).toBeDisabled(); - }); + } // July - December - options.slice(6).forEach((option) => { + for (const option of options.slice(6)) { expect(option).not.toBeDisabled(); - }); + } }); it('has all options enabled given maxDate in a future year', () => { @@ -171,9 +171,9 @@ describe('MonthSelect', () => { const select = container.querySelector('select') as HTMLSelectElement; const options = Array.from(select.querySelectorAll('option')).slice(1); // Getting rid of "--" option - options.forEach((option) => { + for (const option of options) { expect(option).not.toBeDisabled(); - }); + } }); it('has last (month in maxDate) options disabled given maxDate in a current year', () => { @@ -185,12 +185,12 @@ describe('MonthSelect', () => { const options = Array.from(select.querySelectorAll('option')).slice(1); // Getting rid of "--" option // January - July - options.slice(0, 7).forEach((option) => { + for (const option of options.slice(0, 7)) { expect(option).not.toBeDisabled(); - }); + } // August - December - options.slice(7).forEach((option) => { + for (const option of options.slice(7)) { expect(option).toBeDisabled(); - }); + } }); }); diff --git a/packages/react-date-picker/src/DatePicker.tsx b/packages/react-date-picker/src/DatePicker.tsx index 8748c50a..d977bef5 100644 --- a/packages/react-date-picker/src/DatePicker.tsx +++ b/packages/react-date-picker/src/DatePicker.tsx @@ -477,18 +477,18 @@ export default function DatePicker(props: DatePickerProps) { closeCalendar({ reason: 'outsideAction' }); } }, - [calendarWrapper, closeCalendar, wrapper], + [closeCalendar], ); const handleOutsideActionListeners = useCallback( (shouldListen = isOpen) => { - outsideActionEvents.forEach((event) => { + for (const event of outsideActionEvents) { if (shouldListen) { document.addEventListener(event, onOutsideAction); } else { document.removeEventListener(event, onOutsideAction); } - }); + } if (shouldListen) { document.addEventListener('keydown', onKeyDown); diff --git a/packages/react-date-picker/src/shared/utils.spec.ts b/packages/react-date-picker/src/shared/utils.spec.ts index bb67a4c3..6ed3ca75 100644 --- a/packages/react-date-picker/src/shared/utils.spec.ts +++ b/packages/react-date-picker/src/shared/utils.spec.ts @@ -41,7 +41,7 @@ describe('safeMin', () => { it('returns Infinity given no values', () => { const result = safeMin(); - expect(result).toBe(Infinity); + expect(result).toBe(Number.POSITIVE_INFINITY); }); it('returns the smallest value given valid numbers', () => { @@ -79,7 +79,7 @@ describe('safeMax', () => { it('returns -Infinity given no values', () => { const result = safeMax(); - expect(result).toBe(-Infinity); + expect(result).toBe(Number.NEGATIVE_INFINITY); }); it('returns the largest value given valid numbers', () => {