feat(P4): precompute移植+app.js sources双向回填+回归1003/1003+验收体积/文件/data.json
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
<!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 · AutoComplete</title>
|
||||
<link rel="stylesheet" href="../.design_library/aurora-admin/colors_and_type.css" />
|
||||
<link rel="stylesheet" href="./AutoComplete.css" />
|
||||
</head>
|
||||
<body>
|
||||
<div class="aa-page">
|
||||
<h2 class="aa-h2">AutoComplete 自动完成</h2>
|
||||
<p class="aa-desc">输入时匹配建议,高亮关键字,支持键盘上下选择与回车。</p>
|
||||
|
||||
<div class="aa-demo">
|
||||
<div class="aa-autocomplete" id="ac">
|
||||
<input class="aa-autocomplete-input" id="input" placeholder="输入城市,如「北」" />
|
||||
<div class="aa-autocomplete-pop" id="pop" style="display:none"></div>
|
||||
</div>
|
||||
<p class="aa-code-tip" id="out">试试输入:北京、上海、广州…</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
var data = ['北京', '上海', '广州', '深圳', '杭州', '成都', '重庆', '武汉', '西安', '南京', '苏州', '天津'];
|
||||
var root = document.getElementById('ac');
|
||||
var input = document.getElementById('input');
|
||||
var pop = document.getElementById('pop');
|
||||
var out = document.getElementById('out');
|
||||
var active = -1, list = [];
|
||||
|
||||
function highlight(text, q) {
|
||||
if (!q) return text;
|
||||
return text.replace(new RegExp('(' + q.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') + ')', 'g'), '<mark>$1</mark>');
|
||||
}
|
||||
function render() {
|
||||
if (!list.length) { pop.style.display = 'none'; return; }
|
||||
pop.style.display = 'block';
|
||||
pop.innerHTML = list.map(function (it, i) {
|
||||
return '<div class="aa-autocomplete-item' + (i === active ? ' is-active' : '') + '" role="option" data-i="' + i + '">' + highlight(it, input.value.trim()) + '</div>';
|
||||
}).join('');
|
||||
}
|
||||
input.addEventListener('input', function () {
|
||||
var q = input.value.trim();
|
||||
active = -1;
|
||||
list = q ? data.filter(function (d) { return d.indexOf(q) >= 0; }) : [];
|
||||
render();
|
||||
});
|
||||
input.addEventListener('keydown', function (e) {
|
||||
if (pop.style.display === 'none') return;
|
||||
if (e.key === 'ArrowDown') { e.preventDefault(); active = Math.min(list.length - 1, active + 1); render(); }
|
||||
else if (e.key === 'ArrowUp') { e.preventDefault(); active = Math.max(0, active - 1); render(); }
|
||||
else if (e.key === 'Enter' && active >= 0) { choose(active); }
|
||||
else if (e.key === 'Escape') { pop.style.display = 'none'; }
|
||||
});
|
||||
function choose(i) { input.value = list[i]; out.textContent = '已选择:' + list[i]; pop.style.display = 'none'; }
|
||||
pop.addEventListener('click', function (e) {
|
||||
var it = e.target.closest('.aa-autocomplete-item');
|
||||
if (it) choose(parseInt(it.dataset.i, 10));
|
||||
});
|
||||
document.addEventListener('click', function (e) { if (!root.contains(e.target)) pop.style.display = 'none'; });
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,64 @@
|
||||
import React, { useState, useRef, useEffect } from 'react';
|
||||
import './AutoComplete.css';
|
||||
|
||||
const OPTIONS = ['北京', '上海', '广州', '深圳', '杭州', '成都', '重庆', '武汉', '西安', '南京', '苏州', '天津'];
|
||||
|
||||
export default function AutoComplete({ modelValue = '', options = OPTIONS, placeholder = '输入关键字…', onChange, onSelect }) {
|
||||
const [open, setOpen] = useState(false);
|
||||
const [active, setActive] = useState(-1);
|
||||
const [list, setList] = useState([]);
|
||||
const root = useRef(null);
|
||||
|
||||
useEffect(() => {
|
||||
const onDoc = (e) => { if (root.current && !root.current.contains(e.target)) setOpen(false); };
|
||||
document.addEventListener('click', onDoc);
|
||||
return () => document.removeEventListener('click', onDoc);
|
||||
}, []);
|
||||
|
||||
const onInput = (e) => {
|
||||
const q = e.target.value;
|
||||
if (onChange) onChange(q);
|
||||
setActive(-1);
|
||||
setList(q ? options.filter((d) => d.indexOf(q) >= 0) : []);
|
||||
setOpen(true);
|
||||
};
|
||||
const highlight = (it) => {
|
||||
const q = modelValue.trim();
|
||||
if (!q) return it;
|
||||
return it.replace(new RegExp('(' + q.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') + ')', 'g'), '<mark>$1</mark>');
|
||||
};
|
||||
const move = (d) => { setOpen(true); setActive((a) => Math.max(-1, Math.min(list.length - 1, a + d))); };
|
||||
const choose = (i) => { if (onChange) onChange(list[i]); if (onSelect) onSelect(list[i]); setOpen(false); };
|
||||
|
||||
return (
|
||||
<div className="aa-autocomplete" ref={root}>
|
||||
<input
|
||||
className="aa-autocomplete-input"
|
||||
value={modelValue}
|
||||
placeholder={placeholder}
|
||||
onChange={onInput}
|
||||
onKeyDown={(e) => {
|
||||
if (e.key === 'ArrowDown') { e.preventDefault(); move(1); }
|
||||
else if (e.key === 'ArrowUp') { e.preventDefault(); move(-1); }
|
||||
else if (e.key === 'Enter' && active >= 0) choose(active);
|
||||
else if (e.key === 'Escape') setOpen(false);
|
||||
}}
|
||||
/>
|
||||
{open && list.length > 0 && (
|
||||
<div className="aa-autocomplete-pop">
|
||||
{list.map((it, i) => (
|
||||
<div
|
||||
key={i}
|
||||
className={'aa-autocomplete-item' + (i === active ? ' is-active' : '')}
|
||||
onClick={() => choose(i)}
|
||||
dangerouslySetInnerHTML={{ __html: highlight(it) }}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
{open && modelValue && list.length === 0 && (
|
||||
<div className="aa-autocomplete-pop"><div className="aa-autocomplete-empty">无匹配结果</div></div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
<template>
|
||||
<div class="aa-autocomplete" v-click-outside="close">
|
||||
<input
|
||||
class="aa-autocomplete-input"
|
||||
:value="value"
|
||||
:placeholder="placeholder"
|
||||
@input="onInput"
|
||||
@keydown.down.prevent="move(1)"
|
||||
@keydown.up.prevent="move(-1)"
|
||||
@keydown.enter="enter"
|
||||
@keydown.esc="close"
|
||||
/>
|
||||
<div v-show="open && list.length" class="aa-autocomplete-pop">
|
||||
<div
|
||||
v-for="(it, i) in list"
|
||||
:key="i"
|
||||
class="aa-autocomplete-item"
|
||||
:class="{ 'is-active': i === active }"
|
||||
@click="choose(i)"
|
||||
v-html="highlight(it)"
|
||||
></div>
|
||||
</div>
|
||||
<div v-show="open && value && !list.length" class="aa-autocomplete-pop"><div class="aa-autocomplete-empty">无匹配结果</div></div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
name: 'AaAutoComplete',
|
||||
directives: {
|
||||
clickOutside: {
|
||||
bind(el, b) { el.__d = (e) => { if (!el.contains(e.target)) b.value(); }; document.addEventListener('click', el.__d); },
|
||||
unbind(el) { document.removeEventListener('click', el.__d); }
|
||||
}
|
||||
},
|
||||
props: {
|
||||
value: { type: String, default: '' },
|
||||
options: { type: Array, default: () => ['北京', '上海', '广州', '深圳', '杭州', '成都', '重庆', '武汉', '西安', '南京', '苏州', '天津'] },
|
||||
placeholder: { type: String, default: '输入关键字…' }
|
||||
},
|
||||
data() {
|
||||
return { open: false, active: -1, list: [] };
|
||||
},
|
||||
methods: {
|
||||
onInput(e) {
|
||||
var q = e.target.value;
|
||||
this.$emit('input', q);
|
||||
this.active = -1;
|
||||
this.list = q ? this.options.filter(function (d) { return d.indexOf(q) >= 0; }) : [];
|
||||
this.open = true;
|
||||
},
|
||||
highlight(it) {
|
||||
var q = this.value.trim();
|
||||
if (!q) return it;
|
||||
return it.replace(new RegExp('(' + q.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') + ')', 'g'), '<mark>$1</mark>');
|
||||
},
|
||||
move(d) { this.open = true; this.active = Math.max(-1, Math.min(this.list.length - 1, this.active + d)); },
|
||||
enter() { if (this.active >= 0) this.choose(this.active); },
|
||||
choose(i) { this.$emit('input', this.list[i]); this.$emit('select', this.list[i]); this.close(); },
|
||||
close() { this.open = false; }
|
||||
}
|
||||
};
|
||||
</script>
|
||||
|
||||
<style src="./AutoComplete.css"></style>
|
||||
@@ -0,0 +1,62 @@
|
||||
<template>
|
||||
<div class="aa-autocomplete" ref="root">
|
||||
<input
|
||||
class="aa-autocomplete-input"
|
||||
:value="modelValue"
|
||||
:placeholder="placeholder"
|
||||
@input="onInput"
|
||||
@keydown.down.prevent="move(1)"
|
||||
@keydown.up.prevent="move(-1)"
|
||||
@keydown.enter="enter"
|
||||
@keydown.esc="close"
|
||||
/>
|
||||
<div v-show="open && list.length" class="aa-autocomplete-pop">
|
||||
<div
|
||||
v-for="(it, i) in list"
|
||||
:key="i"
|
||||
class="aa-autocomplete-item"
|
||||
:class="{ 'is-active': i === active }"
|
||||
@click="choose(i)"
|
||||
v-html="highlight(it)"
|
||||
></div>
|
||||
</div>
|
||||
<div v-show="open && modelValue && !list.length" class="aa-autocomplete-pop"><div class="aa-autocomplete-empty">无匹配结果</div></div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
import { ref, onMounted, onBeforeUnmount } from 'vue';
|
||||
|
||||
const props = defineProps({
|
||||
modelValue: { type: String, default: '' },
|
||||
options: { type: Array, default: () => ['北京', '上海', '广州', '深圳', '杭州', '成都', '重庆', '武汉', '西安', '南京', '苏州', '天津'] },
|
||||
placeholder: { type: String, default: '输入关键字…' }
|
||||
});
|
||||
const emit = defineEmits(['update:modelValue', 'select']);
|
||||
const root = ref(null);
|
||||
const open = ref(false);
|
||||
const active = ref(-1);
|
||||
const list = ref([]);
|
||||
|
||||
function onInput(e) {
|
||||
const q = e.target.value;
|
||||
emit('update:modelValue', q);
|
||||
active.value = -1;
|
||||
list.value = q ? props.options.filter((d) => d.indexOf(q) >= 0) : [];
|
||||
open.value = true;
|
||||
}
|
||||
function highlight(it) {
|
||||
const q = props.modelValue.trim();
|
||||
if (!q) return it;
|
||||
return it.replace(new RegExp('(' + q.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') + ')', 'g'), '<mark>$1</mark>');
|
||||
}
|
||||
function move(d) { open.value = true; active.value = Math.max(-1, Math.min(list.value.length - 1, active.value + d)); }
|
||||
function enter() { if (active.value >= 0) choose(active.value); }
|
||||
function choose(i) { emit('update:modelValue', list.value[i]); emit('select', list.value[i]); open.value = false; }
|
||||
function close() { open.value = false; }
|
||||
function onDoc(e) { if (root.value && !root.value.contains(e.target)) close(); }
|
||||
onMounted(() => document.addEventListener('click', onDoc));
|
||||
onBeforeUnmount(() => document.removeEventListener('click', onDoc));
|
||||
</script>
|
||||
|
||||
<style src="./AutoComplete.css"></style>
|
||||
Reference in New Issue
Block a user