index.jsx 2.5 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. 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. return () => h(
  43. ElButton,
  44. {
  45. ...context.attrs,
  46. class: [
  47. 'cip-button',
  48. {
  49. 'cip-button--square': props.square,
  50. 'cip-button--map': props.map
  51. },
  52. context.attrs.class
  53. ],
  54. type: type.value, // type.value,
  55. onClick: (...args) => context.emit('click', ...args)
  56. },
  57. {
  58. // 原型按钮和方型按钮不支持文字展示
  59. default: (context.attrs.circle || props.square)
  60. ? undefined
  61. : () => (context.slots.default?.({ text: buttonConfig.value.text ?? '' }) || buttonConfig.value.text),
  62. icon: !_icon.value
  63. ? undefined
  64. : () => typeof _icon.value === 'string'
  65. ? h('i', { class: _icon.value })
  66. : h(_icon.value)
  67. }
  68. )
  69. }
  70. }