'f'
mh-two-thousand-and-two
2024-04-12 26f2711ef9461961fb953e2b497bd314ef95e345
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import {
  emitter,
  listeners
} from '../mixins'
 
const NATIVE_COMPONENT_TYPES = ['u-input', 'u-textarea']
 
function getForm (weex) {
  const modulePlus = weex.requireModule('plus')
  return {
    name: 'Form',
    mixins: [emitter, listeners],
    data: function data () {
      return {
        childrenList: []
      }
    },
    listeners: {
      '@form-submit': '_onSubmit',
      '@form-reset': '_onReset',
      '@form-group-update': '_formGroupUpdateHandler'
    },
    methods: {
      _onSubmit ($event) {
        const data = this._getNativeFormData() || {}
        this.childrenList.forEach(function (vm) {
          if (vm._getFormData && vm._getFormData().key) {
            data[vm._getFormData().key] = vm._getFormData().value
          }
        })
        this.$trigger('submit', {
          value: data
        })
      },
      _onReset ($event) {
        this.$trigger('reset', {})
        this._getNativeFormData(true)
        this.childrenList.forEach(function (vm) {
          vm._resetFormData && vm._resetFormData()
        })
      },
      _formGroupUpdateHandler ($event) {
        if ($event.type === 'add') {
          this.childrenList.push($event.vm)
        }
        else {
          const index = this.childrenList.indexOf($event.vm)
          this.childrenList.splice(index, 1)
        }
      },
      _getNativeFormData (isClear) {
        const data = {}
 
        function find (nodes) {
          nodes.forEach(function (node) {
            if (NATIVE_COMPONENT_TYPES.indexOf(node.tag) >= 0 && node.data.attrs && node.data.attrs['name']) {
              if (isClear) {
                node.elm.setValue('')
              }
              else {
                data[node.data.attrs['name']] = modulePlus.getValue(node.elm.nodeId)
              }
            }
            if (node.children) {
              find(node.children)
            }
          })
        }
 
        find(this.$vnode.componentOptions.children)
 
        return data
      }
    },
    render (createElement) {
      const _vm = this
      return createElement('view', _vm._g({}, _vm.$listeners), [_vm._t('default')], 2)
    }
  }
}
 
export default function init (Vue, weex) {
  Vue.component('form', getForm(weex))
}