courseMultiSelect.vue 6.06 KB
<template>
  <div class="course-select" @click="openPicker">
    <div class="course-row-header">
      <span>{{ label }} <em v-if="required">*</em></span>
      <div class="course-row-link">
        <span class="placeholder">{{ placeholder }}</span>
        <van-icon name="arrow" />
      </div>
    </div>

    <div v-if="selectedCourses.length" class="course-tags">
      <span v-for="course in selectedCourses"
            :key="course.id"
            class="course-tag">
        <span class="course-tag-text">{{ course.name }}</span>
        <van-icon name="cross" @click.stop="removeCourse(course)" />
      </span>
    </div>

    <van-popup v-model="show"
               class="course-popup"
               position="bottom"
               round>
      <div class="course-picker">
        <div class="course-picker-title">
          课程选择
        </div>
        <van-search v-model.trim="keyword"
                    placeholder="搜索"
                    shape="round" />
        <div class="course-list">
          <div v-if="loading" class="course-empty">
            加载中...
          </div>
          <div v-else-if="!filteredCourses.length" class="course-empty">
            暂无课程
          </div>
          <template v-else>
            <div v-for="course in filteredCourses"
                 :key="course.id"
                 class="course-option"
                 :class="{ selected: isSelected(course) }"
                 @click.stop="toggleCourse(course)">
              <span>{{ course.name }}</span>
              <van-icon v-if="isSelected(course)" name="success" />
            </div>
          </template>
        </div>
        <div class="course-actions">
          <button class="course-cancel"
                  type="button"
                  @click.stop="show = false">
            取消
          </button>
          <button class="course-confirm"
                  type="button"
                  @click.stop="confirm">
            确认
          </button>
        </div>
      </div>
    </van-popup>
  </div>
</template>

<script>
export default {
	name: "CourseMultiSelect",
	props: {
		value: {
			type: Array,
			default: () => [],
		},
		courses: {
			type: Array,
			default: () => [],
		},
		label: {
			type: String,
			default: "推送课程",
		},
		placeholder: {
			type: String,
			default: "点击选择(可多选)",
		},
		required: {
			type: Boolean,
			default: false,
		},
		loading: {
			type: Boolean,
			default: false,
		},
	},
	data() {
		return {
			show: false,
			keyword: "",
			tempSelected: [],
		};
	},
	computed: {
		selectedCourses() {
			return this.courses.filter(course => this.value.includes(course.id));
		},
		filteredCourses() {
			const keyword = this.keyword.trim().toLowerCase();
			if (!keyword)
				return this.courses;
			return this.courses.filter(course => course.name.toLowerCase().includes(keyword));
		},
	},
	methods: {
		openPicker() {
			this.keyword = "";
			this.tempSelected = [...this.value];
			this.show = true;
			this.$emit("open");
		},
		isSelected(course) {
			return this.tempSelected.includes(course.id);
		},
		toggleCourse(course) {
			const index = this.tempSelected.indexOf(course.id);
			if (index > -1) {
				this.tempSelected.splice(index, 1);
				return;
			}
			this.tempSelected.push(course.id);
		},
		confirm() {
			this.updateValue(this.tempSelected);
			this.show = false;
		},
		removeCourse(course) {
			this.updateValue(this.value.filter(id => id !== course.id));
		},
		updateValue(ids) {
			const selectedSet = new Set(ids);
			const selectedCourses = this.courses.filter(course => selectedSet.has(course.id));
			this.$emit("input", selectedCourses.map(course => course.id));
			this.$emit("change", selectedCourses);
		},
	},
};
</script>

<style lang="scss" scoped>
.course-select {
	width: 100%;
}

.course-row-header {
	display: flex;
	align-items: center;
	justify-content: space-between;
	min-height: 22px;

	em {
		color: #ee0a24;
		font-style: normal;
	}
}

.course-row-link {
	flex: 1;
	min-width: 0;
	margin-left: 14px;
	display: flex;
	align-items: center;
	justify-content: flex-end;

	.placeholder {
		min-width: 0;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
		color: #00000040;
	}

	.van-icon {
		margin-left: 4px;
		color: #00000040;
	}
}

.course-tags {
	display: flex;
	flex-wrap: wrap;
	gap: 7px;
	justify-content: flex-start;
	margin-top: 8px;
}

.course-tag {
	max-width: 100%;
	height: 22px;
	line-height: 20px;
	padding: 0 8px;
	box-sizing: border-box;
	border: 1px solid #dfe3eb;
	border-radius: 3px;
	background: #f7f8fa;
	color: #1f2d3d;
	font-size: 13px;
	display: inline-flex;
	align-items: center;
	min-width: 0;

	.van-icon {
		flex: 0 0 auto;
		margin-left: 6px;
		color: #8a93a3;
		font-size: 12px;
	}
}

.course-tag-text {
	min-width: 0;
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
}

.course-popup {
	background: #fff;
}

.course-picker {
	height: 712px;
	max-height: 84vh;
	display: flex;
	flex-direction: column;
	background: #fff;
}

.course-picker-title {
	height: 50px;
	line-height: 50px;
	text-align: center;
	font-size: 17px;
	font-weight: 600;
	color: #1f2d3d;
	border-bottom: 1px solid #edf0f5;
}

.course-list {
	flex: 1;
	min-height: 0;
	overflow-y: auto;
	padding: 10px 18px 0;
	box-sizing: border-box;
}

.course-option {
	min-height: 50px;
	display: flex;
	align-items: center;
	justify-content: space-between;
	border-bottom: 1px solid #edf0f5;
	color: #1f2d3d;
	font-size: 14px;
	line-height: 20px;

	span {
		flex: 1;
		min-width: 0;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}

	.van-icon {
		margin-left: 12px;
		color: #2f66f6;
		font-size: 18px;
	}

	&.selected {
		color: #2f66f6;
	}
}

.course-empty {
	padding: 50px 0;
	text-align: center;
	color: #969799;
	font-size: 14px;
}

.course-actions {
	display: flex;
	gap: 14px;
	padding: 14px 18px 20px;
	box-sizing: border-box;
	border-top: 1px solid #f0f0f0;

	button {
		flex: 1;
		height: 40px;
		border: 0;
		border-radius: 8px;
		font-size: 16px;
	}
}

.course-cancel {
	background: #f5f6f8;
	color: #1f2d3d;
}

.course-confirm {
	background: #2f66f6;
	color: #fff;
}
</style>