-
-
Notifications
You must be signed in to change notification settings - Fork 151
feat(utils): inject getId to allow for custom id generation #1836
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
VividLemon
merged 4 commits into
bootstrap-vue-next:main
from
reubns:feature/provide-get-id
Apr 14, 2024
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,10 @@ | ||
import {getId} from '../utils' | ||
import {computed, type ComputedRef, type MaybeRefOrGetter, toValue} from 'vue' | ||
import {idPluginKey} from '../utils' | ||
import {computed, type ComputedRef, inject, type MaybeRefOrGetter, toValue} from 'vue' | ||
|
||
export default (id?: MaybeRefOrGetter<string | undefined>, suffix?: string): ComputedRef<string> => | ||
computed(() => toValue(id) || getId(suffix)) | ||
|
||
export const getId = (suffix = '') => { | ||
const getId = inject(idPluginKey, () => Math.random().toString().slice(2, 8)) | ||
return `__BVID__${getId()}___BV_${suffix}__` | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import {type Plugin} from 'vue' | ||
import type {BootstrapVueOptions} from '../types' | ||
import {idPluginKey} from '../utils' | ||
|
||
export default { | ||
install(app, options: BootstrapVueOptions['plugins']) { | ||
if (options?.id instanceof Object && typeof options.id.getId === 'function') { | ||
app.provide(idPluginKey, options.id.getId) | ||
} | ||
}, | ||
} satisfies Plugin |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
packages/bootstrap-vue-next/tests/composables/idPlugin.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* eslint-disable vue/one-component-per-file */ | ||
import {mount} from '@vue/test-utils' | ||
import {getId} from '../../src/composables' | ||
import {describe, expect, it} from 'vitest' | ||
import {defineComponent, h, provide} from 'vue' | ||
import {useSetup} from '../utils' | ||
import {idPluginKey} from '../../src/utils' | ||
|
||
describe('getId', () => { | ||
it('returns something', () => { | ||
useSetup(() => { | ||
const value = getId() | ||
expect(value).toBeDefined() | ||
}) | ||
}) | ||
|
||
it('returns a string', () => { | ||
useSetup(() => { | ||
const value = getId() | ||
expect(typeof value === 'string').toBe(true) | ||
}) | ||
}) | ||
|
||
it('string contains __BVID__', () => { | ||
useSetup(() => { | ||
const value = getId() | ||
expect(value).toContain('__BVID__') | ||
}) | ||
}) | ||
|
||
it('string contains ___BV_{suffix}__ when suffix is defined', () => { | ||
useSetup(() => { | ||
const value = getId('foobar') | ||
expect(value).toContain('___BV_foobar__') | ||
}) | ||
}) | ||
|
||
it('string contains ___BV___ when not suffix', () => { | ||
useSetup(() => { | ||
const value = getId() | ||
expect(value).toContain('___BV___') | ||
}) | ||
}) | ||
}) | ||
|
||
export function useSetupWithProvideGetId<V>(setup: () => V) { | ||
const Comp = defineComponent({ | ||
setup, | ||
render() { | ||
return h('div') | ||
}, | ||
}) | ||
|
||
const Provider = defineComponent({ | ||
components: {Comp}, | ||
setup() { | ||
provide(idPluginKey, () => `${Math.random().toString().slice(2, 8)}__PROVIDED__`) | ||
}, | ||
template: '<Comp />', | ||
}) | ||
|
||
return mount(Provider, {slots: {default: Comp}}) | ||
} | ||
|
||
describe('provideGetId', () => { | ||
it('returns something', () => { | ||
useSetupWithProvideGetId(() => { | ||
const value = getId() | ||
expect(value).toBeDefined() | ||
}) | ||
}) | ||
|
||
it('returns a string', () => { | ||
useSetupWithProvideGetId(() => { | ||
const value = getId() | ||
expect(typeof value === 'string').toBe(true) | ||
}) | ||
}) | ||
|
||
it('string contains __PROVIDED__', () => { | ||
useSetupWithProvideGetId(() => { | ||
const value = getId() | ||
expect(value).toContain('__PROVIDED__') | ||
}) | ||
}) | ||
|
||
it('string contains __PROVIDED_____BV_{suffix}__ when suffix is defined', () => { | ||
useSetupWithProvideGetId(() => { | ||
const value = getId('foobar') | ||
expect(value).toContain('__PROVIDED_____BV_foobar__') | ||
}) | ||
}) | ||
|
||
it('string contains __PROVIDED_____BV___ when not suffix', () => { | ||
useSetupWithProvideGetId(() => { | ||
const value = getId() | ||
expect(value).toContain('__PROVIDED_____BV___') | ||
}) | ||
}) | ||
}) |
21 changes: 14 additions & 7 deletions
21
packages/bootstrap-vue-next/tests/composables/useId.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,28 @@ | ||
import {useSetup} from '../../tests/utils' | ||
import {useId} from '../../src/composables' | ||
import {describe, expect, it} from 'vitest' | ||
import {reactive} from 'vue' | ||
|
||
describe('useId blackbox test', () => { | ||
it('returns id value when id is defined', () => { | ||
const props = reactive({id: 'foo'}) | ||
const value = useId(() => props.id) | ||
expect(value.value).toBe('foo') | ||
useSetup(() => { | ||
const props = reactive({id: 'foo'}) | ||
const value = useId(() => props.id) | ||
expect(value.value).toBe('foo') | ||
}) | ||
}) | ||
|
||
it('returns something when id is undefined', () => { | ||
const value = useId() | ||
expect(value.value).toBeDefined() | ||
useSetup(() => { | ||
const value = useId() | ||
expect(value.value).toBeDefined() | ||
}) | ||
}) | ||
|
||
it('something returned when undefined contains suffix when suffix', () => { | ||
const value = useId(undefined, 'foobar') | ||
expect(value.value).toContain('foobar') | ||
useSetup(() => { | ||
const value = useId(undefined, 'foobar') | ||
expect(value.value).toContain('foobar') | ||
}) | ||
}) | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,16 @@ | ||
import {createBootstrap} from 'bootstrap-vue-next' | ||
import {defineNuxtPlugin} from '#imports' | ||
import {defineNuxtPlugin, useId} from '#imports' | ||
|
||
export default defineNuxtPlugin((nuxtApp) => { | ||
nuxtApp.vueApp.use( | ||
createBootstrap({ | ||
components: false, | ||
directives: false, | ||
plugins: { | ||
id: { | ||
getId: () => useId(), | ||
}, | ||
}, | ||
}) | ||
) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.