Skip to content

Commit 4880202

Browse files
authored
Fix currency input regex + migrate to storybook/test v9 (#10)
* fix: tighten currency input regex + migrate to storybook/test v9 - Replace permissive regex with two-stage parse: strip non-numeric, then clamp to valid number format (optional leading minus, single decimal point). Prevents malformed input like '1.2.3' or '1-2-3'. - Remove @storybook/test (v8) and @storybook/testing-library, migrate imports to storybook/test (v9 path). Eliminates SB9 compatibility warning. - All 51 interaction tests pass. * fix: hoist regex to module scope + sort imports for biome lint - Extract regex literals to module-level constants (biome/useTopLevelRegex) - Fix import ordering in story files (biome/organizeImports) - All 51 tests still passing * Add changeset for currency input fix (patch)
1 parent 742ec34 commit 4880202

6 files changed

Lines changed: 29 additions & 685 deletions

File tree

.changeset/currency-input-fix.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"@lambdacurry/medusa-forms": patch
3+
---
4+
5+
Fix currency input regex to reject malformed numbers and migrate to storybook/test v9
6+
7+
- Tighten number parsing: strip non-numeric, then clamp to valid format (no more `1.2.3` or `1-2-3`)
8+
- Migrate `@storybook/test@8``storybook/test` (v9), removing compatibility warning

apps/docs/package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@
2323
},
2424
"devDependencies": {
2525
"@storybook/addon-docs": "^9.0.6",
26-
"@storybook/test": "^8.6.14",
2726
"@storybook/test-runner": "^0.22.1",
28-
"@storybook/testing-library": "^0.2.2",
2927
"@tailwindcss/postcss": "^4.1.8",
3028
"@tailwindcss/vite": "^4.0.0",
3129
"@types/react": "^19.0.0",

apps/docs/src/medusa-forms/ControlledCurrencyInput.stories.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { zodResolver } from '@hookform/resolvers/zod';
22
import { ControlledCurrencyInput } from '@lambdacurry/medusa-forms/controlled/ControlledCurrencyInput';
33
import type { Meta, StoryObj } from '@storybook/react-vite';
4-
import { expect, userEvent, waitFor, within } from '@storybook/test';
54
import { useEffect } from 'react';
65
import { FormProvider, useForm } from 'react-hook-form';
6+
import { expect, userEvent, waitFor, within } from 'storybook/test';
77
import { z } from 'zod';
88

99
const meta = {

apps/docs/src/medusa-forms/ControlledInput.stories.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ControlledInput } from '@lambdacurry/medusa-forms/controlled/ControlledInput';
22
import type { Meta, StoryObj } from '@storybook/react-vite';
3-
import { expect, userEvent, waitFor, within } from '@storybook/test';
43
import { FormProvider, useForm } from 'react-hook-form';
4+
import { expect, userEvent, waitFor, within } from 'storybook/test';
55

66
const meta = {
77
title: 'Medusa Forms/Controlled Input',

packages/medusa-forms/src/controlled/ControlledCurrencyInput.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import { Controller, type ControllerProps, type FieldValues, type Path, useFormC
33
import { CurrencyInput, type CurrencyInputProps } from '../ui/CurrencyInput';
44
import { type ControlledRules, serializeDisplayValue, splitTransformRules, transformValue } from './valueTransforms';
55

6+
/** Match a valid number: optional leading minus, digits, optional single decimal point + digits */
7+
const NUMERIC_VALUE_REGEX = /^-?\d*\.?\d*/;
8+
const NON_NUMERIC_REGEX = /[^0-9.-]/g;
9+
610
export type ControlledCurrencyInputProps<T extends FieldValues> = CurrencyInputProps &
711
Omit<ControllerProps<T>, 'render' | 'control' | 'rules'> & {
812
name: Path<T>;
@@ -38,7 +42,8 @@ export const ControlledCurrencyInput = <T extends FieldValues>({
3842
onChange(e);
3943
}
4044

41-
const value = e.target.value.replace(/[^0-9.-]+/g, '');
45+
const cleaned = e.target.value.replace(NON_NUMERIC_REGEX, '');
46+
const value = cleaned.match(NUMERIC_VALUE_REGEX)?.[0] ?? '';
4247
field.onChange(hasTransform ? transformValue(value, rules) : value);
4348
}}
4449
/>

0 commit comments

Comments
 (0)