Skip to content

Commit b18443b

Browse files
committed
feat: add transparent nested path autocomplete to via PathsOf<F>
1 parent 4e0dd92 commit b18443b

2 files changed

Lines changed: 13 additions & 13 deletions

File tree

src/Form.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
FormInterface,
1919
FieldsDefinitions,
2020
FormConfig,
21+
PathsOf,
2122
} from "./models/FormInterface";
2223
import { FieldPropsEnum } from "./models/FieldProps";
2324
import { OptionsEnum } from "./models/OptionsModel";
@@ -184,21 +185,20 @@ export default class Form<F extends Record<string, any> = Record<string, any>> e
184185

185186
/**
186187
* Select a field by key with type inference.
187-
* Provides autocomplete for field keys when the form is typed with a generic `F`.
188+
* Provides transparent autocomplete for both top-level keys (`keyof F`)
189+
* AND nested paths (`PathsOf<F>`) without any type annotations needed.
188190
*
189191
* @example
190-
* // Top-level keys get autocomplete from keyof F:
192+
* // Top-level keys autocomplete from keyof F:
191193
* form.$('username'); // returns Field<string>
192194
*
193195
* @example
194-
* // For nested paths, use the `PathsOf` utility type:
195-
* import { PathsOf } from 'mobx-react-form';
196-
*
197-
* function getField(form: Form<NestedClubFields>, path: PathsOf<NestedClubFields>) {
198-
* return form.$(path); // path autocompletes to "club" | "club.name" | "members[].firstname" | ...
199-
* }
196+
* // Nested paths — autocomplete from PathsOf<F>:
197+
* form.$('club'); // "club"
198+
* form.$('club.name'); // "club.name" ← autocomplete after dot!
199+
* form.$('members[].firstname'); // "members[].firstname"
200200
*/
201-
override $(key: keyof F): Field<F[keyof F]> {
201+
override $(key: keyof F | PathsOf<F>): Field<F[keyof F]> {
202202
return super.$(key as string) as Field<F[keyof F]>;
203203
}
204204

tests/fixtures/nested/form.m1.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ const fields = {
2323
};
2424

2525
const input = {
26-
"club": (value) => (value === null ? "" : value),
26+
club: (value) => (value === null ? "" : value),
2727
"memebers[].firstname": (value) => (value === null ? "" : value),
2828
"memebers[].yearOfBirth": (value) => (value === null ? "" : value),
2929
"memebers[].hobbies[]": (value) => (value === null ? "" : value),
30-
}
30+
};
3131

3232
const labels = {
3333
club: "The Club",
@@ -47,7 +47,7 @@ const schema = (y) =>
4747
lastname: y.string().required(),
4848
yearOfBirth: y.number().required().positive().integer().nullable(),
4949
hobbies: y.array().of(y.string().required().nullable()),
50-
})
50+
}),
5151
),
5252
});
5353

@@ -64,5 +64,5 @@ const options: OptionsModel = {
6464

6565
export default new Form(
6666
{ fields, labels, input },
67-
{ plugins, options, name: "Nested-M1" }
67+
{ plugins, options, name: "Nested-M1" },
6868
);

0 commit comments

Comments
 (0)