label.uvue 3.2 KB
Newer Older
DCloud-WZF's avatar
DCloud-WZF 已提交
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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
<template>
  <view>
    <page-head :title="title"></page-head>
    <view class="uni-common-mt">
      <view class="uni-form-item uni-column">
        <view class="title">表单组件在label内</view>
        <checkbox-group class="uni-list" @change="checkboxChange">
          <label class="uni-list-cell uni-list-cell-pd checkboxItemsTest" v-for="item in checkboxItems"
            :key="item.name">
            <view>
              <checkbox :value="item.name" :checked="item.checked"></checkbox>
            </view>
            <view>{{item.value}}</view>
          </label>
        </checkbox-group>
      </view>

      <view class="uni-form-item uni-column">
        <view class="title">label用for标识表单组件</view>
        <radio-group class="uni-list" @change="radioChange">
          <view class="uni-list-cell uni-list-cell-pd" v-for="(item,index) in radioItems" :key="index">
            <view>
              <radio :id="item.name" :value="item.name" :checked="item.checked"></radio>
            </view>
            <label class="label-2-text" :for="item.name">
              <text>{{item.value}}</text>
            </label>
          </view>
        </radio-group>
      </view>

      <view class="uni-form-item uni-column">
        <view class="title">label内有多个时选中第一个</view>
        <checkbox-group class="uni-list" @change="checkboxChange">
          <label class="label-3">
            <view class="uni-list-cell uni-list-cell-pd">
              <checkbox>选项一</checkbox>
            </view>
            <view class="uni-list-cell uni-list-cell-pd">
              <checkbox>选项二</checkbox>
            </view>
            <view class="uni-link uni-center" style="margin:20rpx 0;">点击该label下的文字默认选中第一个checkbox</view>
          </label>
        </checkbox-group>
      </view>
    </view>
  </view>
</template>
<script lang="uts">
  export default {
    data() {
      return {
        title: 'label',
        checkboxItems: [{
          name: 'USA',
          value: '美国'
        },
        {
          name: 'CHN',
          value: '中国',
          checked: 'true'
        }
        ],
        radioItems: [{
          name: 'USA',
          value: '美国'
        },
        {
          name: 'CHN',
          value: '中国',
          checked: 'true'
        }
        ],
        hidden: false,
        checkboxValue: [] as string[],
        radioValue: ''
      }
    },
    methods: {
      checkboxChange: function (e : UniCheckboxGroupChangeEvent) {
        console.log(e.detail.value)
        this.checkboxValue = e.detail.value
      },
      radioChange: function (e : UniRadioGroupChangeEvent) {
        console.log(e.detail.value)
        this.radioValue = e.detail.value
      }
    }
  }
</script>

<style>
  .uni-list-cell {
    justify-content: flex-start
  }

  .uni-list .label-3 {
    padding: 0;
  }

  .label-2-text {
    flex: 1;
  }

  .uni-form-item {
    display: flex;
    width: 100%;
    padding: 10rpx 0;
  }

  .uni-form-item .title {
    padding: 10rpx 25rpx;
  }

  radio-group {
    padding: 0 20rpx;
  }

  checkbox-group label {
    margin: 0 20rpx;
  }
Anne_LXM's avatar
Anne_LXM 已提交
122
</style>