All files / src/entities/account-cart/ui AccountCartItemRow.vue

47.05% Statements 16/34
37.93% Branches 11/29
26.66% Functions 4/15
46.87% Lines 15/32

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228                      1x         1x           1x 1x   1x 1x           1x 1x                                                   3x       3x       1x     3x                         3x                             3x                 1x                                                                                                                                                                                                                                                
<script setup lang="ts">
import { ref, watch } from "vue";
import { RouterLink } from "vue-router";
 
import {
  formatMoney,
  multiplyMoney,
  type CartItem,
} from "@plain-journal/foundation";
import { PjActionGroup, PjButton } from "@plain-journal/ui";
 
const props = defineProps<{
  item: CartItem;
  busy: boolean;
}>();
 
const emit = defineEmits<{
  quantityChange: [item: CartItem, quantity: number];
  selectionChange: [item: CartItem, selected: boolean];
  remove: [item: CartItem];
}>();
 
const quantityDraft = ref(props.item.quantity);
const confirmingRemoval = ref(false);
 
watch(
  () => props.item.quantity,
  (quantity) => {
    quantityDraft.value = quantity;
  },
);
 
watch(
  () => props.busy,
  (busy, wasBusy) => {
    if (wasBusy && !busy) {
      quantityDraft.value = props.item.quantity;
    }
  },
);
 
function commitQuantity() {
  const quantity = Math.trunc(Number(quantityDraft.value));
  if (!Number.isFinite(quantity) || quantity < 1 || quantity > 999) {
    quantityDraft.value = props.item.quantity;
    return;
  }
  if (quantity !== props.item.quantity) {
    emit("quantityChange", props.item, quantity);
  }
}
 
function confirmRemoval() {
  confirmingRemoval.value = false;
  emit("remove", props.item);
}
</script>
 
<template>
  <article class="account-cart-row">
    <div class="account-cart-row__media" aria-hidden="true">
      <span>{{ item.productTitle.slice(0, 1) }}</span>
    </div>
    <div class="account-cart-row__content">
      <RouterLink
        :to="{ name: 'product-detail', params: { productId: item.productId } }"
      >
        <strong>{{ item.productTitle }}</strong>
      </RouterLink>
      <p>{{ item.skuName }}</p>
      <div class="account-cart-row__controls">
        <label>
          数量
          <input
            v-model.number="quantityDraft"
            type="number"
            min="1"
            max="999"
            inputmode="numeric"
            :disabled="busy"
            @change="commitQuantity"
          />
        </label>
        <label class="account-cart-row__selection">
          <input
            type="checkbox"
            :checked="item.selected"
            :disabled="busy"
            @change="emit(
              'selectionChange',
              item,
              ($event.target as HTMLInputElement).checked,
            )"
          />
          纳入结算
        </label>
      </div>
    </div>
    <div class="account-cart-row__aside">
      <strong>{{ formatMoney(multiplyMoney(item.unitPrice, item.quantity)) }}</strong>
      <PjButton
        v-if="!confirmingRemoval"
        variant="text"
        :disabled="busy"
        @click="confirmingRemoval = true"
      >
        移出
      </PjButton>
      <PjActionGroup
        v-else
        class="account-cart-row__confirmation"
        align="end"
        role="group"
        :aria-label="`从账户购物车移出 ${item.productTitle}`"
      >
        <PjButton variant="text" :disabled="busy" @click="confirmingRemoval = false">
          保留
        </PjButton>
        <PjButton
          class="account-cart-row__remove-confirm"
          variant="text"
          :disabled="busy"
          @click="confirmRemoval"
        >
          确认移出
        </PjButton>
      </PjActionGroup>
    </div>
  </article>
</template>
 
<style scoped>
.account-cart-row {
  display: grid;
  grid-template-columns: 8rem minmax(0, 1fr) auto;
  gap: var(--pj-space-5);
  padding-block: var(--pj-space-5);
  border-bottom: 1px solid var(--pj-border-subtle);
}
 
.account-cart-row__media {
  aspect-ratio: 1;
  display: grid;
  place-items: center;
  overflow: hidden;
  background: var(--pj-surface-media);
  color: var(--pj-brand-primary-hover);
  font-size: var(--pj-font-size-xl);
}
 
.account-cart-row__content p {
  margin: var(--pj-space-2) 0 var(--pj-space-4);
  color: var(--pj-text-secondary);
}
 
.account-cart-row__controls {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: var(--pj-space-5);
  color: var(--pj-text-secondary);
  font-size: var(--pj-font-size-sm);
}
 
.account-cart-row__controls input[type="number"] {
  width: 4rem;
  margin-left: var(--pj-space-2);
  border: 0;
  border-bottom: 1px solid var(--pj-border-strong);
  background: transparent;
  color: inherit;
}
 
.account-cart-row__selection {
  display: inline-flex;
  align-items: center;
  gap: var(--pj-space-2);
}
 
.account-cart-row__aside {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  justify-content: space-between;
  gap: var(--pj-space-3);
}
 
.account-cart-row__aside .pj-button {
  color: var(--pj-text-secondary);
}
 
.account-cart-row__aside .pj-button:disabled {
  opacity: 0.6;
}
 
.account-cart-row__confirmation {
  flex-wrap: wrap;
}
 
.account-cart-row__aside .account-cart-row__remove-confirm {
  color: var(--pj-status-danger-text);
}
 
@media (max-width: 48rem) {
  .account-cart-row {
    grid-template-columns: 5rem minmax(0, 1fr);
  }
 
  .account-cart-row__aside {
    grid-column: 2;
    flex-direction: row;
    align-items: center;
  }
}
 
@media (max-width: 32rem) {
  .account-cart-row {
    grid-template-columns: 4rem minmax(0, 1fr);
    gap: var(--pj-space-4);
  }
 
  .account-cart-row__aside {
    align-items: flex-start;
    flex-direction: column;
  }
}
</style>