You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

112 line
2.1 KiB

  1. <template>
  2. <l-label @click="click" :arrow="!readonly" :required="required" :title="title" :class="className" :style="style">
  3. {{ display || displayPlaceholder }}
  4. <l-modal
  5. v-if="modal"
  6. @ok="confirm"
  7. @cancel="cancel"
  8. @checkboxChange="checkboxChange"
  9. :checkbox="innerChecked"
  10. :value="modalOpen"
  11. :range="range"
  12. :readonly="readonly"
  13. type="checkbox"
  14. />
  15. </l-label>
  16. </template>
  17. <script>
  18. export default {
  19. name: 'l-checkbox-picker',
  20. props: {
  21. value: {},
  22. range: { default: () => [] },
  23. title: {},
  24. required: {},
  25. placeholder: {},
  26. readonly: {}
  27. },
  28. data() {
  29. return {
  30. modal: false,
  31. modalOpen: false,
  32. innerChecked: this.value || []
  33. }
  34. },
  35. methods: {
  36. click() {
  37. if (this.readonly || this.modal || this.modalOpen) {
  38. return
  39. }
  40. this.innerChecked = this.value
  41. this.modal = true
  42. setTimeout(() => {
  43. this.modalOpen = true
  44. this.$emit('open')
  45. }, 300)
  46. },
  47. checkboxChange(newVal) {
  48. this.innerChecked = newVal
  49. },
  50. confirm() {
  51. this.modalOpen = false
  52. setTimeout(() => {
  53. this.modal = false
  54. this.$emit('input', this.innerChecked)
  55. this.$emit('change', this.innerChecked)
  56. this.$emit('close')
  57. }, 300)
  58. },
  59. cancel() {
  60. this.modalOpen = false
  61. setTimeout(() => {
  62. this.modal = false
  63. this.$emit('close')
  64. }, 300)
  65. }
  66. },
  67. computed: {
  68. display() {
  69. if (this.value.length <= 0) {
  70. return null
  71. }
  72. if (!this.objMode) {
  73. return this.value.join(',')
  74. }
  75. return this.value
  76. .reduce((a, b) => {
  77. const selected = this.range.find(t => t.value === b)
  78. return selected ? a.concat(selected.text) : a
  79. }, [])
  80. .join(',')
  81. },
  82. displayPlaceholder() {
  83. if (this.readonly) {
  84. return ''
  85. }
  86. if (this.placeholder) {
  87. return this.placeholder
  88. }
  89. return this.title ? `请选择${this.title}` : '请选择…'
  90. },
  91. objMode() {
  92. return typeof this.range[0] === 'object'
  93. }
  94. }
  95. }
  96. </script>