Browse Source

feat: address 组件优化

yangxiaolu3 4 years ago
parent
commit
4cede4cec1
3 changed files with 102 additions and 160 deletions
  1. 44 76
      src/packages/address/demo.vue
  2. 43 59
      src/packages/address/doc.md
  3. 15 25
      src/packages/address/index.vue

+ 44 - 76
src/packages/address/demo.vue

@@ -9,12 +9,12 @@
     ></nut-cell>
     ></nut-cell>
 
 
     <nut-address
     <nut-address
-      v-model:show="showPopup"
+      v-model:show="normal"
       :province="province"
       :province="province"
       :city="city"
       :city="city"
       :country="country"
       :country="country"
       :town="town"
       :town="town"
-      @on-change="cal => onChange(cal, 'showPopup')"
+      @on-change="cal => onChange(cal, 'normal')"
       @close="close1"
       @close="close1"
       customAddressTitle="请选择所在地区"
       customAddressTitle="请选择所在地区"
     ></nut-address>
     ></nut-address>
@@ -28,10 +28,10 @@
     ></nut-cell>
     ></nut-cell>
 
 
     <nut-address
     <nut-address
-      v-model:show="showPopupExist"
+      v-model:show="exist"
       type="exist"
       type="exist"
       :existAddress="existAddress"
       :existAddress="existAddress"
-      @on-change="cal => onChange(cal, 'showPopupExist')"
+      @on-change="cal => onChange(cal, 'exist')"
       @close="close2"
       @close="close2"
       :isShowCustomAddress="false"
       :isShowCustomAddress="false"
       @selected="selected"
       @selected="selected"
@@ -47,10 +47,10 @@
     ></nut-cell>
     ></nut-cell>
 
 
     <nut-address
     <nut-address
-      v-model:show="showPopupCustomImg"
+      v-model:show="customImg"
       type="exist"
       type="exist"
       :existAddress="existAddress"
       :existAddress="existAddress"
-      @on-change="cal => onChange(cal, 'showPopupCustomImg')"
+      @on-change="cal => onChange(cal, 'customImg')"
       @close="close3"
       @close="close3"
       :isShowCustomAddress="false"
       :isShowCustomAddress="false"
       @selected="selected"
       @selected="selected"
@@ -68,7 +68,7 @@
     ></nut-cell>
     ></nut-cell>
 
 
     <nut-address
     <nut-address
-      v-model:show="showPopupOther"
+      v-model:show="other"
       type="exist"
       type="exist"
       :existAddress="existAddress"
       :existAddress="existAddress"
       :province="province"
       :province="province"
@@ -76,7 +76,7 @@
       :country="country"
       :country="country"
       :town="town"
       :town="town"
       :backBtnIcon="backBtnIcon"
       :backBtnIcon="backBtnIcon"
-      @on-change="cal => onChange(cal, 'showPopupOther')"
+      @on-change="cal => onChange(cal, 'other')"
       @close="close4"
       @close="close4"
       @selected="selected"
       @selected="selected"
       customAndExistTitle="选择其他地址"
       customAndExistTitle="选择其他地址"
