Form: How is really Vue!

How is really Vue!

by WILLIAM CORREA

HTML

<div id="app">
  <div id="el">
    <demo></demo>
  </div>
</div>

CSS

body {
  background: #20262E;
  padding: 20px;
  font-family: Helvetica;
}

#app {
  background: #fff;
  border-radius: 4px;
  padding: 20px;
  transition: all 0.2s;
}

label {
  display: block;
}

Vue

class Prototype {
	constructor () {
  	this.__fields = []
  	console.log('test')
    this.construct()
  }

	field (field, label, width = 100, type = '', on = {}) {
  	this.__fields.push({
        key: field,
        is: 'input',
        layout: {
          form: {
            label: label,
            class: `field width-${width}`
          }
        },
        attrs: {
          type: type,
        },
        on: on
      })
  }

	fields () {
  	return this.__fields
  }
}

class DomainSpecialist extends Prototype {
  construct () {
  	console.log('test')
  	this.field('name', 'Name', 50, 'number', {
      input: ($event) => window.alert($event.target.value)
    })
    /*
    this
    	.field('name', 'Name')
    	.width(50)
      .type('number')
      .onInput(($event) => window.alert($event.target.value))
    */
  	this.field('choose', 'Choose', 50, 'checkbox', {
      input: ($event) => window.alert($event.target.value)
    })
  }
}

class Controller {
	static build () {
  	const instance = new this()
  	const fields = instance.prototype.fields()
  	return {
    	render(h) {
        return h('form', fields.map(field => {
          return h(
            'div',
            {attrs: field.layout.form},
            [h('label', field.layout.form.label), h('component', field)]
          )
        }))
      }
    }
  }
}

class ControllerDomainSpecialist extends Controller {
	constructor () {
  	super()
  	this.prototype = new DomainSpecialist()
  }
}

new Vue({
  el: "#el",
  components: {
  	demo: ControllerDomainSpecialist.build()
  }
})