z-modal.vue 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. <template>
  2. <view>
  3. <view class="mask" :style="{'opacity':show?'1':'0','visibility':show?'visible':'hidden'}"></view>
  4. <view class="modal-box" :style="{'display':show?'block':'none','width':modalWidth}">
  5. <view class="modal-title" :style="{'padding':titlePadding,'fontSize':titleSize,'color':titleColor}">
  6. {{titleText}}
  7. </view>
  8. <view class="modal-content" v-if="contentType==1" :style="{'fontSize':contentSize,'padding':contentPadding,'color':contentColor}">
  9. {{contentText}}
  10. </view>
  11. <view class="modal-content" v-if="contentType==2" :style="{'padding':contentPadding}">
  12. <input :placeholder="placeholderText" v-model="inputText" :maxlength="maxLength" :placeholder-style="placeholderStyleString" :style="{'borderColor':inputBorderColor,'fontSize':contentSize,'color':contentColor}" class="input-area"/>
  13. </view>
  14. <view class="modal-content" v-if="contentType==3" :style="{'padding':contentPadding}">
  15. <textarea :placeholder="placeholderText" v-model="textareaText" :maxlength="maxLength" :placeholder-style="placeholderStyleString" :style="{'borderColor':inputBorderColor,'fontSize':contentSize,'color':contentColor}"/>
  16. </view>
  17. <view class="model-bottom" :style="{'padding':bottomPadding,'justify-content':btnLength>1?'space-between':'center'}">
  18. <block v-for="(item,index) in btnGroup" :key='index'>
  19. <view class="modal-btn" :style="{'width':item.width,'height':item.height,'color':item.color,'background-color':item.bgColor,'font-size':bottomFontSize,'border-radius':shapeObj[item.shape]}" @tap="btnClick(index)">
  20. {{item.text}}
  21. </view>
  22. </block>
  23. </view>
  24. </view>
  25. </view>
  26. </template>
  27. <script>
  28. export default {
  29. name: "z-modal",
  30. props: {
  31. // **************** 使用配置项 ****************
  32. // 文本校验?
  33. check:{
  34. type:Boolean,
  35. default:false
  36. },
  37. checkItem:{
  38. // isRequired-是否必填/ isNumber-是否为纯数字 isChinese-是否为纯中文 isEnglish-是否为纯英文 isEmail-是否为邮箱格式 isIdNum-是否为身份证格式
  39. type:Array,
  40. default:()=>{
  41. return []
  42. }
  43. },
  44. /*
  45. 支持自定义校验,格式为{
  46. key:正则
  47. }
  48. 例如{
  49. 'isNumber':/^\+?[0-9][0-9]*$/
  50. }
  51. */
  52. selfCheckItem:{
  53. type:Object,
  54. default:()=>{
  55. return {}
  56. }
  57. },
  58. // 控制modal的显示隐藏
  59. show:{
  60. type:Boolean,
  61. default:false
  62. },
  63. // 底部按钮组
  64. btnGroup: {
  65. type: Array,
  66. default: () => {
  67. return [{
  68. text: '取消',
  69. color: '#FFFFFF',
  70. bgColor: '#999999',
  71. width: '220rpx',
  72. height: '80rpx',
  73. shape: 'circle',
  74. eventName: 'cancle'
  75. }, {
  76. text: '确定',
  77. color: '#FFFFFF',
  78. bgColor: '#007AFF',
  79. width: '220rpx',
  80. height: '80rpx',
  81. shape: 'circle',
  82. eventName: 'sure'
  83. }]
  84. }
  85. },
  86. // 文本类型 1-展示文字 2-input 3-textarea
  87. contentType:{
  88. type:[Number,String],
  89. default:1
  90. },
  91. // contentType为2或3时的字数限制
  92. maxLength:{
  93. type:Number,
  94. default:20
  95. },
  96. // **************** 尺寸&大小 ****************
  97. // modal整体宽度 rpx
  98. modalWidth:{
  99. type:String,
  100. default:'580rpx'
  101. },
  102. // title区域文字大小
  103. titleSize:{
  104. type:String,
  105. default:'32rpx'
  106. },
  107. // content区域文字大小
  108. contentSize:{
  109. type:String,
  110. default:'28rpx'
  111. },
  112. // 底部(按钮)区域的字号
  113. bottomFontSize:{
  114. type:String,
  115. default:'28rpx'
  116. },
  117. // title(标题) 区域的padding
  118. titlePadding:{
  119. type:String,
  120. default:'20rpx 0'
  121. },
  122. // content(内容) 区域的padding
  123. contentPadding:{
  124. type:String,
  125. default:'10rpx 40rpx'
  126. },
  127. // 底部(按钮)区域的padding
  128. bottomPadding:{
  129. type:String,
  130. default:'30rpx 40rpx'
  131. },
  132. // **************** 颜色 ****************
  133. // 标题文字颜色
  134. titleColor:{
  135. type:String,
  136. default:'#333333'
  137. },
  138. // 内容文字颜色
  139. contentColor:{
  140. type:String,
  141. default:'#333333'
  142. },
  143. // placeholderColor-提示文字的颜色
  144. placeholderColor:{
  145. type:String,
  146. default:'#999'
  147. },
  148. inputBorderColor:{
  149. type:String,
  150. default:'#999'
  151. },
  152. // **************** 内容 ****************
  153. // 标题内容
  154. titleText:{
  155. type:String,
  156. default:'titleText'
  157. },
  158. // 文本内容
  159. contentText:{
  160. type:String,
  161. default:'Please type in your text'
  162. },
  163. // input或textarea的placeholder
  164. placeholderText:{
  165. type:String,
  166. default:'请输入你的内容'
  167. }
  168. },
  169. computed:{
  170. btnLength(){
  171. return this.btnGroup.length
  172. },
  173. placeholderStyleString(){
  174. return `color:${this.placeholderColor};font-size:${this.contentSize}`
  175. }
  176. },
  177. data() {
  178. return {
  179. // 底部按钮shape对应值
  180. shapeObj:{
  181. 'circle':'1000rpx',
  182. 'straight':'0',
  183. 'fillet':'12rpx'
  184. },
  185. inputText:'',
  186. textareaText:'',
  187. // ***校验项,是否通过,true-通过,false不通过
  188. checkCollection: {
  189. isRequired :function(val){
  190. return !(val === null || val === '' || val === undefined)
  191. },
  192. isNumber :function(val){
  193. let reg = /^\+?[0-9][0-9]*$/
  194. return reg.test(val)
  195. },
  196. isChinese :function(val){
  197. let reg = /^[\u4e00-\u9fa5]+$/
  198. return reg.test(val)
  199. },
  200. isEnglish :function(val){
  201. let reg = /^[a-zA-Z]+$/
  202. return reg.test(val)
  203. },
  204. isEmail :function(val){
  205. let reg = /^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/
  206. return reg.test(val)
  207. },
  208. isIdNum :function(val){
  209. let reg = /^\d{6}(18|19|20)?\d{2}(0[1-9]|1[12])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/i
  210. return reg.test(val)
  211. }
  212. }
  213. };
  214. },
  215. methods:{
  216. btnClick(index){
  217. let val = this.contentType==2?this.inputText:this.textareaText
  218. if(this.check && this.checkItem.length && this.contentType!=1){
  219. let err = false
  220. // 进入校验,如果不通过直接返回,通过则吐出按钮事件
  221. for(let item of this.checkItem){
  222. if(!this.checkCollection[item](val)){
  223. this.$emit('error',item)
  224. err = true
  225. break
  226. }
  227. }
  228. if(err) return
  229. }
  230. let selfCheckArr = Object.entries(this.selfCheckItem)
  231. if(this.check && selfCheckArr.length && this.contentType!=1){
  232. let err = false
  233. for(let item of selfCheckArr){
  234. let reg = new RegExp(item[1])
  235. if(!reg.test(val)){
  236. this.$emit('error',item[0])
  237. err = true
  238. break
  239. }
  240. }
  241. if(err) return
  242. }
  243. this.$emit(this.btnGroup[index]['eventName'],{
  244. inputText:this.inputText || '',
  245. textareaText:this.textareaText || ''
  246. })
  247. this.inputText = ''
  248. this.textareaText = ''
  249. }
  250. }
  251. }
  252. </script>
  253. <style>
  254. .mask {
  255. position: fixed;
  256. z-index: 98;
  257. background-color: rgba(0, 0, 0, .7);
  258. top: 0;
  259. left: 0;
  260. right: 0;
  261. bottom: 0;
  262. visibility: hidden;
  263. opacity: 0;
  264. transition: all .3s;
  265. }
  266. .modal-box {
  267. background-color: #FFFFFF;
  268. position: fixed;
  269. z-index: 99;
  270. border-radius: 12rpx;
  271. left: 50%;
  272. top: 50%;
  273. transform: translate(-50%, -50%);
  274. }
  275. .modal-title {
  276. box-sizing: border-box;
  277. color: #333333;
  278. display: flex;
  279. align-items: center;
  280. justify-content: center;
  281. }
  282. .modal-content {
  283. box-sizing: border-box;
  284. color: #333333;
  285. word-break: break-all;
  286. display: flex;
  287. justify-content: center;
  288. }
  289. .model-bottom {
  290. display: flex;
  291. align-items: center;
  292. }
  293. .modal-btn {
  294. display: flex;
  295. align-items: center;
  296. justify-content: center;
  297. }
  298. .input-area{
  299. border: 1rpx solid #999;
  300. width: 100%;
  301. padding:0 16rpx;
  302. box-sizing: border-box;
  303. height: 60rpx;
  304. border-radius: 6rpx;
  305. outline: none;
  306. }
  307. textarea{
  308. border: 1rpx solid #999;
  309. width: 92%;
  310. padding:16rpx;
  311. box-sizing: border-box;
  312. border-radius: 6rpx;
  313. height: 200rpx;
  314. }
  315. </style>