MCPcopy Create free account
hub / github.com/Xu22Web/tech-study-js / Select

Function Select

tech-study.js:4581–4682  ·  view source on GitHub ↗
({ data, maxlength, placeholder = '', onchange, onblur, value, keep, })

Source from the content-addressed store, hash-verified

4579 ]);
4580}
4581function Select({ data, maxlength, placeholder = '', onchange, onblur, value, keep, }) {
4582 const selectData = reactive(data.map((v) => ({ selected: false, active: false, ele: undefined, ...v })));
4583 const focus = ref(false);
4584 const input = shallowRef(undefined);
4585 const list = shallowRef(undefined);
4586 const valueRef = ref('');
4587 value &&
4588 watch(value, () => {
4589 const item = selectData.find((v) => v.value === value.value);
4590 valueRef.value = item ? item.label : '';
4591 if (!item) {
4592 selectData.forEach((v) => (v.selected = false));
4593 list.value && (list.value.scrollTop = 0);
4594 }
4595 }, true);
4596 return createElementNode('div', undefined, {
4597 class: 'egg_select',
4598 }, [
4599 createElementNode('input', { value: valueRef }, {
4600 class: 'egg_select_input',
4601 type: 'text',
4602 placeholder,
4603 maxlength,
4604 ref: input,
4605 onfocus() {
4606 if (list.value && input.value) {
4607 focus.value = true;
4608 if (input.value.value && valueRef.value) {
4609 const index = selectData.findIndex((v) => v.label === valueRef.value);
4610 if (index + 1) {
4611 list.value.scrollTop = selectData[index].ele?.offsetTop || 0;
4612 selectData.forEach((v, i) => (v.selected = i === index));
4613 }
4614 return;
4615 }
4616 }
4617 },
4618 oninput() {
4619 if (list.value && input.value) {
4620 const { value } = input.value;
4621 // 文本存在
4622 if (value) {
4623 const index = selectData.findIndex((v) => v.label.includes(value));
4624 // 存在匹配
4625 if (index + 1) {
4626 list.value.scrollTop = selectData[index].ele?.offsetTop || 0;
4627 selectData.forEach((v, i) => {
4628 v.active = i === index;
4629 v.active &&
4630 setTimeout(() => {
4631 v.active = false;
4632 }, 300);
4633 });
4634 }
4635 return;
4636 }
4637 // 清除
4638 selectData.forEach((v) => (v.active = v.selected = false));

Callers 2

TimeInputFunction · 0.70
SettingsPanelFunction · 0.70

Calls 10

reactiveFunction · 0.70
refFunction · 0.70
shallowRefFunction · 0.70
watchFunction · 0.70
createElementNodeFunction · 0.70
watchEffectRefFunction · 0.70
watchRefFunction · 0.70
debounceFunction · 0.70
onchangeFunction · 0.70
createTextNodeFunction · 0.70

Tested by

no test coverage detected