Skip to content
This repository has been archived by the owner on Jan 10, 2025. It is now read-only.

Numeric string fields #2643

Closed
Closed
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
6 changes: 4 additions & 2 deletions packages/react-form/src/hooks/field/field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {useCallback, useEffect, useMemo} from 'react';
import isEqual from 'fast-deep-equal';

import type {Validates, Field, DirtyStateComparator} from '../../types';
import {normalizeValidation, isChangeEvent} from '../../utilities';
import {normalizeValidation, isChangeEvent, isNumericString} from '../../utilities';

import {
updateAction,
Expand Down Expand Up @@ -138,7 +138,9 @@ export function useField<Value = string>(
return undefined;
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[state.value, ...dependencies],
[state.value, ...dependencies].map(value =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add some unit tests that showcase the behaviour that would have broken without this change?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes @BPScott , I have added tests in utilities.test.ts. I'll add the changeset entry now, thanks!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests in utilities.test.ts test the new helper that you've added. They don't test this line we're commenting on. It would be totally possible to undo the change on this line and all the tests would continue to pass. Yet it's this line that you're fixing a bug on, the addition of utility functions is kinda incidental.

I would like to see an test of useField that uses a numeric string, that fails if this line is as it is on main, and passes when this line is changed, so that I can understand the expected behaviour that you want to see.

isNumericString(value) ? Number(value) : value),

);

const onChange = useCallback(
Expand Down
24 changes: 24 additions & 0 deletions packages/react-form/src/tests/utilities.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
validateAll,
fieldsToArray,
reduceFields,
isNumericString
} from '../utilities';
import type {Field} from '../types';

Expand Down Expand Up @@ -236,3 +237,26 @@ describe('reduceFields()', () => {
expect(fieldsArray).toContain(work);
});
});

describe('isNumericString()', () => {
it('returns true for numeric strings', () => {
expect(isNumericString('123')).toBe(true);
expect(isNumericString('3.14')).toBe(true);
expect(isNumericString('0.0001')).toBe(true);
expect(isNumericString('-50')).toBe(true);
});

it('returns false for non-numeric strings', () => {
expect(isNumericString('abc')).toBe(false);
expect(isNumericString('12a')).toBe(false);
expect(isNumericString('')).toBe(false);
});

it('returns false for non-string values', () => {
expect(isNumericString(123)).toBe(false);
expect(isNumericString(3.14)).toBe(false);
expect(isNumericString(true)).toBe(false);
expect(isNumericString(undefined)).toBe(false);
expect(isNumericString(null)).toBe(false);
});
});
4 changes: 4 additions & 0 deletions packages/react-form/src/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,3 +248,7 @@ export function makeCleanDynamicLists(dynamicLists?: DynamicListBag) {
});
}
}

export function isNumericString(value: any): value is string {
return typeof value === 'string' && value !== '' && !isNaN(Number(value));
}