feat(Form): add Calendar demo

This commit is contained in:
陈嘉涵 2020-02-11 15:40:58 +08:00
parent fb3892e302
commit 200da364ff
6 changed files with 138 additions and 47 deletions

View File

@ -212,7 +212,7 @@ export default {
show-toolbar show-toolbar
:columns="columns" :columns="columns"
@confirm="onConfirm" @confirm="onConfirm"
@cancel="onCancel" @cancel="showPicker = false"
/> />
</van-popup> </van-popup>
``` ```
@ -231,9 +231,6 @@ export default {
this.value = value; this.value = value;
this.showPicker = false; this.showPicker = false;
}, },
onCancel() {
this.showPicker = false;
},
}, },
}; };
``` ```
@ -252,10 +249,9 @@ export default {
/> />
<van-popup v-model="showPicker" position="bottom"> <van-popup v-model="showPicker" position="bottom">
<van-datetime-picker <van-datetime-picker
v-model="currentDate" type="time"
type="date"
@confirm="onConfirm" @confirm="onConfirm"
@cancel="onCancel" @cancel="showPicker = false"
/> />
</van-popup> </van-popup>
``` ```
@ -266,18 +262,11 @@ export default {
return { return {
value: '', value: '',
showPicker: false, showPicker: false,
currentDate: new Date(),
}; };
}, },
methods: { methods: {
formatDate(date) { onConfirm(time) {
return `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`; this.value = time;
},
onConfirm(date) {
this.value = this.formatDate(date);
this.showPicker = false;
},
onCancel() {
this.showPicker = false; this.showPicker = false;
}, },
}, },
@ -300,7 +289,7 @@ export default {
<van-area <van-area
:area-list="areaList" :area-list="areaList"
@confirm="onConfirm" @confirm="onConfirm"
@cancel="onCancel" @cancel="showArea = false"
/> />
</van-popup> </van-popup>
``` ```
@ -319,8 +308,37 @@ export default {
this.value = values.map(item => item.name).join('/'); this.value = values.map(item => item.name).join('/');
this.showArea = false; this.showArea = false;
}, },
onCancel() { },
this.showArea = false; };
```
### Field Type - Calendar
```html
<van-field
readonly
clickable
name="calendar"
:value="value"
label="Calendar"
placeholder="Select date"
@click="showCalendar = true"
/>
<van-calendar v-model="showCalendar" @confirm="onConfirm" />
```
```js
export default {
data() {
return {
value: '',
showCalendar: false,
};
},
methods: {
onConfirm(date) {
this.value = `${date.getMonth() + 1}/${date.getDate()}`;
this.showCalendar = false;
}, },
}, },
}; };

View File

@ -218,7 +218,7 @@ export default {
show-toolbar show-toolbar
:columns="columns" :columns="columns"
@confirm="onConfirm" @confirm="onConfirm"
@cancel="onCancel" @cancel="showPicker = false"
/> />
</van-popup> </van-popup>
``` ```
@ -237,9 +237,6 @@ export default {
this.value = value; this.value = value;
this.showPicker = false; this.showPicker = false;
}, },
onCancel() {
this.showPicker = false;
},
}, },
}; };
``` ```
@ -258,10 +255,9 @@ export default {
/> />
<van-popup v-model="showPicker" position="bottom"> <van-popup v-model="showPicker" position="bottom">
<van-datetime-picker <van-datetime-picker
v-model="currentDate" type="time"
type="date"
@confirm="onConfirm" @confirm="onConfirm"
@cancel="onCancel" @cancel="showPicker = false"
/> />
</van-popup> </van-popup>
``` ```
@ -272,18 +268,11 @@ export default {
return { return {
value: '', value: '',
showPicker: false, showPicker: false,
currentDate: new Date(),
}; };
}, },
methods: { methods: {
formatDate(date) { onConfirm(time) {
return `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`; this.value = time;
},
onConfirm(date) {
this.value = this.formatDate(date);
this.showPicker = false;
},
onCancel() {
this.showPicker = false; this.showPicker = false;
}, },
}, },
@ -306,7 +295,7 @@ export default {
<van-area <van-area
:area-list="areaList" :area-list="areaList"
@confirm="onConfirm" @confirm="onConfirm"
@cancel="onCancel" @cancel="showArea = false"
/> />
</van-popup> </van-popup>
``` ```
@ -317,7 +306,7 @@ export default {
return { return {
value: '', value: '',
showArea: false, showArea: false,
areaList: {} // 数据格式见 Area 组件文档 areaList: {}, // 数据格式见 Area 组件文档
}; };
}, },
methods: { methods: {
@ -325,8 +314,37 @@ export default {
this.value = values.map(item => item.name).join('/'); this.value = values.map(item => item.name).join('/');
this.showArea = false; this.showArea = false;
}, },
onCancel() { },
this.showArea = false; };
```
### 表单项类型 - 日历
```html
<van-field
readonly
clickable
name="calendar"
:value="value"
label="日历"
placeholder="点击选择日期"
@click="showCalendar = true"
/>
<van-calendar v-model="showCalendar" @confirm="onConfirm" />
```
```js
export default {
data() {
return {
value: '',
showCalendar: false,
};
},
methods: {
onConfirm(date) {
this.value = `${date.getMonth() + 1}/${date.getDate()}`;
this.showCalendar = false;
}, },
}, },
}; };

