Skip to content

Commit

Permalink
fix(core): UID doesn't exist on the component public instance in Vue 3
Browse files Browse the repository at this point in the history
…vuelidate#1090  (vuelidate#1091)

* test: 🧪 test that uid are undefined in vue3

* fix(core): 🐛 uid was undefined on vue3

create our own uid to be agnostic of vue version

Co-authored-by: Florent Bouisset <[email protected]>
  • Loading branch information
Florent-Bouisset and Florent Bouisset authored Aug 4, 2022
1 parent e002e8e commit 8bdf2ac
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
10 changes: 5 additions & 5 deletions packages/vuelidate/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ import { ComputedProxyFactory } from './utils/ComputedProxyFactory'
* @param {GlobalConfig} [globalConfig] - Config Object
* @return {ComputedRef<*>}
*/

let uid = 0

export function useVuelidate (validations, state, globalConfig = {}) {
// if we pass only one argument, its most probably the globalConfig.
// This use case is so parents can just collect results of child forms.
Expand All @@ -40,11 +43,8 @@ export function useVuelidate (validations, state, globalConfig = {}) {
? instance.$options
: {}
// if there is no registration name, add one.
if (!$registerAs && instance) {
// NOTE:
// ._uid // Vue 2.x Composition-API plugin
// .uid // Vue 3.0
const uid = instance.uid || instance._uid
if (!$registerAs) {
uid += 1
$registerAs = `_vuelidate_${uid}`
}
const validationResults = ref({})
Expand Down
27 changes: 27 additions & 0 deletions packages/vuelidate/test/unit/specs/validation.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,33 @@ describe('useVuelidate', () => {
})
})

it('collects multiple child validation giving them unique id if registerAs is omitted', async () => {
const { state, validations } = simpleValidation()
const child1 = createSimpleComponent(() => useVuelidate(validations, state))
const child2 = createSimpleComponent(() => useVuelidate(validations, state))

const Component = {
setup () {
const state = simpleValidation()
const v = useVuelidate(state.validations, state.state)
return { v }
},
render () {
return h('div', [h(child1), h(child2)])
}
}
const wrapper = mount(Component)
const uidRegex = /_vuelidate_.*/
const childValidation = []
for (const key in wrapper.vm.v) {
if (key.match(uidRegex)) {
childValidation.push(key)
}
}
expect(childValidation).toHaveLength(2)
expect(childValidation[0]).not.toEqual(childValidation[1])
})

describe('$error', () => {
it('returns `true` if both `$invalid` and $dirty` are true, but initially false', async () => {
const number = ref(2)
Expand Down

0 comments on commit 8bdf2ac

Please sign in to comment.