config.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. // @ts-nocheck
  2. import { h } from 'vue'
  3. import ElCheckbox from '@element-plus/components/checkbox'
  4. import { ElIcon } from '@element-plus/components/icon'
  5. import { ArrowRight, Loading } from '@element-plus/icons-vue'
  6. import { getProp } from '@element-plus/utils'
  7. import type { VNode } from 'vue'
  8. import type { TableColumnCtx } from './table-column/defaults'
  9. import type { Store } from './store'
  10. import type { TreeNode } from './table/defaults'
  11. const defaultClassNames = {
  12. selection: 'table-column--selection',
  13. expand: 'table__expand-column',
  14. }
  15. export const cellStarts = {
  16. default: {
  17. order: '',
  18. },
  19. selection: {
  20. width: 48,
  21. minWidth: 48,
  22. realWidth: 48,
  23. order: '',
  24. },
  25. expand: {
  26. width: 48,
  27. minWidth: 48,
  28. realWidth: 48,
  29. order: '',
  30. },
  31. index: {
  32. width: 48,
  33. minWidth: 48,
  34. realWidth: 48,
  35. order: '',
  36. },
  37. }
  38. export const getDefaultClassName = (type) => {
  39. return defaultClassNames[type] || ''
  40. }
  41. // 这些选项不应该被覆盖
  42. export const cellForced = {
  43. selection: {
  44. renderHeader<T>({ store }: { store: Store<T> }) {
  45. function isDisabled() {
  46. return store.states.data.value && store.states.data.value.length === 0
  47. }
  48. return h(ElCheckbox, {
  49. disabled: isDisabled(),
  50. size: store.states.tableSize.value,
  51. indeterminate:
  52. store.states.selection.value.length > 0 &&
  53. !store.states.isAllSelected.value,
  54. 'onUpdate:modelValue': store.toggleAllSelection,
  55. modelValue: store.states.isAllSelected.value,
  56. })
  57. },
  58. renderCell<T>({
  59. row,
  60. column,
  61. store,
  62. $index,
  63. }: {
  64. row: T
  65. column: TableColumnCtx<T>
  66. store: Store<T>
  67. $index: string
  68. }) {
  69. return h(ElCheckbox, {
  70. disabled: column.selectable
  71. ? !column.selectable.call(null, row, $index)
  72. : false,
  73. size: store.states.tableSize.value,
  74. onChange: () => {
  75. store.commit('rowSelectedChanged', row)
  76. },
  77. onClick: (event: Event) => event.stopPropagation(),
  78. modelValue: store.isSelected(row),
  79. })
  80. },
  81. sortable: false,
  82. resizable: false,
  83. },
  84. index: {
  85. renderHeader<T>({ column }: { column: TableColumnCtx<T> }) {
  86. return column.label || '#'
  87. },
  88. renderCell<T>({
  89. column,
  90. $index,
  91. }: {
  92. column: TableColumnCtx<T>
  93. $index: number
  94. }) {
  95. let i = $index + 1
  96. const index = column.index
  97. if (typeof index === 'number') {
  98. i = $index + index
  99. } else if (typeof index === 'function') {
  100. i = index($index)
  101. }
  102. return h('div', {}, [i])
  103. },
  104. sortable: false,
  105. },
  106. expand: {
  107. renderHeader<T>({ column }: { column: TableColumnCtx<T> }) {
  108. return column.label || ''
  109. },
  110. renderCell<T>({
  111. row,
  112. store,
  113. expanded,
  114. }: {
  115. row: T
  116. store: Store<T>
  117. expanded: boolean
  118. }) {
  119. const { ns } = store
  120. const classes = [ns.e('expand-icon')]
  121. if (expanded) {
  122. classes.push(ns.em('expand-icon', 'expanded'))
  123. }
  124. const callback = function (e: Event) {
  125. e.stopPropagation()
  126. store.toggleRowExpansion(row)
  127. }
  128. return h(
  129. 'div',
  130. {
  131. class: classes,
  132. onClick: callback,
  133. },
  134. {
  135. default: () => {
  136. return [
  137. h(ElIcon, null, {
  138. default: () => {
  139. return [h(ArrowRight)]
  140. },
  141. }),
  142. ]
  143. },
  144. }
  145. )
  146. },
  147. sortable: false,
  148. resizable: false,
  149. },
  150. }
  151. export function defaultRenderCell<T>({
  152. row,
  153. column,
  154. $index,
  155. }: {
  156. row: T
  157. column: TableColumnCtx<T>
  158. $index: number
  159. }) {
  160. const property = column.property
  161. const value = property && getProp(row, property).value
  162. if (column && column.formatter) {
  163. return column.formatter(row, column, value, $index)
  164. }
  165. return value?.toString?.() || ''
  166. }
  167. export function treeCellPrefix<T>(
  168. {
  169. row,
  170. treeNode,
  171. store,
  172. }: {
  173. row: T
  174. treeNode: TreeNode
  175. store: Store<T>
  176. },
  177. createPlaceholder = false
  178. ) {
  179. const { ns } = store
  180. if (!treeNode) {
  181. if (createPlaceholder) {
  182. return [
  183. h('span', {
  184. class: ns.e('placeholder'),
  185. }),
  186. ]
  187. }
  188. return null
  189. }
  190. const ele: VNode[] = []
  191. const callback = function (e) {
  192. e.stopPropagation()
  193. if (treeNode.loading) {
  194. return
  195. }
  196. store.loadOrToggle(row)
  197. }
  198. if (treeNode.indent) {
  199. ele.push(
  200. h('span', {
  201. class: ns.e('indent'),
  202. style: { 'padding-left': `${treeNode.indent}px` },
  203. })
  204. )
  205. }
  206. if (typeof treeNode.expanded === 'boolean' && !treeNode.noLazyChildren) {
  207. const expandClasses = [
  208. ns.e('expand-icon'),
  209. treeNode.expanded ? ns.em('expand-icon', 'expanded') : '',
  210. ]
  211. let icon = ArrowRight
  212. if (treeNode.loading) {
  213. icon = Loading
  214. }
  215. ele.push(
  216. h(
  217. 'div',
  218. {
  219. class: expandClasses,
  220. onClick: callback,
  221. },
  222. {
  223. default: () => {
  224. return [
  225. h(
  226. ElIcon,
  227. { class: { [ns.is('loading')]: treeNode.loading } },
  228. {
  229. default: () => [h(icon)],
  230. }
  231. ),
  232. ]
  233. },
  234. }
  235. )
  236. )
  237. } else {
  238. ele.push(
  239. h('span', {
  240. class: ns.e('placeholder'),
  241. })
  242. )
  243. }
  244. return ele
  245. }