index.vue 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <template>
  2. <u-checkbox-group class="jnpf-checkbox" :disabled='disabled' :wrap="direction == 'horizontal' ? false : true"
  3. @change="onChange">
  4. <u-checkbox v-model="item.checked" v-for="(item, index) in optionList" :key="index" :name="item[props.value]"
  5. :class="{'jnpf-checkbox-disabled':disabled}">
  6. {{item[props.label]}}
  7. </u-checkbox>
  8. </u-checkbox-group>
  9. </template>
  10. <script>
  11. export default {
  12. name: 'jnpf-checkbox',
  13. inheritAttrs: false,
  14. props: {
  15. modelValue: {
  16. type: Array,
  17. default: () => []
  18. },
  19. direction: {
  20. type: String,
  21. default: "horizontal"
  22. },
  23. options: {
  24. type: Array,
  25. default: () => []
  26. },
  27. props: {
  28. type: Object,
  29. default: () => ({
  30. label: 'fullName',
  31. value: 'id'
  32. })
  33. },
  34. disabled: {
  35. type: Boolean,
  36. default: false
  37. }
  38. },
  39. data() {
  40. return {
  41. optionList: []
  42. }
  43. },
  44. watch: {
  45. modelValue: {
  46. handler(val) {
  47. if (!val || !val?.length) return this.setColumnData()
  48. this.setDefault()
  49. },
  50. immediate: true,
  51. },
  52. options: {
  53. handler(val) {
  54. this.setColumnData()
  55. },
  56. immediate: true,
  57. }
  58. },
  59. methods: {
  60. setDefault() {
  61. if (!this.modelValue || !this.modelValue?.length) return
  62. outer: for (let i = 0; i < this.modelValue.length; i++) {
  63. inner: for (let j = 0; j < this.optionList.length; j++) {
  64. if (this.modelValue[i] === this.optionList[j][this.props.value]) {
  65. this.optionList[j].checked = true
  66. break inner
  67. }
  68. }
  69. }
  70. },
  71. setColumnData() {
  72. this.optionList = this.options.map(o => ({
  73. ...o,
  74. checked: false
  75. }))
  76. this.setDefault()
  77. },
  78. onChange(value) {
  79. const selectData = this.optionList.filter(o => o.checked) || []
  80. this.$emit('update:modelValue', value)
  81. this.$emit('change', value, selectData)
  82. },
  83. }
  84. }
  85. </script>
  86. <style lang="scss" scoped>
  87. :deep(.u-checkbox__icon-wrap--square) {
  88. border-color: #D9D9D9 !important;
  89. }
  90. .jnpf-checkbox-disabled {
  91. :deep(.u-checkbox__icon-wrap--disabled) {
  92. background-color: #E6E6E6 !important;
  93. border-color: #D9D9D9 !important;
  94. }
  95. }
  96. </style>