CenterPopup.uvue 5 KB
<template>
    <un-popup :show="visible"
              closeOnClickOverlay
              :z-index="99"
              :title="title"
              :closeable="true"
              @close="close"
              @close-icon-click="close">
            <view class="popup-panel" :style="popupStyle">
                <view class="popup-content">
                    <view v-for="(item, index) in list"
                          :key="index"
                          :class="['popup-item', itemRowActive(item) ? 'popup-item-active' : '']"
                          @tap="handleChoose(index)">
                        <view class="item-icon">
                            <text class="item-icon-text">{{ (item as UTSJSONObject).icon ?? '' }}</text>
                        </view>
                        <view class="item-text">
                            <text class="item-title">{{ (item as UTSJSONObject).title ?? '' }}</text>
                            <text class="item-describe">{{ (item as UTSJSONObject).describe ?? '' }}</text>
                        </view>
                        <view v-if="itemRowActive(item)" class="item-active-pill">
                            <text class="item-active-pill-text">当前</text>
                        </view>
                        <view class="item-arrow">
                            <un-icon name="arrow-right" size="14" color="#8aa0bf"></un-icon>
                        </view>
                    </view>
                </view>
            </view>
    </un-popup>
</template>

<script setup lang="uts">
import { computed, defineExpose, ref, withDefaults } from "vue";

const props = withDefaults(defineProps<{
  list?: any[]
  title?: string
  /** 受控显示,父组件传 true 即打开 */
  visible?: boolean
  /** 与列表项 `typeCode` 比对,高亮当前选中(Pad 业务类型等) */
  selectedTypeCode?: string | null
}>(), {
	selectedTypeCode: null,
});

const emit = defineEmits<{
  choose: [index: number]
  "update:visible": [value: boolean]
}>();

const internalVisible = ref(false);
const popupStyle = computed((): UTSJSONObject => {
	const info = uni.getWindowInfo();
	const winW = info.windowWidth > 0 ? info.windowWidth : 375;
	const sideGapPx = 32;
	const maxPanelWidthPx = 700;
	const widthByWindow = winW - sideGapPx;
	const widthPx = Math.max(280, Math.min(maxPanelWidthPx, widthByWindow));
	return {
		width: `${widthPx}px`
	} as UTSJSONObject;
});

const visible = computed((): boolean => {
	if (props.visible != null) return props.visible == true;
	return internalVisible.value;
});

function itemRowActive(item: any): boolean {
	const sel = props.selectedTypeCode;
	if (sel == null || sel === "") {
		return false;
	}
	const obj = item as UTSJSONObject;
	const code = obj["typeCode"] as string | null;
	return code != null && code !== "" && code === sel;
}

function open(): void {
	if (props.visible != null) {
		emit("update:visible", true);
	} else {
		internalVisible.value = true;
	}
}

function close(): void {
	internalVisible.value = false;
	emit("update:visible", false);
}

function handleChoose(index: number): void {
	emit("choose", index);
	close();
}

defineExpose({
	open,
	close
});
</script>

<style scoped lang="scss">
.popup-panel {
  border-radius: 16px;
  overflow: hidden;
  background-color: var(--bg-card, #FFFFFF);
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  align-items: stretch;
}

.popup-content {
  padding: 12px;
  box-sizing: border-box;
}


.popup-item {
  display: flex;
  flex-direction: row;
  align-items: center;
  padding: 12px 12px;
  margin-bottom: 10px;
  background: rgba(255, 255, 255, 0.96);
  border-radius: 12px;
  border: 1px solid #dbe6f7;
  box-shadow: 0 6px 14px rgba(15, 23, 42, 0.08);
}

.popup-item:last-child {
  margin-bottom: 0;
}

.popup-item-active {
  border-color: rgba(44, 102, 247, 0.55);
  background: rgba(44, 102, 247, 0.08);
  box-shadow: 0 6px 16px rgba(44, 102, 247, 0.18);
}

.item-active-pill {
  flex-shrink: 0;
  margin-right: 6px;
  padding: 2px 8px;
  border-radius: 10px;
  background: rgba(44, 102, 247, 0.16);
  border-width: 1px;
  border-style: solid;
  border-color: rgba(44, 102, 247, 0.35);
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
}

.item-active-pill-text {
  font-size: 11px;
  font-weight: 600;
  color: #2c66f7;
  line-height: 14px;
}

.item-icon {
  width: 40px;
  height: 40px;
  margin-right: 10px;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  background: #edf4ff;
  border-radius: 10px;
}

.item-icon-text {
  font-size: 21px;
  line-height: 1;
  text-align: center;
}

.item-text {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.item-title {
  font-weight: 600;
  font-size: 14px;
  color: #1e293b;
  margin-bottom: 2px;
}

.item-describe {
  font-size: 12px;
  color: #8091aa;
  white-space: pre-wrap;
}

.item-arrow {
  margin-left: 8px;
  width: 16px;
  height: 16px;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
}
</style>