tm-radio.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <template>
  2. <view @click="onclick" class=" tm-checkbox " :class="[dense?'':'pa-20',inline?'d-inline-block ':'fulled']">
  3. <view class="flex-start fulled">
  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. >
  12. <view :class="[changValue?'ani_toMaxToMin_on':'']" class="absolute flex-center" style="width: 100%;height: 100%;">
  13. <tm-icons dense v-show="model === 'normal'" :size="sizes.gou" :color="disabled?'opacity-5 white':'white'" :name="changValue?icon:' '"></tm-icons>
  14. <view v-show="model === 'round'&&changValue" class=" rounded d-inline-block"
  15. :class="[disabled?'opacity-5 white':'white']"
  16. :style="{width: sizes.yuan,height: sizes.yuan}"></view>
  17. </view>
  18. </view>
  19. </slot>
  20. <view v-if="label" :class="[black?'bk':'','px-10 ','flex-start fulled']" :style="{minHeight: sizes.wk}" class=" tm-checkbox-boey-label ">
  21. <view class="flex-center fulled-height ">
  22. <slot name="label" :label="{label:label,checked:changValue}">
  23. <text class=" text-size-n">{{label}}</text>
  24. </slot>
  25. </view>
  26. </view>
  27. </view>
  28. </view>
  29. </template>
  30. <script>
  31. /**
  32. * 单选框
  33. * @description 可以单独或者在tm-groupradio中使用。
  34. * @property {Boolean} value = [true|false] 如果想双向绑定需要value.snyc等同v-model。推荐v-model.
  35. * @property {Function} input 等同value.snyc和v-model和change
  36. * @property {Function} change 变化是会返回 {index,checked,value:name携带的数据}
  37. * @property {Boolean} disabled = [true|false] 默认false,禁用
  38. * @property {String} color = [primary|blue] 默认primary,主题色名称。
  39. * @property {String} border-color = [] 默认 '',边线主题色,默认同color可不填。
  40. * @property {String} model = [normal|round] 默认normal, 内部:normal打勾,round:内部为圆点
  41. * @property {String} icon = [icon-check] 默认icon-check,自定义选中时的图标。
  42. * @property {String|Number} round = [2|rounded] 默认2, 圆角,rounded时即圆形。
  43. * @property {String|Number} size = [] 默认32, 大小单位upx
  44. * @property {String|Boolean} dense = [true|false] 默认false, 是否去除外间隙。
  45. * @property {String} label = [] 默认"", 文右边显示的选项文字
  46. * @property {String|Boolean} black = [true|false] 默认false, 暗黑模式
  47. * @property {String|Boolean} inline = [true|false] 默认false, 是否内联模式
  48. * @property {String | Array | Object | Number} name = [] 默认 "", 选中时携带的自定义数据,会通过change带回。
  49. * @example <tm-radio v-model="checked" label="按个计算"></tm-radio>
  50. */
  51. import tmIcons from "@/tm-vuetify/components/tm-icons/tm-icons.vue"
  52. export default {
  53. components:{tmIcons},
  54. name:'tm-radio',
  55. model:{
  56. prop: 'value',
  57. event: 'input'
  58. },
  59. props:{
  60. //是否内联模式
  61. inline:{
  62. type:Boolean,
  63. default:true
  64. },
  65. // 禁用。
  66. disabled:Boolean,
  67. // 使用时::checked.sync 需要带sync
  68. value:Boolean,
  69. color:{
  70. type:String,
  71. default:'primary'
  72. },
  73. borderColor: {
  74. type: String,
  75. default: ''
  76. },
  77. // 内部:normal打勾,round:内部为圆点
  78. model:{
  79. type:String,
  80. default:'round'
  81. },
  82. // 自定义选中时的图标。
  83. icon:{
  84. type:String,
  85. default:'icon-check'
  86. },
  87. // 外部形状:== rounded时即圆形。
  88. round:{
  89. type:String|Number,
  90. default:'rounded'
  91. },
  92. // 单位upx
  93. size:{
  94. type:String|Number,
  95. default:38
  96. },
  97. // 是否去除外间隙。
  98. dense:{
  99. type:Boolean|String,
  100. default:false
  101. },
  102. // 名称。
  103. label:{
  104. type:String,
  105. default:''
  106. },
  107. black:{
  108. type:Boolean|String,
  109. default:false
  110. },
  111. name:{
  112. type:String|Array|Object|Number,
  113. default:''
  114. },
  115. // 跟随主题色的改变而改变。
  116. fllowTheme:{
  117. type:Boolean|String,
  118. default:true
  119. }
  120. },
  121. data() {
  122. return {
  123. };
  124. },
  125. mounted() {
  126. },
  127. watch:{
  128. value:function(newval,oldval){
  129. if(newval !== oldval){
  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. onclick(e){
  161. let t= this;
  162. if(this.disabled) return;
  163. this.changValue = true;
  164. },
  165. change() {
  166. let box = [];
  167. let selfIndex;
  168. let __uid = this._uid;
  169. let t = this;
  170. function findchild(p,index){
  171. let preat = p;
  172. if(preat.$options?.name==='tm-radio'){
  173. if(preat._uid!==__uid){
  174. if(preat.changValue===true && preat.changValue === t.changValue){
  175. preat.changValue = false;
  176. }
  177. }else if(preat._uid===__uid){
  178. box.push({index:index,name:preat.name,checked:preat.changValue})
  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 = this.$tm.getParentAls('tm-groupradio', this.$parent);
  190. // 如果不在tm-groupradio里面将不作反选。并且始终为true.
  191. if(preat){
  192. findchild(preat,0);
  193. t.$emit('change',{index:selfIndex,checked:t.changValue,name:t.name});
  194. preat.change(box)
  195. }else{
  196. this.$emit('change',{index:0,checked:this.changValue,name:this.name});
  197. }
  198. }
  199. },
  200. }
  201. </script>
  202. <style lang="scss" scoped>
  203. .tm-checkbox{
  204. vertical-align: middle;
  205. .tm-checkbox-boey,.tm-checkbox-boey-label{
  206. vertical-align: middle;
  207. }
  208. .ani {
  209. animation: ani 0.2s linear;
  210. }
  211. .ani_toMaxToMin_on {
  212. animation: ani_toMaxToMin_on 0.35s linear;
  213. }
  214. @keyframes ani_toMaxToMin_on {
  215. 0% {
  216. transform: scale(0.7);
  217. opacity:0.7;
  218. }
  219. 50% {
  220. transform: scale(1.5)
  221. }
  222. 100% {
  223. transform: scale(1);
  224. opacity:1;
  225. }
  226. }
  227. @keyframes ani {
  228. 0% {
  229. transform: scale(0.9)
  230. }
  231. 50% {
  232. transform: scale(1.1)
  233. }
  234. 100% {
  235. transform: scale(0.9)
  236. }
  237. }
  238. }
  239. </style>