Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | 7x 7x 7x | <template> <nav :class="$style.root" :readonly="readonly" :disabled="disabled"> <slot></slot> </nav> </template> <script> import MEmitter from '../m-emitter.vue'; import { MParent } from '../m-parent.vue'; export default { name: 'm-singlex', groupName: 'm-singlex-group', childName: 'm-singlex-item', mixins: [MEmitter, MParent], props: { value: null, autoSelect: { type: Boolean, default: false }, cancelable: { type: Boolean, default: false }, router: { type: Boolean, default: false }, readonly: { type: Boolean, default: false }, disabled: { type: Boolean, default: false }, }, data() { return { // @inherit: groupVMs: [], // @inherit: itemVMs: [], selectedVM: undefined, }; }, watch: { value(value) { // 无需剪枝 this.watchValue(value); }, selectedVM(selectedVM, oldVM) { const value = selectedVM ? selectedVM.value : undefined; const oldValue = oldVM ? oldVM.value : undefined; if (value === oldValue) return; this.$emit('change', { value, oldValue, item: selectedVM ? selectedVM.item : undefined, oldItem: oldVM && oldVM.item, itemVM: selectedVM, oldVM, }, this); }, // This method just run once after pushing many itemVMs itemVMs(itemVMs) { if (!itemVMs.includes(this.selectedVM)) { if (!this.router) { // 更新列表之后,原来的选择可能已不存在,这里暂存然后重新查找一遍 const value = this.selectedVM ? this.selectedVM.value : this.value; this.selectedVM = undefined; this.watchValue(value); } else { this.selectedVM = this.itemVMs.find( (itemVM) => itemVM.active, ); } } }, }, mounted() { // Don't need trigger `value` watcher at mounted hook. // Because there's a watcher for itemVMs. // this.watchValue(this.value); this.$emit('update', this.value, this); }, methods: { watchValue(value) { if (this.selectedVM && this.selectedVM.value === value) // 下面需要走 value === undefined return; if (value === undefined) { if (this.autoSelect) this.selectedVM = this.itemVMs.find((itemVM) => !itemVM.disabled) || undefined; else this.selectedVM = undefined; } else { this.selectedVM = this.itemVMs.find( (itemVM) => itemVM.value === value || (typeof value !== 'object' && String(itemVM.value) === String(value)), ); if (!this.selectedVM && this.selectedValuesData && Array.isArray(this.selectedValuesData)) { this.selectedVM = this.selectedValuesData.find( (itemData) => itemData.value === value, ); } this.selectedVM && this.selectedVM.groupVM && this.selectedVM.groupVM.toggle(true); } }, click(itemVM) { // Check if enabled if (this.readonly || this.disabled || (itemVM && itemVM.disabled)) return; // Prevent replication // Emit a `click` event const value = this.selectedVM && this.selectedVM.value; const selectedItem = this.selectedVM && this.selectedVM.item; this.$emit('click', { value, selectedVM: this.selectedVM, selectedItem, itemVM, item: itemVM && itemVM.item, }, this); }, select(itemVM, cancelable) { // Check if enabled if (this.readonly || this.disabled || (itemVM && itemVM.disabled)) return; // Prevent replication const oldValue = this.value; const oldVM = this.selectedVM; if (cancelable === undefined) cancelable = this.cancelable; if (!cancelable && !this.router && itemVM === oldVM) return; // Emit a `before-` event with preventDefault() if (this.$emitPrevent('before-select', { value: itemVM && itemVM.value, oldValue, itemVM, item: itemVM && itemVM.item, oldVM, oldItem: oldVM && oldVM.item, }, this)) return; if (cancelable && itemVM === oldVM) this.selectedVM = undefined; else this.selectedVM = itemVM; // Assign and sync `value` const value = this.selectedVM && this.selectedVM.value; const selectedItem = this.selectedVM && this.selectedVM.item; this.$emit('input', value, this); this.$emit('update', value, this); this.$emit('update:value', value, this); // Emit `after-` events this.$emit('select', { value, oldValue, selectedVM: this.selectedVM, selectedItem, itemVM, item: itemVM && itemVM.item, oldVM, oldItem: oldVM && oldVM.item, }, this); }, }, }; </script> <style module> .root { /* @Private */ user-select: none; position: relative; } </style> |