tm-checkbox.vue 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <template>
  2. <view @click="onclick" class=" tm-checkbox " :class="[dense?'':'pa-20',inline?'d-inline-block':'']">
  3. <view class=" flex-start">
  4. <slot name="default" :checkData="{label:label,checked:changValue}" :on="onclick">
  5. <view :style="{width: sizes.wk,height: sizes.wk}" class="tm-checkbox-boey relative d-inline-block"
  6. :class="[black?'bk':'','flex-shrink mr-10',
  7. changValue?'ani':'',
  8. changValue?color_tmeme+' border-'+(borderColor||color_tmeme)+'-a-1':'border-'+(borderColor||color_tmeme)+'-a-1',
  9. disabled?'grey-lighten-2 border-grey-lighten-1-a-1':'',
  10. round==='rounded'?'rounded':'round-'+round]">
  11. <view :class="[changValue?'ani_toMaxToMin_on':'']" class="absolute flex-center" style="width: 100%;height: 100%;">
  12. <tm-icons dense v-show="model === 'normal'" :color="disabled?'opacity-5 white':'white'" :size="sizes.gou" :name="changValue?icon:' '"></tm-icons>
  13. <view v-show="model === 'round'&&changValue" class=" rounded d-inline-block"
  14. :class="[disabled?'opacity-5 white':'white']"
  15. :style="{width: sizes.yuan,height: sizes.yuan}"></view>
  16. </view>
  17. </view>
  18. </slot>
  19. <view v-if="label" :class="[black?'bk':'','px-10 fulled flex-start']" :style="{minHeight: sizes.wk}" class=" tm-checkbox-boey-label ">
  20. <view class="flex-center fulled-height">
  21. <slot name="label" :label="{label:label,checked:changValue}">
  22. <text class=" text-size-n">{{label}}</text>
  23. </slot>
  24. </view>
  25. </view>
  26. </view>
  27. </view>
  28. </template>
  29. <script>
  30. /**
  31. * 复选框,可以单独或者在tm-groupcheckbox中使用。
  32. * @property {Boolean} value = [true|false] 如果想双向绑定需要value.snyc等同v-model。推荐v-model.
  33. * @property {Function} input 等同value.snyc和v-model和change
  34. * @property {Function} change 变化是会返回 {index,checked,value:name携带的数据}
  35. * @property {Boolean} disabled = [true|false] 默认false,禁用
  36. * @property {String} color = [primary|blue] 默认primary,主题色名称。
  37. * @property {String} border-color = [] 默认 '',边线主题色,默认同color可不填。
  38. * @property {String} model = [normal|round] 默认normal, 内部:normal打勾,round:内部为圆点
  39. * @property {String} icon = [icon-check] 默认icon-check,自定义选中时的图标。
  40. * @property {String|Number} round = [2|rounded] 默认2, 圆角,rounded时即圆形。
  41. * @property {String|Number} size = [] 默认32, 大小单位upx
  42. * @property {String|Boolean} dense = [true|false] 默认false, 是否去除外间隙。
  43. * @property {String} label = [] 默认"", 文右边显示的选项文字
  44. * @property {String|Boolean} black = [true|false] 默认false, 暗黑模式
  45. * @property {String|Boolean} inline = [true|false] 默认false, 是否内联模式
  46. * @property {String | Array | Object | Number} name = [] 默认 "", 选中时携带的自定义数据,会通过change带回。
  47. * @example <tm-checkbox v-model="checked" label="苹果"></tm-checkbox>
  48. */
  49. import tmIcons from "@/tm-vuetify/components/tm-icons/tm-icons.vue"
  50. export default {
  51. components:{tmIcons},
  52. name: 'tm-checkbox',
  53. model: {
  54. prop: 'value',
  55. event: 'input'
  56. },
  57. props: {
  58. // 禁用。
  59. disabled: Boolean,
  60. //是否内联模式
  61. inline:{
  62. type:Boolean,
  63. default:true
  64. },
  65. // 使用时::checked.sync 需要带sync
  66. value: Boolean,
  67. color: {
  68. type: String,
  69. default: 'primary'
  70. },
  71. borderColor: {
  72. type: String,
  73. default: ''
  74. },
  75. // 内部:normal打勾,round:内部为圆点
  76. model: {
  77. type: String,
  78. default: 'normal'
  79. },
  80. // 自定义选中时的图标。
  81. icon: {
  82. type: String,
  83. default: 'icon-check'
  84. },
  85. // 外部形状:== rounded时即圆形。
  86. round: {
  87. type: String | Number,
  88. default: '2'
  89. },
  90. // 单位upx
  91. size: {
  92. type: String | Number,
  93. default: 38
  94. },
  95. // 是否去除外间隙。
  96. dense: {
  97. type: Boolean | String,
  98. default: false
  99. },
  100. // 名称。
  101. label: {
  102. type: String,
  103. default: ''
  104. },
  105. black: {
  106. type: Boolean | String,
  107. default: false
  108. },
  109. name: {
  110. type: String | Array | Object | Number,
  111. default: ''
  112. },
  113. // 跟随主题色的改变而改变。
  114. fllowTheme:{
  115. type:Boolean|String,
  116. default:true
  117. }
  118. },
  119. data() {
  120. return {
  121. };
  122. },
  123. watch: {
  124. value: function(newval, oldval) {
  125. if (newval !== oldval) {
  126. if (!this.jiancMax()) {
  127. this.changValue = false;
  128. return;
  129. }
  130. this.change();
  131. }
  132. }
  133. },
  134. computed: {
  135. color_tmeme:function(){
  136. if(this.$tm.vx.state().tmVuetify.color!==null&&this.$tm.vx.state().tmVuetify.color && this.fllowTheme){
  137. return this.$tm.vx.state().tmVuetify.color;
  138. }
  139. return this.color;
  140. },
  141. changValue: {
  142. get: function() {
  143. return this.value;
  144. },
  145. set: function(newValue) {
  146. this.$emit('input', newValue)
  147. // 如果不想用v-model. 直接使用value,需要:value.sync
  148. this.$emit('update:value', newValue);
  149. }
  150. },
  151. sizes: function() {
  152. return {
  153. wk: uni.upx2px(this.size) + 'px',
  154. gou: uni.upx2px(this.size / 3 * 2) + 'px',
  155. yuan: uni.upx2px(this.size / 2) + 'px',
  156. }
  157. }
  158. },
  159. methods: {
  160. // 检查是否超过了最大选择。
  161. jiancMax() {
  162. let t= this;
  163. let box = [];
  164. let selfIndex;
  165. let __uid = this._uid;
  166. // 同时检测是否被禁用。如果禁用了,也不能设置。
  167. if(this.disabled) return false;
  168. function findchild(p, index) {
  169. let preat = p;
  170. if (preat.$options?.name === 'tm-checkbox') {
  171. if (preat.changValue) {
  172. box.push({
  173. index: index,
  174. value: preat.name,
  175. checked: preat.changValue
  176. })
  177. }
  178. if (preat._uid === __uid) {
  179. selfIndex = index;
  180. }
  181. } else {
  182. if (preat.$children.length > 0) {
  183. preat.$children.forEach(item => {
  184. findchild(item, index++);
  185. })
  186. }
  187. }
  188. };
  189. let preat = t.$tm.getParentAls('tm-groupcheckbox', t.$parent);
  190. if (preat) {
  191. findchild(preat, 0);
  192. if (box.length > preat.max) {
  193. preat.error()
  194. return false
  195. }
  196. }
  197. return true
  198. },
  199. onclick(e) {
  200. if (this.disabled) return;
  201. if (!this.jiancMax()) {
  202. return;
  203. }
  204. this.changValue = !this.changValue;
  205. },
  206. change() {
  207. let box = [];
  208. let selfIndex;
  209. let __uid = this._uid;
  210. function findchild(p, index) {
  211. let preat = p;
  212. if (preat.$options?.name === 'tm-checkbox') {
  213. if (preat.changValue) {
  214. box.push({
  215. index: index,
  216. value: preat.name,
  217. checked: preat.changValue
  218. })
  219. }
  220. if (preat._uid === __uid) {
  221. selfIndex = index;
  222. }
  223. } else {
  224. if (preat.$children.length > 0) {
  225. preat.$children.forEach(item => {
  226. findchild(item, index++);
  227. })
  228. }
  229. }
  230. };
  231. let preat = this.$tm.getParentAls('tm-groupcheckbox', this.$parent);
  232. if (preat) {
  233. findchild(preat, 0);
  234. this.$emit('change', {
  235. index: selfIndex,
  236. checked: this.changValue,
  237. value: this.name
  238. });
  239. preat.change(box)
  240. } else {
  241. this.$emit('change', {
  242. index: 0,
  243. checked: this.changValue,
  244. value: this.name
  245. });
  246. }
  247. }
  248. },
  249. }
  250. </script>
  251. <style lang="scss" scoped>
  252. .tm-checkbox{
  253. vertical-align: middle;
  254. .tm-checkbox-boey,.tm-checkbox-boey-label{
  255. vertical-align: middle;
  256. }
  257. .ani {
  258. animation: ani 0.2s linear;
  259. }
  260. .ani_toMaxToMin_on {
  261. animation: ani_toMaxToMin_on 0.35s ease-in-out;
  262. }
  263. @keyframes ani_toMaxToMin_on {
  264. 0% {
  265. transform: scale(0.3);
  266. opacity:0.7;
  267. }
  268. 50% {
  269. transform: scale(1.5)
  270. }
  271. 100% {
  272. transform: scale(1);
  273. opacity:1;
  274. }
  275. }
  276. @keyframes ani {
  277. 0% {
  278. transform: scale(0.9)
  279. }
  280. 50% {
  281. transform: scale(1.1)
  282. }
  283. 100% {
  284. transform: scale(0.9)
  285. }
  286. }
  287. }
  288. </style>