11import { zodResolver } from '@hookform/resolvers/zod' ;
22import { ControlledCurrencyInput } from '@lambdacurry/medusa-forms/controlled/ControlledCurrencyInput' ;
33import type { Meta , StoryObj } from '@storybook/react-vite' ;
4+ import { useEffect } from 'react' ;
45import { FormProvider , useForm } from 'react-hook-form' ;
56import { z } from 'zod' ;
67
@@ -21,7 +22,7 @@ export default meta;
2122type Story = StoryObj < typeof meta > ;
2223
2324interface CurrencyFormData {
24- price : string ;
25+ price : string | number ;
2526}
2627
2728// Base wrapper component for stories
@@ -61,6 +62,55 @@ const CurrencyInputWithHookForm = ({
6162 ) ;
6263} ;
6364
65+ const CurrencyInputWithRequiredError = ( ) => {
66+ const form = useForm < CurrencyFormData > ( {
67+ defaultValues : { price : '' } ,
68+ mode : 'onChange' ,
69+ } ) ;
70+
71+ useEffect ( ( ) => {
72+ form . trigger ( 'price' ) . then ( ( ) => undefined ) ;
73+ } , [ form ] ) ;
74+
75+ return (
76+ < FormProvider { ...form } >
77+ < div className = "w-[400px]" >
78+ < ControlledCurrencyInput
79+ name = "price"
80+ label = "Required price"
81+ symbol = "$"
82+ code = "usd"
83+ rules = { { required : 'Price is required' } }
84+ />
85+ </ div >
86+ </ FormProvider >
87+ ) ;
88+ } ;
89+
90+ const CurrencyInputWithValueAsNumber = ( ) => {
91+ const form = useForm < CurrencyFormData > ( {
92+ defaultValues : { price : '' } ,
93+ } ) ;
94+ const price = form . watch ( 'price' ) ;
95+
96+ return (
97+ < FormProvider { ...form } >
98+ < div className = "w-[400px] space-y-4" >
99+ < ControlledCurrencyInput < CurrencyFormData >
100+ name = "price"
101+ label = "Numeric price"
102+ symbol = "$"
103+ code = "usd"
104+ rules = { { valueAsNumber : true } }
105+ />
106+ < pre className = "rounded bg-gray-100 p-2 text-xs" >
107+ { JSON . stringify ( { value : price , type : typeof price } , null , 2 ) }
108+ </ pre >
109+ </ div >
110+ </ FormProvider >
111+ ) ;
112+ } ;
113+
64114// 1. Different Currency Symbols
65115export const USDCurrency : Story = {
66116 args : {
@@ -194,6 +244,24 @@ export const RequiredFieldValidation: Story = {
194244 ) ,
195245} ;
196246
247+ export const RequiredRuleError : Story = {
248+ args : {
249+ name : 'price' ,
250+ symbol : '$' ,
251+ code : 'usd' ,
252+ } ,
253+ render : ( ) => < CurrencyInputWithRequiredError /> ,
254+ } ;
255+
256+ export const ValueAsNumber : Story = {
257+ args : {
258+ name : 'price' ,
259+ symbol : '$' ,
260+ code : 'usd' ,
261+ } ,
262+ render : ( ) => < CurrencyInputWithValueAsNumber /> ,
263+ } ;
264+
197265const customValidationSchema = z . object ( {
198266 price : z . string ( ) . refine ( ( val ) => {
199267 const num = Number . parseFloat ( val ) ;
0 commit comments