-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(react-components): Add filter command and make mocks for the set…
…tings commands. (#4688) * Initial commit * Update according to review
- Loading branch information
1 parent
b755327
commit 3871a35
Showing
28 changed files
with
1,187 additions
and
162 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
126 changes: 126 additions & 0 deletions
126
react-components/src/architecture/base/commands/BaseFilterCommand.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 |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/*! | ||
* Copyright 2024 Cognite AS | ||
*/ | ||
|
||
import { RenderTargetCommand } from './RenderTargetCommand'; | ||
import { type TranslateDelegate } from '../utilities/TranslateKey'; | ||
import { type Color } from 'three'; | ||
import { type BaseCommand } from './BaseCommand'; | ||
|
||
export abstract class BaseFilterCommand extends RenderTargetCommand { | ||
// ================================================== | ||
// INSTANCE FIELDS/PROPERTIES | ||
// ================================================== | ||
|
||
protected _children: BaseFilterItemCommand[] | undefined = undefined; | ||
|
||
public get children(): BaseFilterItemCommand[] | undefined { | ||
return this._children; | ||
} | ||
|
||
public get hasChildren(): boolean { | ||
return this._children !== undefined && this._children.length > 0; | ||
} | ||
|
||
// ================================================== | ||
// OVERRIDES | ||
// ================================================== | ||
|
||
public override get icon(): string { | ||
return 'Filter'; | ||
} | ||
|
||
protected override *getChildren(): Generator<BaseCommand> { | ||
if (this._children === undefined) { | ||
return; | ||
} | ||
for (const child of this._children) { | ||
yield child; | ||
} | ||
} | ||
|
||
// ================================================== | ||
// VIRTUAL METHODS (To be overridden) | ||
// ================================================== | ||
|
||
public initializeChildrenIfNeeded(): void { | ||
if (this._children !== undefined) { | ||
return; | ||
} | ||
this._children = this.createChildren(); | ||
this.attachChildren(); | ||
} | ||
|
||
protected abstract createChildren(): BaseFilterItemCommand[]; | ||
|
||
/** | ||
* Checks if all the children of the current instance are checked. | ||
* Override this method to optimize the logic. | ||
* @returns A boolean value indicating whether all the children are checked. | ||
*/ | ||
public get isAllChecked(): boolean { | ||
if (this._children === undefined || this._children.length === 0) { | ||
return false; | ||
} | ||
for (const child of this._children) { | ||
if (!child.isChecked) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* Toggles the checked state of all child filter items. | ||
* Override this method to optimize the logic. | ||
* If there are no child items, this method does nothing. | ||
*/ | ||
public toggleAllChecked(): void { | ||
if (this._children === undefined || this._children.length === 0) { | ||
return; | ||
} | ||
const isAllChecked = this.isAllChecked; | ||
for (const child of this._children) { | ||
child.setChecked(!isAllChecked); | ||
} | ||
} | ||
|
||
// ================================================== | ||
// INSTANCE METHODS | ||
// ================================================== | ||
|
||
public getSelectedLabel(translate: TranslateDelegate): string { | ||
if (this._children === undefined) { | ||
return this.getNoneLabel(translate); | ||
} | ||
const selected = this._children.filter((child) => child.isChecked); | ||
const counter = selected.length; | ||
if (counter === 0) { | ||
return this.getNoneLabel(translate); | ||
} | ||
if (counter === this._children.length) { | ||
return this.getAllLabel(translate); | ||
} | ||
if (counter === 1) { | ||
return selected[0].getLabel(translate); | ||
} | ||
return counter.toString() + ' ' + this.getSelected(translate); | ||
} | ||
|
||
public getAllLabel(translate: TranslateDelegate): string { | ||
return translate('ALL', 'All'); | ||
} | ||
|
||
public getNoneLabel(translate: TranslateDelegate): string { | ||
return translate('NONE', 'None'); | ||
} | ||
|
||
public getSelected(translate: TranslateDelegate): string { | ||
return translate('SELECTED', 'Selected'); | ||
} | ||
} | ||
|
||
export abstract class BaseFilterItemCommand extends RenderTargetCommand { | ||
public abstract get color(): Color | undefined; | ||
public abstract setChecked(value: boolean): void; | ||
} |
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
32 changes: 32 additions & 0 deletions
32
react-components/src/architecture/base/commands/mocks/MockActionCommand.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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/*! | ||
* Copyright 2024 Cognite AS | ||
*/ | ||
|
||
import { type TranslateKey } from '../../utilities/TranslateKey'; | ||
import { RenderTargetCommand } from '../RenderTargetCommand'; | ||
|
||
export class MockActionCommand extends RenderTargetCommand { | ||
// ================================================== | ||
// OVERRIDES | ||
// ================================================== | ||
|
||
public override get tooltip(): TranslateKey { | ||
return { fallback: 'Action' }; | ||
} | ||
|
||
public override get icon(): string { | ||
return 'Sun'; | ||
} | ||
|
||
public override get shortCutKey(): string | undefined { | ||
return 'A'; | ||
} | ||
|
||
public override get shortCutKeyOnCtrl(): boolean { | ||
return true; | ||
} | ||
|
||
protected override invokeCore(): boolean { | ||
return true; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
react-components/src/architecture/base/commands/mocks/MockCheckableCommand.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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/*! | ||
* Copyright 2024 Cognite AS | ||
*/ | ||
|
||
import { type TranslateKey } from '../../utilities/TranslateKey'; | ||
import { RenderTargetCommand } from '../RenderTargetCommand'; | ||
|
||
export class MockCheckableCommand extends RenderTargetCommand { | ||
public value = false; | ||
// ================================================== | ||
// OVERRIDES | ||
// ================================================== | ||
|
||
public override get tooltip(): TranslateKey { | ||
return { fallback: 'Checkable action' }; | ||
} | ||
|
||
public override get icon(): string { | ||
return 'Snow'; | ||
} | ||
|
||
public override get isChecked(): boolean { | ||
return this.value; | ||
} | ||
|
||
protected override invokeCore(): boolean { | ||
this.value = !this.value; | ||
return true; | ||
} | ||
} |
Oops, something went wrong.