Skip to content

Commit 6d794e6

Browse files
committed
feat(useGroup): add new browse function
1 parent a632ffe commit 6d794e6

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

packages/0/src/composables/useGroup/index.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,5 +414,14 @@ describe('useGroup', () => {
414414

415415
expect(context.selectedIds.size).toBe(0)
416416
})
417+
418+
it('should return ID for registered value and undefined for non-existent value', () => {
419+
const group = useGroup('test-group')[2]
420+
421+
const ticket = group.register({ value: 'test-value' })
422+
423+
expect(group.browse('test-value')).toBe(ticket.id)
424+
expect(group.browse('non-existent')).toBeUndefined()
425+
})
417426
})
418427
})

packages/0/src/composables/useGroup/index.ts

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,14 @@ export type GroupContext = RegistrarContext & {
2828
selectedIds: Reactive<Set<ID>>
2929
selectedValues: ComputedRef<Set<unknown>>
3030
register: (item?: Partial<GroupTicket>, id?: ID) => Reactive<GroupTicket>
31+
/** Select the first available value */
3132
mandate: () => void
33+
/** Select item(s) by ID(s) */
3234
select: (ids: ID | ID[]) => void
35+
/** Clear all selected IDs, reindex, and mandate a value */
3336
reset: () => void
34-
lookup: (index: number) => ID | undefined
37+
/** Browse for an ID by value */
38+
browse: (value: unknown) => ID | undefined
3539
}
3640

3741
export type GroupOptions = {
@@ -41,7 +45,7 @@ export type GroupOptions = {
4145
}
4246

4347
/**
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.
4549
* This function provides a way to register, unregister, and manage group selections,
4650
* allowing for dynamic group management in applications.
4751
*
@@ -50,6 +54,8 @@ export type GroupOptions = {
5054
* @template Z The type of the group tickets managed by the registrar.
5155
* @template E The type of the group context.
5256
* @returns A tuple containing the inject function, provide function, and the group context.
57+
*
58+
* @see https://0.vuetifyjs.com/composables/selection/use-group
5359
*/
5460
export function useGroup<
5561
Z extends GroupContext,
@@ -60,6 +66,7 @@ export function useGroup<
6066
) {
6167
const [useRegistrarContext, provideRegistrarContext, registrar] = useRegistrar<Z, E>(namespace)
6268

69+
const catalog = reactive(new Map<unknown, ID>())
6370
const selectedIds = reactive(new Set<ID>())
6471
let initialValue: unknown | unknown[] = null
6572

@@ -81,6 +88,10 @@ export function useGroup<
8188
)
8289
})
8390

91+
function browse (value: unknown): ID | undefined {
92+
return catalog.get(value)
93+
}
94+
8495
function mandate () {
8596
if (!options?.mandatory || selectedIds.size > 0 || registrar.tickets.size === 0) return
8697

@@ -92,8 +103,8 @@ export function useGroup<
92103

93104
for (const item of registrar.tickets.values()) {
94105
if (item.disabled) continue
95-
96106
selectedIds.add(item.id)
107+
97108
break
98109
}
99110
}
@@ -152,6 +163,8 @@ export function useGroup<
152163

153164
const ticket = registrar.register(item, id)
154165

166+
catalog.set(ticket.value, ticket.id)
167+
155168
if (initialValue != null) {
156169
const shouldSelect = Array.isArray(initialValue)
157170
? initialValue.includes(ticket.value)
@@ -188,6 +201,7 @@ export function useGroup<
188201
reindex,
189202
mandate,
190203
select,
204+
browse,
191205
} as Z
192206

193207
function provideGroupContext (
@@ -220,14 +234,12 @@ export function useGroup<
220234

221235
selectedIds.clear()
222236

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)
226239

227-
selectedIds.add(id)
240+
if (!id) continue
228241

229-
break
230-
}
242+
selectedIds.add(id)
231243
}
232244
})
233245

@@ -240,9 +252,7 @@ export function useGroup<
240252
})
241253
}
242254

243-
provideRegistrarContext(model, _context, app)
244-
245-
return _context
255+
return provideRegistrarContext(model, _context, app)
246256
}
247257

248258
return createTrinity<Z>(useRegistrarContext, provideGroupContext, context)

0 commit comments

Comments
 (0)