Commit 2d5ba278377194ec9c40ab3b8f1de50c74be8280

Authored by 梁灏
1 parent e31ab8ad

support Switch

support Switch
@@ -7,4 +7,6 @@ value 改为了 label,使用 v-model,废弃 checked @@ -7,4 +7,6 @@ value 改为了 label,使用 v-model,废弃 checked
7 ### Checkbox 7 ### Checkbox
8 使用 v-model 8 使用 v-model
9 ### CheckboxGroup 9 ### CheckboxGroup
10 -value 改为了 label,使用 v-model,废弃 checked  
11 \ No newline at end of file 10 \ No newline at end of file
  11 +value 改为了 label,使用 v-model,废弃 checked
  12 +### Switch
  13 +废弃checked, 改为了 value,使用 v-model
12 \ No newline at end of file 14 \ No newline at end of file
@@ -23,7 +23,7 @@ @@ -23,7 +23,7 @@
23 - [x] Input 23 - [x] Input
24 - [x] Radio 24 - [x] Radio
25 - [x] Checkbox 25 - [x] Checkbox
26 -- [ ] Switch 26 +- [x] Switch
27 - [ ] Table 27 - [ ] Table
28 - [ ] Select 28 - [ ] Select
29 - [ ] Slider 29 - [ ] Slider
src/components/switch/switch.vue
1 <template> 1 <template>
2 <span :class="wrapClasses" @click="toggle"> 2 <span :class="wrapClasses" @click="toggle">
3 <span :class="innerClasses"> 3 <span :class="innerClasses">
4 - <slot name="open" v-if="checked"></slot>  
5 - <slot name="close" v-if="!checked"></slot> 4 + <slot name="open" v-if="currentValue"></slot>
  5 + <slot name="close" v-if="!currentValue"></slot>
6 </span> 6 </span>
7 </span> 7 </span>
8 </template> 8 </template>
@@ -13,7 +13,7 @@ @@ -13,7 +13,7 @@
13 13
14 export default { 14 export default {
15 props: { 15 props: {
16 - checked: { 16 + value: {
17 type: Boolean, 17 type: Boolean,
18 default: false 18 default: false
19 }, 19 },
@@ -27,12 +27,17 @@ @@ -27,12 +27,17 @@
27 } 27 }
28 } 28 }
29 }, 29 },
  30 + data () {
  31 + return {
  32 + currentValue: this.value
  33 + }
  34 + },
