Skip to content

Commit

Permalink
Enhance defaultValuesOf type definition for array types
Browse files Browse the repository at this point in the history
  • Loading branch information
mununki committed Oct 21, 2024
1 parent 7e354d2 commit a19dce5
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 4 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# v0.0.3 (unreleased)


- Enhance defaultValuesOf type definition for array types
- For array types (e.g., `array<t>`), the defaultValuesOf type now uses
`defaultValuesOfT` for the array items instead of `array<t>`
- This allows for more precise default value handling in nested array structures

# v0.0.2

Expand Down
3 changes: 1 addition & 2 deletions doc/transformation.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type inputsWithId = {
type defaultValuesOfInputs = {
example?: string,
exampleRequired?: string,
cart?: array<item>
cart?: array<defaultValuesOfItem>
}
type rec fieldStateOfInputs = {invalid: bool, isDirty: bool, isTouched: bool, error: fieldErrorOfInputs}
and fieldErrorOfInputs = {message?: string}
Expand All @@ -39,7 +39,6 @@ type rec watchReturnOfInputs =
| Object(Js.Dict.t<watchReturnOfInputs>)
| Array(array<watchReturnOfInputs>)
type rec useFormReturnOfInputs<'setValueAs> = {
control: controlOfInputs,
register: (variantOfInputs, ~options: registerOptionsOfInputs<'setValueAs>=?) => JsxDOM.domProps,
Expand Down
26 changes: 26 additions & 0 deletions src/ppx/signature.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
26 changes: 26 additions & 0 deletions src/ppx/structure.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Binary file modified test/bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion test/src/pages/field_array_res/FieldArray.res
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down

0 comments on commit a19dce5

Please sign in to comment.