@@ -28,10 +28,14 @@ export type GroupContext = RegistrarContext & {
28
28
selectedIds : Reactive < Set < ID > >
29
29
selectedValues : ComputedRef < Set < unknown > >
30
30
register : ( item ?: Partial < GroupTicket > , id ?: ID ) => Reactive < GroupTicket >
31
+ /** Select the first available value */
31
32
mandate : ( ) => void
33
+ /** Select item(s) by ID(s) */
32
34
select : ( ids : ID | ID [ ] ) => void
35
+ /** Clear all selected IDs, reindex, and mandate a value */
33
36
reset : ( ) => void
34
- lookup : ( index : number ) => ID | undefined
37
+ /** Browse for an ID by value */
38
+ browse : ( value : unknown ) => ID | undefined
35
39
}
36
40
37
41
export type GroupOptions = {
@@ -41,7 +45,7 @@ export type GroupOptions = {
41
45
}
42
46
43
47
/**
44
- * Creates a group registrar for managing group tickets within a specific namespace.
48
+ * Creates a group registrar for managing group tickets within a specific namespace.
45
49
* This function provides a way to register, unregister, and manage group selections,
46
50
* allowing for dynamic group management in applications.
47
51
*
@@ -50,6 +54,8 @@ export type GroupOptions = {
50
54
* @template Z The type of the group tickets managed by the registrar.
51
55
* @template E The type of the group context.
52
56
* @returns A tuple containing the inject function, provide function, and the group context.
57
+ *
58
+ * @see https://0.vuetifyjs.com/composables/selection/use-group
53
59
*/
54
60
export function useGroup <
55
61
Z extends GroupContext ,
@@ -60,6 +66,7 @@ export function useGroup<
60
66
) {
61
67
const [ useRegistrarContext , provideRegistrarContext , registrar ] = useRegistrar < Z , E > ( namespace )
62
68
69
+ const catalog = reactive ( new Map < unknown , ID > ( ) )
63
70
const selectedIds = reactive ( new Set < ID > ( ) )
64
71
let initialValue : unknown | unknown [ ] = null
65
72
@@ -81,6 +88,10 @@ export function useGroup<
81
88
)
82
89
} )
83
90
91
+ function browse ( value : unknown ) : ID | undefined {
92
+ return catalog . get ( value )
93
+ }
94
+
84
95
function mandate ( ) {
85
96
if ( ! options ?. mandatory || selectedIds . size > 0 || registrar . tickets . size === 0 ) return
86
97
@@ -92,8 +103,8 @@ export function useGroup<
92
103
93
104
for ( const item of registrar . tickets . values ( ) ) {
94
105
if ( item . disabled ) continue
95
-
96
106
selectedIds . add ( item . id )
107
+
97
108
break
98
109
}
99
110
}
@@ -152,6 +163,8 @@ export function useGroup<
152
163
153
164
const ticket = registrar . register ( item , id )
154
165
166
+ catalog . set ( ticket . value , ticket . id )
167
+
155
168
if ( initialValue != null ) {
156
169
const shouldSelect = Array . isArray ( initialValue )
157
170
? initialValue . includes ( ticket . value )
@@ -188,6 +201,7 @@ export function useGroup<
188
201
reindex,
189
202
mandate,
190
203
select,
204
+ browse,
191
205
} as Z
192
206
193
207
function provideGroupContext (
@@ -220,14 +234,12 @@ export function useGroup<
220
234
221
235
selectedIds . clear ( )
222
236
223
- for ( const val of values ) {
224
- for ( const [ id , item ] of registrar . tickets ) {
225
- if ( item . value !== val ) continue
237
+ for ( const value of values ) {
238
+ const id = browse ( value )
226
239
227
- selectedIds . add ( id )
240
+ if ( ! id ) continue
228
241
229
- break
230
- }
242
+ selectedIds . add ( id )
231
243
}
232
244
} )
233
245
@@ -240,9 +252,7 @@ export function useGroup<
240
252
} )
241
253
}
242
254
243
- provideRegistrarContext ( model , _context , app )
244
-
245
- return _context
255
+ return provideRegistrarContext ( model , _context , app )
246
256
}
247
257
248
258
return createTrinity < Z > ( useRegistrarContext , provideGroupContext , context )
0 commit comments