詳解釘釘小程序組件之自定義模態(tài)框(彈窗封裝實現(xiàn))
背景
開發(fā)釘釘小程序中需要用到模態(tài)框 文檔里也沒有 自己搞一個…
效果大概長這個樣
點擊指定按鈕,彈出模態(tài)框,里面的內(nèi)容可以自定義,可以是簡單的文字提示,也可以輸入框等復雜布局。操作完點擊取消或確定關閉。
開始封裝
上圖所示文件內(nèi)容放入項目即可 (路徑自己高興著來)
modal.js
內(nèi)容不多 但都是精華
/** * 自定義modal浮層 * 使用方法: * <modal show="{{showModal}}" height='80%' onCancel="modalCancel" onSubmit='modalSubmit'> <view>你自己需要展示的內(nèi)容</view> </modal> 屬性說明: show: 控制modal顯示與隱藏 height:modal的高度 onCancel:點擊取消按鈕的回調函數(shù) onSubmit:點擊確定按鈕的回調函數(shù) */ Component({ /** * 組件的屬性列表 */ props: { // modal的默認高度 height: '60%', //是否顯示modal show: false, // submit() onSubmit:(data) => console.log(data), // onCancel() onCancel:(data) => console.log(data), }, /** * 組件的初始數(shù)據(jù) */ data: { }, /** * 組件的方法列表 */ methods: { clickMask() { // this.setData({show: false}) }, cancel(e) { // this.setData({ show: false }); this.props.onCancel(e); }, submit(e) { // this.setData({ show: false }); this.props.onSubmit(e); } } })
代碼使用 props 屬性設置屬性默認值, 調用的時候傳遞指定值即可
modal.json
這就是個申明 啥也不是
{ "component": true, "usingComponents": {} }
開發(fā)者需要在 .json 文件中指明自定義組件的依賴
modal.acss
這玩意我一個寫后端的調了半天才勉強看得下去 求大佬改版發(fā)我
.mask{ position: absolute; top: 0; bottom: 0; width: 100%; height: 100%; display: flex; justify-content: center; align-items: center; background-color: rgba(0,0,0,0.4); z-index: 9999; } .modal-content{ flex-direction: column; width: 90%; /* height: 80%; */ position: fixed; top: 10%; left: 5%; background-color: #fff; border-radius: 10rpx; } .modal-btn-wrapper{ display: flex; flex-direction: row; height: 100rpx; line-height: 100rpx; background-color: #fff; border-radius: 10rpx; border-top: 2rpx solid rgba(7,17,27,0.1); } .cancel-btn, .confirm-btn{ flex: 1; height: 100rpx; line-height: 100rpx; text-align: center; font-size: 32rpx; } .cancel-btn{ border-right: 2rpx solid rgba(7,17,27,0.1); } .main-content{ flex: 1; height: 100%; overflow-y: hidden; }
modal.axml
敲重點 slot 標簽
可以將 slot 理解為槽位,default slot就是默認槽位,如果調用者在組件標簽之間不傳遞 axml,則最終會將默認槽位渲染出來。而如果調用者在組件標簽之間傳遞有 axml,則使用其替代默認槽位,進而組裝出最終的 axml 以供渲染。
簡而言之 你在調用的時候所編輯的axml都被塞進slot里面了
<view class='mask' a:if='{{show}}' onTap='clickMask'> <view class='modal-content' style='height:{{height}}'> <scroll-view scroll-y class='main-content'> <slot></slot> </scroll-view> <view class='modal-btn-wrapper'> <view class='cancel-btn' style='color:rgba(7,17,27,0.6)' onTap='cancel'>取消</view> <view class='confirm-btn' style='color:#13b5f5' onTap='submit'>確定</view> </view> </view> </view>
使用
這個相對簡單鳥
page/xx/page.json
首先申明我要調用這個組件 標簽名我就叫modal 路徑自己別搞錯就好
{ "usingComponents": { "modal": "/page/components/modal/modal" } }
page/xx/page.axml
就是這樣 喵~
<modal show="{{showSearchModal}}" height='80%' onCancel="searchModalCancel" onSubmit='searchModalSubmit'> <view>你自己的布局</view> </modal>
page/xx/page.js
這個你就寫你自己的邏輯就沒毛病了
let app = getApp(); Page({ data: { showSearchModal: false, }, onLoad() { }, searchModalCancel(){ this.setData({ showSearchModal: false, }); dd.alert({ title: '提示', content: '用戶點擊了取消', }); }, searchModalSubmit(){ this.setData({ showSearchModal: false, }); dd.alert({ title: '提示', content: '用戶點擊了提交', buttonText: '我知道了', }); }, });
小結
激動的心,顫抖的手。。。
總之先閱讀官方文檔
釘釘開放平臺 => 前端API => 小程序 => 框架 => 自定義組件
https://ding-doc.dingtalk.com/doc#/dev/develop-custom-component
本案例相對簡單,業(yè)務復雜的需求看看文檔基本都能實現(xiàn)。
關于微信小程序實現(xiàn)自定義modal彈窗封裝的方法 ,可以點擊查看。
總結
到此這篇關于釘釘小程序組件之自定義模態(tài)框(彈窗封裝實現(xiàn))的文章就介紹到這了,更多相關小程序組件自定義模態(tài)框內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
JS中DOM元素的attribute與property屬性示例詳解
這篇文章主要給大家介紹了關于JS中DOM元素的attribute與property屬性的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起看看吧2018-09-09js判斷出兩個字符串最大子串的函數(shù)實現(xiàn)方法
下面小編就為大家?guī)硪黄猨s判斷出兩個字符串最大子串的函數(shù)實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-11-11suggestion開發(fā)小結以及對鍵盤事件的總結(針對中文輸入法狀態(tài))
suggestion開發(fā)小結以及對鍵盤事件的總結(針對中文輸入法狀態(tài)),需要的朋友可以參考下。2011-12-12