index.jsx 2.6 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. import { getUsingConfig } from '@cip/utils/util'
  6. import { defaultButtonConfigMap } from './config'
  7. import { generateProps, generateEmits } from '../helper/component-util'
  8. import { componentScheme } from './component.scheme'
  9. export default {
  10. name: 'CipButton',
  11. inheritAttrs: false,
  12. props: generateProps(componentScheme),
  13. emits: generateEmits(componentScheme),
  14. setup (props, context) {
  15. const cipConfig = useCipConfig()
  16. const buttonConfigMap = computed(() => { // 默认buttonConfig配置与cipConfig配置合并
  17. const result = {}
  18. const defaultKeys = Object.keys(defaultButtonConfigMap)
  19. defaultKeys.forEach(key => {
  20. const defaultButtonConfig = defaultButtonConfigMap[key]
  21. if (cipConfig.buttonConfigMap?.[key]) {
  22. result[key] = Object.assign({}, defaultButtonConfig, cipConfig.buttonConfigMap?.[key])
  23. } else {
  24. result[key] = defaultButtonConfig
  25. }
  26. })
  27. const globalButtonConfigMapKeys = Object.keys(cipConfig.buttonConfigMap || {})
  28. if (globalButtonConfigMapKeys.length > 0) {
  29. globalButtonConfigMapKeys
  30. .filter(key => !defaultKeys.includes(key))
  31. .forEach(key => {
  32. result[key] = cipConfig.buttonConfigMap[key]
  33. })
  34. }
  35. return result
  36. })
  37. const buttonConfig = computed(() => {
  38. return buttonConfigMap.value[props.buttonType] || {}
  39. })
  40. const type = computed(() => getUsingConfig(context.attrs.type, buttonConfig.value.type))
  41. const _icon = computed(() => getUsingConfig(props.icon, buttonConfig.value.icon))
  42. const _plain = computed(() => getUsingConfig(props.plain, buttonConfig.value.plain))
  43. return () => h(
  44. ElButton,
  45. {
  46. ...context.attrs,
  47. class: [
  48. 'cip-button',
  49. {
  50. 'cip-button--square': props.square,
  51. 'cip-button--map': props.map
  52. },
  53. context.attrs.class
  54. ],
  55. type: type.value, // type.value,
  56. plain: _plain.value,
  57. onClick: (...args) => context.emit('click', ...args)
  58. },
  59. {
  60. // 原型按钮和方型按钮不支持文字展示
  61. default: (context.attrs.circle || props.square)
  62. ? undefined
  63. : () => (context.slots.default?.({ text: buttonConfig.value.text ?? '' }) || buttonConfig.value.text),
  64. icon: !_icon.value
  65. ? undefined
  66. : () => typeof _icon.value === 'string'
  67. ? h('i', { class: _icon.value })
  68. : h(_icon.value)
  69. }
  70. )
  71. }
  72. }