fs-collapse.vue 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <template>
  2. <view class="fs-collapse"><slot></slot></view>
  3. </template>
  4. <script>
  5. /**
  6. * 折叠面板组件
  7. * @description 折叠面板组件
  8. * @property {Boolean} accordion 手风琴效果
  9. * @property {Number, String} active 激活项name
  10. * @property {String} position 箭头位置
  11. * @property {Number, String} rotate 旋转
  12. * @property {String} activeColor 高亮颜色
  13. * @property {String} activeType 高亮颜色类型
  14. * @property {Boolean} allOpen 是否全部展开
  15. * @property {Boolean} border 是否显示边框
  16. * @event {Function} change change事件
  17. */
  18. export default {
  19. name: 'fs-collapse'
  20. }
  21. </script>
  22. <script setup>
  23. import { computed, provide, watch, reactive, onMounted, getCurrentInstance } from 'vue'
  24. const props = defineProps({
  25. accordion: Boolean,
  26. active: {
  27. type: [Number, String],
  28. default: '0'
  29. },
  30. position: {
  31. type: String,
  32. default: 'right',
  33. validator(value) {
  34. return ['left', 'right'].includes(value)
  35. }
  36. },
  37. rotate: {
  38. type: [Number, String],
  39. default: 90
  40. },
  41. activeType: String,
  42. activeColor: String,
  43. allOpen: Boolean,
  44. border: {
  45. type: Boolean,
  46. default: true
  47. }
  48. })
  49. const emits = defineEmits(['change'])
  50. const emitEvent = name => {
  51. setActive(name)
  52. emits('change', name)
  53. }
  54. const children = reactive([])
  55. const setActive = name => {
  56. children.forEach(item => {
  57. if (props.accordion) {
  58. item.setActive(name === item.name ? !item.open : false)
  59. } else {
  60. if (name === item.name) {
  61. item.setActive(!item.open)
  62. }
  63. }
  64. })
  65. }
  66. provide(
  67. 'collapse',
  68. reactive({
  69. ...props,
  70. children,
  71. emitEvent
  72. })
  73. )
  74. watch(
  75. () => props.active,
  76. value => {
  77. setActive(value)
  78. }
  79. )
  80. defineExpose({
  81. children
  82. })
  83. </script>
  84. <style lang="scss" scoped>
  85. .fs-collapse {
  86. // background-color: #fff;
  87. }
  88. </style>