GlCell.vue 7.14 KB
<template>
  <view>
    <view v-for="(items, boxIdx) in formList" :key="boxIdx">
      <view class="cell-box">
        <template v-for="(item, idx) in items">
          <view v-if="!item.hidden">
            <view class="cell-row" :key="idx">
              <view class="cell" @click="onCellClick(item)" :class="[
                item.noBorder ? '' : 'border',
                item.onlyLabel ? 'small-height' : '',
                item.class
              ]">
                <view :class="['cell-label', { 'only-label': item.onlyLabel }]">
                  <view v-if="item.labellIcon" :class="['sprite-icon', `icon-${item.labellIcon}`]"></view>
                  {{ item.label }}
                  <view v-if="item.required" class="required">*</view>
                </view>
                <view class="cell-value">
                  <!-- 输入框 -->
                  <template v-if="item.type === 'input'">
                    <view class="input-box">
                      <input class="input-value" v-model="formData[item.value]" :type="item.inputType || 'text'"
                        @blur="handleBlur($event, item)" :placeholder="item.placeholder || '请输入'"
                        :maxlength="item.inputType === 'number' ? false : (item.maxlength || -1)"
                        @input="item.inputType === 'number' && item.maxlength ? (formData[item.value] = String($event.target.value).slice(0, item.maxlength)) : null" />
                      <view v-if="item.valueIcon" :class="['sprite-icon', `icon-${item.valueIcon}`]"
                        @click="onValueClick(item)"></view>
                    </view>
                  </template>
                  <!-- 下拉选择 -->
                  <template v-else-if="item.type === 'select'">
                    <view class="value">
                      <template v-if="!item.addHtml && formData[item.value] && formData[item.value]">
                        <template v-if="item.showText && userInfo && userInfo[item.showText]">
                          {{ userInfo[item.showText] }}<text>|</text>
                        </template>
                        {{ formData[item.value] }}
                      </template>
                      <template v-else>
                        <span class="placeholder">请选择</span>
                      </template>
                      <view class="sprite-icon icon-icon-select"></view>
                    </view>
                  </template>
                  <!-- 单选框 -->
                  <template v-else-if="item.type === 'radio'">
                    <radio-group class="radio-group value" @change="onRadioChange($event, item)">
                      <label class="uni-list-cell uni-list-cell-pd" v-for="radio in item.radioList"
                        :key="radio.value + item.value">
                        <radio style="transform:scale(0.7)" :value="radio.value" :disabled="radio.disabled"
                          :checked="radio.value == item.current" />
                        <view class="radio-name">{{ radio.name }}</view>
                      </label>
                    </radio-group>
                  </template>
                  <!-- 多行文本 -->
                  <template v-else-if="item.type === 'textarea'">
                    <textarea class="textarea" v-model="formData[item.value]" :placeholder="item.placeholder || '请输入'"
                      placeholder-class="textarea-placeholder" confirm-type="done" auto-height
                      :maxlength="item.maxlength"></textarea>
                  </template>
                </view>
              </view>
              <slot v-if="item.addHtml" :name="item.addHtml"></slot>
            </view>
          </view>
        </template>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  name: "GlCell",
  props: {
    /**
     * noBorder:默认false:有下边框,true:无下边框
     * onlyLabel:判断是否只有label,默认false,主要是为了做样式区分
     * class:cell-break:不一行显示,主要用于文本域
     * required:是否为必填项,添加星号
     * value: 绑定的字段名称
     * key:如果绑定的字段名称为对象,则需要传入需要显示在页面上的属性名
     * type: 表单的类型 有:input、select、radio、textarea
     * placeholder: 占位符,input、textarea默认为[请输入],select默认为[请选择],
     * radioList: type为radio时传入
     * current: type为radio时传入,设置radio的默认值
     */
    formList: {
      type: Array,
      default: () => []
    },
    formData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      userInfo: {}
    }
  },
  mounted() {
    this.userInfo = uni.$storage.get('userInfo') || {}
  },
  methods: {
    onCellClick(item) {
      if (item.type === 'select') {
        this.$emit('handleCell', item);
      }
    },
    onRadioChange(e, item) {
      this.$emit('radioChange', e, item);
    },
    onValueClick(item) {
      this.$emit('handleValue', item)
    },
    handleBlur(e, item) {
      this.$emit('inputBlur', e, item)
    }
  }
};
</script>

<style scoped lang="scss">
.cell-box {
  background-color: #fff;
  border-radius: toRpx(16);
  overflow: hidden;
  margin-bottom: toRpx(18);
}

.cell-row {
  padding-left: toRpx(32);
}

.cell {
  display: flex;
  align-items: center;
  padding-right: toRpx(32);
  height: toRpx(96);

  &.small-height {
    height: toRpx(72);
  }

  &.cell-break {
    flex-direction: column;
    align-items: normal;
    height: auto;
  }

  &.border {
    border-bottom: toRpx(2) solid #f7f7f6;
  }
}

.cell-label {
  color: #333;
  font-size: toRpx(28);
  display: flex;
  align-items: center;

  .sprite-icon {
    margin-right: toRpx(8);
  }

  .required {
    display: inline-block;
    margin-left: toRpx(8);
    color: #F24724;
  }

  &.only-label {
    font-size: toRpx(24);
    color: #777;
  }
}

.cell-break .cell-label {
  height: toRpx(96);
  line-height: toRpx(96);
}

.cell-break .cell-value {
  width: 100%;
  margin-left: 0;
}

.cell-value {
  position: relative;
  flex: 1;
  height: 100%;
  margin-left: toRpx(20);
  overflow: hidden;
  font-size: toRpx(28);

  uni-input {
    font-size: toRpx(28);
  }

  .uni-input-placeholder {
    color: #999;
  }

  .value {
    display: flex;
    justify-content: flex-end;
    align-items: center;
    height: 100%;
  }

  .input-box {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    height: 100%;
    width: 100%;

    .sprite-icon {
      margin-left: toRpx(8);
    }
  }

  .input-value {
    height: 100%;
    text-align: right;
    width: 100%;
  }

  .input-bottom {
    min-height: toRpx(100);
    line-height: toRpx(56);
    font-size: toRpx(38);
    padding: toRpx(32);
  }

  .placeholder {
    color: #999;
    font-size: toRpx(28);
  }

  .uni-list-cell {
    margin-left: toRpx(57.2);
  }

  .radio-name {
    display: inline-block;
  }



  .textarea {
    width: 100%;
    border-radius: toRpx(16);
    border: toRpx(2) solid #eff0f2;
    background: #f4f5f7;
    padding: toRpx(24);
    margin-bottom: toRpx(32);
  }

  .textarea-placeholder {
    line-height: normal;
    font-size: toRpx(28);
  }
}
</style>