fa-check-radio.vue 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <template>
  2. <view>
  3. <view class="" v-if="type == 'checkbox'">
  4. <u-checkbox-group @change="checkboxGroupChange" :width="radioCheckWidth" :wrap="radioCheckWrap">
  5. <u-checkbox v-model="item.checked" :active-color="theme.bgColor" v-for="(item, index) in list" :key="index" :name="item.value">
  6. {{ item.lable }}
  7. </u-checkbox>
  8. </u-checkbox-group>
  9. </view>
  10. <view class="" v-else>
  11. <u-radio-group v-model="radioValue" @change="radioGroupChange">
  12. <u-radio v-for="(item, index) in list" :active-color="theme.bgColor" :key="index" :name="item.value">{{ item.lable }}</u-radio>
  13. </u-radio-group>
  14. </view>
  15. </view>
  16. </template>
  17. <script>
  18. import Emitter from '@/uni_modules/uview-ui/libs/util/emitter.js';
  19. export default {
  20. name: 'fa-check-radio',
  21. mixins: [Emitter],
  22. props: {
  23. value:{
  24. type:[String,Number],
  25. default:''
  26. },
  27. faList: {
  28. type: [Object, Array],
  29. default: []
  30. },
  31. type: {
  32. type: String,
  33. default: 'checkbox'
  34. },
  35. checkValue: {
  36. type: [String, Array],
  37. default: ''
  38. }
  39. },
  40. watch: {
  41. faList: {
  42. immediate: true,
  43. handler(val) {
  44. let value = [];
  45. if (this.type == 'checkbox') {
  46. value = this.checkValue ? this.checkValue.split(',') : [];
  47. } else {
  48. this.radioValue = this.checkValue;
  49. }
  50. let list = [];
  51. for (let i in val) {
  52. list.push({
  53. lable: val[i],
  54. value: i,
  55. checked: value.indexOf(i) != -1
  56. });
  57. }
  58. this.list = list;
  59. }
  60. }
  61. },
  62. data() {
  63. return {
  64. radioCheckWidth: 'auto',
  65. radioCheckWrap: false,
  66. list: [],
  67. radioValue: ''
  68. };
  69. },
  70. methods: {
  71. checkboxGroupChange(e) {
  72. this.sendValue(e.join(','));
  73. },
  74. radioGroupChange(e) {
  75. this.sendValue(e);
  76. },
  77. sendValue(value){
  78. this.$emit('input', value);
  79. setTimeout(() => {
  80. this.dispatch('u-form-item', 'on-form-blur',value);
  81. }, 50);
  82. }
  83. }
  84. };
  85. </script>
  86. <style lang="scss"></style>