feat(P4): precompute移植+app.js sources双向回填+回归1003/1003+验收体积/文件/data.json
This commit is contained in:
@@ -0,0 +1,207 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Aurora Admin · Select (H5)</title>
|
||||
<link rel="stylesheet" href="../.design_library/aurora-admin/colors_and_type.css">
|
||||
<style>
|
||||
/* 对齐 components.css 与 select.json 契约(触发器样式同 Input) */
|
||||
.demo { padding: 24px; background: var(--au-color-page-bg); color: var(--au-color-text-body); font-family: var(--font-family); }
|
||||
h1 { font-size: 20px; margin: 0 0 4px; }
|
||||
.sub { color: var(--au-color-text-secondary); margin: 0 0 24px; font-size: 12px; }
|
||||
.group { margin-bottom: 24px; }
|
||||
.group h2 { font-size: 16px; margin: 0 0 12px; color: var(--au-color-text-title); }
|
||||
.row { display: flex; flex-wrap: wrap; gap: 16px; align-items: flex-start; }
|
||||
|
||||
.aa-select { position: relative; display: inline-block; min-width: 220px; }
|
||||
.aa-select-trigger {
|
||||
display: flex; align-items: center; gap: 8px; min-height: 32px; height: auto;
|
||||
padding: 2px 28px 2px 12px; background: var(--au-color-card-bg); border: 1px solid var(--au-color-border);
|
||||
border-radius: 4px; box-sizing: border-box; cursor: pointer; transition: border-color .15s, box-shadow .15s;
|
||||
font-size: 14px; color: var(--au-color-text-body); flex-wrap: wrap;
|
||||
}
|
||||
.aa-select-trigger:hover { border-color: var(--au-color-brand); }
|
||||
.aa-select.is-open .aa-select-trigger { border-color: var(--au-color-brand); box-shadow: 0 0 0 2px rgba(47,84,235,0.2); }
|
||||
.aa-select.is-disabled .aa-select-trigger { background: var(--au-color-disabled-bg); border-color: transparent; color: var(--au-color-text-disabled); cursor: not-allowed; }
|
||||
.aa-select-trigger .placeholder { color: var(--au-color-text-placeholder); }
|
||||
.aa-select-trigger .caret { position: absolute; right: 10px; top: 50%; transform: translateY(-50%); transition: transform .15s; color: var(--au-color-text-secondary); pointer-events: none; }
|
||||
.aa-select.is-open .caret { transform: translateY(-50%) rotate(180deg); }
|
||||
.aa-select-trigger .tag {
|
||||
display: inline-flex; align-items: center; gap: 4px; background: var(--au-color-brand-bg); color: var(--au-color-brand);
|
||||
border-radius: 2px; padding: 1px 6px; font-size: 12px; line-height: 20px;
|
||||
}
|
||||
.aa-select-trigger .tag .x { cursor: pointer; }
|
||||
|
||||
.aa-select-panel {
|
||||
position: absolute; z-index: 10; top: calc(100% + 4px); left: 0; right: 0;
|
||||
background: var(--au-color-card-bg); border-radius: 4px; box-shadow: 0 4px 16px rgba(0,0,0,0.12);
|
||||
max-height: 256px; overflow-y: auto; padding: 4px 0;
|
||||
}
|
||||
.aa-select-search { padding: 6px 8px; border-bottom: 1px solid var(--au-color-border-strong); }
|
||||
.aa-select-search input { width: 100%; box-sizing: border-box; border: 1px solid var(--au-color-border); border-radius: 4px; padding: 4px 8px; font-size: 13px; outline: none; }
|
||||
.aa-select-search input:focus { border-color: var(--au-color-brand); }
|
||||
.aa-select-group-label { padding: 6px 12px; font-size: 12px; color: var(--au-color-text-secondary); }
|
||||
.aa-select-option {
|
||||
display: flex; align-items: center; gap: 8px; padding: 0 12px; height: 32px; cursor: pointer;
|
||||
font-size: 14px; color: var(--au-color-text-body); box-sizing: border-box;
|
||||
}
|
||||
.aa-select-option:hover { background: var(--au-color-page-bg); }
|
||||
.aa-select-option.is-selected { background: var(--au-color-brand-bg); color: var(--au-color-brand); }
|
||||
.aa-select-option .check { visibility: hidden; margin-left: auto; }
|
||||
.aa-select-option.is-selected .check { visibility: visible; }
|
||||
.aa-select-option.is-disabled { color: var(--au-color-text-disabled); cursor: not-allowed; }
|
||||
.aa-select-empty { padding: 16px 12px; text-align: center; color: var(--au-color-text-placeholder); font-size: 13px; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="demo">
|
||||
<h1>Aurora Admin · Select 组件(纯 H5)</h1>
|
||||
<p class="sub">对齐 select.json 契约:触发器同 Input,面板圆角4px / 阴影中 / 最大高度256px,选中项品牌色 + 勾选图标</p>
|
||||
|
||||
<div class="group">
|
||||
<h2>1. 单选</h2>
|
||||
<div class="row">
|
||||
<div class="aa-select" data-select data-mode="single" data-name="city">
|
||||
<div class="aa-select-trigger" role="button" aria-haspopup="listbox" aria-expanded="false" tabindex="0"><span class="placeholder">请选择城市</span><span class="caret">▾</span></div>
|
||||
<div class="aa-select-panel" hidden>
|
||||
<div class="aa-select-option" role="option" data-value="bj">北京</div>
|
||||
<div class="aa-select-option" role="option" data-value="sh">上海</div>
|
||||
<div class="aa-select-option" role="option" data-value="gz">广州</div>
|
||||
<div class="aa-select-option" role="option" data-value="sz">深圳</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="group">
|
||||
<h2>2. 多选</h2>
|
||||
<div class="row">
|
||||
<div class="aa-select" data-select data-mode="multiple" data-name="role">
|
||||
<div class="aa-select-trigger" role="button" aria-haspopup="listbox" aria-expanded="false" tabindex="0"><span class="placeholder">请选择角色</span><span class="caret">▾</span></div>
|
||||
<div class="aa-select-panel" hidden>
|
||||
<div class="aa-select-option" role="option" data-value="admin">管理员</div>
|
||||
<div class="aa-select-option" role="option" data-value="editor">编辑</div>
|
||||
<div class="aa-select-option" role="option" data-value="viewer">访客</div>
|
||||
<div class="aa-select-option is-disabled" role="option" data-value="guest" data-disabled="1">受限访客</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="group">
|
||||
<h2>3. 可搜索</h2>
|
||||
<div class="row">
|
||||
<div class="aa-select" data-select data-mode="single" data-searchable="1" data-name="user">
|
||||
<div class="aa-select-trigger" role="button" aria-haspopup="listbox" aria-expanded="false" tabindex="0"><span class="placeholder">搜索并选择用户</span><span class="caret">▾</span></div>
|
||||
<div class="aa-select-panel" hidden>
|
||||
<div class="aa-select-search"><input placeholder="输入关键字"></div>
|
||||
<div class="aa-select-option" role="option" data-value="u1">张伟</div>
|
||||
<div class="aa-select-option" role="option" data-value="u2">李娜</div>
|
||||
<div class="aa-select-option" role="option" data-value="u3">王芳</div>
|
||||
<div class="aa-select-option" role="option" data-value="u4">刘强</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="group">
|
||||
<h2>4. 分组</h2>
|
||||
<div class="row">
|
||||
<div class="aa-select" data-select data-mode="single" data-grouped="1" data-name="dept">
|
||||
<div class="aa-select-trigger" role="button" aria-haspopup="listbox" aria-expanded="false" tabindex="0"><span class="placeholder">请选择部门</span><span class="caret">▾</span></div>
|
||||
<div class="aa-select-panel" hidden>
|
||||
<div class="aa-select-group-label">技术中心</div>
|
||||
<div class="aa-select-option" role="option" data-value="fe">前端组</div>
|
||||
<div class="aa-select-option" role="option" data-value="be">后端组</div>
|
||||
<div class="aa-select-group-label">产品中心</div>
|
||||
<div class="aa-select-option" role="option" data-value="pm">产品组</div>
|
||||
<div class="aa-select-option" role="option" data-value="ux">设计组</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
document.querySelectorAll('[data-select]').forEach(root => {
|
||||
const trigger = root.querySelector('.aa-select-trigger');
|
||||
const panel = root.querySelector('.aa-select-panel');
|
||||
const mode = root.dataset.mode;
|
||||
const multiple = mode === 'multiple';
|
||||
const searchable = root.dataset.searchable === '1';
|
||||
const grouped = root.dataset.grouped === '1';
|
||||
let selected = multiple ? [] : null; // value | array of value
|
||||
|
||||
function optionLabel(opt) { return opt.textContent.trim(); }
|
||||
|
||||
function renderTrigger() {
|
||||
if (multiple) {
|
||||
if (!selected.length) { trigger.innerHTML = '<span class="placeholder">请选择角色</span><span class="caret">▾</span>'; return; }
|
||||
const tags = selected.map(v => {
|
||||
const txt = root.querySelector(`.aa-select-option[data-value="${v}"]`).textContent.trim();
|
||||
return `<span class="tag" data-tag="${v}">${txt}<span class="x" data-x="${v}">✕</span></span>`;
|
||||
}).join('');
|
||||
trigger.innerHTML = tags + '<span class="caret">▾</span>';
|
||||
} else {
|
||||
if (selected == null) { trigger.innerHTML = '<span class="placeholder">请选择</span><span class="caret">▾</span>'; return; }
|
||||
const txt = root.querySelector(`.aa-select-option[data-value="${selected}"]`).textContent.trim();
|
||||
trigger.innerHTML = `<span>${txt}</span><span class="caret">▾</span>`;
|
||||
}
|
||||
}
|
||||
|
||||
function syncSelectedClasses() {
|
||||
root.querySelectorAll('.aa-select-option').forEach(o => {
|
||||
const v = o.dataset.value;
|
||||
const isSel = multiple ? selected.includes(v) : selected === v;
|
||||
o.classList.toggle('is-selected', isSel);
|
||||
});
|
||||
}
|
||||
|
||||
function open() {
|
||||
root.classList.add('is-open');
|
||||
panel.hidden = false;
|
||||
if (searchable) panel.querySelector('.aa-select-search input').focus();
|
||||
}
|
||||
function close() { root.classList.remove('is-open'); panel.hidden = true; }
|
||||
|
||||
trigger.addEventListener('click', (e) => {
|
||||
if (root.classList.contains('is-disabled')) return;
|
||||
if (e.target.dataset.x) {
|
||||
const v = e.target.dataset.x;
|
||||
selected = selected.filter(x => x !== v);
|
||||
syncSelectedClasses(); renderTrigger(); return;
|
||||
}
|
||||
root.classList.contains('is-open') ? close() : open();
|
||||
});
|
||||
|
||||
panel.addEventListener('click', (e) => {
|
||||
const opt = e.target.closest('.aa-select-option');
|
||||
if (!opt || opt.classList.contains('is-disabled')) return;
|
||||
const v = opt.dataset.value;
|
||||
if (multiple) {
|
||||
selected.includes(v) ? selected = selected.filter(x => x !== v) : selected.push(v);
|
||||
syncSelectedClasses(); renderTrigger();
|
||||
} else {
|
||||
selected = v; syncSelectedClasses(); renderTrigger(); close();
|
||||
}
|
||||
});
|
||||
|
||||
if (searchable) {
|
||||
panel.querySelector('.aa-select-search input').addEventListener('input', (e) => {
|
||||
const q = e.target.value.trim().toLowerCase();
|
||||
root.querySelectorAll('.aa-select-option').forEach(o => {
|
||||
o.style.display = o.textContent.toLowerCase().includes(q) ? '' : 'none';
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
document.addEventListener('click', (e) => {
|
||||
if (!root.contains(e.target)) close();
|
||||
});
|
||||
|
||||
renderTrigger();
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,177 @@
|
||||
import React, { useState, useRef, useEffect, useMemo } from 'react';
|
||||
import './Select.css';
|
||||
|
||||
/**
|
||||
* Aurora Admin Select(React)
|
||||
* 对齐 select.json 契约:
|
||||
* - mode: single | multiple(multiple 属性)
|
||||
* - feature: searchable | grouped(groups 属性)
|
||||
* - state: default | open | disabled
|
||||
* 触发器样式同 Input;面板圆角4px / 阴影中 / 最大高度256px;选中项品牌色 + 勾选图标
|
||||
* 受控:<Select value={v} onChange={setV} options={...} />
|
||||
* 非受控:<Select defaultValue="bj" options={...} />
|
||||
*/
|
||||
function flatten(groups) {
|
||||
return (groups || []).flatMap((g) => g.options);
|
||||
}
|
||||
|
||||
export default function Select({
|
||||
value,
|
||||
defaultValue = null,
|
||||
options = [],
|
||||
groups = null,
|
||||
multiple = false,
|
||||
searchable = false,
|
||||
disabled = false,
|
||||
placeholder = '请选择',
|
||||
className = '',
|
||||
onChange,
|
||||
...rest
|
||||
}) {
|
||||
const isControlled = value !== undefined;
|
||||
const [inner, setInner] = useState(defaultValue);
|
||||
const [open, setOpen] = useState(false);
|
||||
const [keyword, setKeyword] = useState('');
|
||||
const rootRef = useRef(null);
|
||||
|
||||
const current = isControlled ? value : inner;
|
||||
const selectedArr = Array.isArray(current) ? current : [];
|
||||
const selectedVal = Array.isArray(current) ? null : current;
|
||||
|
||||
const filteredOptions = useMemo(() => {
|
||||
if (!searchable || !keyword) return options;
|
||||
const q = keyword.trim().toLowerCase();
|
||||
return options.filter((o) => String(o.label).toLowerCase().includes(q));
|
||||
}, [options, searchable, keyword]);
|
||||
|
||||
const filteredGroups = useMemo(() => {
|
||||
if (!groups) return [];
|
||||
if (!searchable || !keyword) return groups;
|
||||
const q = keyword.trim().toLowerCase();
|
||||
return groups
|
||||
.map((g) => ({ label: g.label, options: g.options.filter((o) => String(o.label).toLowerCase().includes(q)) }))
|
||||
.filter((g) => g.options.length);
|
||||
}, [groups, searchable, keyword]);
|
||||
|
||||
const isEmpty = groups ? filteredGroups.length === 0 : filteredOptions.length === 0;
|
||||
|
||||
const flat = options.length ? options : flatten(groups);
|
||||
|
||||
function labelOf(v) {
|
||||
const found = flat.find((o) => o.value === v);
|
||||
return found ? found.label : v;
|
||||
}
|
||||
function isSelected(v) {
|
||||
return multiple ? selectedArr.includes(v) : selectedVal === v;
|
||||
}
|
||||
function emit(next) {
|
||||
if (!isControlled) setInner(next);
|
||||
onChange && onChange(next);
|
||||
}
|
||||
function choose(o) {
|
||||
if (o.disabled) return;
|
||||
if (multiple) {
|
||||
const next = selectedArr.includes(o.value)
|
||||
? selectedArr.filter((x) => x !== o.value)
|
||||
: [...selectedArr, o.value];
|
||||
emit(next);
|
||||
} else {
|
||||
emit(o.value);
|
||||
setOpen(false);
|
||||
}
|
||||
}
|
||||
function remove(v) {
|
||||
emit(selectedArr.filter((x) => x !== v));
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
function onDocClick(e) {
|
||||
if (rootRef.current && !rootRef.current.contains(e.target)) setOpen(false);
|
||||
}
|
||||
document.addEventListener('click', onDocClick);
|
||||
return () => document.removeEventListener('click', onDocClick);
|
||||
}, []);
|
||||
|
||||
const classes = ['aa-select', open ? 'is-open' : '', disabled ? 'is-disabled' : '', className]
|
||||
.filter(Boolean)
|
||||
.join(' ');
|
||||
|
||||
return (
|
||||
<div className={classes} ref={rootRef} {...rest}>
|
||||
<div className="aa-select-trigger" onClick={() => !disabled && setOpen((o) => !o)} tabIndex={0}>
|
||||
{multiple ? (
|
||||
selectedArr.length ? (
|
||||
selectedArr.map((v) => (
|
||||
<span className="tag" key={v}>
|
||||
{labelOf(v)}
|
||||
<span className="x" onClick={(e) => { e.stopPropagation(); remove(v); }}>✕</span>
|
||||
</span>
|
||||
))
|
||||
) : (
|
||||
<span className="placeholder">{placeholder}</span>
|
||||
)
|
||||
) : selectedVal == null ? (
|
||||
<span className="placeholder">{placeholder}</span>
|
||||
) : (
|
||||
<span>{labelOf(selectedVal)}</span>
|
||||
)}
|
||||
<span className="caret">▾</span>
|
||||
</div>
|
||||
|
||||
{open && (
|
||||
<div className="aa-select-panel">
|
||||
{searchable && (
|
||||
<div className="aa-select-search">
|
||||
<input
|
||||
placeholder="输入关键字"
|
||||
value={keyword}
|
||||
onChange={(e) => setKeyword(e.target.value)}
|
||||
onClick={(e) => e.stopPropagation()}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{groups ? (
|
||||
filteredGroups.map((g) => (
|
||||
<React.Fragment key={g.label}>
|
||||
<div className="aa-select-group-label">{g.label}</div>
|
||||
{g.options.map((o) => (
|
||||
<div
|
||||
key={o.value}
|
||||
className={`aa-select-option${isSelected(o.value) ? ' is-selected' : ''}${o.disabled ? ' is-disabled' : ''}`}
|
||||
onClick={() => choose(o)}
|
||||
>
|
||||
{o.label}<span className="check">✓</span>
|
||||
</div>
|
||||
))}
|
||||
</React.Fragment>
|
||||
))
|
||||
) : (
|
||||
filteredOptions.map((o) => (
|
||||
<div
|
||||
key={o.value}
|
||||
className={`aa-select-option${isSelected(o.value) ? ' is-selected' : ''}${o.disabled ? ' is-disabled' : ''}`}
|
||||
onClick={() => choose(o)}
|
||||
>
|
||||
{o.label}<span className="check">✓</span>
|
||||
</div>
|
||||
))
|
||||
)}
|
||||
|
||||
{isEmpty && <div className="aa-select-empty">无匹配选项</div>}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
/*
|
||||
用法示例:
|
||||
import Select from './Select';
|
||||
|
||||
const cities = [{ value: 'bj', label: '北京' }, { value: 'sh', label: '上海' }];
|
||||
<Select options={cities} placeholder="请选择城市" />
|
||||
<Select multiple options={cities} defaultValue={['bj']} />
|
||||
<Select searchable options={cities} />
|
||||
<Select groups={[{ label: '技术中心', options: cities }]} />
|
||||
*/
|
||||
@@ -0,0 +1,167 @@
|
||||
<template>
|
||||
<div class="aa-select" :class="{ 'is-open': open, 'is-disabled': disabled }" ref="root">
|
||||
<div class="aa-select-trigger" @click="toggle" tabindex="0">
|
||||
<template v-if="multiple">
|
||||
<span v-if="!selectedArr.length" class="placeholder">{{ placeholder }}</span>
|
||||
<span v-for="v in selectedArr" :key="v" class="tag">
|
||||
{{ labelOf(v) }}<span class="x" @click.stop="remove(v)">✕</span>
|
||||
</span>
|
||||
</template>
|
||||
<span v-else-if="selectedVal == null" class="placeholder">{{ placeholder }}</span>
|
||||
<span v-else>{{ labelOf(selectedVal) }}</span>
|
||||
<span class="caret">▾</span>
|
||||
</div>
|
||||
|
||||
<div class="aa-select-panel" v-show="open">
|
||||
<div v-if="searchable" class="aa-select-search">
|
||||
<input v-model="keyword" placeholder="输入关键字" @click.stop>
|
||||
</div>
|
||||
|
||||
<template v-if="groups && groups.length">
|
||||
<template v-for="g in filteredGroups">
|
||||
<div class="aa-select-group-label">{{ g.label }}</div>
|
||||
<div
|
||||
v-for="o in g.options" :key="o.value"
|
||||
class="aa-select-option"
|
||||
:class="{ 'is-selected': isSelected(o.value), 'is-disabled': o.disabled }"
|
||||
@click="choose(o)"
|
||||
>
|
||||
{{ o.label }}<span class="check">✓</span>
|
||||
</div>
|
||||
</template>
|
||||
</template>
|
||||
|
||||
<template v-else>
|
||||
<div
|
||||
v-for="o in filteredOptions" :key="o.value"
|
||||
class="aa-select-option"
|
||||
:class="{ 'is-selected': isSelected(o.value), 'is-disabled': o.disabled }"
|
||||
@click="choose(o)"
|
||||
>
|
||||
{{ o.label }}<span class="check">✓</span>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<div v-if="isEmpty" class="aa-select-empty">无匹配选项</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
name: 'AaSelect',
|
||||
props: {
|
||||
value: { type: [String, Number, Array], default: null },
|
||||
options: { type: Array, default: () => [] }, // [{ value, label, disabled? }]
|
||||
groups: { type: Array, default: null }, // [{ label, options: [...] }]
|
||||
multiple: { type: Boolean, default: false },
|
||||
searchable: { type: Boolean, default: false },
|
||||
disabled: { type: Boolean, default: false },
|
||||
placeholder: { type: String, default: '请选择' }
|
||||
},
|
||||
data() {
|
||||
return { open: false, keyword: '' };
|
||||
},
|
||||
computed: {
|
||||
selectedArr() { return Array.isArray(this.value) ? this.value : []; },
|
||||
selectedVal() { return Array.isArray(this.value) ? null : this.value; },
|
||||
filteredOptions() {
|
||||
if (!this.searchable || !this.keyword) return this.options;
|
||||
const q = this.keyword.trim().toLowerCase();
|
||||
return this.options.filter(o => String(o.label).toLowerCase().includes(q));
|
||||
},
|
||||
filteredGroups() {
|
||||
if (!this.groups) return [];
|
||||
if (!this.searchable || !this.keyword) return this.groups;
|
||||
const q = this.keyword.trim().toLowerCase();
|
||||
return this.groups
|
||||
.map(g => ({ label: g.label, options: g.options.filter(o => String(o.label).toLowerCase().includes(q)) }))
|
||||
.filter(g => g.options.length);
|
||||
},
|
||||
isEmpty() {
|
||||
return this.groups ? this.filteredGroups.length === 0 : this.filteredOptions.length === 0;
|
||||
}
|
||||
},
|
||||
watch: {
|
||||
open(v) { if (v && this.searchable) this.keyword = ''; }
|
||||
},
|
||||
methods: {
|
||||
toggle() {
|
||||
if (this.disabled) return;
|
||||
this.open = !this.open;
|
||||
},
|
||||
isSelected(v) {
|
||||
return this.multiple ? this.selectedArr.includes(v) : this.selectedVal === v;
|
||||
},
|
||||
labelOf(v) {
|
||||
const flat = this.options.length ? this.options : (this.groups || []).flatMap(g => g.options);
|
||||
const found = flat.find(o => o.value === v);
|
||||
return found ? found.label : v;
|
||||
},
|
||||
choose(o) {
|
||||
if (o.disabled) return;
|
||||
if (this.multiple) {
|
||||
const next = this.selectedArr.includes(o.value)
|
||||
? this.selectedArr.filter(x => x !== o.value)
|
||||
: [...this.selectedArr, o.value];
|
||||
this.$emit('input', next);
|
||||
this.$emit('change', next);
|
||||
} else {
|
||||
this.$emit('input', o.value);
|
||||
this.$emit('change', o.value);
|
||||
this.open = false;
|
||||
}
|
||||
},
|
||||
remove(v) {
|
||||
this.$emit('input', this.selectedArr.filter(x => x !== v));
|
||||
this.$emit('change', this.selectedArr.filter(x => x !== v));
|
||||
},
|
||||
onDocClick(e) {
|
||||
if (this.$refs.root && !this.$refs.root.contains(e.target)) this.open = false;
|
||||
}
|
||||
},
|
||||
mounted() { document.addEventListener('click', this.onDocClick); },
|
||||
beforeDestroy() { document.removeEventListener('click', this.onDocClick); }
|
||||
};
|
||||
</script>
|
||||
|
||||
<!-- 样式对齐 components.css 与 select.json 契约(触发器样式同 Input) -->
|
||||
<style>
|
||||
.aa-select { position: relative; display: inline-block; min-width: 220px; }
|
||||
.aa-select-trigger {
|
||||
display: flex; align-items: center; gap: 8px; min-height: 32px; height: auto;
|
||||
padding: 2px 28px 2px 12px; background: #fff; border: 1px solid #E8ECF1;
|
||||
border-radius: 4px; box-sizing: border-box; cursor: pointer; transition: border-color .15s, box-shadow .15s;
|
||||
font-size: 14px; color: #262626; flex-wrap: wrap; position: relative;
|
||||
}
|
||||
.aa-select-trigger:hover { border-color: #2F54EB; }
|
||||
.aa-select.is-open .aa-select-trigger { border-color: #2F54EB; box-shadow: 0 0 0 2px rgba(47,84,235,0.2); }
|
||||
.aa-select.is-disabled .aa-select-trigger { background: #F5F5F5; border-color: transparent; color: #BFBFBF; cursor: not-allowed; }
|
||||
.aa-select-trigger .placeholder { color: #767676; }
|
||||
.aa-select-trigger .caret { position: absolute; right: 10px; top: 50%; transform: translateY(-50%); transition: transform .15s; color: #6E6E6E; }
|
||||
.aa-select.is-open .caret { transform: translateY(-50%) rotate(180deg); }
|
||||
.aa-select-trigger .tag {
|
||||
display: inline-flex; align-items: center; gap: 4px; background: #F0F5FF; color: #2F54EB;
|
||||
border-radius: 2px; padding: 1px 6px; font-size: 12px; line-height: 20px;
|
||||
}
|
||||
.aa-select-trigger .tag .x { cursor: pointer; }
|
||||
.aa-select-panel {
|
||||
position: absolute; z-index: 10; top: calc(100% + 4px); left: 0; right: 0;
|
||||
background: #fff; border-radius: 4px; box-shadow: 0 4px 16px rgba(0,0,0,0.12);
|
||||
max-height: 256px; overflow-y: auto; padding: 4px 0;
|
||||
}
|
||||
.aa-select-search { padding: 6px 8px; border-bottom: 1px solid #F0F0F0; }
|
||||
.aa-select-search input { width: 100%; box-sizing: border-box; border: 1px solid #E8ECF1; border-radius: 4px; padding: 4px 8px; font-size: 13px; outline: none; }
|
||||
.aa-select-search input:focus { border-color: #2F54EB; }
|
||||
.aa-select-group-label { padding: 6px 12px; font-size: 12px; color: #6E6E6E; }
|
||||
.aa-select-option {
|
||||
display: flex; align-items: center; gap: 8px; padding: 0 12px; height: 32px; cursor: pointer;
|
||||
font-size: 14px; color: #262626; box-sizing: border-box;
|
||||
}
|
||||
.aa-select-option:hover { background: #F5F7FA; }
|
||||
.aa-select-option.is-selected { background: #F0F5FF; color: #2F54EB; }
|
||||
.aa-select-option .check { visibility: hidden; margin-left: auto; }
|
||||
.aa-select-option.is-selected .check { visibility: visible; }
|
||||
.aa-select-option.is-disabled { color: #BFBFBF; cursor: not-allowed; }
|
||||
.aa-select-empty { padding: 16px 12px; text-align: center; color: #767676; font-size: 13px; }
|
||||
</style>
|
||||
@@ -0,0 +1,168 @@
|
||||
<template>
|
||||
<div class="aa-select" :class="{ 'is-open': open, 'is-disabled': disabled }" ref="root">
|
||||
<div class="aa-select-trigger" @click="toggle" tabindex="0">
|
||||
<template v-if="multiple">
|
||||
<span v-if="!selectedArr.length" class="placeholder">{{ placeholder }}</span>
|
||||
<span v-for="v in selectedArr" :key="v" class="tag">
|
||||
{{ labelOf(v) }}<span class="x" @click.stop="remove(v)">✕</span>
|
||||
</span>
|
||||
</template>
|
||||
<span v-else-if="selectedVal == null" class="placeholder">{{ placeholder }}</span>
|
||||
<span v-else>{{ labelOf(selectedVal) }}</span>
|
||||
<span class="caret">▾</span>
|
||||
</div>
|
||||
|
||||
<div class="aa-select-panel" v-show="open">
|
||||
<div v-if="searchable" class="aa-select-search">
|
||||
<input v-model="keyword" placeholder="输入关键字" @click.stop>
|
||||
</div>
|
||||
|
||||
<template v-if="groups && groups.length">
|
||||
<template v-for="g in filteredGroups" :key="g.label">
|
||||
<div class="aa-select-group-label">{{ g.label }}</div>
|
||||
<div
|
||||
v-for="o in g.options" :key="o.value"
|
||||
class="aa-select-option"
|
||||
:class="{ 'is-selected': isSelected(o.value), 'is-disabled': o.disabled }"
|
||||
@click="choose(o)"
|
||||
>
|
||||
{{ o.label }}<span class="check">✓</span>
|
||||
</div>
|
||||
</template>
|
||||
</template>
|
||||
|
||||
<template v-else>
|
||||
<div
|
||||
v-for="o in filteredOptions" :key="o.value"
|
||||
class="aa-select-option"
|
||||
:class="{ 'is-selected': isSelected(o.value), 'is-disabled': o.disabled }"
|
||||
@click="choose(o)"
|
||||
>
|
||||
{{ o.label }}<span class="check">✓</span>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<div v-if="isEmpty" class="aa-select-empty">无匹配选项</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
import { computed, ref, watch, onMounted, onBeforeUnmount } from 'vue';
|
||||
|
||||
const props = defineProps({
|
||||
modelValue: { type: [String, Number, Array], default: null },
|
||||
options: { type: Array, default: () => [] }, // [{ value, label, disabled? }]
|
||||
groups: { type: Array, default: null }, // [{ label, options: [...] }]
|
||||
multiple: { type: Boolean, default: false },
|
||||
searchable: { type: Boolean, default: false },
|
||||
disabled: { type: Boolean, default: false },
|
||||
placeholder: { type: String, default: '请选择' }
|
||||
});
|
||||
|
||||
const emit = defineEmits(['update:modelValue', 'change']);
|
||||
|
||||
const open = ref(false);
|
||||
const keyword = ref('');
|
||||
const root = ref(null);
|
||||
|
||||
const selectedArr = computed(() => Array.isArray(props.modelValue) ? props.modelValue : []);
|
||||
const selectedVal = computed(() => Array.isArray(props.modelValue) ? null : props.modelValue);
|
||||
|
||||
const filteredOptions = computed(() => {
|
||||
if (!props.searchable || !props.keyword) return props.options;
|
||||
const q = props.keyword.trim().toLowerCase();
|
||||
return props.options.filter(o => String(o.label).toLowerCase().includes(q));
|
||||
});
|
||||
const filteredGroups = computed(() => {
|
||||
if (!props.groups) return [];
|
||||
if (!props.searchable || !props.keyword) return props.groups;
|
||||
const q = props.keyword.trim().toLowerCase();
|
||||
return props.groups
|
||||
.map(g => ({ label: g.label, options: g.options.filter(o => String(o.label).toLowerCase().includes(q)) }))
|
||||
.filter(g => g.options.length);
|
||||
});
|
||||
const isEmpty = computed(() =>
|
||||
props.groups ? filteredGroups.value.length === 0 : filteredOptions.value.length === 0
|
||||
);
|
||||
|
||||
watch(open, (v) => { if (v && props.searchable) keyword.value = ''; });
|
||||
|
||||
function toggle() {
|
||||
if (props.disabled) return;
|
||||
open.value = !open.value;
|
||||
}
|
||||
function isSelected(v) {
|
||||
return props.multiple ? selectedArr.value.includes(v) : selectedVal.value === v;
|
||||
}
|
||||
function labelOf(v) {
|
||||
const flat = props.options.length ? props.options : (props.groups || []).flatMap(g => g.options);
|
||||
const found = flat.find(o => o.value === v);
|
||||
return found ? found.label : v;
|
||||
}
|
||||
function choose(o) {
|
||||
if (o.disabled) return;
|
||||
if (props.multiple) {
|
||||
const next = selectedArr.value.includes(o.value)
|
||||
? selectedArr.value.filter(x => x !== o.value)
|
||||
: [...selectedArr.value, o.value];
|
||||
emit('update:modelValue', next);
|
||||
emit('change', next);
|
||||
} else {
|
||||
emit('update:modelValue', o.value);
|
||||
emit('change', o.value);
|
||||
open.value = false;
|
||||
}
|
||||
}
|
||||
function remove(v) {
|
||||
const next = selectedArr.value.filter(x => x !== v);
|
||||
emit('update:modelValue', next);
|
||||
emit('change', next);
|
||||
}
|
||||
function onDocClick(e) {
|
||||
if (root.value && !root.value.contains(e.target)) open.value = false;
|
||||
}
|
||||
onMounted(() => document.addEventListener('click', onDocClick));
|
||||
onBeforeUnmount(() => document.removeEventListener('click', onDocClick));
|
||||
</script>
|
||||
|
||||
<!-- 样式对齐 components.css 与 select.json 契约(触发器样式同 Input) -->
|
||||
<style scoped>
|
||||
.aa-select { position: relative; display: inline-block; min-width: 220px; }
|
||||
.aa-select-trigger {
|
||||
display: flex; align-items: center; gap: 8px; min-height: 32px; height: auto;
|
||||
padding: 2px 28px 2px 12px; background: #fff; border: 1px solid #E8ECF1;
|
||||
border-radius: 4px; box-sizing: border-box; cursor: pointer; transition: border-color .15s, box-shadow .15s;
|
||||
font-size: 14px; color: #262626; flex-wrap: wrap; position: relative;
|
||||
}
|
||||
.aa-select-trigger:hover { border-color: #2F54EB; }
|
||||
.aa-select.is-open .aa-select-trigger { border-color: #2F54EB; box-shadow: 0 0 0 2px rgba(47,84,235,0.2); }
|
||||
.aa-select.is-disabled .aa-select-trigger { background: #F5F5F5; border-color: transparent; color: #BFBFBF; cursor: not-allowed; }
|
||||
.aa-select-trigger .placeholder { color: #767676; }
|
||||
.aa-select-trigger .caret { position: absolute; right: 10px; top: 50%; transform: translateY(-50%); transition: transform .15s; color: #6E6E6E; }
|
||||
.aa-select.is-open .caret { transform: translateY(-50%) rotate(180deg); }
|
||||
.aa-select-trigger .tag {
|
||||
display: inline-flex; align-items: center; gap: 4px; background: #F0F5FF; color: #2F54EB;
|
||||
border-radius: 2px; padding: 1px 6px; font-size: 12px; line-height: 20px;
|
||||
}
|
||||
.aa-select-trigger .tag .x { cursor: pointer; }
|
||||
.aa-select-panel {
|
||||
position: absolute; z-index: 10; top: calc(100% + 4px); left: 0; right: 0;
|
||||
background: #fff; border-radius: 4px; box-shadow: 0 4px 16px rgba(0,0,0,0.12);
|
||||
max-height: 256px; overflow-y: auto; padding: 4px 0;
|
||||
}
|
||||
.aa-select-search { padding: 6px 8px; border-bottom: 1px solid #F0F0F0; }
|
||||
.aa-select-search input { width: 100%; box-sizing: border-box; border: 1px solid #E8ECF1; border-radius: 4px; padding: 4px 8px; font-size: 13px; outline: none; }
|
||||
.aa-select-search input:focus { border-color: #2F54EB; }
|
||||
.aa-select-group-label { padding: 6px 12px; font-size: 12px; color: #6E6E6E; }
|
||||
.aa-select-option {
|
||||
display: flex; align-items: center; gap: 8px; padding: 0 12px; height: 32px; cursor: pointer;
|
||||
font-size: 14px; color: #262626; box-sizing: border-box;
|
||||
}
|
||||
.aa-select-option:hover { background: #F5F7FA; }
|
||||
.aa-select-option.is-selected { background: #F0F5FF; color: #2F54EB; }
|
||||
.aa-select-option .check { visibility: hidden; margin-left: auto; }
|
||||
.aa-select-option.is-selected .check { visibility: visible; }
|
||||
.aa-select-option.is-disabled { color: #BFBFBF; cursor: not-allowed; }
|
||||
.aa-select-empty { padding: 16px 12px; text-align: center; color: #767676; font-size: 13px; }
|
||||
</style>
|
||||
Reference in New Issue
Block a user