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

enhancement(tables): split formatting vs evaluation #1686

Merged
merged 1 commit into from
Jan 7, 2025
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
33 changes: 31 additions & 2 deletions src/Spec2-Core/SpTableColumn.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ Class {
'evaluation',
'expandable',
'sortFunction',
'width'
'width',
'formattingBlock'
],
#category : 'Spec2-Core-Widgets-Table',
#package : 'Spec2-Core',
Expand Down Expand Up @@ -92,6 +93,18 @@ SpTableColumn >> beNotExpandable [
expandable := false
]

{ #category : 'private' }
SpTableColumn >> evaluateObject: anObject [

"Apply the evaluation to the object of the table row.
If no evaluation is set, evaluate to the object itself.

IMPORTANT: Sorting happens on the results of evaluation and not formatting"

self evaluation ifNil: [ ^ anObject ].
^ self evaluation cull: anObject
]

{ #category : 'api' }
SpTableColumn >> evaluated: aBlock [
"Define how the column will evaluate the element of the table/tree model
Expand All @@ -111,6 +124,22 @@ SpTableColumn >> evaluation [
^ evaluation
]

{ #category : 'private' }
SpTableColumn >> formatObject: anObject [
"Returns the string representation of the argument to display in the table cell"

formattingBlock ifNil: [ ^ anObject ].
^ formattingBlock value: anObject
]

{ #category : 'api' }
SpTableColumn >> formatted: aBlock [
"Defines how each element is formatted to be shown.
By default items will be shown using #asString"

formattingBlock := aBlock
]

{ #category : 'testing' }
SpTableColumn >> hasFixedWidth [

Expand Down Expand Up @@ -161,7 +190,7 @@ SpTableColumn >> isSortable [
{ #category : 'private' }
SpTableColumn >> readObject: anObject [

^ self evaluation cull: anObject
^ self formatObject: (self evaluateObject: anObject)
]

{ #category : 'api' }
Expand Down
28 changes: 28 additions & 0 deletions src/Spec2-Tests/SpStringTableColumnTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,34 @@ Class {
#tag : 'Core-Widgets'
}

{ #category : 'tests' }
SpStringTableColumnTest >> testDisplayObjectRespectsFormatting [

| column dateToShow |
column := SpStringTableColumn new.
column formatted: [ :e | e displayString ].

dateToShow := Date today.

self
assert: (column readObject: dateToShow)
equals: dateToShow displayString
]

{ #category : 'tests' }
SpStringTableColumnTest >> testDisplayObjectWithoutFormattingReturnsTheObjectItself [

| column dateToShow |
column := SpStringTableColumn new.

dateToShow := Date today.

"It's up to the table to stringify the object later"
self
assert: (column readObject: dateToShow)
equals: dateToShow
]

{ #category : 'tests' }
SpStringTableColumnTest >> testIsSortable [
|widget|
Expand Down
Loading