index.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { computed, h } from 'vue'
  2. import { useCipConfig } from '../hooks/use-cip-config'
  3. import { ElButton } from 'element-plus'
  4. import './index.less'
  5. const defaultButtonConfigMap = {
  6. search: {
  7. type: 'primary',
  8. icon: 'el-icon-search',
  9. text: '搜索'
  10. },
  11. reset: {
  12. type: 'default',
  13. icon: 'el-icon-refresh-left',
  14. text: '重置'
  15. },
  16. create: {
  17. type: 'primary',
  18. icon: 'el-icon-plus',
  19. text: '新建'
  20. },
  21. batchDelete: {
  22. type: 'danger',
  23. icon: 'el-icon-delete',
  24. text: '批量删除'
  25. },
  26. export: {
  27. type: 'warning',
  28. icon: 'el-icon-download',
  29. text: '导出'
  30. }
  31. }
  32. export default {
  33. name: 'CipButton',
  34. props: {
  35. buttonType: String,
  36. size: {
  37. type: String,
  38. default: 'small'
  39. }
  40. },
  41. setup (props, context) {
  42. const cipConfig = useCipConfig()
  43. const buttonConfigMap = computed(() => { // 默认buttonConfig配置与cipConfig配置合并
  44. const result = {}
  45. const defaultKeys = Object.keys(defaultButtonConfigMap)
  46. defaultKeys.forEach(key => {
  47. const defaultButtonConfig = defaultButtonConfigMap[key]
  48. if (cipConfig.buttonConfigMap?.[key]) {
  49. result[key] = Object.assign({}, defaultButtonConfig, cipConfig.buttonConfigMap?.[key])
  50. } else {
  51. result[key] = defaultButtonConfig
  52. }
  53. })
  54. const globalButtonConfigMapKeys = Object.keys(cipConfig.buttonConfigMap || {})
  55. if (globalButtonConfigMapKeys.length > 0) {
  56. globalButtonConfigMapKeys
  57. .filter(key => !defaultKeys.includes(key))
  58. .forEach(key => {
  59. result[key] = cipConfig.buttonConfigMap[key]
  60. })
  61. }
  62. return result
  63. })
  64. const buttonConfig = computed(() => {
  65. return buttonConfigMap.value[props.buttonType] || {}
  66. })
  67. return () => h(ElButton, { ...context.attrs, class: 'cip-button', size: props.size, type: buttonConfig.value.type, icon: buttonConfig.value.icon }, {
  68. default: context.attrs.circle ? undefined : () => (context.slots.default?.({ text: buttonConfig.value.text ?? '' }) || buttonConfig.value.text)
  69. })
  70. }
  71. }