@@ -93,33 +93,33 @@ const { createDemo } = createComponent('address');
 export default createDemo({
 export default createDemo({
   props: {},
   props: {},
   setup() {
   setup() {
-    const showPopup = ref(false);
-    const province = ref([
-      { id: 1, name: '北京' },
-      { id: 2, name: '广西' },
-      { id: 3, name: '江西' },
-      { id: 4, name: '四川' }
-    ]); // 省
-
-    const city = ref([
-      { id: 7, name: '朝阳区' },
-      { id: 8, name: '崇文区' },
-      { id: 9, name: '昌平区' },
-      { id: 6, name: '石景山区' }
-    ]); // 市
-
-    const country = ref([
-      { id: 3, name: '八里庄街道' },
-      { id: 9, name: '北苑' },
-      { id: 4, name: '常营乡' }
-    ]); // 县
-
-    const town = ref([]); // 镇
-
-    const showPopupExist = ref(false);
-    const showPopupCustomImg = ref(false);
+    const address = reactive({
+      province: [
+        { id: 1, name: '北京' },
+        { id: 2, name: '广西' },
+        { id: 3, name: '江西' },
+        { id: 4, name: '四川' }
+      ],
+      city: [
+        { id: 7, name: '朝阳区' },
+        { id: 8, name: '崇文区' },
+        { id: 9, name: '昌平区' },
+        { id: 6, name: '石景山区' }
+      ],
+      country: [
+        { id: 3, name: '八里庄街道' },
+        { id: 9, name: '北苑' },
+        { id: 4, name: '常营乡' }
+      ],
+      town: []
+    });
 
 
-    const showPopupOther = ref(false);
+    const showPopup = reactive({
+      normal: false,
+      exist: false,
+      customImg: false,
+      other: false
+    });
 
 
     const icon = reactive({
     const icon = reactive({
       selectedIcon: 'heart-fill',
       selectedIcon: 'heart-fill',
@@ -175,40 +175,13 @@ export default createDemo({
     });
     });
 
 
     const showAddress = () => {
     const showAddress = () => {
-      showPopup.value = !showPopup.value;
+      showPopup.normal = !showPopup.normal;
     };
     };
 
 
     const onChange = (cal, tag) => {
     const onChange = (cal, tag) => {
-      let name = province;
-      switch (cal.next) {
-        case 'province':
-          name = province;
-          break;
-        case 'city':
-          name = city;
-          break;
-        case 'country':
-          name = city;
-          break;
-        default:
-          name = town;
-          break;
-      }
-      if (name.value.length < 1) {
-        switch (tag) {
-          case 'showPopup':
-            showPopup.value = false;
-            break;
-          case 'showPopupExist':
-            showPopupExist.value = false;
-            break;
-          case 'showPopupCustomImg':
-            showPopupCustomImg.value = false;
-            break;
-          default:
-            showPopupOther.value = false;
-            break;
-        }
+      const name = address[cal.next];
+      if (name.length < 1) {
+        showPopup[tag] = false;
       }
       }
     };
     };
     const close1 = val => {
     const close1 = val => {
@@ -217,7 +190,7 @@ export default createDemo({
     };
     };
 
 
     const showAddressExist = () => {
     const showAddressExist = () => {
-      showPopupExist.value = true;
+      showPopup.exist = true;
     };
     };
 
 
     const close2 = val => {
     const close2 = val => {
@@ -242,10 +215,10 @@ export default createDemo({
     };
     };
 
 
     const showAddressOther = () => {
     const showAddressOther = () => {
-      showPopupOther.value = true;
+      showPopup.other = true;
     };
     };
     const showCustomImg = () => {
     const showCustomImg = () => {
-      showPopupCustomImg.value = true;
+      showPopup.customImg = true;
     };
     };
 
 
     const close3 = val => {
     const close3 = val => {
@@ -296,19 +269,12 @@ export default createDemo({
 
 
     return {
     return {
       showAddress,
       showAddress,
-      province,
-      city,
-      country,
-      town,
       showPopup,
       showPopup,
       onChange,
       onChange,
       close1,
       close1,
       showAddressExist,
       showAddressExist,
       close2,
       close2,
       selected,
       selected,
-      showPopupExist,
-      showPopupCustomImg,
-      showPopupOther,
       existAddress,
       existAddress,
       showAddressOther,
       showAddressOther,
       showCustomImg,
       showCustomImg,
@@ -317,7 +283,9 @@ export default createDemo({
       switchModule,
       switchModule,
       closeMask,
       closeMask,
       ...toRefs(icon),
       ...toRefs(icon),
-      ...toRefs(text)
+      ...toRefs(text),
+      ...toRefs(showPopup),
+      ...toRefs(address)
     };
     };
   }
   }
 });
 });

+ 43 - 59
src/packages/address/doc.md

@@ -37,50 +37,35 @@ app.use(Popup);
 ```javascript
 ```javascript
 setup() {
 setup() {
     const showPopup = ref(false);
     const showPopup = ref(false);
-    const province = ref([
-      { id: 1, name: '北京' },
-      { id: 2, name: '广西' },
-      { id: 3, name: '江西' },
-      { id: 4, name: '四川' }
-    ]); // 省
-
-    const city = ref([
-      { id: 7, name: '朝阳区' },
-      { id: 8, name: '崇文区' },
-      { id: 9, name: '昌平区' },
-      { id: 6, name: '石景山区' }
-    ]); // 市
-
-    const country = ref([
-      { id: 3, name: '八里庄街道' },
-      { id: 9, name: '北苑' },
-      { id: 4, name: '常营乡' }
-    ]); // 县
+    const address = reactive({
+      province:[
+        { id: 1, name: '北京' },
+        { id: 2, name: '广西' },
+        { id: 3, name: '江西' },
+        { id: 4, name: '四川' }
+      ],
+      city:[
+        { id: 7, name: '朝阳区' },
+        { id: 8, name: '崇文区' },
+        { id: 9, name: '昌平区' },
+        { id: 6, name: '石景山区' }
+      ],
+      country:[
+        { id: 3, name: '八里庄街道' },
+        { id: 9, name: '北苑' },
+        { id: 4, name: '常营乡' }
+      ],
+      town:[]
+    })
 
 
     const text = ref('请选择地址')
     const text = ref('请选择地址')
 
 
-    const town = ref([]); // 镇
-
     const showAddress = () => {
     const showAddress = () => {
       showPopup.value = !showPopup.value;
       showPopup.value = !showPopup.value;
     };
     };
 
 
     const onChange = (cal) => {
     const onChange = (cal) => {
-      let name = province;
-      switch (cal.next) {
-        case 'province':
-          name = province;
-          break;
-        case 'city':
-          name = city;
-          break;
-        case 'country':
-          name = city;
-          break;
-        default:
-          name = town;
-          break;
-      }
+      const name = address[cal.next]
       if (name.value.length < 1) {
       if (name.value.length < 1) {
         showPopup.value = false;
         showPopup.value = false;
       }
       }
@@ -90,7 +75,7 @@ setup() {
       text.value = val.data.addressStr;
       text.value = val.data.addressStr;
     };
     };
 
 
-    return { showPopup, province, city, country, town, text, showAddress, onChange, close };
+    return { showPopup, text, showAddress, onChange, close, ...toRefs(address) };
 }
 }
 ```
 ```
 
 
@@ -277,27 +262,26 @@ setup() {
 ```javascript
 ```javascript
 setup() {
 setup() {
     const showPopupOther = ref(false);
     const showPopupOther = ref(false);
-    const province = ref([
-      { id: 1, name: '北京' },
-      { id: 2, name: '广西' },
-      { id: 3, name: '江西' },
-      { id: 4, name: '四川' }
-    ]); // 省
-
-    const city = ref([
-      { id: 7, name: '朝阳区' },
-      { id: 8, name: '崇文区' },
-      { id: 9, name: '昌平区' },
-      { id: 6, name: '石景山区' }
-    ]); // 市
-
-    const country = ref([
-      { id: 3, name: '八里庄街道' },
-      { id: 9, name: '北苑' },
-      { id: 4, name: '常营乡' }
-    ]); // 县
-
-    const town = ref([]); // 镇
+    const address = reactive({
+      province:[
+        { id: 1, name: '北京' },
+        { id: 2, name: '广西' },
+        { id: 3, name: '江西' },
+        { id: 4, name: '四川' }
+      ],
+      city:[
+        { id: 7, name: '朝阳区' },
+        { id: 8, name: '崇文区' },
+        { id: 9, name: '昌平区' },
+        { id: 6, name: '石景山区' }
+      ],
+      country:[
+        { id: 3, name: '八里庄街道' },
+        { id: 9, name: '北苑' },
+        { id: 4, name: '常营乡' }
+      ],
+      town:[]
+    })
     const existAddress = ref([
     const existAddress = ref([
       {
       {
         id: 1,
         id: 1,
@@ -369,7 +353,7 @@ setup() {
       console.log('关闭弹层', val);
       console.log('关闭弹层', val);
     };
     };
 
 
-    return { showPopupOther, province, city, country, town, text, showAddressOther, switchModule, closeMask, close, selected, backBtnIcon };
+    return { showPopupOther, text, showAddressOther, switchModule, closeMask, close, selected, backBtnIcon, ...toRefs(address) };
 }
 }
 ```
 ```
 # API
 # API

+ 15 - 25
src/packages/address/index.vue

@@ -37,7 +37,7 @@
             :class="[index == tabIndex ? 'active' : '']"
             :class="[index == tabIndex ? 'active' : '']"
             v-for="(item, key, index) in selectedRegion"
             v-for="(item, key, index) in selectedRegion"
             :key="index"
             :key="index"
-            :ref="'tabItem' + key"
+            :ref="key"
             @click="changeRegionTab(item, key, index)"
             @click="changeRegionTab(item, key, index)"
             ><span>{{ getTabName(item, index) }}</span></view
             ><span>{{ getTabName(item, index) }}</span></view
           >
           >
@@ -198,10 +198,17 @@ export default create({
     console.log('componentName', componentName);
     console.log('componentName', componentName);
 
 
     const regionLine = ref<null | HTMLElement>(null);
     const regionLine = ref<null | HTMLElement>(null);
-    const tabItemprovince = ref<null | HTMLElement>(null);
-    const tabItemcity = ref<null | HTMLElement>(null);
-    const tabItemcountry = ref<null | HTMLElement>(null);
-    const tabItemtown = ref<null | HTMLElement>(null);
+    // const tabItemprovince = ref<null | HTMLElement>(null);
+    // const tabItemcity = ref<null | HTMLElement>(null);
+    // const tabItemcountry = ref<null | HTMLElement>(null);
+    // const tabItemtown = ref<null | HTMLElement>(null);
+
+    const tabItemRef = reactive({
+      province: ref<null | HTMLElement>(null),
+      city: ref<null | HTMLElement>(null),
+      country: ref<null | HTMLElement>(null),
+      town: ref<null | HTMLElement>(null)
+    });
 
 
     const showPopup = ref(false);
     const showPopup = ref(false);
     const showModule = ref('exist'); //展示 exist 还是 custom 主要用于’选择其他地址‘
     const showModule = ref('exist'); //展示 exist 还是 custom 主要用于’选择其他地址‘
@@ -250,21 +257,7 @@ export default create({
     };
     };
     // 移动下面的红线
     // 移动下面的红线
     const lineAnimation = () => {
     const lineAnimation = () => {
-      let name;
-      switch (tabName.value[tabIndex.value]) {
-        case 'province':
-          name = tabItemprovince.value;
-          break;
-        case 'city':
-          name = tabItemcity.value;
-          break;
-        case 'country':
-          name = tabItemcountry.value;
-          break;
-        default:
-          name = tabItemtown.value;
-      }
-
+      const name = tabItemRef[tabName.value[tabIndex.value]];
       nextTick(() => {
       nextTick(() => {
         if (name) {
         if (name) {
           const distance = name.offsetLeft;
           const distance = name.offsetLeft;
@@ -463,15 +456,12 @@ export default create({
       getTabName,
       getTabName,
       nextAreaList,
       nextAreaList,
       regionLine,
       regionLine,
-      tabItemprovince,
-      tabItemcity,
-      tabItemcountry,
-      tabItemtown,
       changeRegionTab,
       changeRegionTab,
       selectedExist,
       selectedExist,
       clickOverlay,
       clickOverlay,
       handClose,
       handClose,
-      ...toRefs(props)
+      ...toRefs(props),
+      ...toRefs(tabItemRef)
     };
     };
   }
   }
 });
 });