selectList 100%

This commit is contained in:
fofolee 2022-04-12 00:30:53 +08:00
parent 596afcfed3
commit 70c23e63c8
3 changed files with 37 additions and 18 deletions

View File

@ -31,9 +31,12 @@
}" }"
> >
<q-item-section v-if="isText">{{ item }}</q-item-section> <q-item-section v-if="isText">{{ item }}</q-item-section>
<q-item-section v-else-if="isJson"> <q-item-section
<q-avatar v-if="item.icon"> v-else-if="isJson"
<q-icon :name="item.icon" /> class="content-start q-gutter-md"
>
<q-avatar size="40px" v-if="item.icon">
<q-img :src="item.icon" />
</q-avatar> </q-avatar>
<q-item-label>{{ item.title }}</q-item-label> <q-item-label>{{ item.title }}</q-item-label>
<q-item-label caption>{{ item.description }}</q-item-label> <q-item-label caption>{{ item.description }}</q-item-label>
@ -45,6 +48,7 @@
</template> </template>
</q-virtual-scroll> </q-virtual-scroll>
<q-btn <q-btn
v-if="options.showCancelButton"
class="absolute-bottom-right q-ma-xs" class="absolute-bottom-right q-ma-xs"
round round
color="primary" color="primary"
@ -59,6 +63,7 @@
export default { export default {
data() { data() {
return { return {
items: this.initItems,
listMaxHeight: 500, listMaxHeight: 500,
currentIndex: 0, currentIndex: 0,
itemHeight: 50, itemHeight: 50,
@ -70,7 +75,7 @@ export default {
}, },
mounted() { mounted() {
window.SelectList = this; window.SelectList = this;
this.setSubInput(); this.options.enableSearch && this.setSubInput();
this.setUtoolsHeight(this.itemHeight * this.matchedItemsSize); this.setUtoolsHeight(this.itemHeight * this.matchedItemsSize);
}, },
computed: { computed: {
@ -98,8 +103,7 @@ export default {
}, },
props: { props: {
options: Object, options: Object,
items: Array, initItems: Array,
pinyinMatch: Function,
}, },
emits: ["ok", "hide"], emits: ["ok", "hide"],
methods: { methods: {
@ -125,7 +129,7 @@ export default {
text: this.matchedItems[this.currentIndex], text: this.matchedItems[this.currentIndex],
}; };
this.$emit("ok", selected); this.$emit("ok", selected);
this.hide(); this.options.closeOnSelect && this.hide();
}, },
onCancelClick() { onCancelClick() {

View File

@ -20,7 +20,7 @@ const quickcommand = {
hints: [], hints: [],
title: title title: title
} }
if (!(options instanceof Object)) return reject(new TypeError("必须为数组或对象")) if (!(options instanceof Object)) return reject(new TypeError(`应为 Object, 而非 ${typeof options}`))
if (options instanceof Array) props.labels = options if (options instanceof Array) props.labels = options
else Object.assign(props, options) else Object.assign(props, options)
Dialog.create({ Dialog.create({
@ -34,7 +34,7 @@ const quickcommand = {
}), }),
showButtonBox: (labels = ["确定"], title = "") => new Promise((reslove, reject) => { showButtonBox: (labels = ["确定"], title = "") => new Promise((reslove, reject) => {
if (!(labels instanceof Array)) return reject(new TypeError("必须为数组")) if (!(labels instanceof Array)) return reject(new TypeError(`应为 Array, 而非 ${typeof labels}`))
let props = { let props = {
labels: labels, labels: labels,
title: title title: title
@ -90,11 +90,18 @@ const quickcommand = {
}), }),
showSelectList: (selects, options = {}) => new Promise((reslove, reject) => { showSelectList: (selects, options = {}) => new Promise((reslove, reject) => {
if (!options.placeholder) options.placeholder = "输入进行筛选,支持拼音" if (!(selects instanceof Array)) return reject(new TypeError(`应为 Array, 而非 ${typeof selects}`))
if (!options.optionType) options.optionType = "plaintext" let defaultOptions = {
placeholder: "输入进行筛选,支持拼音",
optionType: "plaintext",
enableSearch: true,
showCancelButton: false,
closeOnSelect: true
}
Object.assign(defaultOptions, options)
let props = { let props = {
items: selects, initItems: selects,
options: options options: defaultOptions
} }
Dialog.create({ Dialog.create({
component: SelectList, component: SelectList,
@ -107,7 +114,7 @@ const quickcommand = {
}), }),
// 更新选项列表 // 更新选项列表
updateSelectList: (opt, id) => { updateSelectList: () => {
}, },
} }

View File

@ -58,7 +58,7 @@ interface quickcommandApi {
* var opt = [] * var opt = []
* for (var i = 0; i < 15; i++) { * for (var i = 0; i < 15; i++) {
* // 每一个选项为 json 格式 * // 每一个选项为 json 格式
* opt.push({title: `选项${i}`, description: `选项${i}的描述`, icon: `选项${i}的图标`,abcd: `选项${i}的自定义属性`}) * opt.push({title: `选项${i}`, description: `选项${i}的描述`, icon: `http://www.u.tools/favicon.ico`,abcd: `选项${i}的自定义属性`})
* } * }
* quickcommand.showSelectList(opt, {optionType: 'json'}).then(choise => { * quickcommand.showSelectList(opt, {optionType: 'json'}).then(choise => {
* console.log(`选择的选项为${choise.title}`) * console.log(`选择的选项为${choise.title}`)
@ -76,11 +76,19 @@ interface quickcommandApi {
* ``` * ```
* *
* @param selects * @param selects
* @param options placeholder: 搜索框占位符,optionType: 选项的格式plaintext * @param options placeholder: 搜索框占位符optionType: 选项的格式plaintext
* enableSearch trueshowCancelButton falsecloseOnSelect
* true
*/ */
showSelectList( showSelectList(
selects: array, selects: array,
options?: { placeholder: string; optionType: "plaintext" | "html" | "json" } options?: {
placeholder: string;
optionType: "plaintext" | "html" | "json";
enableSearch: boolean;
showCancelButton: boolean;
closeOnSelect: boolean;
}
): Promise<{ id: number; text: string | object }>; ): Promise<{ id: number; text: string | object }>;
/** /**