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

feat(state): support filter by link field #192

Closed
wants to merge 2 commits into from
Closed
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
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
34 changes: 34 additions & 0 deletions packages/state/src/utils/field/model/link.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { isEmpty } from '../../common';
import { AITableFilterCondition, AITableFilterOperation } from '../../../types';
import { Field } from './field';
import { AITableField, FieldValue, LinkFieldValue } from '@ai-table/grid';

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 | null, cv2: LinkFieldValue | 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 | null): string {
return cellValue?.text || '';
}
}
Loading