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: add number formatting option to BackgroundBarFormat #306

Merged
merged 2 commits into from
Jul 22, 2024
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
5 changes: 4 additions & 1 deletion .github/workflows/base.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ jobs:
- name: Yarn Cache
uses: actions/cache@v4
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
path: |
${{ steps.yarn-cache-dir-path.outputs.dir }}
${{ github.workspace }}/.cache
~/.cache
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
Expand Down
12 changes: 9 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,15 @@ jobs:
- name: Yarn Cache
uses: actions/cache@v4
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
path: |
${{ steps.yarn-cache-dir-path.outputs.dir }}
${{ github.workspace }}/.cache
~/.cache
duyet marked this conversation as resolved.
Show resolved Hide resolved
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-

- name: Yarn Cache
- name: Next Cache
uses: actions/cache@v4
with:
path: .next/cache
Expand Down Expand Up @@ -180,7 +183,10 @@ jobs:
- name: Yarn Cache
uses: actions/cache@v4
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
path: |
${{ steps.yarn-cache-dir-path.outputs.dir }}
${{ github.workspace }}/.cache
~/.cache
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
Expand Down
2 changes: 1 addition & 1 deletion app/[query]/more/top-usage-columns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const topUsageColumnsConfig: QueryConfig = {
ORDER BY 2 DESC`,
columns: ['column', 'count'],
columnFormats: {
count: ColumnFormat.BackgroundBar,
count: [ColumnFormat.BackgroundBar, { numberFormat: true }],
},
defaultParams: { table: '' },
}
107 changes: 107 additions & 0 deletions components/data-table/cells/background-bar-format.cy.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { BackgroundBarFormat } from './background-bar-format'

describe('<BackgroundBarFormat />', () => {
const mockTable = {
getCoreRowModel: () => ({
rows: [1, 2, 3],
}),
}

const mockRow = {
original: {
column1: 100,
pct_column1: 50,
column2: 200,
readable_column2: 'Two hundred',
pct_column2: 75,
},
}

it('renders with basic props', () => {
cy.mount(
<BackgroundBarFormat
table={mockTable}
row={mockRow}
columnName="column1"
value={100}
/>
)

cy.get('div[aria-roledescription="background-bar"]').as('cell')

cy.get('@cell').should('contain.text', '100')
cy.get('@cell').should('have.attr', 'title', '100 (50%)')
cy.get('@cell')
.should('have.css', 'background')
.and('include', 'linear-gradient')
})

it('renders with numberFormat option', () => {
cy.mount(
<BackgroundBarFormat
table={mockTable}
row={mockRow}
columnName="column1"
value={1000}
options={{ numberFormat: true }}
/>
)
cy.get('div[aria-roledescription="background-bar"]').as('cell')
cy.get('@cell').should('contain.text', '1,000')
})

it('handles readable column names', () => {
cy.mount(
<BackgroundBarFormat
table={mockTable}
row={mockRow}
columnName="readable_column2"
value="Two hundred"
/>
)

cy.get('div[aria-roledescription="background-bar"]').as('cell')
cy.get('@cell').should('contain.text', 'Two hundred')
cy.get('@cell').should('have.attr', 'title', '200 (75%)')
})

it('returns raw value when table has 1 or fewer rows', () => {
const singleRowTable = {
getCoreRowModel: () => ({
rows: [1],
}),
}

cy.mount(
<BackgroundBarFormat
table={singleRowTable}
row={mockRow}
columnName="column1"
value={100}
/>
)

cy.get('div[aria-roledescription="background-bar"]').should('not.exist')
cy.contains('100').should('exist')
})

it('handles missing percentage column', () => {
const rowWithoutPct = {
original: {
column1: 100,
},
}

cy.mount(
<BackgroundBarFormat
table={mockTable}
row={rowWithoutPct}
columnName="column1"
value={100}
/>
)

cy.get('div').should('contain.text', '100')
cy.get('div').should('not.have.attr', 'title')
})
})
12 changes: 11 additions & 1 deletion components/data-table/cells/background-bar-format.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
import { formatReadableQuantity } from '@/lib/format-readable'

export interface BackgroundBarOptions {
numberFormat?: boolean
}

interface BackgroundBarFormatProps {
table: any
row: any
columnName: string
value: any
options?: BackgroundBarOptions
}

export function BackgroundBarFormat({
table,
row,
columnName,
value,
options,
}: BackgroundBarFormatProps) {
// Disable if row count <= 1
if (table.getCoreRowModel()?.rows?.length <= 1) return value
Expand Down Expand Up @@ -38,8 +46,10 @@ export function BackgroundBarFormat({
transparent ${pct}%, transparent 100%)`,
}}
title={`${orgValue} (${pct}%)`}
aria-label={`${orgValue} (${pct}%)`}
aria-roledescription="background-bar"
>
{value}
{options?.numberFormat ? formatReadableQuantity(value, 'long') : value}
duyet marked this conversation as resolved.
Show resolved Hide resolved
</div>
)
}
6 changes: 5 additions & 1 deletion components/data-table/format-cell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import { ColumnFormat, type ColumnFormatOptions } from '@/types/column-format'

import { ActionFormat } from './cells/action-format'
import { type Action } from './cells/actions/types'
import { BackgroundBarFormat } from './cells/background-bar-format'
import {
BackgroundBarFormat,
type BackgroundBarOptions,
} from './cells/background-bar-format'
import { BadgeFormat } from './cells/badge-format'
import { BooleanFormat } from './cells/boolean-format'
import {
Expand Down Expand Up @@ -43,6 +46,7 @@ export const formatCell = <TData extends RowData, TValue>(
row={row}
columnName={columnName}
value={value}
options={columnFormatOptions as BackgroundBarOptions}
duyet marked this conversation as resolved.
Show resolved Hide resolved
/>
)

Expand Down
2 changes: 2 additions & 0 deletions types/column-format.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { LinkProps } from 'next/link'

import { type Action } from '@/components/data-table/cells/actions/types'
import { type BackgroundBarOptions } from '@/components/data-table/cells/background-bar-format'
import { type CodeDialogOptions } from '@/components/data-table/cells/code-dialog-format'
import { type CodeToggleOptions } from '@/components/data-table/cells/code-toggle-format'
import { type HoverCardOptions } from '@/components/data-table/cells/hover-card-format'
Expand Down Expand Up @@ -29,6 +30,7 @@ export type ColumnFormatWithArgs =
| [ColumnFormat.HoverCard, HoverCardOptions]
| [ColumnFormat.CodeDialog, CodeDialogOptions]
| [ColumnFormat.CodeToggle, CodeToggleOptions]
| [ColumnFormat.BackgroundBar, BackgroundBarOptions]

// Union of all possible format options
export type ColumnFormatOptions = ColumnFormatWithArgs[1]