11import { CircularProgress , FormControl , InputLabel , MenuItem , Select , TextField , Tooltip } from "@mui/material" ;
22import { Suspense } from "@suspensive/react" ;
33import * as React from "react" ;
4+ import { Control , Controller , FieldValues } from "react-hook-form" ;
45import * as R from "remeda" ;
56
67import { PriceDisplay } from "./price_display" ;
@@ -33,7 +34,8 @@ const SelectableOptionGroupInput: React.FC<{
3334 defaultValue ?: string ;
3435 disabled ?: boolean ;
3536 disabledReason ?: string ;
36- } > = ( { language, optionGroup, options, defaultValue, disabled, disabledReason } ) => {
37+ control : Control < FieldValues , unknown , FieldValues > ;
38+ } > = ( { language, optionGroup, options, defaultValue, disabled, disabledReason, control } ) => {
3739 const optionElements = options . map ( ( option ) => {
3840 const isOptionOutOfStock = R . isNumber ( option . leftover_stock ) && option . leftover_stock <= 0 ;
3941
@@ -54,9 +56,14 @@ const SelectableOptionGroupInput: React.FC<{
5456 const selectElement = (
5557 < FormControl fullWidth >
5658 < InputLabel id = { `${ optionGroup . id } label` } > { optionGroup . name } </ InputLabel >
57- < Select label = { `${ optionGroup . id } label` } name = { optionGroup . id } defaultValue = { defaultValue } disabled = { disabled } required >
58- { optionElements }
59- </ Select >
59+ < Controller
60+ control = { control }
61+ name = { optionGroup . id }
62+ rules = { { required : true } }
63+ disabled = { disabled }
64+ defaultValue = { defaultValue || "" }
65+ render = { ( { field } ) => < Select label = { `${ optionGroup . id } label` } { ...field } children = { optionElements } /> }
66+ />
6067 </ FormControl >
6168 ) ;
6269
@@ -68,17 +75,19 @@ const CustomResponseOptionGroupInput: React.FC<{
6875 defaultValue ?: string ;
6976 disabled ?: boolean ;
7077 disabledReason ?: string ;
71- } > = ( { optionGroup, defaultValue, disabled, disabledReason } ) => {
72- const pattern = ShopAPIUtil . getCustomResponsePattern ( optionGroup ) ?. source ;
73-
78+ control : Control < FieldValues , unknown , FieldValues > ;
79+ } > = ( { optionGroup, defaultValue, disabled, disabledReason, control } ) => {
7480 const textFieldElement = (
75- < TextField
76- label = { optionGroup . name }
81+ < Controller
82+ control = { control }
7783 name = { optionGroup . id }
78- required
79- defaultValue = { defaultValue }
84+ rules = { { pattern : ShopAPIUtil . getCustomResponsePattern ( optionGroup ) , required : true } }
8085 disabled = { disabled }
81- slotProps = { { htmlInput : { pattern } } }
86+ defaultValue = { defaultValue || "" }
87+ render = { ( { field, formState : { errors } } ) => {
88+ const errorMessage : string | undefined = errors ?. [ optionGroup . id ] ?. message ?. toString ( ) ;
89+ return < TextField label = { optionGroup . name } { ...field } error = { ! ! errors [ optionGroup . id ] } helperText = { errorMessage } /> ;
90+ } }
8291 />
8392 ) ;
8493
@@ -93,9 +102,17 @@ export const OptionGroupInput: React.FC<{
93102 defaultValue ?: string ;
94103 disabled ?: boolean ;
95104 disabledReason ?: string ;
96- } > = ( { language, optionGroup, options, defaultValue, disabled, disabledReason } ) =>
105+
106+ control : Control < FieldValues , unknown , FieldValues > ;
107+ } > = ( { language, optionGroup, options, defaultValue, disabled, disabledReason, control } ) =>
97108 optionGroup . is_custom_response ? (
98- < CustomResponseOptionGroupInput optionGroup = { optionGroup } defaultValue = { defaultValue } disabled = { disabled } disabledReason = { disabledReason } />
109+ < CustomResponseOptionGroupInput
110+ optionGroup = { optionGroup }
111+ defaultValue = { defaultValue }
112+ disabled = { disabled }
113+ disabledReason = { disabledReason }
114+ control = { control }
115+ />
99116 ) : (
100117 < SelectableOptionGroupInput
101118 language = { language || "ko" }
@@ -104,14 +121,16 @@ export const OptionGroupInput: React.FC<{
104121 defaultValue = { defaultValue }
105122 disabled = { disabled }
106123 disabledReason = { disabledReason }
124+ control = { control }
107125 />
108126 ) ;
109127
110128export const OrderProductRelationOptionInput : React . FC < {
111129 optionRel : ShopSchemas . OrderProductItem [ "options" ] [ number ] ;
112130 disabled ?: boolean ;
113131 disabledReason ?: string ;
114- } > = Suspense . with ( { fallback : < CircularProgress /> } , ( { optionRel, disabled, disabledReason } ) => {
132+ control : Control < FieldValues , unknown , FieldValues > ;
133+ } > = Suspense . with ( { fallback : < CircularProgress /> } , ( { optionRel, disabled, disabledReason, control } ) => {
115134 const { language } = ShopHooks . useShopContext ( ) ;
116135 let defaultValue : string | null = null ;
117136 let guessedDisabledReason : string | undefined = undefined ;
@@ -147,6 +166,7 @@ export const OrderProductRelationOptionInput: React.FC<{
147166 defaultValue = { defaultValue || undefined }
148167 disabled = { disabled || ! ShopAPIUtil . isOrderProductOptionModifiable ( optionRel ) }
149168 disabledReason = { disabledReason || guessedDisabledReason }
169+ control = { control }
150170 />
151171 ) ;
152172} ) ;
0 commit comments