-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #972 from 3YOURMIND/refactor-kotti-row-modifiers
refactor(KtTable): use Composition API for TableBody*Row components
- Loading branch information
Showing
10 changed files
with
336 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 26 additions & 28 deletions
54
packages/kotti-ui/source/kotti-table/components/TableBodyEmptyRow.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,30 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import type { CreateElement, VNode } from 'vue' | ||
import { KT_TABLE, KT_STORE, KT_LAYOUT } from '../constants' | ||
import { computed, defineComponent, h, inject } from 'vue' | ||
import { KT_TABLE } from '../constants' | ||
|
||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
export const TableBodyEmptyRow: any = { | ||
export const TableBodyEmptyRow = defineComponent({ | ||
name: 'TableBodyEmptyRow', | ||
inject: { KT_TABLE, KT_STORE, KT_LAYOUT }, | ||
render(h: CreateElement): VNode { | ||
const { colSpan, render } = this | ||
return h('tr', {}, [ | ||
h( | ||
'td', | ||
{ | ||
domProps: { colSpan }, | ||
class: 'kt-table__no-row', | ||
}, | ||
[h('span', { class: 'kt-table__empty-text' }, [render(h)])], | ||
), | ||
]) | ||
}, | ||
computed: { | ||
colSpan(): unknown { | ||
// @ts-expect-error `this[KT_TABLE]` seems to emulate a provide/inject pattern of sorts | ||
return this[KT_TABLE].colSpan | ||
}, | ||
render(): unknown { | ||
// @ts-expect-error `this[KT_TABLE]` seems to emulate a provide/inject pattern of sorts | ||
return this[KT_TABLE]._renderEmpty | ||
}, | ||
setup() { | ||
const tableState = inject(KT_TABLE) | ||
|
||
if (!tableState) | ||
throw new Error( | ||
'TableRowCell: Component was used without providing the right contexts', | ||
) | ||
|
||
const colSpan = computed(() => tableState.colSpan) | ||
const render = computed(() => tableState._renderEmpty) | ||
|
||
return () => | ||
h('tr', {}, [ | ||
h( | ||
'td', | ||
{ | ||
domProps: { colSpan: colSpan.value }, | ||
class: 'kt-table__no-row', | ||
}, | ||
[h('span', { class: 'kt-table__empty-text' }, [render.value(h)])], | ||
), | ||
]) | ||
}, | ||
} | ||
}) |
67 changes: 37 additions & 30 deletions
67
packages/kotti-ui/source/kotti-table/components/TableBodyExpandRow.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,50 @@ | ||
/* eslint-disable @typescript-eslint/naming-convention */ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import type { CreateElement, VNode } from 'vue' | ||
import { KT_TABLE, KT_STORE, KT_LAYOUT } from '../constants' | ||
import { computed, defineComponent, h, inject, type PropType } from 'vue' | ||
import { KT_TABLE, KT_STORE } from '../constants' | ||
import type { KottiTable } from '../types' | ||
|
||
export const TableBodyExpandRow: any = { | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
export const TableBodyExpandRow = defineComponent({ | ||
name: 'TableBodyExpandRow', | ||
props: { | ||
row: Object, | ||
rowIndex: Number, | ||
row: { | ||
type: Object as PropType<KottiTable.Row.Props>, | ||
required: true, | ||
}, | ||
rowIndex: { | ||
type: Number, | ||
required: true, | ||
}, | ||
}, | ||
inject: { KT_TABLE, KT_STORE, KT_LAYOUT }, | ||
render(h: CreateElement): VNode { | ||
const { isExpanded, row, rowIndex, colSpan, renderExpand } = this | ||
return ( | ||
isExpanded && | ||
setup(props) { | ||
const tableState = inject(KT_TABLE) | ||
const tableStore = inject(KT_STORE) | ||
|
||
if (!tableState || !tableStore) | ||
throw new Error( | ||
'TableRowCell: Component was used without providing the right contexts', | ||
) | ||
|
||
const colSpan = computed(() => tableState.colSpan) | ||
const isExpanded = computed(() => tableStore.get('isExpanded', props.row)) | ||
const render = computed(() => tableState._renderExpand) | ||
|
||
return () => | ||
isExpanded.value && | ||
h('tr', {}, [ | ||
h( | ||
'td', | ||
{ | ||
attrs: { colSpan }, | ||
attrs: { colSpan: colSpan.value }, | ||
class: 'kt-table__expanded-cell', | ||
}, | ||
[renderExpand?.(h, { row, data: row, rowIndex })], | ||
[ | ||
render.value(h, { | ||
row: props.row, | ||
data: props.row, | ||
rowIndex: props.rowIndex, | ||
}), | ||
], | ||
), | ||
]) | ||
) | ||
}, | ||
computed: { | ||
colSpan(): unknown { | ||
// @ts-expect-error `this[KT_TABLE]` seems to emulate a provide/inject pattern of sorts | ||
return this[KT_TABLE].colSpan | ||
}, | ||
isExpanded(): unknown { | ||
// @ts-expect-error `this[KT_STORE]` seems to emulate a provide/inject pattern of sorts | ||
return this[KT_STORE].get('isExpanded', this.row) | ||
}, | ||
renderExpand(): unknown { | ||
// @ts-expect-error `this[KT_TABLE]` seems to emulate a provide/inject pattern of sorts | ||
return this[KT_TABLE]._renderExpand | ||
}, | ||
}, | ||
} | ||
}) |
52 changes: 25 additions & 27 deletions
52
packages/kotti-ui/source/kotti-table/components/TableBodyLoadingRow.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,31 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import type { CreateElement } from 'vue' | ||
import { KT_TABLE, KT_STORE, KT_LAYOUT } from '../constants' | ||
import { computed, defineComponent, h, inject } from 'vue' | ||
import { KT_TABLE } from '../constants' | ||
|
||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
export const TableBodyLoadingRow: any = { | ||
export const TableBodyLoadingRow: any = defineComponent({ | ||
name: 'TableBodyLoadingRow', | ||
inject: { KT_TABLE, KT_STORE, KT_LAYOUT }, | ||
computed: { | ||
colSpan(): unknown { | ||
// @ts-expect-error `this[KT_TABLE]` seems to emulate a provide/inject pattern of sorts | ||
return this[KT_TABLE].colSpan | ||
}, | ||
render(): unknown { | ||
// @ts-expect-error `this[KT_TABLE]` seems to emulate a provide/inject pattern of sorts | ||
return this[KT_TABLE]._renderLoading | ||
}, | ||
}, | ||
render(h: CreateElement) { | ||
const { colSpan, render } = this | ||
setup() { | ||
const tableState = inject(KT_TABLE) | ||
|
||
if (!tableState) | ||
throw new Error( | ||
'TableRowCell: Component was used without providing the right contexts', | ||
) | ||
|
||
const colSpan = computed(() => tableState.colSpan) | ||
const render = computed(() => tableState._renderLoading) | ||
|
||
return h('tr', {}, [ | ||
h( | ||
'td', | ||
{ | ||
attrs: { colSpan }, | ||
class: 'kt-table__loader', | ||
}, | ||
[render(h)], | ||
), | ||
]) | ||
return () => | ||
h('tr', {}, [ | ||
h( | ||
'td', | ||
{ | ||
attrs: { colSpan: colSpan.value }, | ||
class: 'kt-table__loader', | ||
}, | ||
[render.value(h)], | ||
), | ||
]) | ||
}, | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.