tm-checkbox.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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,extendData}" :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. options: {
  52. virtualHost: true,
  53. styleIsolation: 'shared'
  54. },
  55. components:{tmIcons},
  56. name: 'tm-checkbox',
  57. model: {
  58. prop: 'value',
  59. event: 'input'
  60. },
  61. props: {
  62. // 禁用。
  63. disabled: Boolean,
  64. //是否内联模式
  65. inline:{
  66. type:Boolean,
  67. default:true
  68. },
  69. // 使用时::checked.sync 需要带sync
  70. value: Boolean,
  71. color: {
  72. type: String,
  73. default: 'primary'
  74. },
  75. borderColor: {
  76. type: String,
  77. default: ''
  78. },
  79. // 内部:normal打勾,round:内部为圆点
  80. model: {
  81. type: String,
  82. default: 'normal'
  83. },
  84. // 自定义选中时的图标。
  85. icon: {
  86. type: String,
  87. default: 'icon-check'
  88. },
  89. // 外部形状:== rounded时即圆形。
  90. round: {
  91. type: String | Number,
  92. default: '2'
  93. },
  94. // 单位upx
  95. size: {
  96. type: String | Number,
  97. default: 38
  98. },
  99. // 是否去除外间隙。
  100. dense: {
  101. type: Boolean | String,
  102. default: false
  103. },
  104. // 名称。
  105. label: {
  106. type: String,
  107. default: ''
  108. },
  109. black: {
  110. type: Boolean | String,
  111. default: false
  112. },
  113. name: {
  114. type: String | Array | Object | Number,
  115. default: ''
  116. },
  117. // 跟随主题色的改变而改变。
  118. fllowTheme:{
  119. type:Boolean|String,
  120. default:true
  121. },
  122. extendData:{
  123. type:Object,
  124. default:()=>({})
  125. }
  126. },
  127. data() {
  128. return {
  129. };
  130. },
  131. watch: {
  132. value: function(newval, oldval) {
  133. if (newval !== oldval) {
  134. if (!this.jiancMax()) {
  135. this.changValue = false;
  136. return;
  137. }
  138. this.change();
  139. }
  140. }
  141. },
  142. computed: {
  143. color_tmeme:function(){
  144. if(this.$tm.vx.state().tmVuetify.color!==null&&this.$tm.vx.state().tmVuetify.color && this.fllowTheme){
  145. return this.$tm.vx.state().tmVuetify.color;
  146. }
  147. return this.color;
  148. },
  149. changValue: {
  150. get: function() {
  151. return this.value;
  152. },
  153. set: function(newValue) {
  154. this.$emit('input', newValue)
  155. // 如果不想用v-model. 直接使用value,需要:value.sync
  156. this.$emit('update:value', newValue);
  157. }
  158. },
  159. sizes: function() {
  160. return {
  161. wk: uni.upx2px(this.size) + 'px',
  162. gou: uni.upx2px(this.size / 3 * 2) + 'px',
  163. yuan: uni.upx2px(this.size / 2) + 'px',
  164. }
  165. }
  166. },
  167. methods: {
  168. // 检查是否超过了最大选择。
  169. jiancMax() {
  170. let t= this;
  171. let box = [];
  172. let selfIndex;
  173. let __uid = this._uid;
  174. // 同时检测是否被禁用。如果禁用了,也不能设置。
  175. if(this.disabled) return false;
  176. function findchild(p, index) {
  177. let preat = p;
  178. if (preat.$options?.name === 'tm-checkbox') {
  179. if (preat.changValue) {
  180. box.push({
  181. index: index,
  182. value: preat.name,
  183. checked: preat.changValue
  184. })
  185. }
  186. if (preat._uid === __uid) {
  187. selfIndex = index;
  188. }
  189. } else {
  190. if (preat.$children.length > 0) {
  191. preat.$children.forEach(item => {
  192. findchild(item, index++);
  193. })
  194. }
  195. }
  196. };
  197. let preat = t.$tm.getParentAls('tm-groupcheckbox', t.$parent);
  198. if (preat) {
  199. findchild(preat, 0);
  200. if (box.length > preat.max) {
  201. preat.error()
  202. return false
  203. }
  204. }
  205. return true
  206. },
  207. onclick(e) {
  208. if (this.disabled) return;
  209. if (!this.jiancMax()) {
  210. return;
  211. }
  212. this.changValue = !this.changValue;
  213. },
  214. change() {
  215. let box = [];
  216. let selfIndex;
  217. let __uid = this._uid;
  218. function findchild(p, index) {
  219. let preat = p;
  220. if (preat.$options?.name === 'tm-checkbox') {
  221. if (preat.changValue) {
  222. box.push({
  223. index: index,
  224. value: preat.name,
  225. checked: preat.changValue
  226. })
  227. }
  228. if (preat._uid === __uid) {
  229. selfIndex = index;
  230. }
  231. } else {
  232. if (preat.$children.length > 0) {
  233. preat.$children.forEach(item => {
  234. findchild(item, index++);
  235. })
  236. }
  237. }
  238. };
  239. let preat = this.$tm.getParentAls('tm-groupcheckbox', this.$parent);
  240. if (preat) {
  241. findchild(preat, 0);
  242. this.$emit('change', {
  243. index: selfIndex,
  244. checked: this.changValue,
  245. value: this.name
  246. });
  247. preat.change(box)
  248. } else {
  249. this.$emit('change', {
  250. index: 0,
  251. checked: this.changValue,
  252. value: this.name
  253. });
  254. }
  255. }
  256. },
  257. }
  258. </script>
  259. <style lang="scss" scoped>
  260. .tm-checkbox{
  261. vertical-align: middle;
  262. .tm-checkbox-boey,.tm-checkbox-boey-label{
  263. vertical-align: middle;
  264. }
  265. .ani {
  266. animation: ani 0.2s linear;
  267. }
  268. .ani_toMaxToMin_on {
  269. animation: ani_toMaxToMin_on 0.35s ease-in-out;
  270. }
  271. @keyframes ani_toMaxToMin_on {
  272. 0% {
  273. transform: scale(0.3);
  274. opacity:0.7;
  275. }
  276. 50% {
  277. transform: scale(1.5)
  278. }
  279. 100% {
  280. transform: scale(1);
  281. opacity:1;
  282. }
  283. }
  284. @keyframes ani {
  285. 0% {
  286. transform: scale(0.9)
  287. }
  288. 50% {
  289. transform: scale(1.1)
  290. }
  291. 100% {
  292. transform: scale(0.9)
  293. }
  294. }
  295. }
  296. </style>