Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: set member default value to [] #45

Merged
merged 2 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions packages/grid/src/core/utils/field.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { FieldsMap } from '../constants/field';
import { AITable, AITableFieldType } from '../types';
import { AITable, AITableField, AITableFieldType } from '../types';
import { idCreator } from './id-creator';

export function getDefaultFieldValue(type: AITableFieldType) {
export function getDefaultFieldValue(field: AITableField) {
if ([AITableFieldType.member, AITableFieldType.createdBy, AITableFieldType.updatedBy].includes(field.type)) {
return [];
}
return '';
}

Expand Down
2 changes: 1 addition & 1 deletion packages/grid/src/core/utils/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export function getDefaultRecord(fields: AITableFields) {
values: {}
};
fields.map((item) => {
newRow.values[item._id] = getDefaultFieldValue(item.type);
newRow.values[item._id] = getDefaultFieldValue(item);
});
return newRow;
}
48 changes: 28 additions & 20 deletions packages/grid/src/grid.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@
@switch (field.type) {
@case (AITableFieldType.select) {
@if (!field.isMultiple && record.values[field._id] | selectOption: field['options']; as selectedOption) {
<thy-tag [thyColor]="selectedOption!.color!">{{ selectedOption.text }}</thy-tag>
<thy-tag [thyColor]="selectedOption!.color! || selectedOption!.bg_color! || 'default'">{{
selectedOption.text
}}</thy-tag>
}
}
@case (AITableFieldType.date) {
Expand Down Expand Up @@ -112,12 +114,14 @@
}
@case (AITableFieldType.member) {
@if (!field.isMultiple) {
<thy-avatar
[thyName]="record.values[field._id].display_name"
[thySrc]="record.values[field._id].avatar"
thySize="xs"
thyShowName="true"
></thy-avatar>
@if (record.values[field._id][0]; as recordValue) {
<thy-avatar
[thyName]="recordValue.display_name"
[thySrc]="recordValue.avatar"
thySize="xs"
thyShowName="true"
></thy-avatar>
}
} @else {
<thy-avatar-list thyAvatarSize="xs">
@for (item of record.values[field._id]; track $index) {
Expand All @@ -127,20 +131,24 @@
}
}
@case (AITableFieldType.createdBy) {
<thy-avatar
[thyName]="record.values[field._id].display_name"
[thySrc]="record.values[field._id].avatar"
thySize="xs"
thyShowName="true"
></thy-avatar>
@if (record.values[field._id][0]; as recordValue) {
<thy-avatar
[thyName]="recordValue.display_name"
[thySrc]="recordValue.avatar"
thySize="xs"
thyShowName="true"
></thy-avatar>
}
}
@case (AITableFieldType.updatedBy) {
<thy-avatar
[thyName]="record.values[field._id].display_name"
[thySrc]="record.values[field._id].avatar"
thySize="xs"
thyShowName="true"
></thy-avatar>
@if (record.values[field._id][0]; as recordValue) {
<thy-avatar
[thyName]="recordValue.display_name"
[thySrc]="recordValue.avatar"
thySize="xs"
thyShowName="true"
></thy-avatar>
}
}
@default {
<span class="text-truncate"> {{ record.values[field._id] }}</span>
Expand All @@ -157,4 +165,4 @@
</div>
</div>

<div #activeBorder class="active-border"></div>
<div #activeBorder class="active-border"></div>
12 changes: 2 additions & 10 deletions packages/grid/src/utils/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ import { createDraft, finishDraft } from 'immer';
import { AITableFields, AITableFieldType, AITableRecords, FieldsMap } from '../core';
import { AITableGridData, AITableReferences, AITableSelection, AITableUserInfo } from '../types';

export const buildGridData = (
recordValue: AITableRecords,
fieldsValue: AITableFields,
references?: AITableReferences
): AITableGridData => {
export const buildGridData = (recordValue: AITableRecords, fieldsValue: AITableFields, references?: AITableReferences): AITableGridData => {
const fields = fieldsValue.map((item) => {
return {
...item,
Expand Down Expand Up @@ -41,11 +37,7 @@ export function buildRecordsByReferences(records: AITableRecords, fields: AITabl
draftRecords.forEach((record) => {
memberFields.forEach((field) => {
const value = record.values[field._id];
if (field.isMultiple) {
record.values[field._id] = value.map((uid: string) => uidToMember[uid]).filter(Boolean);
} else {
record.values[field._id] = uidToMember[value] || {};
}
record.values[field._id] = value.map((uid: string) => uidToMember[uid]).filter(Boolean);
});
});
records = finishDraft(draftRecords);
Expand Down
2 changes: 1 addition & 1 deletion src/app/share/apply-to-yjs/add-field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default function addField(sharedType: SharedType, action: AddFieldAction)
const records = sharedType.get('records') as SyncArrayElement;
if (records) {
for (let value of records) {
const newRecord = getDefaultFieldValue(action.field.type);
const newRecord = getDefaultFieldValue(action.field);
const customField = value.get(1);
customField.insert(path, [newRecord]);
}
Expand Down
12 changes: 6 additions & 6 deletions src/app/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ export function getDefaultValue() {
url: 'https://www.baidu.com',
text: '百度链接'
},
'column-9': 'member_01',
'column-9': ['member_01'],
'column-10': 1682235946,
'column-11': 'member_02',
'column-11': ['member_02'],
'column-12': 1720490727
}
},
Expand All @@ -63,9 +63,9 @@ export function getDefaultValue() {
'column-6': 50,
'column-7': 1,
'column-8': {},
'column-9': 'member_01',
'column-9': ['member_01'],
'column-10': 1682235946,
'column-11': 'member_02',
'column-11': ['member_02'],
'column-12': 1720490727
}
},
Expand All @@ -84,9 +84,9 @@ export function getDefaultValue() {
'column-6': 100,
'column-7': 1,
'column-8': {},
'column-9': '',
'column-9': [],
'column-10': 1682235946,
'column-11': 'member_02',
'column-11': ['member_02'],
'column-12': 1720490727
}
}
Expand Down