30 computed: { 35 computed: {
31 wrapClasses () { 36 wrapClasses () {
32 return [ 37 return [
33 `${prefixCls}`, 38 `${prefixCls}`,
34 { 39 {
35 - [`${prefixCls}-checked`]: this.checked, 40 + [`${prefixCls}-checked`]: this.currentValue,
36 [`${prefixCls}-disabled`]: this.disabled, 41 [`${prefixCls}-disabled`]: this.disabled,
37 [`${prefixCls}-${this.size}`]: !!this.size 42 [`${prefixCls}-${this.size}`]: !!this.size
38 } 43 }
@@ -48,9 +53,17 @@ @@ -48,9 +53,17 @@
48 return false; 53 return false;
49 } 54 }
50 55
51 - this.checked = !this.checked;  
52 - this.$emit('on-change', this.checked);  
53 - this.$dispatch('on-form-change', this.checked); 56 + const checked = !this.currentValue;
  57 + this.currentValue = checked;
  58 + this.$emit('input', checked);
  59 + this.$emit('on-change', checked);
  60 + // todo 事件
  61 +// this.$dispatch('on-form-change', this.checked);
  62 + }
  63 + },
  64 + watch: {
  65 + value (val) {
  66 + this.currentValue = val;
54 } 67 }
55 } 68 }
56 }; 69 };
@@ -32,7 +32,7 @@ import Radio from &#39;./components/radio&#39;; @@ -32,7 +32,7 @@ import Radio from &#39;./components/radio&#39;;
32 // import Slider from './components/slider'; 32 // import Slider from './components/slider';
33 // import Spin from './components/spin'; 33 // import Spin from './components/spin';
34 import Steps from './components/steps'; 34 import Steps from './components/steps';
35 -// import Switch from './components/switch'; 35 +import Switch from './components/switch';
36 // import Table from './components/table'; 36 // import Table from './components/table';
37 // import Tabs from './components/tabs'; 37 // import Tabs from './components/tabs';
38 // import Tag from './components/tag'; 38 // import Tag from './components/tag';
@@ -98,7 +98,7 @@ const iview = { @@ -98,7 +98,7 @@ const iview = {
98 // Spin, 98 // Spin,
99 Step: Steps.Step, 99 Step: Steps.Step,
100 Steps, 100 Steps,
101 - // Switch, 101 + iSwitch: Switch,
102 // iTable: Table, 102 // iTable: Table,
103 // Tabs: Tabs, 103 // Tabs: Tabs,
104 // TabPane: Tabs.Pane, 104 // TabPane: Tabs.Pane,
@@ -21,6 +21,7 @@ li + li { border-left: solid 1px #bbb; padding-left: 10px; margin-left: 10px; } @@ -21,6 +21,7 @@ li + li { border-left: solid 1px #bbb; padding-left: 10px; margin-left: 10px; }
21 <li><router-link to="/checkbox">Checkbox</router-link></li> 21 <li><router-link to="/checkbox">Checkbox</router-link></li>
22 <li><router-link to="/steps">Steps</router-link></li> 22 <li><router-link to="/steps">Steps</router-link></li>
23 <li><router-link to="/timeline">Timeline</router-link></li> 23 <li><router-link to="/timeline">Timeline</router-link></li>
  24 + <li><router-link to="/switch">Switch</router-link></li>
24 </ul> 25 </ul>
25 </nav> 26 </nav>
26 <router-view></router-view> 27 <router-view></router-view>
@@ -48,6 +48,10 @@ const router = new VueRouter({ @@ -48,6 +48,10 @@ const router = new VueRouter({
48 { 48 {
49 path: '/timeline', 49 path: '/timeline',
50 component: require('./routers/timeline.vue') 50 component: require('./routers/timeline.vue')
  51 + },
  52 + {
  53 + path: '/switch',
  54 + component: require('./routers/switch.vue')
51 } 55 }
52 ] 56 ]
53 }); 57 });
test/routers/switch.vue
1 <template> 1 <template>
2 - <Switch @on-change="change"></Switch>  
3 - <Switch>  
4 - <span slot="open">开</span>  
5 - <span slot="close">关</span>  
6 - </Switch>  
7 - <Switch size="large">  
8 - <span slot="open">ON</span>  
9 - <span slot="close">OFF</span>  
10 - </Switch>  
11 - <Switch>  
12 - <Icon type="android-done" slot="open"></Icon>  
13 - <Icon type="android-close" slot="close"></Icon>  
14 - </Switch>  
15 - <Switch :disabled="disabled"></Switch>  
16 - <i-button type="primary" @click="disabled = !disabled">Toggle Disabled</i-button>  
17 - <Switch size="small"></Switch> 2 + <div>
  3 + <i-switch v-model="m1">
  4 + <span slot="open">开</span>
  5 + <span slot="close">关</span>
  6 + </i-switch>
  7 + {{ m1 }}
  8 + <div @click="m1 = !m1">toggle</div>
  9 + </div>
18 </template> 10 </template>
19 <script> 11 <script>
20 - import { Switch, Message, iButton, Icon } from 'iview';  
21 export default { 12 export default {
22 - components: { Switch, Message, iButton, Icon },  
23 data () { 13 data () {
24 return { 14 return {
25 - disabled: true 15 + m1: false
26 } 16 }
27 }, 17 },
28 methods: { 18 methods: {
29 change (status) { 19 change (status) {
30 - Message.info('开关状态:' + status); 20 + console.log(status)
31 } 21 }
32 } 22 }
33 } 23 }