@ariakit/core 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (163) hide show
  1. package/.eslintignore +6 -0
  2. package/CHANGELOG.md +18 -0
  3. package/checkbox/checkbox-store/package.json +7 -0
  4. package/cjs/__chunks/2YFRPUZP.cjs +30 -0
  5. package/cjs/__chunks/3BBA3Z5G.cjs +143 -0
  6. package/cjs/__chunks/3KP2MDG6.cjs +1491 -0
  7. package/cjs/__chunks/5D5Y5EI4.cjs +146 -0
  8. package/cjs/__chunks/7ZXWQTAY.cjs +71 -0
  9. package/cjs/__chunks/AZVDLKO3.cjs +286 -0
  10. package/cjs/__chunks/EFEGT32M.cjs +12 -0
  11. package/cjs/__chunks/EIDN2CWH.cjs +57 -0
  12. package/cjs/__chunks/GDZQUFNP.cjs +30 -0
  13. package/cjs/__chunks/NX5OHIMM.cjs +169 -0
  14. package/cjs/__chunks/OFNGELMA.cjs +182 -0
  15. package/cjs/checkbox/checkbox-store.cjs +27 -0
  16. package/cjs/checkbox/checkbox-store.d.ts +34 -0
  17. package/cjs/collection/collection-store.cjs +9 -0
  18. package/cjs/collection/collection-store.d.ts +65 -0
  19. package/cjs/combobox/combobox-store.cjs +163 -0
  20. package/cjs/combobox/combobox-store.d.ts +67 -0
  21. package/cjs/composite/composite-overflow-store.cjs +15 -0
  22. package/cjs/composite/composite-overflow-store.d.ts +10 -0
  23. package/cjs/composite/composite-store.cjs +11 -0
  24. package/cjs/composite/composite-store.d.ts +202 -0
  25. package/cjs/dialog/dialog-store.cjs +9 -0
  26. package/cjs/dialog/dialog-store.d.ts +10 -0
  27. package/cjs/disclosure/disclosure-store.cjs +8 -0
  28. package/cjs/disclosure/disclosure-store.d.ts +87 -0
  29. package/cjs/form/form-store.cjs +241 -0
  30. package/cjs/form/form-store.d.ts +236 -0
  31. package/cjs/form/types.cjs +1 -0
  32. package/cjs/form/types.d.ts +38 -0
  33. package/cjs/hovercard/hovercard-store.cjs +11 -0
  34. package/cjs/hovercard/hovercard-store.d.ts +45 -0
  35. package/cjs/index.cjs +5 -0
  36. package/cjs/index.d.ts +2 -0
  37. package/cjs/menu/menu-bar-store.cjs +27 -0
  38. package/cjs/menu/menu-bar-store.d.ts +10 -0
  39. package/cjs/menu/menu-store.cjs +108 -0
  40. package/cjs/menu/menu-store.d.ts +70 -0
  41. package/cjs/popover/popover-store.cjs +10 -0
  42. package/cjs/popover/popover-store.d.ts +150 -0
  43. package/cjs/radio/radio-store.cjs +36 -0
  44. package/cjs/radio/radio-store.d.ts +31 -0
  45. package/cjs/select/select-store.cjs +164 -0
  46. package/cjs/select/select-store.d.ts +87 -0
  47. package/cjs/tab/tab-store.cjs +126 -0
  48. package/cjs/tab/tab-store.d.ts +78 -0
  49. package/cjs/toolbar/toolbar-store.cjs +27 -0
  50. package/cjs/toolbar/toolbar-store.d.ts +21 -0
  51. package/cjs/tooltip/tooltip-store.cjs +98 -0
  52. package/cjs/tooltip/tooltip-store.d.ts +28 -0
  53. package/cjs/tsconfig.build.tsbuildinfo +1 -0
  54. package/cjs/utils/array.cjs +12 -0
  55. package/cjs/utils/array.d.ts +29 -0
  56. package/cjs/utils/dom.cjs +38 -0
  57. package/cjs/utils/dom.d.ts +105 -0
  58. package/cjs/utils/events.cjs +132 -0
  59. package/cjs/utils/events.d.ts +73 -0
  60. package/cjs/utils/focus.cjs +222 -0
  61. package/cjs/utils/focus.d.ts +117 -0
  62. package/cjs/utils/misc.cjs +40 -0
  63. package/cjs/utils/misc.d.ts +111 -0
  64. package/cjs/utils/platform.cjs +15 -0
  65. package/cjs/utils/platform.d.ts +20 -0
  66. package/cjs/utils/store.cjs +9 -0
  67. package/cjs/utils/store.d.ts +99 -0
  68. package/cjs/utils/types.cjs +1 -0
  69. package/cjs/utils/types.d.ts +72 -0
  70. package/collection/collection-store/package.json +7 -0
  71. package/combobox/combobox-store/package.json +7 -0
  72. package/composite/composite-overflow-store/package.json +7 -0
  73. package/composite/composite-store/package.json +7 -0
  74. package/dialog/dialog-store/package.json +7 -0
  75. package/disclosure/disclosure-store/package.json +7 -0
  76. package/esm/__chunks/5XEKIOCW.js +30 -0
  77. package/esm/__chunks/6U25WEDX.js +286 -0
  78. package/esm/__chunks/6UPCMUXT.js +1491 -0
  79. package/esm/__chunks/ADRUFBEO.js +12 -0
  80. package/esm/__chunks/DXA3K2FY.js +30 -0
  81. package/esm/__chunks/HCKYJLMC.js +71 -0
  82. package/esm/__chunks/KLKI3AIB.js +146 -0
  83. package/esm/__chunks/L7KN5AYP.js +169 -0
  84. package/esm/__chunks/RX3ZUQ6U.js +57 -0
  85. package/esm/__chunks/UOJSZ35L.js +143 -0
  86. package/esm/__chunks/UVCATTRC.js +182 -0
  87. package/esm/checkbox/checkbox-store.d.ts +34 -0
  88. package/esm/checkbox/checkbox-store.js +27 -0
  89. package/esm/collection/collection-store.d.ts +65 -0
  90. package/esm/collection/collection-store.js +9 -0
  91. package/esm/combobox/combobox-store.d.ts +67 -0
  92. package/esm/combobox/combobox-store.js +163 -0
  93. package/esm/composite/composite-overflow-store.d.ts +10 -0
  94. package/esm/composite/composite-overflow-store.js +15 -0
  95. package/esm/composite/composite-store.d.ts +202 -0
  96. package/esm/composite/composite-store.js +11 -0
  97. package/esm/dialog/dialog-store.d.ts +10 -0
  98. package/esm/dialog/dialog-store.js +9 -0
  99. package/esm/disclosure/disclosure-store.d.ts +87 -0
  100. package/esm/disclosure/disclosure-store.js +8 -0
  101. package/esm/form/form-store.d.ts +236 -0
  102. package/esm/form/form-store.js +241 -0
  103. package/esm/form/types.d.ts +38 -0
  104. package/esm/form/types.js +0 -0
  105. package/esm/hovercard/hovercard-store.d.ts +45 -0
  106. package/esm/hovercard/hovercard-store.js +11 -0
  107. package/esm/index.d.ts +2 -0
  108. package/esm/index.js +5 -0
  109. package/esm/menu/menu-bar-store.d.ts +10 -0
  110. package/esm/menu/menu-bar-store.js +27 -0
  111. package/esm/menu/menu-store.d.ts +70 -0
  112. package/esm/menu/menu-store.js +108 -0
  113. package/esm/popover/popover-store.d.ts +150 -0
  114. package/esm/popover/popover-store.js +10 -0
  115. package/esm/radio/radio-store.d.ts +31 -0
  116. package/esm/radio/radio-store.js +36 -0
  117. package/esm/select/select-store.d.ts +87 -0
  118. package/esm/select/select-store.js +164 -0
  119. package/esm/tab/tab-store.d.ts +78 -0
  120. package/esm/tab/tab-store.js +126 -0
  121. package/esm/toolbar/toolbar-store.d.ts +21 -0
  122. package/esm/toolbar/toolbar-store.js +27 -0
  123. package/esm/tooltip/tooltip-store.d.ts +28 -0
  124. package/esm/tooltip/tooltip-store.js +98 -0
  125. package/esm/tsconfig.build.tsbuildinfo +1 -0
  126. package/esm/utils/array.d.ts +29 -0
  127. package/esm/utils/array.js +12 -0
  128. package/esm/utils/dom.d.ts +105 -0
  129. package/esm/utils/dom.js +38 -0
  130. package/esm/utils/events.d.ts +73 -0
  131. package/esm/utils/events.js +132 -0
  132. package/esm/utils/focus.d.ts +117 -0
  133. package/esm/utils/focus.js +222 -0
  134. package/esm/utils/misc.d.ts +111 -0
  135. package/esm/utils/misc.js +40 -0
  136. package/esm/utils/platform.d.ts +20 -0
  137. package/esm/utils/platform.js +15 -0
  138. package/esm/utils/store.d.ts +99 -0
  139. package/esm/utils/store.js +9 -0
  140. package/esm/utils/types.d.ts +72 -0
  141. package/esm/utils/types.js +0 -0
  142. package/form/form-store/package.json +7 -0
  143. package/form/types/package.json +7 -0
  144. package/hovercard/hovercard-store/package.json +7 -0
  145. package/license +21 -0
  146. package/menu/menu-bar-store/package.json +7 -0
  147. package/menu/menu-store/package.json +7 -0
  148. package/package.json +137 -6
  149. package/popover/popover-store/package.json +7 -0
  150. package/radio/radio-store/package.json +7 -0
  151. package/select/select-store/package.json +7 -0
  152. package/tab/tab-store/package.json +7 -0
  153. package/toolbar/toolbar-store/package.json +7 -0
  154. package/tooltip/tooltip-store/package.json +7 -0
  155. package/tsconfig.build.json +4 -0
  156. package/utils/array/package.json +7 -0
  157. package/utils/dom/package.json +7 -0
  158. package/utils/events/package.json +7 -0
  159. package/utils/focus/package.json +7 -0
  160. package/utils/misc/package.json +7 -0
  161. package/utils/platform/package.json +7 -0
  162. package/utils/store/package.json +7 -0
  163. package/utils/types/package.json +7 -0
