<template>
  <div class="my-form-item">
    <!-- 左侧的文本 -->
    <label v-if="label" class="my-form-item__label">{{ label }}</label>
    <!-- 右侧的内容 -->
    <div
      class="my-form-item__content"
      :style="{ 'margin-left': getLabelWidth }"
    >
      <slot></slot>
    </div>
  </div>
</template>

<script>
export default {
  name: 'my-form-item',
  // inject 如果和props同名 会出现覆盖的情况
  inject: ['formlabelWidth'],
  props: {
    label: {
      type: String
    },
    labelWidth: {
      type: String
    }
  },
  computed: {
    getLabelWidth () {
      if (this.labelWidth) {
        return this.labelWidth
      }
      // 说明自己的宽度木有
      // 直接问祖先要
      return this.formlabelWidth
    }
  }
}
</script>

<style lang="less">
.my-form-item {
  margin-bottom: 22px;
  &::before {
    display: table;
    content: '';
  }
  // 文本
  .my-form-item__label {
    text-align: right;
    vertical-align: middle;
    float: left;
    font-size: 14px;
    color: #606266;
    line-height: 40px;
    padding: 0 12px 0 0;
    box-sizing: border-box;
  }
  // 内容
  .my-form-item__content {
    line-height: 40px;
    position: relative;
    font-size: 14px;
    &::before {
      display: table;
      content: '';
    }
  }
}
</style>
