소스 검색

Watch for value changes and update view accordingly, move this ogic into function for DRYness

sbkwgh 8 년 전
부모
커밋
0b982efeb2
1개의 변경된 파일26개의 추가작업 그리고 17개의 파일을 삭제
  1. 26 17
      src/components/SelectButton.vue

+ 26 - 17
src/components/SelectButton.vue

@@ -20,23 +20,6 @@
 	export default {
 		name: 'SelectButton',
 		props: ['options', 'value', 'name'],
-		data () {
-			var self = this;
-			var index = 0;
-
-			if(this.value !== null) {
-				this.options.forEach((option, i) => {
-					if(option.value === self.value) {
-						index = i;
-					}
-				})
-			}
-
-			return {
-				selectedIndex: index,
-				hideMenu: true
-			}
-		},
 		methods: {
 			toggleMenu () {
 				this.hideMenu = !this.hideMenu;
@@ -46,6 +29,31 @@
 				this.hideMenu = true;
 
 				this.$emit('input', this.options[index].value);
+			},
+			getIndexFromValue () {
+				var index = 0;
+				var self = this;
+
+				if(this.value !== null) {
+					this.options.forEach((option, i) => {
+						if(option.value === self.value) {
+							index = i;
+						}
+					})
+				}
+
+				return index;
+			}
+		},
+		data () {
+			return {
+				selectedIndex: this.getIndexFromValue(),
+				hideMenu: true
+			}
+		},
+		watch: {
+			value () {
+				this.selectedIndex = this.getIndexFromValue();
 			}
 		}
 	}
@@ -58,6 +66,7 @@
 
 		@at-root #{&}__options {
 			position: absolute;
+			z-index: 1;
 			overflow: hidden;
 			background-color: #fff;
 			width: 15rem;