diff --git a/CHANGELOG.md b/CHANGELOG.md index fdde474..a0ded52 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ # v0.0.3 (unreleased) - +- Enhance defaultValuesOf type definition for array types + - For array types (e.g., `array`), the defaultValuesOf type now uses + `defaultValuesOfT` for the array items instead of `array` + - This allows for more precise default value handling in nested array structures # v0.0.2 diff --git a/doc/transformation.md b/doc/transformation.md index c81908c..5618958 100644 --- a/doc/transformation.md +++ b/doc/transformation.md @@ -26,7 +26,7 @@ type inputsWithId = { type defaultValuesOfInputs = { example?: string, exampleRequired?: string, - cart?: array + cart?: array } type rec fieldStateOfInputs = {invalid: bool, isDirty: bool, isTouched: bool, error: fieldErrorOfInputs} and fieldErrorOfInputs = {message?: string} @@ -39,7 +39,6 @@ type rec watchReturnOfInputs = | Object(Js.Dict.t) | Array(array) - type rec useFormReturnOfInputs<'setValueAs> = { control: controlOfInputs, register: (variantOfInputs, ~options: registerOptionsOfInputs<'setValueAs>=?) => JsxDOM.domProps, diff --git a/src/ppx/signature.ml b/src/ppx/signature.ml index 104f4b1..c1ffa7d 100644 --- a/src/ppx/signature.ml +++ b/src/ppx/signature.ml @@ -40,8 +40,34 @@ let map_type_decl (Ptype_record (lds |> List.map (fun ld -> + let new_pld_type = + match ld.pld_type with + | { + ptyp_desc = + Ptyp_constr + ({ txt = Lident "array" }, [ item_type ]); + } -> ( + match item_type.ptyp_desc with + | Ptyp_constr ({ txt = Lident item_name }, []) + when not + (List.mem item_name + [ + "string"; "float"; "int"; "bool"; + ]) -> + Typ.constr (lid "array") + [ + Typ.constr + (lid + ("defaultValuesOf" + ^ capitalize item_name)) + []; + ] + | _ -> ld.pld_type) + | _ -> ld.pld_type + in { ld with + pld_type = new_pld_type; pld_attributes = remove_optional_attribute ld.pld_attributes |> add_optional_attribute; diff --git a/src/ppx/structure.ml b/src/ppx/structure.ml index 974f0df..cdf6a9e 100644 --- a/src/ppx/structure.ml +++ b/src/ppx/structure.ml @@ -40,8 +40,34 @@ let map_type_decl (Ptype_record (lds |> List.map (fun ld -> + let new_pld_type = + match ld.pld_type with + | { + ptyp_desc = + Ptyp_constr + ({ txt = Lident "array" }, [ item_type ]); + } -> ( + match item_type.ptyp_desc with + | Ptyp_constr ({ txt = Lident item_name }, []) + when not + (List.mem item_name + [ + "string"; "float"; "int"; "bool"; + ]) -> + Typ.constr (lid "array") + [ + Typ.constr + (lid + ("defaultValuesOf" + ^ capitalize item_name)) + []; + ] + | _ -> ld.pld_type) + | _ -> ld.pld_type + in { ld with + pld_type = new_pld_type; pld_attributes = remove_optional_attribute ld.pld_attributes |> add_optional_attribute; diff --git a/test/bun.lockb b/test/bun.lockb index 2de3644..24f7e10 100755 Binary files a/test/bun.lockb and b/test/bun.lockb differ diff --git a/test/src/pages/field_array_res/FieldArray.res b/test/src/pages/field_array_res/FieldArray.res index 407823b..53bc10c 100644 --- a/test/src/pages/field_array_res/FieldArray.res +++ b/test/src/pages/field_array_res/FieldArray.res @@ -13,7 +13,7 @@ let default = () => { let {register, control, handleSubmit, formState: {errors}} = useFormOfInputs( ~options={ defaultValues: { - cart: [{name: "text", quantity: 1., price: 23.}], + cart: [{quantity: 1., price: 23.}], }, mode: #onBlur, },