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(table): auto resize actions column #381

Merged
merged 5 commits into from
Nov 14, 2023
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
47 changes: 47 additions & 0 deletions lib/components/STable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
watch
} from 'vue'
import { type Table } from '../composables/Table'
import { getTextWidth } from '../support/Text'
import SInputCheckbox from './SInputCheckbox.vue'
import SSpinner from './SSpinner.vue'
import STableCell from './STableCell.vue'
Expand Down Expand Up @@ -204,6 +205,52 @@

const resizeObserver = useResizeObserver(head, handleResize)

const font = typeof document !== 'undefined'
? `500 12px ${getComputedStyle(document.body).fontFamily}`
: '500 12px Inter'

Check warning on line 210 in lib/components/STable.vue

View check run for this annotation

Codecov / codecov/patch

lib/components/STable.vue#L210

Added line #L210 was not covered by tests

const actionsColumnWidth = computed(() => {
const { cell } = unref(props.options.columns).actions ?? {}

if (
typeof document === 'undefined'
|| !cell
|| typeof cell === 'function'
|| cell.type !== 'actions'

Check warning on line 219 in lib/components/STable.vue

View check run for this annotation

Codecov / codecov/patch

lib/components/STable.vue#L219

Added line #L219 was not covered by tests
) {
return undefined
}

const { actions } = cell

const widths = actions.map(({ icon, label }) => {
// has only icon
if (icon && !label) {
return 1 /* border */ + 5 /* padding */ + 16 /* icon */ + 5 /* padding */ + 1 /* border */
}

// has only label
if (label && !icon) {
return 1 /* border */ + 12 /* padding */ + getTextWidth(label, font) + 12 /* padding */ + 1 /* border */
}

// has both icon and label
if (icon && label) {
return 1 /* border */ + 8 /* padding */ + 16 /* icon */ + 4 /* padding */ + getTextWidth(label, font) + 10 /* padding */ + 1 /* border */
}

return 0
})

return 8 /* padding */ + widths.reduce((a, b) => a + b, 0) + 8 /* padding */

Check warning on line 245 in lib/components/STable.vue

View check run for this annotation

Codecov / codecov/patch

lib/components/STable.vue#L223-L245

Added lines #L223 - L245 were not covered by tests
})

watch(actionsColumnWidth, (newValue) => {
if (newValue) {
updateColWidth('actions', `${newValue}px`)
}

Check warning on line 251 in lib/components/STable.vue

View check run for this annotation

Codecov / codecov/patch

lib/components/STable.vue#L250-L251

Added lines #L250 - L251 were not covered by tests
}, { immediate: true, flush: 'post' })

function stopObserving() {
const orders = ordersToShow.value
const lastOrder
Expand Down
2 changes: 1 addition & 1 deletion lib/components/STableCellActions.vue
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ defineProps<{
min-height: 40px;
display: flex;
align-items: center;
justify-content: center;
flex-wrap: nowrap;
flex-direction: row;
padding: 0 8px;
}
</style>
25 changes: 25 additions & 0 deletions lib/support/Text.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Adapted from https://stackoverflow.com/a/21015393/11613622

let _canvas: HTMLCanvasElement

export function getTextWidth(text: string, font: string): number
export function getTextWidth(text: string, el: HTMLElement): number

export function getTextWidth(text: string, fontOrEl: string | HTMLElement): number {
const canvas = _canvas || (_canvas = document.createElement('canvas'))
const context = canvas.getContext('2d')!
context.font = typeof fontOrEl === 'string' ? fontOrEl : getCanvasFont(fontOrEl)
const metrics = context.measureText(text)

return metrics.width
}

Check warning on line 15 in lib/support/Text.ts

View check run for this annotation

Codecov / codecov/patch

lib/support/Text.ts#L9-L15

Added lines #L9 - L15 were not covered by tests

function getCanvasFont(el: HTMLElement) {
const {
fontWeight = 'normal',
fontSize = '16px',
fontFamily = 'Times New Roman'
} = getComputedStyle(el)

return `${fontWeight} ${fontSize} ${fontFamily}`
}

Check warning on line 25 in lib/support/Text.ts

View check run for this annotation

Codecov / codecov/patch

lib/support/Text.ts#L17-L25

Added lines #L17 - L25 were not covered by tests
8 changes: 6 additions & 2 deletions lib/types/shims.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
declare module 'v-calendar' {
export const DatePicker: any
/// <reference types="vite/client" />

declare module '*.vue' {
import { DefineComponent } from 'vue'
const component: DefineComponent<{}, {}, any>
export default component
}
7 changes: 0 additions & 7 deletions lib/types/vue-shims.d.ts

This file was deleted.

1 change: 0 additions & 1 deletion stories/components/STable.01_Playground.story.vue
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,6 @@ function updateTagsFilter(value: string) {
.table :deep(.col-width) { --table-col-width: 128px; }
.table :deep(.col-tags) { --table-col-width: 192px; }
.table :deep(.col-createdAt) { --table-col-width: 192px; }
.table :deep(.col-actions) { --table-col-width: 100px; }

.table {
margin-bottom: 16px;
Expand Down
Loading