index.jsx 2.4 KB

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