Skip to content
This repository has been archived by the owner on Feb 28, 2023. It is now read-only.

Commit

Permalink
fix: deep map schema fields against lookup components fixes #224 (#234)
Browse files Browse the repository at this point in the history
* fix: deep map schema fields against lookup components

* test: add integration e2e with vee and lookup

Co-authored-by: Marina Mosti <[email protected]>
  • Loading branch information
logaretm and marina-mosti authored Sep 26, 2021
1 parent f2e1ce9 commit 77c1a0f
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 5 deletions.
57 changes: 57 additions & 0 deletions packages/formvuelate/tests/e2e/specs/SchemaFormFactory.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { mount } from '@cypress/vue'
import { h, ref, shallowRef } from 'vue'
import { SchemaFormFactory, useSchemaForm } from '../../../src/index'
import LookupPlugin, { lookupSubSchemas } from '../../../../plugin-lookup/src/index'
import VeeValidatePlugin from '../../../../plugin-vee-validate/src/index'
import { BaseInput } from '../../utils/components'

describe('SchemaFormFactory', () => {
Expand Down Expand Up @@ -70,4 +71,60 @@ describe('SchemaFormFactory', () => {
cy.get('label').eq(3).should('have.text', 'Double nested text')
})
})

describe('with lookup and vee-validate', () => {
it('integrates lookup and vee plugins in nested schemas', () => {
const SCHEMA = [
{
model: 'firstName',
component: 'Text',
label: 'First Name'
},
{
model: 'nested',
component: 'SchemaForm',
schema: [
{
model: 'nestedfirstName',
component: 'Text',
label: 'First Name nested',
validations: value => value && value.length > 3
}
]
}
]

const SchemaFormWithPlugins = SchemaFormFactory([
LookupPlugin({
mapComponents: {
Text: BaseInput
}
}),
VeeValidatePlugin({})
])

mount({
components: { SchemaFormWithPlugins },
setup () {
const model = ref({})
lookupSubSchemas(SchemaFormWithPlugins)
useSchemaForm(model)

const schemaRef = shallowRef(SCHEMA)

return () => h(SchemaFormWithPlugins, {
schema: schemaRef
})
}
})

cy.get('input').should('have.length', 2)
cy.get('label').eq(1).should('have.text', 'First Name nested')

cy.get('input').eq(1).type('Ma')
cy.get('.error').should('have.text', 'First Name nested is not valid.')

cy.get('input').eq(1).type('rina')
})
})
})
12 changes: 10 additions & 2 deletions packages/formvuelate/tests/utils/components.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
import { h } from 'vue'

export const BaseInput = {
props: ['label', 'modelValue'],
props: ['label', 'modelValue', 'validation'],
render () {
return [
h('label', this.label),
h('input', {
...this.$attrs,
value: this.modelValue,
onInput: ($event) => this.$emit('update:modelValue', $event.target.value)
})
}),
this.$props.validation?.errorMessage
? h('div',
{
class: 'error'
},
this.$props.validation.errorMessage
)
: null
]
}
}
25 changes: 22 additions & 3 deletions packages/plugin-lookup/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,36 @@ export const mapElementsInSchema = (schema, fn) => schema.map(row => row.map(el
* @returns {Array}
*/
const mapComps = (schema, mapComponents) => {
return mapElementsInSchema(schema, el => {
function mapSchemaElement (el) {
const newKey = mapComponents[el.component]

if (el.schema) return { ...el }
// recursively exhaust all sub schemas
if (el.schema) {
const schemaArray = Array.isArray(el.schema)
? el.schema
: Object.keys(el.schema).map(model => {
return {
model,
...el.schema[model]
}
})

return {
...el,
component: mapComponents[el.component] || el.component,
schema: schemaArray.map(mapSchemaElement)
}
}

if (!newKey) return { ...el }

return {
...el,
component: mapComponents[el.component]
}
})
}

return mapElementsInSchema(schema, mapSchemaElement)
}

/**
Expand Down

0 comments on commit 77c1a0f

Please sign in to comment.