View File

@ -50,6 +50,7 @@
<field-type-picker /> <field-type-picker />
<field-type-datetime-picker /> <field-type-datetime-picker />
<field-type-area /> <field-type-area />
<field-type-calendar />
<div style="margin: 16px 16px 0;"> <div style="margin: 16px 16px 0;">
<van-button type="info" round block>{{ $t('submit') }}</van-button> <van-button type="info" round block>{{ $t('submit') }}</van-button>
@ -61,6 +62,7 @@
<script> <script>
import FieldTypeArea from './FieldTypeArea'; import FieldTypeArea from './FieldTypeArea';
import FieldTypePicker from './FieldTypePicker'; import FieldTypePicker from './FieldTypePicker';
import FieldTypeCalendar from './FieldTypeCalendar';
import FieldTypeDatetimePicker from './FieldTypeDatetimePicker'; import FieldTypeDatetimePicker from './FieldTypeDatetimePicker';
export default { export default {
@ -98,6 +100,7 @@ export default {
components: { components: {
FieldTypeArea, FieldTypeArea,
FieldTypePicker, FieldTypePicker,
FieldTypeCalendar,
FieldTypeDatetimePicker, FieldTypeDatetimePicker,
}, },

View File

@ -0,0 +1,47 @@
<template>
<div>
<van-field
readonly
clickable
name="calendar"
:value="value"
:label="$t('calendar')"
:placeholder="$t('placeholder')"
@click="showCalendar = true"
/>
<van-calendar v-model="showCalendar" @confirm="onConfirm" />
</div>
</template>
<script>
export default {
i18n: {
'zh-CN': {
calendar: '日历',
placeholder: '点击选择日期',
},
'en-US': {
calendar: 'Calendar',
placeholder: 'Select date',
},
},
data() {
return {
value: '',
showCalendar: false,
};
},
methods: {
formatDate(date) {
return `${date.getMonth() + 1}/${date.getDate()}`;
},
onConfirm(date) {
this.value = this.formatDate(date);
this.showCalendar = false;
},
},
};
</script>

View File

@ -11,8 +11,7 @@
/> />
<van-popup v-model="showPicker" position="bottom"> <van-popup v-model="showPicker" position="bottom">
<van-datetime-picker <van-datetime-picker
v-model="currentDate" type="time"
type="date"
@confirm="onConfirm" @confirm="onConfirm"
@cancel="onCancel" @cancel="onCancel"
/> />
@ -37,16 +36,13 @@ export default {
return { return {
value: '', value: '',
showPicker: false, showPicker: false,
currentDate: new Date(),
}; };
}, },
methods: { methods: {
formatDate(date) { onConfirm(time) {
return `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`; console.log('time', time);
}, this.value = time;
onConfirm(date) {
this.value = this.formatDate(date);
this.showPicker = false; this.showPicker = false;
}, },
onCancel() { onCancel() {

View File

@ -184,6 +184,15 @@ exports[`renders demo correctly 1`] = `
</div> </div>
<!----> <!---->
</div> </div>
<div>
<div role="button" tabindex="0" class="van-cell van-cell--clickable van-field">
<div class="van-cell__title van-field__label"><span>日历</span></div>
<div class="van-cell__value van-field__value">
<div class="van-field__body"><input type="text" name="calendar" readonly="readonly" placeholder="点击选择日期" class="van-field__control"></div>
</div>
</div>
<!---->
</div>
<div style="margin: 16px 16px 0px;"><button class="van-button van-button--info van-button--normal van-button--block van-button--round"><span class="van-button__text">提交</span></button></div> <div style="margin: 16px 16px 0px;"><button class="van-button van-button--info van-button--normal van-button--block van-button--round"><span class="van-button__text">提交</span></button></div>
</form> </form>
</div> </div>