index.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import { computed, h, inject } from 'vue'
  2. import { ElButton } from 'element-plus'
  3. const defaultButtonConfigMap = {
  4. search: {
  5. type: 'primary',
  6. icon: 'el-icon-search',
  7. text: '搜索'
  8. },
  9. reset: {
  10. type: 'warning',
  11. icon: 'el-icon-refresh-left',
  12. text: '重置'
  13. },
  14. create: {
  15. type: 'primary',
  16. icon: 'el-icon-plus',
  17. text: '新建'
  18. },
  19. batchDelete: {
  20. type: 'danger',
  21. icon: 'el-icon-delete',
  22. text: '批量删除'
  23. }
  24. }
  25. export default {
  26. name: 'CipButton',
  27. props: {
  28. buttonType: String,
  29. size: {
  30. type: String,
  31. default: 'small'
  32. }
  33. },
  34. setup (props, context) {
  35. const cipConfig = inject('cip-config', {})
  36. const buttonConfigMap = computed(() => { // 默认buttonConfig配置与cipConfig配置合并
  37. const result = {}
  38. Object.keys(defaultButtonConfigMap).forEach(key => {
  39. const defaultButtonConfig = defaultButtonConfigMap[key]
  40. if (cipConfig.buttonConfigMap?.[key]) {
  41. result[key] = Object.assign({}, defaultButtonConfig, cipConfig.buttonConfigMap?.[key])
  42. } else {
  43. result[key] = defaultButtonConfig
  44. }
  45. })
  46. return result
  47. })
  48. const buttonConfig = computed(() => {
  49. return buttonConfigMap.value[props.buttonType] || {}
  50. })
  51. return () => h(ElButton, { ...context.attrs, size: props.size, type: buttonConfig.value.type, icon: buttonConfig.value.icon }, {
  52. default: () => (context.slots.default?.({ text: buttonConfig.value.text ?? '' }) || buttonConfig.value.text)
  53. })
  54. }
  55. }