Skip to content

Commit

Permalink
feat(state): support filter and sort by link field (#195)
Browse files Browse the repository at this point in the history
  • Loading branch information
minlovehua authored Dec 18, 2024
1 parent 2f0a8e0 commit fe6ef2e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
3 changes: 3 additions & 0 deletions packages/grid/src/core/types/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ export enum AITableSelectOptionStyle {

export type TextFieldValue = string;

export type LinkFieldValue = { url: string; text: string };

export type SelectFieldValue = Id[]; // 数字

export type NumberFieldValue = number;
Expand All @@ -112,6 +114,7 @@ export type RateFieldValue = 1 | 2 | 3 | 4 | 5;

export type FieldValue =
| TextFieldValue
| LinkFieldValue
| SelectFieldValue
| NumberFieldValue
| DateFieldValue
Expand Down
3 changes: 2 additions & 1 deletion packages/state/src/utils/field/model/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { SelectField } from './select';
import { DateField } from './date';
import { NumberField } from './number';
import { RateField } from './rate';
import { LinkField } from './link';

export const ViewOperationMap: Record<AITableFieldType, Field> = {
[AITableFieldType.text]: new TextField(),
Expand All @@ -15,7 +16,7 @@ export const ViewOperationMap: Record<AITableFieldType, Field> = {
[AITableFieldType.updatedAt]: new DateField(),
[AITableFieldType.number]: new NumberField(),
[AITableFieldType.rate]: new RateField(),
[AITableFieldType.link]: new TextField(),
[AITableFieldType.link]: new LinkField(),
[AITableFieldType.member]: new SelectField(),
[AITableFieldType.progress]: new NumberField(),
[AITableFieldType.createdBy]: new SelectField(),
Expand Down
42 changes: 42 additions & 0 deletions packages/state/src/utils/field/model/link.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { isEmpty } from '../../common';
import { AITableFilterCondition, AITableFilterOperation } from '../../../types';
import { Field } from './field';
import { AITableField, FieldValue, LinkFieldValue } from '@ai-table/grid';
import { isObject, isString } from 'ngx-tethys/util';

export class LinkField extends Field {
override isMeetFilter(condition: AITableFilterCondition<string>, cellValue: FieldValue) {
const cellTextValue = this.cellValueToString(cellValue);
switch (condition.operation) {
case AITableFilterOperation.empty:
return isEmpty(cellTextValue);
case AITableFilterOperation.exists:
return !isEmpty(cellTextValue);
case AITableFilterOperation.contain:
return !isEmpty(cellTextValue) && this.stringInclude(cellTextValue, condition.value);
default:
return super.isMeetFilter(condition, cellTextValue);
}
}

override eq(cv1: LinkFieldValue | string | null, cv2: LinkFieldValue | string | null): boolean {
return this.cellValueToString(cv1) === this.cellValueToString(cv2);
}

override compare(cellValue1: FieldValue, cellValue2: FieldValue, field: AITableField): number {
const cellTextValue1 = this.cellValueToString(cellValue1);
const cellTextValue2 = this.cellValueToString(cellValue2);

return super.compare(cellTextValue1, cellTextValue2, field);
}

cellValueToString(cellValue: LinkFieldValue | string | null): string {
if (isString(cellValue)) {
return cellValue;
}
if (isObject(cellValue)) {
return cellValue.text;
}
return '';
}
}

0 comments on commit fe6ef2e

Please sign in to comment.