示例
注意
drawer
组件会依次遍历parent组件,寻找第一个page
组件作为自己的容器,如果没有找到,会使用document.body作为容器。
x-component组件内部使用drawer的时候,可以使用main作为根节点,这样drawer组件会找x-component外部的x-page作为容器。
基础用法
drawer
支持设置标题title
,副标题subtitle
等。
PCPhonePad
<page>
<module title="drawer的基础使用">
<actions>
<button bind-click="showDrawer" text="使用自定义按钮显示抽屉编辑"></button>
<button bind-click="showDrawer2" text="使用自带关闭按钮"></button>
</actions>
<form type="info" clear-button="{{false}}" confirm-button="{{false}}">
<item label="班级" name="class" value="{{formData.class}}"></item>
<item label="姓名" name='name' value="{{formData.name}}"></item>
</form>
</module>
<drawer
x='demoDrawer2'
title="我是标题"
subtitle="我是副标题"
bind-shown="handleShown"
bind-hided="handleHided"
>
drawer内容
</drawer>
<drawer
x='demoDrawer'
title="我是标题"
subtitle="我是副标题"
bind-shown="handleShown"
bind-hided="handleHided"
>
<form x="demoForm" clearButton="{{false}}" confirmButton="{{false}}" bindconfirm="submitForm">
<item node="input" name="name" label="姓名" required/>
<item node="input" name="class" label="班级"/>
</form>
<actions>
<button bindclick="hideDrawer" text="取消"></button>
<button target="demoForm.confirm" text="确定" status="primary"></button>
</actions>
</drawer>
</page>
export default XPage({
data: {
formData: {
class: '',
name: ''
},
},
show() {},
showDrawer() {
this.getComponent('demoDrawer').show();
},
showDrawer2() {
this.getComponent('demoDrawer2').show();
},
hideDrawer() {
this.getComponent('demoDrawer').hide();
},
handleShown() {
console.info('drawer打开了');
},
handleHided() {
console.info('drawer关闭了');
},
submitForm() {
this.getComponent('demoForm').validate((error, formData) => {
console.info('formData', formData);
this.setData({
formData,
})
this.hideDrawer();
});
},
});
方向
drawer``Pc
端支持从 4 个方向弹出:top、left、bottom、right。移动端只能从底部弹出。
PCPhonePad
<page>
<module title="drawer的基础使用">
<actions>
<button bind-click="showDrawer('t')" text="Top"></button>
<button bind-click="showDrawer('l')" text="Left"></button>
<button bind-click="showDrawer('b')" text="Bottom"></button>
<button bind-click="showDrawer('r')" text="Right"></button>
</actions>
</module>
<drawer
x='demoDrawer'
title="{{title}}"
direction="{{direction}}"
>
<actions>
<button text="关闭" bind-click="hideDrawer"></button>
</actions>
</drawer>
</page>
export default XPage({
data: {
title:'',
direction:'b',
},
show() {},
showDrawer(direction) {
let title='';
switch (direction) {
case 't':
title='从上弹出';
break;
case 'l':
title='从左弹出';
break;
case 'b':
title='从底弹出';
break;
case 'r':
title='从右弹出';
break;
}
this.setData({
title,
direction,
});
setTimeout(()=>{
this.getComponent('demoDrawer').show();
},0);
},
hideDrawer(){
this.getComponent('demoDrawer').hide();
},
});
蒙层
drawer
默认点击蒙层会关闭抽屉,支持配置wrapperClosable
禁止点击蒙层关闭。
PCPhonePad
<page>
<module title="点击蒙层不能关闭弹框">
<actions>
<button text="显示弹框" bind-click="showDrawer"></button>
</actions>
</module>
<drawer x="demoDrawer" title="我是标题" wrapperClosable="{{false}}">
<actions>
<button text="关闭" bind-click="hideDrawer"></button>
</actions>
</drawer>
</page>
export default XPage({
data: {
},
show() {},
showDrawer(){
this.getComponent('demoDrawer').show();
},
hideDrawer(){
this.getComponent('demoDrawer').hide();
},
});
尺寸
drawer
支持Pc
端设置size
设置抽屉的大小,direction为t/b时表示设置height,为b/r时表示设置宽度。size
边界:30% <= size <=
80%。
移动端为固定尺寸,不支持自定义。
PCPhonePad
<page>
<module title="drawer的size设置">
<actions>
<button bind-click="showDrawer('t','0')" text="Top:高400px"></button>
<button bind-click="showDrawer('l','1900px')" text="Left:宽400px"></button>
<button bind-click="showDrawer('b','90%')" text="Bottom:高60%"></button>
<button bind-click="showDrawer('r','20%')" text="Right:宽60%"></button>
</actions>
</module>
<drawer
x='demoDrawer'
title="{{title}}"
direction="{{direction}}"
size="{{size}}"
>
<actions>
<button text="关闭" bind-click="hideDrawer"></button>
</actions>
</drawer>
</page>
export default XPage({
data: {
title:'',
direction:'b',
size:undefined,
},
show() {},
showDrawer(direction,size) {
let title='';
switch (direction) {
case 't':
title=`从上弹出,高:${size}`;
break;
case 'l':
title=`从左弹出,宽:${size}`;
break;
case 'b':
title=`从底弹出,高:${size}`;
break;
case 'r':
title=`从右弹出,宽:${size}`;
break;
}
this.setData({
title,
direction,
size,
});
setTimeout(()=>{
this.getComponent('demoDrawer').show();
},0);
},
hideDrawer(){
this.getComponent('demoDrawer').hide();
},
});
提示信息notice
drawer
支持固定展示提示信息。可在XPage中调用this.getComponent('myDrawer').notice(...)
实现,其中myDrawer为DML中drawer的x属性值。具体notice配置参考下表Methods
。
PCPhonePad
<page>
<module title="drawer展示notice提示">
<actions>
<button bind-click="showDrawer" text="显示弹框"></button>
</actions>
</module>
<drawer
x='demoDrawer'
title="我是标题"
subtitle="我是副标题"
height="300px"
>
<actions>
<button bindclick="showNotice('info')" text="默认notice"></button>
<button bindclick="showNotice('warning')" text="警告notice"></button>
<button bindclick="showNotice('error')" text="错误notice"></button>
<button bindclick="showNotice('success')" text="成功notice"></button>
<button bindclick="hideNotice" text="关闭notice"></button>
</actions>
</drawer>
</page>
export default XPage({
data: {
noticeConfig: {
text: '提示文案',
closable: true,
buttonOptions: [
{
text: '按钮',
click: () => {
console.info('notice的按钮点击了')
},
},
],
}
},
show() {},
showDrawer() {
this.getComponent('demoDrawer').show();
},
showNotice(type) {
const drawer = this.getComponent('demoDrawer');
const noticeConfig = Object.assign(this.data.noticeConfig, {status: type});
drawer.notice(noticeConfig);
},
hideNotice(){
this.getComponent('demoDrawer').hideNotice();
},
});
Attributes
属性 | 说明 | 类型 | 可选值 | 默认值 | 端 | 版本 |
---|---|---|---|---|---|---|
title | 标题 | string | -- | -- | PC/Mobile | -- |
subtitle | 副标题 | string | -- | -- | PC | -- |
direction | drawer显示的方位 | string | t(top)/l(left)/b(bottom)/r(right)' | b | PC | -- |
size | direction为t/b时表示height,为b/r时表示宽度 | string | px/string | 70% | PC | -- |
wrapperClosable | 点击蒙层是否允许关闭 | Boolean | true | PC/Mobile | -- | |
cancelButton | 取消按钮 | Boolean/string/Object<{show:Boolean,text:String}> | -- | -- | PC/Mobile | -- |
isStay | 关闭弹框时不销毁组件实例 | Boolean | -- | -- | Mobile | -- |
Events
名称 | 说明 | 回调参数 | 端 | 版本 |
---|---|---|---|---|
shown | 显示drawer后触发 | -- | PC/Mobile | -- |
hided | 隐藏drawer(如:主动触发comps.hide()、或点击drawer右上角的x、或点击蒙层时)后触发 | -- | PC/Mobile | -- |
Methods
通过getComponent
函数获取x
属性定义的drawer后调用其对应Methods。
this.getComponent('myDrawer').notice({ text: '我是提示信息', closable: true, status }
名称 | 说明 | 入参 | 返回 | 端 | 版本 |
---|---|---|---|---|---|
notice | 显示容器提示信息,支持最多两个操作按钮,入参参考下表 | -- | -- | PC/Mobile | -- |
hideNotice | 关闭提示 | -- | -- | PC/Mobile | -- |
show | 显示drawer | -- | -- | PC/Mobile | -- |
hide | 关闭drawer | -- | -- | PC/Mobile | -- |
Methods - notice
notice函数支持的入参如下
属性 | 说明 | 类型 | 可选值 | 默认值 | 端 | 版本 |
---|---|---|---|---|---|---|
text | 通知信息 | string | -- | -- | PC/Mobile | -- |
status | 状态 | string | info/success/worning/error | info | PC/Mobile | -- |
buttonOptions | 操作按钮,最多支持两个按钮 | Array[{text:string<按钮文案>,click:Function<点击回调>}] | -- | -- | PC/Mobile | -- |
closable | 是否可关闭 | Boolean | -- | false | PC/Mobile | -- |