@@ -0,0 +1,202 @@
1
+ import type { CollectionStoreFunctions, CollectionStoreItem, CollectionStoreOptions, CollectionStoreState } from "../collection/collection-store.js";
2
+ import type { Store, StoreOptions, StoreProps } from "../utils/store.js";
3
+ import type { SetState } from "../utils/types.js";
4
+ type Orientation = "horizontal" | "vertical" | "both";
5
+ type Item = CollectionStoreItem & {
6
+ rowId?: string;
7
+ disabled?: boolean;
8
+ children?: string;
9
+ };
10
+ /**
11
+ * Creates a composite store.
12
+ */
13
+ export declare function createCompositeStore<T extends Item = Item>(props?: CompositeStoreProps<T>): CompositeStore<T>;
14
+ export type CompositeStoreOrientation = Orientation;
15
+ export type CompositeStoreItem = Item;
16
+ export interface CompositeStoreState<T extends Item = Item> extends CollectionStoreState<T> {
17
+ /**
18
+ * The composite element.
19
+ */
20
+ baseElement: HTMLElement | null;
21
+ /**
22
+ * If enabled, the composite element will act as an
23
+ * [aria-activedescendant](https://www.w3.org/WAI/ARIA/apg/practices/keyboard-interface/#kbd_focus_activedescendant)
24
+ * container instead of [roving
25
+ * tabindex](https://www.w3.org/WAI/ARIA/apg/practices/keyboard-interface/#kbd_roving_tabindex).
26
+ * DOM focus will remain on the composite element while its items receive
27
+ * virtual focus.
28
+ * @default false
29
+ */
30
+ virtualFocus: boolean;
31
+ /**
32
+ * Defines the orientation of the composite widget. If the composite has a
33
+ * single row or column (one-dimensional), the `orientation` value determines
34
+ * which arrow keys can be used to move focus:
35
+ * - `both`: all arrow keys work.
36
+ * - `horizontal`: only left and right arrow keys work.
37
+ * - `vertical`: only up and down arrow keys work.
38
+ *
39
+ * It doesn't have any effect on two-dimensional composites.
40
+ * @default "both"
41
+ */
42
+ orientation: Orientation;
43
+ /**
44
+ * Determines how the `next` and `previous` functions will behave. If `rtl` is
45
+ * set to `true`, they will be inverted. This only affects the composite
46
+ * widget behavior. You still need to set `dir="rtl"` on HTML/CSS.
47
+ * @default false
48
+ */
49
+ rtl: boolean;
50
+ /**
51
+ * Determines how the focus behaves when the user reaches the end of the
52
+ * composite widget.
53
+ *
54
+ * On one-dimensional composites:
55
+ * - `true` loops from the last item to the first item and vice-versa.
56
+ * - `horizontal` loops only if `orientation` is `horizontal` or not set.
57
+ * - `vertical` loops only if `orientation` is `vertical` or not set.
58
+ * - If `includesBaseElement` is set to `true` (or `activeId` is initially
59
+ * set to `null`), the composite element will be focused in between the
60
+ * last and first items.
61
+ *
62
+ * On two-dimensional composites (when using `CompositeRow`):
63
+ * - `true` loops from the last row/column item to the first item in the
64
+ * same row/column and vice-versa. If it's the last item in the last row,
65
+ * it moves to the first item in the first row and vice-versa.
66
+ * - `horizontal` loops only from the last row item to the first item in the
67
+ * same row.
68
+ * - `vertical` loops only from the last column item to the first item in
69
+ * the column row.
70
+ * - If `includesBaseElement` is set to `true` (or `activeId` is initially
71
+ * set to `null`), vertical loop will have no effect as moving down from
72
+ * the last row or up from the first row will focus the composite element.
73
+ * - If `focusWrap` matches the value of `focusLoop`, it'll wrap between the
74
+ * last item in the last row or column and the first item in the first row
75
+ * or column and vice-versa.
76
+ * @default false
77
+ */
78
+ focusLoop: boolean | Orientation;
79
+ /**
80
+ * **Works only on two-dimensional composites**. If enabled, moving to the
81
+ * next item from the last one in a row or column will focus the first item in
82
+ * the next row or column and vice-versa.
83
+ * - `true` wraps between rows and columns.
84
+ * - `horizontal` wraps only between rows.
85
+ * - `vertical` wraps only between columns.
86
+ * - If `focusLoop` matches the value of `focusWrap`, it'll wrap between the
87
+ * last item in the last row or column and the first item in the first row
88
+ * or column and vice-versa.
89
+ * @default false
90
+ */
91
+ focusWrap: boolean | Orientation;
92
+ /**
93
+ * **Works only on two-dimensional composites**. If enabled, moving up or down
94
+ * when there's no next item or when the next item is disabled will shift to
95
+ * the item right before it.
96
+ * @default false
97
+ */
98
+ focusShift: boolean;
99
+ /**
100
+ * The number of times the `move` function has been called.
101
+ */
102
+ moves: number;
103
+ /**
104
+ * Indicates whether the composite element should be included in the focus
105
+ * order.
106
+ * @default false
107
+ */
108
+ includesBaseElement: boolean;
109
+ /**
110
+ * The current focused item `id`.
111
+ * - `null` focuses the base composite element and users will be able to
112
+ * navigate out of it using arrow keys.
113
+ * - If `activeId` is initially set to `null`, the `includesBaseElement`
114
+ * prop will also default to `true`, which means the base composite
115
+ * element itself will have focus and users will be able to navigate to it
116
+ * using arrow keys.
117
+ */
118
+ activeId: string | null | undefined;
119
+ }
120
+ export interface CompositeStoreFunctions<T extends Item = Item> extends CollectionStoreFunctions<T> {
121
+ /**
122
+ * Sets the `baseElement`.
123
+ */
124
+ setBaseElement: SetState<CompositeStoreState<T>["baseElement"]>;
125
+ /**
126
+ * Sets the `activeId` state without moving focus. If you want to move focus,
127
+ * use the `move` function instead.
128
+ * @example
129
+ * // Sets the composite element as the active item
130
+ * store.setActiveId(null);
131
+ * // Sets the item with id "item-1" as the active item
132
+ * store.setActiveId("item-1");
133
+ * // Sets the next item as the active item
134
+ * store.setActiveId(store.next());
135
+ */
136
+ setActiveId: SetState<CompositeStoreState<T>["activeId"]>;
137
+ /**
138
+ * Moves focus to a given item id and sets it as the active item. Passing
139
+ * `null` will focus the composite element itself.
140
+ * @param id The item id to move focus to.
141
+ * @example
142
+ * // Moves focus to the composite element
143
+ * store.move(null);
144
+ * // Moves focus to the item with id "item-1"
145
+ * store.move("item-1");
146
+ * // Moves focus to the next item
147
+ * store.move(store.next());
148
+ */
149
+ move: (id?: string | null) => void;
150
+ /**
151
+ * Returns the id of the next item based on the current `activeId` state.
152
+ * @param skip The number of items to skip. Defaults to 1.
153
+ * @example
154
+ * const nextId = store.next();
155
+ * const nextNextId = store.next(2);
156
+ */
157
+ next: (skip?: number) => string | null | undefined;
158
+ /**
159
+ * Returns the id of the previous item based on the current `activeId` state.
160
+ * @param skip The number of items to skip. Defaults to 1.
161
+ * @example
162
+ * const previousId = store.previous();
163
+ * const previousPreviousId = store.previous(2);
164
+ */
165
+ previous: (skip?: number) => string | null | undefined;
166
+ /**
167
+ * Returns the id of the item above based on the current `activeId` state.
168
+ * @param skip The number of items to skip. Defaults to 1.
169
+ * @example
170
+ * const upId = store.up();
171
+ * const upUpId = store.up(2);
172
+ */
173
+ up: (skip?: number) => string | null | undefined;
174
+ /**
175
+ * Returns the id of the item below based on the current `activeId` state.
176
+ * @param skip The number of items to skip. Defaults to 1.
177
+ * @example
178
+ * const downId = store.down();
179
+ * const downDownId = store.down(2);
180
+ */
181
+ down: (skip?: number) => string | null | undefined;
182
+ /**
183
+ * Returns the id of the first item.
184
+ */
185
+ first: () => string | null | undefined;
186
+ /**
187
+ * Returns the id of the last item.
188
+ */
189
+ last: () => string | null | undefined;
190
+ }
191
+ export interface CompositeStoreOptions<T extends Item = Item> extends StoreOptions<CompositeStoreState<T>, "virtualFocus" | "orientation" | "rtl" | "focusLoop" | "focusWrap" | "focusShift" | "includesBaseElement" | "activeId">, CollectionStoreOptions<T> {
192
+ /**
193
+ * The composite item id that should be active by default when the composite
194
+ * widget is rendered. If `null`, the composite element itself will have focus
195
+ * and users will be able to navigate to it using arrow keys. If `undefined`,
196
+ * the first enabled item will be focused.
197
+ */
198
+ defaultActiveId?: CompositeStoreState<T>["activeId"];
199
+ }
200
+ export type CompositeStoreProps<T extends Item = Item> = CompositeStoreOptions<T> & StoreProps<CompositeStoreState<T>>;
201
+ export type CompositeStore<T extends Item = Item> = CompositeStoreFunctions<T> & Store<CompositeStoreState<T>>;
202
+ export {};
@@ -0,0 +1,11 @@
1
+ import {
2
+ createCompositeStore
3
+ } from "../__chunks/6U25WEDX.js";
4
+ import "../__chunks/L7KN5AYP.js";
5
+ import "../__chunks/UOJSZ35L.js";
6
+ import "../__chunks/KLKI3AIB.js";
7
+ import "../__chunks/UVCATTRC.js";
8
+ import "../__chunks/5XEKIOCW.js";
9
+ export {
10
+ createCompositeStore
11
+ };
@@ -0,0 +1,10 @@
1
+ import type { DisclosureStore, DisclosureStoreFunctions, DisclosureStoreOptions, DisclosureStoreProps, DisclosureStoreState } from "../disclosure/disclosure-store.js";
2
+ /**
3
+ * Creates a dialog store.
4
+ */
5
+ export declare function createDialogStore(props?: DialogStoreProps): DialogStore;
6
+ export type DialogStoreState = DisclosureStoreState;
7
+ export type DialogStoreFunctions = DisclosureStoreFunctions;
8
+ export type DialogStoreOptions = DisclosureStoreOptions;
9
+ export type DialogStoreProps = DisclosureStoreProps;
10
+ export type DialogStore = DisclosureStore;
@@ -0,0 +1,9 @@
1
+ import {
2
+ createDialogStore
3
+ } from "../__chunks/ADRUFBEO.js";
4
+ import "../__chunks/HCKYJLMC.js";
5
+ import "../__chunks/UOJSZ35L.js";
6
+ import "../__chunks/KLKI3AIB.js";
7
+ export {
8
+ createDialogStore
9
+ };
@@ -0,0 +1,87 @@
1
+ import type { Store, StoreOptions, StoreProps } from "../utils/store.js";
2
+ import type { SetState } from "../utils/types.js";
3
+ /**
4
+ * Creates a disclosure store.
5
+ */
6
+ export declare function createDisclosureStore(props?: DisclosureStoreProps): DisclosureStore;
7
+ export interface DisclosureStoreState {
8
+ /**
9
+ * The visibility state of the content.
10
+ * @default false
11
+ */
12
+ open: boolean;
13
+ /**
14
+ * The mounted state of the content. If `animated` is `false` or not defined,
15
+ * this will be the same as `open`. Otherwise, it will wait for the animation
16
+ * to complete before becoming `false` so the content is not unmounted while
17
+ * animating.
18
+ */
19
+ mounted: boolean;
20
+ /**
21
+ * Determines whether the content should animate when it is shown or hidden.
22
+ * - If `true`, the `animating` state will be `true` when the content is
23
+ * shown or hidden and it will wait for `stopAnimation` to be called or a
24
+ * CSS animation/transition to end before becoming `false`.
25
+ * - If it's set to a number, the `animating` state will be `true` when the
26
+ * content is shown or hidden and it will wait for the number of
27
+ * milliseconds to pass before becoming `false`.
28
+ * @default false
29
+ */
30
+ animated: boolean | number;
31
+ /**
32
+ * Whether the content is currently animating.
33
+ */
34
+ animating: boolean;
35
+ /**
36
+ * The content element that is being shown or hidden.
37
+ */
38
+ contentElement: HTMLElement | null;
39
+ /**
40
+ * The disclosure button element that toggles the content.
41
+ */
42
+ disclosureElement: HTMLElement | null;
43
+ }
44
+ export interface DisclosureStoreFunctions {
45
+ /**
46
+ * Sets the `open` state.
47
+ * @example
48
+ * store.setOpen(true);
49
+ * store.setOpen((open) => !open);
50
+ */
51
+ setOpen: SetState<DisclosureStoreState["open"]>;
52
+ /**
53
+ * Sets the `open` state to `true`.
54
+ */
55
+ show: () => void;
56
+ /**
57
+ * Sets the `open` state to `false`.
58
+ */
59
+ hide: () => void;
60
+ /**
61
+ * Toggles the `open` state.
62
+ */
63
+ toggle: () => void;
64
+ /**
65
+ * Sets the `animating` state to `false`, which will automatically set the
66
+ * `mounted` state to `false` if it was `true`. This means that the content
67
+ * element can be safely unmounted.
68
+ */
69
+ stopAnimation: () => void;
70
+ /**
71
+ * Sets the `contentElement` state.
72
+ */
73
+ setContentElement: SetState<DisclosureStoreState["contentElement"]>;
74
+ /**
75
+ * Sets the `disclosureElement` state.
76
+ */
77
+ setDisclosureElement: SetState<DisclosureStoreState["disclosureElement"]>;
78
+ }
79
+ export interface DisclosureStoreOptions extends StoreOptions<DisclosureStoreState, "open" | "animated"> {
80
+ /**
81
+ * The default visibility state of the content.
82
+ * @default false
83
+ */
84
+ defaultOpen?: DisclosureStoreState["open"];
85
+ }
86
+ export type DisclosureStoreProps = DisclosureStoreOptions & StoreProps<DisclosureStoreState>;
87
+ export type DisclosureStore = DisclosureStoreFunctions & Store<DisclosureStoreState>;
@@ -0,0 +1,8 @@
1
+ import {
2
+ createDisclosureStore
3
+ } from "../__chunks/HCKYJLMC.js";
4
+ import "../__chunks/UOJSZ35L.js";
5
+ import "../__chunks/KLKI3AIB.js";
6
+ export {
7
+ createDisclosureStore
8
+ };
@@ -0,0 +1,236 @@
1
+ import type { CollectionStoreFunctions, CollectionStoreItem, CollectionStoreOptions, CollectionStoreState } from "../collection/collection-store.js";
2
+ import type { Store, StoreOptions, StoreProps } from "../utils/store.js";
3
+ import type { AnyObject, PickRequired, SetState, SetStateAction } from "../utils/types.js";
4
+ import type { DeepMap, DeepPartial, Names, StringLike } from "./types.js";
5
+ type Values = AnyObject;
6
+ type ErrorMessage = string | undefined | null;
7
+ type Item = CollectionStoreItem & {
8
+ type: "field" | "label" | "description" | "error" | "button";
9
+ name: string;
10
+ };
11
+ export declare function hasMessages(object: Values): boolean;
12
+ export declare function get<T>(values: Values, path: StringLike | string[], defaultValue?: T): T;
13
+ /**
14
+ * Creates a form store.
15
+ */
16
+ export declare function createFormStore<T extends Values = Values>(props: PickRequired<FormStoreProps<T>, "values" | "defaultValues" | "errors" | "defaultErrors" | "touched" | "defaultTouched">): FormStore<T>;
17
+ export declare function createFormStore(props: FormStoreProps): FormStore;
18
+ export type FormStoreCallback<T extends FormStoreState = FormStoreState> = (state: T) => void | Promise<void>;
19
+ export type FormStoreValues = Values;
20
+ export type FormStoreItem = Item;
21
+ export interface FormStoreState<T extends Values = Values> extends CollectionStoreState<Item> {
22
+ /**
23
+ * Form values.
24
+ * @default {}
25
+ */
26
+ values: T;
27
+ /**
28
+ * Form errors.
29
+ */
30
+ errors: DeepPartial<DeepMap<T, ErrorMessage>>;
31
+ /**
32
+ * The touched state of the form.
33
+ */
34
+ touched: DeepPartial<DeepMap<T, boolean>>;
35
+ /**
36
+ * Whether the form is valid.
37
+ */
38
+ valid: boolean;
39
+ /**
40
+ * Whether the form is validating.
41
+ */
42
+ validating: boolean;
43
+ /**
44
+ * Whether the form is submitting.
45
+ */
46
+ submitting: boolean;
47
+ /**
48
+ * The number of times `form.submit` has been called with a successful
49
+ * response.
50
+ */
51
+ submitSucceed: number;
52
+ /**
53
+ * The number of times `form.submit` has been called with an error response.
54
+ */
55
+ submitFailed: number;
56
+ }
57
+ export interface FormStoreFunctions<T extends Values = Values> extends CollectionStoreFunctions<Item> {
58
+ /**
59
+ * An object containing the names of the form fields for type-safety.
60
+ * @example
61
+ * store.names.name; // "name"
62
+ * store.names.name.first; // "name.first"
63
+ * store.names.name.last; // "name.last"
64
+ */
65
+ names: Names<T>;
66
+ /**
67
+ * Sets the `values` state.
68
+ * @example
69
+ * store.setValues({ name: "John" });
70
+ * store.setValues((values) => ({ ...values, name: "John" }));
71
+ */
72
+ setValues: SetState<FormStoreState<T>["values"]>;
73
+ /**
74
+ * Retrieves a field value.
75
+ * @param name The field name.
76
+ * @example
77
+ * const nameValue = store.getValue("name");
78
+ * // Can also use store.names for type-safety.
79
+ * const emailValue = store.getValue(store.names.email);
80
+ */
81
+ getValue: <T = any>(name: StringLike) => T;
82
+ /**
83
+ * Sets a field value.
84
+ * @param name The field name.
85
+ * @param value The field value.
86
+ * @example
87
+ * store.setValue("name", "John");
88
+ * store.setValue("name", (value) => value + " Doe");
89
+ * // Can also use store.names for type-safety.
90
+ * store.setValue(store.names.name, "John");
91
+ */
92
+ setValue: <T>(name: StringLike, value: SetStateAction<T>) => void;
93
+ /**
94
+ * Pushes a value to an array field.
95
+ * @param name The array field name.
96
+ * @param value The value to push.
97
+ * @example
98
+ * store.pushValue("tags", "new tag");
99
+ * store.pushValue("tags", { id: 1, name: "new tag" });
100
+ * // Can also use store.names for type-safety.
101
+ * store.pushValue(store.names.tags, "new tag");
102
+ */
103
+ pushValue: <T>(name: StringLike, value: T) => void;
104
+ /**
105
+ * Removes a value from an array field.
106
+ * @param name The array field name.
107
+ * @param index The index of the value to remove.
108
+ * @example
109
+ * store.removeValue("tags", 0);
110
+ * store.removeValue("tags", 1);
111
+ * // Can also use store.names for type-safety.
112
+ * store.removeValue(store.names.tags, 0);
113
+ */
114
+ removeValue: (name: StringLike, index: number) => void;
115
+ /**
116
+ * Sets the `errors` state.
117
+ * @example
118
+ * store.setErrors({ name: "Name is required" });
119
+ * store.setErrors((errors) => ({ ...errors, name: "Name is required" }));
120
+ */
121
+ setErrors: SetState<FormStoreState<T>["errors"]>;
122
+ /**
123
+ * Retrieves a field error.
124
+ * @param name The field name.
125
+ * @example
126
+ * const nameError = store.getError("name");
127
+ * // Can also use store.names for type-safety.
128
+ * const emailError = store.getError(store.names.email);
129
+ */
130
+ getError: (name: StringLike) => ErrorMessage;
131
+ /**
132
+ * Sets a field error.
133
+ * @param name The field name.
134
+ * @param error The field error.
135
+ * @example
136
+ * store.setError("name", "Name is required");
137
+ * store.setError("name", (error) => error + "!");
138
+ * // Can also use store.names for type-safety.
139
+ * store.setError(store.names.name, "Name is required");
140
+ */
141
+ setError: (name: StringLike, error: SetStateAction<ErrorMessage>) => void;
142
+ /**
143
+ * Sets the `touched` state.
144
+ * @example
145
+ * store.setTouched({ name: true });
146
+ * store.setTouched((touched) => ({ ...touched, name: true }));
147
+ */
148
+ setTouched: SetState<FormStoreState<T>["touched"]>;
149
+ /**
150
+ * Retrieves a field touched state.
151
+ * @param name The field name.
152
+ * @example
153
+ * const nameTouched = store.getFieldTouched("name");
154
+ * // Can also use store.names for type-safety.
155
+ * const emailTouched = store.getFieldTouched(store.names.email);
156
+ */
157
+ getFieldTouched: (name: StringLike) => boolean;
158
+ /**
159
+ * Sets a field touched state.
160
+ * @param name The field name.
161
+ * @param value The field touched state.
162
+ * @example
163
+ * store.setFieldTouched("name", true);
164
+ * store.setFieldTouched("name", (value) => !value);
165
+ * // Can also use store.names for type-safety.
166
+ * store.setFieldTouched(store.names.name, true);
167
+ */
168
+ setFieldTouched: (name: StringLike, value: SetStateAction<boolean>) => void;
169
+ /**
170
+ * Function that accepts a callback that will be used to validate the form
171
+ * when `validate` is called. It returns a cleanup function that will remove
172
+ * the callback.
173
+ * @param callback The callback function.
174
+ * @example
175
+ * const cleanup = store.onValidate(async (state) => {
176
+ * const errors = await api.validate(state.values);
177
+ * if (errors) {
178
+ * store.setErrors(errors);
179
+ * }
180
+ * });
181
+ */
182
+ onValidate: (callback: FormStoreCallback<FormStoreState<T>>) => void;
183
+ /**
184
+ * Function that accepts a callback that will be used to submit the form
185
+ * when `submit` is called. It returns a cleanup function that will remove
186
+ * the callback.
187
+ * @param callback The callback function.
188
+ * @example
189
+ * const cleanup = store.onSubmit(async (state) => {
190
+ * try {
191
+ * await api.submit(state.values);
192
+ * } catch (errors) {
193
+ * store.setErrors(errors);
194
+ * }
195
+ * });
196
+ */
197
+ onSubmit: (callback: FormStoreCallback<FormStoreState<T>>) => void;
198
+ /**
199
+ * Validates the form.
200
+ * @example
201
+ * if (await store.validate()) {
202
+ * // Form is valid.
203
+ * }
204
+ */
205
+ validate: () => Promise<boolean>;
206
+ /**
207
+ * Submits the form. This also triggers validation.
208
+ * @example
209
+ * if (await form.submit()) {
210
+ * // Form is submitted.
211
+ * }
212
+ */
213
+ submit: () => Promise<boolean>;
214
+ /**
215
+ * Resets the form to its default values.
216
+ */
217
+ reset: () => void;
218
+ }
219
+ export interface FormStoreOptions<T extends Values = Values> extends StoreOptions<FormStoreState<T>, "values" | "errors" | "touched">, CollectionStoreOptions<Item> {
220
+ /**
221
+ * The default values of the form.
222
+ * @default {}
223
+ */
224
+ defaultValues?: FormStoreState<T>["values"];
225
+ /**
226
+ * The default errors of the form.
227
+ */
228
+ defaultErrors?: FormStoreState<T>["errors"];
229
+ /**
230
+ * The default touched state of the form.
231
+ */
232
+ defaultTouched?: FormStoreState<T>["touched"];
233
+ }
234
+ export type FormStoreProps<T extends Values = Values> = FormStoreOptions<T> & StoreProps<FormStoreState<T>>;
235
+ export type FormStore<T extends Values = Values> = FormStoreFunctions<T> & Store<FormStoreState<T>>;
236
+ export {};