-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor to create base classes for table columns that display text (#…
…1239) # Pull Request ## 🤨 Rationale As part of the vision described in #1224 in service of #1011 and other column types, we will want to share rendering logic across columns that display their data as text. This could eventually include: - the existing text column - the mapping column discussed in #1220 - columns that display numbers, dates, elapsed time, and more - columns defined in applications with custom formatting logic ## 👩💻 Implementation 1. Extract abstract base classes, templates, and styles from the existing text column for: - the column custom element - the cell view custom element - the group header custom element These live in `table-column/text-base`. 2. Derive concrete instances and register custom elements of each of these for the `text` column 3. Define and use some table field types to provide stricter typing some generic parameters ## 🧪 Testing Relying on existing tests. We have a future task to add docs/examples/tests for applications deriving from these base classes. ## ✅ Checklist <!--- Review the list and put an x in the boxes that apply or ~~strike through~~ around items that don't (along with an explanation). --> - [x] I have updated the project documentation to reflect my changes or determined no changes are needed. --------- Co-authored-by: Milan Raj <[email protected]>
- Loading branch information
Showing
14 changed files
with
195 additions
and
90 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
change/@ni-nimble-components-3e24d2ec-291c-4b81-89cc-39ea6544f957.json
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,7 @@ | ||
{ | ||
"type": "patch", | ||
"comment": "Refactor to create base classes for table columns that display text", | ||
"packageName": "@ni/nimble-components", | ||
"email": "[email protected]", | ||
"dependentChangeType": "patch" | ||
} |
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
44 changes: 44 additions & 0 deletions
44
packages/nimble-components/src/table-column/text-base/cell-view/index.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,44 @@ | ||
import { observable, volatile } from '@microsoft/fast-element'; | ||
import { TableCellView } from '../../base/cell-view'; | ||
import type { TableCellRecord } from '../../base/types'; | ||
|
||
/** | ||
* The cell view base class for displaying fields of any type as text. | ||
*/ | ||
export abstract class TableColumnTextCellViewBase< | ||
TCellRecord extends TableCellRecord = TableCellRecord, | ||
TColumnConfig = unknown | ||
> extends TableCellView<TCellRecord, TColumnConfig> { | ||
/** @internal */ | ||
@observable | ||
public isValidContentAndHasOverflow = false; | ||
|
||
/** @internal */ | ||
public textSpan!: HTMLElement; | ||
|
||
/** | ||
* Returns the text to render in the cell when it contains a valid value (i.e. when shouldUsePlaceholder() is false). | ||
* If the implementation has branching code paths then it must be marked with @volatile. | ||
* https://www.fast.design/docs/fast-element/observables-and-state/#observable-features | ||
*/ | ||
public abstract get text(): string; | ||
|
||
/** | ||
* Returns the text to render in the cell when it contains an invalid value (i.e. when shouldUsePlaceholder() is true). | ||
* If the implementation has branching code paths then it must be marked with @volatile. | ||
* https://www.fast.design/docs/fast-element/observables-and-state/#observable-features | ||
*/ | ||
public abstract get placeholder(): string; | ||
|
||
/** | ||
* Returns whether to display the placeholder value or the text value | ||
* If the implementation has branching code paths then it must be marked with @volatile. | ||
* https://www.fast.design/docs/fast-element/observables-and-state/#observable-features | ||
* */ | ||
public abstract get shouldUsePlaceholder(): boolean; | ||
|
||
@volatile | ||
public get content(): string { | ||
return this.shouldUsePlaceholder ? this.placeholder : this.text; | ||
} | ||
} |
File renamed without changes.
6 changes: 3 additions & 3 deletions
6
...c/table-column/text/cell-view/template.ts → ...le-column/text-base/cell-view/template.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
55 changes: 55 additions & 0 deletions
55
packages/nimble-components/src/table-column/text-base/group-header-view/index.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,55 @@ | ||
import { observable, volatile } from '@microsoft/fast-element'; | ||
import { TableGroupHeaderView } from '../../base/group-header-view'; | ||
import type { TableFieldValue } from '../../../table/types'; | ||
import type { TableColumnWithPlaceholderColumnConfig } from '../../base/types'; | ||
|
||
/** | ||
* The group header view base class for displaying fields of any type as text. | ||
*/ | ||
export abstract class TableColumnTextGroupHeaderViewBase< | ||
TGroupValue = TableFieldValue, | ||
TColumnConfig = TableColumnWithPlaceholderColumnConfig | ||
> extends TableGroupHeaderView<TGroupValue, TColumnConfig> { | ||
/** @internal */ | ||
public textSpan!: HTMLElement; | ||
|
||
/** @internal */ | ||
@observable | ||
public hasOverflow = false; | ||
|
||
/** | ||
* Returns the text to render in the cell when it contains a valid value (i.e. when shouldUsePlaceholder() is false). | ||
* If the implementation has branching code paths then it must be marked with @volatile. | ||
* https://www.fast.design/docs/fast-element/observables-and-state/#observable-features | ||
*/ | ||
public abstract get text(): string; | ||
|
||
/** | ||
* Returns the text to render in the cell when it contains an invalid value (i.e. when shouldUsePlaceholder() is true). | ||
* If the implementation has branching code paths then it must be marked with @volatile. | ||
* https://www.fast.design/docs/fast-element/observables-and-state/#observable-features | ||
*/ | ||
public abstract get placeholder(): string; | ||
|
||
/** | ||
* Returns whether to display the placeholder value or the text value | ||
* If the implementation has branching code paths then it must be marked with @volatile. | ||
* https://www.fast.design/docs/fast-element/observables-and-state/#observable-features | ||
* */ | ||
public abstract get shouldUsePlaceholder(): boolean; | ||
|
||
@volatile | ||
public get content(): string { | ||
return this.shouldUsePlaceholder ? this.placeholder : this.text; | ||
} | ||
|
||
/** @internal */ | ||
public updateTitleOverflow(): void { | ||
this.hasOverflow = this.textSpan.offsetWidth < this.textSpan.scrollWidth; | ||
} | ||
|
||
/** @internal */ | ||
public clearTitleOverflow(): void { | ||
this.hasOverflow = false; | ||
} | ||
} |
File renamed without changes.
14 changes: 14 additions & 0 deletions
14
packages/nimble-components/src/table-column/text-base/group-header-view/template.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,14 @@ | ||
import { html, ref } from '@microsoft/fast-element'; | ||
import type { TableColumnTextGroupHeaderViewBase } from '.'; | ||
|
||
export const template = html<TableColumnTextGroupHeaderViewBase>` | ||
<span | ||
${ref('textSpan')} | ||
class="${x => (x.shouldUsePlaceholder ? 'placeholder' : '')}" | ||
@mouseover="${x => x.updateTitleOverflow()}" | ||
@mouseout="${x => x.clearTitleOverflow()}" | ||
title="${x => (x.hasOverflow && x.content ? x.content : undefined)}" | ||
> | ||
${x => x.content} | ||
</span> | ||
`; |
31 changes: 31 additions & 0 deletions
31
packages/nimble-components/src/table-column/text-base/index.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,31 @@ | ||
import { attr } from '@microsoft/fast-element'; | ||
import { mixinFractionalWidthColumnAPI } from '../mixins/fractional-width-column'; | ||
import { TableColumn } from '../base'; | ||
import type { TableColumnWithPlaceholderColumnConfig } from '../base/types'; | ||
import { mixinGroupableColumnAPI } from '../mixins/groupable-column'; | ||
|
||
/** | ||
* The base class for table columns that display fields of any type as text. | ||
*/ | ||
export abstract class TableColumnTextBase extends mixinGroupableColumnAPI( | ||
mixinFractionalWidthColumnAPI( | ||
TableColumn<TableColumnWithPlaceholderColumnConfig> | ||
) | ||
) { | ||
@attr({ attribute: 'field-name' }) | ||
public fieldName?: string; | ||
|
||
@attr | ||
public placeholder?: string; | ||
|
||
protected fieldNameChanged(): void { | ||
this.columnInternals.dataRecordFieldNames = [this.fieldName]; | ||
this.columnInternals.operandDataRecordFieldName = this.fieldName; | ||
} | ||
|
||
protected placeholderChanged(): void { | ||
this.columnInternals.columnConfig = { | ||
placeholder: this.placeholder ?? '' | ||
}; | ||
} | ||
} |
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
41 changes: 15 additions & 26 deletions
41
packages/nimble-components/src/table-column/text/group-header-view/index.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
16 changes: 0 additions & 16 deletions
16
packages/nimble-components/src/table-column/text/group-header-view/template.ts
This file was deleted.
Oops, something went wrong.
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