GLCustForm.js 7.02 KB
// components/GLCustForm/GLCustForm.js
Component({
  externalClasses: ['custom-class'],
  options: {
    multipleSlots: true, // 在组件定义时的选项中启用多 slot 支持
    styleIsolation: 'shared', // 自定义覆盖vant样式 
  },
  /**
   * 组件的属性列表
   */
  properties: {
    formData: {
      type: Object,
      value: () => { },
      observer: function (n) {
        this.setData({
          formData: n
        })
      }
    },
    formDataList: {
      type: Array,
      observer: function(n) {
        if(n) {
          this.changeStyle()
        }
      }
    },
    labelWidth: {
      type: String,
      value: 'var(--fontSize140)'
    },
    // 页面存放表单对象的对象名,用于直接变更page的值
    dataAttrName: {
      type: String,
      value: null
    },
    disable: {
      type: Boolean,
      value: false
    },
    glCellClass: {
      type: String,
      value: false
    },
    // 背景色
    background: {
      type: String,
      value: 'transparent'
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    targetIdx: -1,
    areaList: []
  },

  lifetimes: {
    attached() {
      let thisPage = wx.$router.getPageInstance()
      console.log(this.data.formDataList, 'formDataList---')
      this.changeStyle()
      wx.$eventbus.on('set_car_info', n => {
        let {
          targetIdx,
          dataAttrName
        } = this.data
        let formData = thisPage?.data[dataAttrName] || null
        if (formData) {
          if (targetIdx != -1) {
            let targetItem = this.data.formDataList[targetIdx] || {}
            if (targetItem.handleChange) {
              targetItem.handleChange(thisPage, dataAttrName, n)
            }
            if (!targetItem.noDefault) {
              thisPage.setData({
                [dataAttrName + '.' + targetItem.labelName]: n.vehicleBrandName + '/' + n.vehicleSeriesName + '/' + n.vehicleShapeName + '/' + n.vehicleSubBrandName
              })
            }
          }
          formData = Object.assign(formData, n)
          thisPage.setData({
            [dataAttrName]: formData
            // carInfo: n,
          })
        }
      })
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    /**
     * 默认样式为label和value在同一行
     * 改为label和value为两行
     */
    changeStyle() {
      let { formDataList } = this.data
      formDataList.forEach((item, idx) => {
        if(item.isLabelNoLine) {
          formDataList[idx].noBorder = true
          formDataList[idx].noPadding = true
          formDataList[idx].cellStyle = 'display: block !important; height: auto !important; padding: 0 40rpx !important;'
          formDataList[idx].labelStyle = 'width: 100% !important; padding: 24rpx 0 16rpx 0;'
          formDataList[idx].cellValueStyle = `box-sizing: border-box; width: 100% !important; border: ${item.type == 'input' ? '2rpx': '0'} solid #ececec; padding: ${item.type == 'input' ? '0 24rpx 0 0 !important': '0'}; border-radius: ${item.type == 'input' ? '8rpx': '0'} ;`
          formDataList[idx].customStyle = `padding: 0 0; display: flex; align-items: center; justify-content: space-between;`
          formDataList[idx].inputCustomStyle = `width: ${item.suffix ? 'calc(100% - 4rpx)' : '100%'} !important; flex-shrink: 0; padding: 0 0;`
        }
      })
      this.setData({
        formDataList
      })
    },
    getHeight(selector) {
      return new Promise(resolve => {
        wx.$util.getViewInfo(selector, res => {
          return resolve(res)
        }, this)
      })
    },
    parentSetData(attr, value) {
      let {
        dataAttrName
      } = this.data
      let thisPage = wx.$router.getPageInstance()
      thisPage.setData({
        [dataAttrName + '.' + attr]: value
      })
    },
    handleRadio(e) {
      let value = e.detail
      let {
        attr
      } = e.target.dataset
      let {
        dataAttrName
      } = this.data
      let thisPage = wx.$router.getPageInstance()
      thisPage.setData({
        [dataAttrName + '.' + attr]: value
      })
      this.triggerEvent('radioEvent', {
        attr
      })
    },
    handleInput(e) {
      let value = e.detail
      let {
        attr,
        item
      } = e.target.dataset
      let {
        dataAttrName
      } = this.data
      //   if (attr == 'price') {
      //     value = value >= 2000000 ? '2000000' : value
      //     // wx.$eventbus.emit('delete_goodsSelect')
      //   }
      // console.log(item,'handleInput');
      let thisPage = wx.$router.getPageInstance()
      if (item.inputLimit && item.inputLimit.maxVal) {
        value = value >= item.inputLimit.maxVal ? item.inputLimit.maxVal : value
      }
      if (item.inputLimit && item.inputLimit.minVal) {
        value = value <= item.inputLimit.minVal ? item.inputLimit.minVal : value
      }
      if (item.inputLimit && item.inputLimit.type == 'digit') {
        let [number, decimal] = value.toString().split('.')
        if (decimal != undefined) {
          decimal = decimal.substring(0, item.inputLimit.decimalDigit || 2)
          number += `.${decimal}`
          value = number
        }
      }
      const diffValue = value.toString()
      const trimValue = value.toString().trim()
      if (diffValue != trimValue) {
        value = trimValue
      }

      thisPage.setData({
        [dataAttrName + '.' + attr]: value + ''
      })
      this.triggerEvent('inputEvent', {
        attr
      })
    },
    handlePickerChange(e) {
      let idx = e.detail.value || 0
      let {
        target
      } = e.currentTarget.dataset
      let {
        dataAttrName
      } = this.data
      let thisPage = wx.$router.getPageInstance()
      console.log(target, 'target---')
      thisPage.setData({
        [dataAttrName + '.' + target.name]: target.optionList[idx][target.optionId],
        [dataAttrName + '.' + target.labelName]: target.optionList[idx][target.optionLabel]
      })
      this.triggerEvent('pickerChange', {
        obj: target.optionList[idx]
      })
    },
    handleTimeChange(e) {
      let {
        value
      } = e.detail
      let {
        target
      } = e.currentTarget.dataset
      let {
        dataAttrName
      } = this.data
      let thisPage = wx.$router.getPageInstance()
      thisPage.setData({
        [dataAttrName + '.' + target.name]: value
      })
      // console.log(this.data.formData)
    },

    // 品牌车型
    handleJumpBrand(e) {
      wx.$router.go('packageEquity.carBrands')
    },

    handleCellClick(e) {
      let {
        target,
        idx
      } = e.currentTarget.dataset
      switch (target.type) {
        case 'brand':
          this.setData({
            targetIdx: idx
          })
          this.handleJumpBrand()
          break
        default:
          break
      }
    },
    showAreaFun() {
      let {
        showArea,
        areaList
      } = this.data
      if (!areaList.length) areaList = wx.$areaList // 省市区数据
      this.setData({
        showArea: !showArea,
        areaList
      })
    },
    bindRegionChange(e) {
      this.triggerEvent('RegionChange', e)
      this.showAreaFun()
    }
  }
})