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.
 
 
 
 
 
 

115 lines
2.2 KiB

  1. <template>
  2. <l-label @click="click" :arrow="!readonly" :required="required" :title="title">
  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. uni.$emit('modal', true)
  45. this.$emit('open')
  46. }, 300)
  47. },
  48. checkboxChange(newVal) {
  49. this.innerChecked = newVal
  50. },
  51. confirm() {
  52. this.modalOpen = false
  53. setTimeout(() => {
  54. this.modal = false
  55. this.$emit('input', this.innerChecked)
  56. this.$emit('change', this.innerChecked)
  57. uni.$emit('modal', false)
  58. this.$emit('close')
  59. }, 300)
  60. },
  61. cancel() {
  62. this.modalOpen = false
  63. setTimeout(() => {
  64. this.modal = false
  65. uni.$emit('modal', false)
  66. this.$emit('close')
  67. }, 300)
  68. }
  69. },
  70. computed: {
  71. display() {
  72. if (this.value.length <= 0) {
  73. return null
  74. }
  75. if (!this.objMode) {
  76. return this.value.join(',')
  77. }
  78. return this.value
  79. .reduce((a, b) => {
  80. const selected = this.range.find(t => t.value === b)
  81. return selected ? a.concat(selected.text) : a
  82. }, [])
  83. .join(',')
  84. },
  85. displayPlaceholder() {
  86. if (this.readonly) {
  87. return ''
  88. }
  89. if (this.placeholder) {
  90. return this.placeholder
  91. }
  92. return this.title ? `请选择${this.title}` : '请选择…'
  93. },
  94. objMode() {
  95. return typeof this.range[0] === 'object'
  96. }
  97. }
  98. }
  99. </script>