Skip to content

Commit ac30492

Browse files
committed
Replace toRaw with deepToRaw to avoid some potential errors.
1 parent 4fafa86 commit ac30492

File tree

8 files changed

+39
-32
lines changed

8 files changed

+39
-32
lines changed

docs/TableDemo.vue

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<script setup lang="ts">
2-
import type { Ref } from 'vue';
3-
import { onMounted, ref, toRaw } from 'vue';
4-
import { IwEvents, IwProps, IwUtils } from '../src';
5-
import type { EditableDataResp } from '../src/props';
2+
import type { Ref } from 'vue'
3+
import { onMounted, ref } from 'vue'
4+
import { IwEvents, IwProps, IwUtils } from '../src'
5+
import type { EditableDataResp } from '../src/props'
6+
import { deepToRaw } from '../src/utils/vueHelper'
67
78
const selectedRecordPks: Ref<any[]> = ref([])
89
@@ -69,7 +70,7 @@ function attachDict(data: { [columnName: string]: any }[]) {
6970
7071
const events: IwProps.TableEventProps = {
7172
loadData: async (quickSearchContent?: string, filter?: IwProps.FilterDataProps, sort?: IwProps.SortDataProps, group?: IwProps.GroupDataProps, agg?: IwProps.AggDataProps, hideSubData?: boolean, byGroupValue?: any, slice?: IwProps.DataQuerySliceReq, returnColumnNames?: string[], _returnOnlyAgg?: boolean): Promise<IwProps.DataResp | IwProps.DataGroupResp[]> => {
72-
let data: { [columnName: string]: any }[] = toRaw(DATA)
73+
let data: { [columnName: string]: any }[] = deepToRaw(DATA)
7374
if (quickSearchContent) {
7475
data = data.filter((d) => {
7576
return d.name.includes(quickSearchContent)

src/components/eventbus.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type { Ref } from 'vue'
2-
import { toRaw } from 'vue'
32
import locales from '../locales'
43

54
import type { DataGroupResp, DataQuerySliceReq, DataResp, DictItemsResp, EditableDataResp, LayoutModifyProps, SimpleLayoutProps, TableStyleModifyProps } from '../props'
@@ -45,7 +44,7 @@ export async function watch() {
4544
export async function loadData(byGroupValue?: any, returnOnlyAgg?: boolean, layoutId?: string) {
4645
const layout = layoutId ? layoutsConf.find(layout => layout.id === layoutId)! : layoutsConf.find(layout => layout.id === currentLayoutId.value)!
4746

48-
const rawLayout = toRaw(layout)
47+
const rawLayout = deepToRaw(layout)
4948

5049
const showColumns = rawLayout.columns.filter(column => !column.hide).map(column => column.name).slice()
5150
if (tableConf.parentPkColumnName && rawLayout.columns.findIndex(column => column.name === tableConf.parentPkColumnName) === -1) {
@@ -153,7 +152,7 @@ export async function loadData(byGroupValue?: any, returnOnlyAgg?: boolean, layo
153152
* @param newRecords 要新建的数据 / Records to be created
154153
*/
155154
export async function newData(newRecords: { [columnName: string]: any }[]) {
156-
newRecords = toRaw(newRecords)
155+
newRecords = deepToRaw(newRecords)
157156
const layout = layoutsConf.find(layout => layout.id === currentLayoutId.value)!
158157

159158
if (!events.newData) {
@@ -182,7 +181,7 @@ export async function newData(newRecords: { [columnName: string]: any }[]) {
182181
* @param targetRecordPks 要复制的数据主键 / Data primary keys to be copied
183182
*/
184183
export async function copyData(targetRecordPks: any[]) {
185-
targetRecordPks = toRaw(targetRecordPks)
184+
targetRecordPks = deepToRaw(targetRecordPks)
186185
const layout = layoutsConf.find(layout => layout.id === currentLayoutId.value)!
187186

188187
if (!events.copyData) {
@@ -211,7 +210,7 @@ export async function copyData(targetRecordPks: any[]) {
211210
* @param changedRecords 要修改的数据 / Data to be modified
212211
*/
213212
export async function modifyData(changedRecords: { [columnName: string]: any }[]) {
214-
changedRecords = toRaw(changedRecords)
213+
changedRecords = deepToRaw(changedRecords)
215214

216215
const layout = layoutsConf.find(layout => layout.id === currentLayoutId.value)!
217216

@@ -243,7 +242,7 @@ export async function modifyData(changedRecords: { [columnName: string]: any }[]
243242
* @param deletedRecordPks 要删除的数据主键 / Data primary keys to be deleted
244243
*/
245244
export async function deleteData(deletedRecordPks: any[]) {
246-
deletedRecordPks = toRaw(deletedRecordPks)
245+
deletedRecordPks = deepToRaw(deletedRecordPks)
247246

248247
const layout = layoutsConf.find(layout => layout.id === currentLayoutId.value)!
249248

@@ -273,7 +272,7 @@ export async function deleteData(deletedRecordPks: any[]) {
273272
* @param checkRecordPks 要检查的数据主键 / Data primary keys to be checked
274273
*/
275274
export async function loadEditableData(checkRecordPks: any[]): Promise<EditableDataResp> {
276-
checkRecordPks = toRaw(checkRecordPks)
275+
checkRecordPks = deepToRaw(checkRecordPks)
277276

278277
const layout = layoutsConf.find(layout => layout.id === currentLayoutId.value)!
279278

@@ -299,7 +298,7 @@ export async function loadEditableData(checkRecordPks: any[]): Promise<EditableD
299298
* @param selectedRecordPks 选择的数据主键 / Selected data primary keys
300299
*/
301300
export async function selectData(selectedRecordPks: any[]) {
302-
selectedRecordPks = toRaw(selectedRecordPks)
301+
selectedRecordPks = deepToRaw(selectedRecordPks)
303302

304303
const layout = layoutsConf.find(layout => layout.id === currentLayoutId.value)!
305304

@@ -354,7 +353,7 @@ export async function clickCell(clickedRecordPk: any, clickedColumnName: string)
354353
* @returns 字典项列表 / Dictionary item list
355354
*/
356355
export async function loadCellDictItems(dictName: string, filterValue?: any, slice?: DataQuerySliceReq): Promise<DictItemsResp> {
357-
slice = toRaw(slice)
356+
slice = deepToRaw(slice)
358357

359358
const layout = layoutsConf.find(layout => layout.id === currentLayoutId.value)!
360359

@@ -381,8 +380,8 @@ export async function loadCellDictItems(dictName: string, filterValue?: any, sli
381380
* @returns 字典项列表 / Dictionary item list
382381
*/
383382
export async function loadCellDictItemsWithMultiConds(conds: { [columnName: string]: any[] }, slice?: DataQuerySliceReq): Promise<{ [columnName: string]: DictItemsResp }> {
384-
conds = toRaw(conds)
385-
slice = toRaw(slice)
383+
conds = deepToRaw(conds)
384+
slice = deepToRaw(slice)
386385

387386
const layout = layoutsConf.find(layout => layout.id === currentLayoutId.value)!
388387

@@ -407,7 +406,7 @@ export async function loadCellDictItemsWithMultiConds(conds: { [columnName: stri
407406
* @param changedStyleProps 修改的样式属性 / Modified style properties
408407
*/
409408
export async function modifyStyles(changedStyleProps: TableStyleModifyProps) {
410-
changedStyleProps = toRaw(changedStyleProps)
409+
changedStyleProps = deepToRaw(changedStyleProps)
411410

412411
const layout = layoutsConf.find(layout => layout.id === currentLayoutId.value)!
413412

@@ -525,9 +524,10 @@ export async function modifyLayout(changedLayoutProps: LayoutModifyProps, byGrou
525524

526525
changedLayoutProps.slice !== undefined && (layout.slice = changedLayoutProps.slice)
527526
changedLayoutProps.showSelectColumn !== undefined && (layout.showSelectColumn = changedLayoutProps.showSelectColumn)
527+
changedLayoutProps.actionColumn !== undefined && (layout.actionColumn = changedLayoutProps.actionColumn)
528+
528529
changedLayoutProps.subDataShowKind !== undefined && (layout.subDataShowKind = changedLayoutProps.subDataShowKind)
529530

530-
changedLayoutProps.actionColumn !== undefined && (layout.actionColumn = changedLayoutProps.actionColumn)
531531
changedLayoutProps.gantt !== undefined && (layout.gantt = changedLayoutProps.gantt)
532532
changedLayoutProps.filter !== undefined && (layout.filter = changedLayoutProps.filter)
533533
changedLayoutProps.group !== undefined && (layout.group = changedLayoutProps.group)

src/components/function/BasicSetting.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ const props = defineProps<{
1515
}>()
1616
const iconPickerCompRef = ref<InstanceType<typeof MenuComp>>()
1717
18-
async function renameLayoutTitle() {
18+
async function renameLayoutTitle(e: Event) {
1919
const layout: LayoutModifyProps = {
20-
title: props.layoutConf.title,
20+
title: (e.target as HTMLInputElement).value,
2121
}
2222
await eb.modifyLayout(layout)
2323
}
@@ -37,7 +37,7 @@ async function setLayoutIcon(icon: string) {
3737
class="cursor-pointer" @click="e => { iconPickerCompRef?.show(e, MenuOffsetKind.RIGHT_TOP, undefined, true) }"
3838
/>
3939
<input
40-
v-model="props.layoutConf.title" class="flex-1 iw-input iw-input-bordered iw-input-sm w-28" type="text"
40+
:value="props.layoutConf.title" class="flex-1 iw-input iw-input-bordered iw-input-sm w-28" type="text"
4141
:placeholder="$t('layout.title.rename')"
4242
@change="renameLayoutTitle"
4343
>

src/components/function/FilterSetting.vue

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script setup lang="ts">
2-
import { onMounted, ref, toRaw } from 'vue'
2+
import { onMounted, ref } from 'vue'
33
import * as iconSvg from '../../assets/icon'
44
import type { DataKind, DictItemProps, DictItemsResp, FilterDataGroupProps, FilterDataItemProps, FilterDataProps, LayoutModifyProps } from '../../props'
55
import { OperatorKind, translateOperatorKind } from '../../props'
@@ -9,6 +9,7 @@ import { MenuOffsetKind, MenuSizeKind } from '../common/Menu'
99
import MenuComp from '../common/Menu.vue'
1010
import type { ColumnConf } from '../conf'
1111
import * as eb from '../eventbus'
12+
import { deepToRaw } from '../../utils/vueHelper'
1213
1314
const props = defineProps<{
1415
// 布局ID
@@ -153,7 +154,7 @@ async function showFilterGroupContainer(e: Event, filterGroupIdx?: number) {
153154
154155
// 设置已选中的过滤项
155156
// Set the selected filter item
156-
const filterItems = toRaw(props.filter.groups[filterGroupIdx].items)
157+
const filterItems = deepToRaw(props.filter.groups[filterGroupIdx].items)
157158
selectedFilterItems.value = filterItems.map((item: FilterDataItemProps) => {
158159
return convertFilterDataItemToFilterItem(item)
159160
})
@@ -182,7 +183,7 @@ async function showFilterGroupContainer(e: Event, filterGroupIdx?: number) {
182183
* @param filterGroupIdx 过滤组ID / Filter group ID
183184
*/
184185
async function deleteFilterGroup(filterGroupIdx: number) {
185-
const filter = toRaw(props.filter!)
186+
const filter = deepToRaw(props.filter!)
186187
filter.groups.splice(filterGroupIdx, 1)
187188
await eb.modifyLayout({
188189
filter,
@@ -439,7 +440,7 @@ async function saveFilterGroup() {
439440
if (currFilterGroup.items.length === 0) {
440441
return
441442
}
442-
const filterGroups = toRaw(props.filter.groups)
443+
const filterGroups = deepToRaw(props.filter.groups)
443444
if (selectedFilterGroupIdx.value === undefined) {
444445
filterGroups.push(currFilterGroup)
445446
}

src/components/function/Pagination.vue

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<script setup lang="ts">
2-
import { ref, toRaw } from 'vue'
2+
import { ref } from 'vue'
33
import * as iconSvg from '../../assets/icon'
44
import type { DataSliceProps, GroupDataProps, LayoutModifyProps } from '../../props'
5+
import { deepToRaw } from '../../utils/vueHelper'
56
import { MenuOffsetKind, MenuSizeKind } from '../common/Menu'
67
import MenuComp from '../common/Menu.vue'
78
import * as eb from '../eventbus'
@@ -132,7 +133,7 @@ async function setSlice(newPage?: number, newFetchNumber?: number) {
132133
fetchNumbers: props.slice.fetchNumbers,
133134
}
134135
}
135-
const groupSlices = toRaw(props.groupProps?.slices) ?? {}
136+
const groupSlices = deepToRaw(props.groupProps?.slices) ?? {}
136137
groupSlices![props.groupValue!] = newSlice
137138
const changedLayoutReq: LayoutModifyProps = {
138139
group: {

src/components/function/RowSortSetting.vue

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<script setup lang="ts">
22
import Sortable from 'sortablejs'
3-
import { onMounted, ref, toRaw } from 'vue'
3+
import { onMounted, ref } from 'vue'
44
import * as iconSvg from '../../assets/icon'
55
import type { SortDataProps } from '../../props'
6+
import { deepToRaw } from '../../utils/vueHelper'
67
import { MenuOffsetKind } from '../common/Menu'
78
import MenuComp from '../common/Menu.vue'
89
import type { ColumnConf } from '../conf'
@@ -33,7 +34,7 @@ onMounted(() => {
3334
async onEnd(evt) {
3435
if (evt.oldIndex !== evt.newIndex && evt.oldIndex !== -1 && evt.newIndex !== -1) {
3536
const oriSort = props.sort.items[evt.oldIndex! - 1]
36-
const sort = toRaw(props.sort)
37+
const sort = deepToRaw(props.sort)
3738
sort.items.splice(evt.oldIndex! - 1, 1)
3839
sort.items.splice(evt.newIndex! - 1, 0, oriSort)
3940
await eb.modifyLayout({

src/components/layout/list/ListColumnAgg.vue

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
<script setup lang="ts">
2-
import { h, ref, toRaw } from 'vue'
2+
import { h, ref } from 'vue'
33
import type { AggDataProps, AggregateKind, TableStyleProps } from '../../../props'
44
import { showAggMappingByDataKind, translateAggregateKind } from '../../../props'
55
import { MenuOffsetKind, MenuSizeKind } from '../../common/Menu'
66
import MenuComp from '../../common/Menu.vue'
77
import type { ColumnConf } from '../../conf'
88
import * as eb from '../../eventbus'
9+
import { deepToRaw } from '../../../utils/vueHelper'
910
1011
const props = defineProps<{
1112
// 布局ID
@@ -72,7 +73,7 @@ function showAggsContextMenu(e: MouseEvent, colIdx: number) {
7273
}
7374
7475
async function changeColumnAggs(aggKind: AggregateKind, columnName: string) {
75-
const agg = toRaw(props.agg)
76+
const agg = deepToRaw(props.agg)
7677
const idx = agg.items.findIndex(item => item.columnName === columnName)
7778
if (idx === -1) {
7879
agg.items.push({ columnName, aggKind })

src/utils/vueHelper.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ import {
1515
* @param sourceObj Vue的引用对象 / Vue reference object
1616
* @returns 原始对象 / Original object
1717
*/
18-
export function deepToRaw<T extends Record<string, any>>(sourceObj: T): T {
18+
export function deepToRaw<T extends Record<string, any>>(sourceObj?: T): T {
19+
if (sourceObj === undefined)
20+
return {} as T
1921
const objectIterator = (input: any): any => {
2022
if (Array.isArray(input)) {
2123
return input.map(item => objectIterator(item))

0 commit comments

Comments
 (0)