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

[atable] Fix atable modal attach to cell #220

Merged
merged 7 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
6 changes: 6 additions & 0 deletions aform/src/components/form/ADate.vue
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ const showPicker = () => {
<style scoped>
div {
min-width: 40ch;
width: 100%;
box-sizing: border-box;
border: 1px solid transparent;
padding: 0rem;
margin: 0rem;
Expand All @@ -52,6 +54,7 @@ div {

input {
width: calc(100% - 1ch);
box-sizing: border-box;
outline: 1px solid transparent;
border: 1px solid var(--sc-input-border-color);
padding: 1ch 0.5ch 0.5ch 1ch;
Expand All @@ -69,12 +72,14 @@ label {
margin: 0rem;
border: 1px solid transparent;
margin-bottom: 0.25rem;
box-sizing: border-box;
}

p {
width: 100%;
color: red;
font-size: 85%;
box-sizing: border-box;
}

label {
Expand All @@ -84,6 +89,7 @@ label {
background: white;
margin: calc(-1.5rem - calc(2.15rem / 2)) 0 0 1ch;
padding: 0 0.25ch 0 0.25ch;
box-sizing: border-box;
}

input:focus {
Expand Down
9 changes: 5 additions & 4 deletions atable/src/components/ACell.vue
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const {
}>()

const cellRef = useTemplateRef<HTMLTableCellElement>('cell')
const { bottom, left } = useElementBounding(cellRef)
const { width, height } = useElementBounding(cellRef)

// keep a shallow copy of the original cell value for comparison
const originalData = store.getCellData(colIndex, rowIndex)
Expand Down Expand Up @@ -92,9 +92,10 @@ const showModal = () => {
state.modal.colIndex = colIndex
state.modal.rowIndex = rowIndex
state.modal.parent = cellRef.value
state.modal.top = bottom.value
state.modal.left = left.value
state.modal.width = cellWidth
state.modal.top = cellRef.value.offsetTop + cellRef.value.offsetHeight
state.modal.left = cellRef.value.offsetLeft
state.modal.width = width.value
state.modal.height = height.value

if (typeof column.modalComponent === 'function') {
state.modal.component = column.modalComponent({ table: state.table, row, column })
Expand Down
7 changes: 2 additions & 5 deletions atable/src/components/ATable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,7 @@
:colIndex="store.modal.colIndex"
:rowIndex="store.modal.rowIndex"
:store="store"
:style="{
left: store.modal.left + 'px',
top: store.modal.top + 'px',
maxWidth: store.modal.width + 'px',
}">
:container="tableRef">
<template #default>
<component
:key="`${store.modal.rowIndex}:${store.modal.colIndex}`"
Expand Down Expand Up @@ -244,6 +240,7 @@ td.sticky-index {
<style scoped>
@import url('@stonecrop/themes/default.css');
.atable {
position: relative;
font-family: var(--sc-atable-font-family);
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
Expand Down
45 changes: 34 additions & 11 deletions atable/src/components/ATableModal.vue
Original file line number Diff line number Diff line change
@@ -1,26 +1,49 @@
<template>
<div class="amodal" tabindex="-1" @click="handleInput" @input="handleInput">
<div class="amodal" ref="amodal" tabindex="-1" @click="handleInput" @input="handleInput" :style="amodalStyles">
<slot />
</div>
</template>

<script setup lang="ts">
import { useElementBounding } from '@vueuse/core'
import { useTemplateRef, computed } from 'vue'

import { createTableStore } from '@/stores/table'

/* const { colIndex, rowIndex, store } = */ defineProps<{
const { store, container } = defineProps<{
colIndex?: number
rowIndex?: number
store?: ReturnType<typeof createTableStore>
container?: HTMLElement
}>()

// const cellBackgroundColor = computed(() => {
// if (store.modal.parent) {
// let computedstyle = window.getComputedStyle(store.modal.parent)
// return 'blue'
// } else {
// return 'inherit'
// }
// })
const amodalRef = useTemplateRef('amodal')
const { width, height } = useElementBounding(amodalRef)

const amodalStyles = computed(() => {
// the modal usually will appear left align with and below the element it is associated with,
// but needs to detect and adjust its position if it would overflow the rightr or bottom edges

// get the dimensions of whatever container is holding this modal, e.g. Table, Window, div so the modal can detect edges
const containerWidth = container?.offsetWidth || 0
const containerHeight = container?.offsetHeight || 0

// if this modal would go outside the edge of its container, instead place it above the element (Y) or along the right side (X)
const modalLeft =
store.modal.left + width.value > containerWidth
? store.modal.left - (width.value - store.modal.width)
: store.modal.left

const modalTop =
store.modal.top + height.value > containerHeight
? store.modal.top - height.value - store.modal.height
: store.modal.top

return {
left: `${modalLeft}px`,
top: `${modalTop}px`,
}
})

const handleInput = (event: Event) => {
event.stopPropagation()
Expand All @@ -31,7 +54,7 @@ const handleInput = (event: Event) => {
@import url('@stonecrop/themes/default.css');

.amodal {
position: fixed;
position: absolute;
background-color: var(--sc-row-color-zebra-dark);
z-index: 100;
}
Expand Down
3 changes: 2 additions & 1 deletion atable/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,13 @@ export type TableRow = {
export type TableModal = {
colIndex?: number
event?: string
height?: number
left?: number
parent?: HTMLElement
rowIndex?: number
top?: number
visible?: boolean
width?: string
width?: number

component?: string
componentProps?: Record<string, any>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@stonecrop/aform",
"comment": "small style update to Adate",
"type": "none"
}
],
"packageName": "@stonecrop/aform"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@stonecrop/atable",
"comment": "modal scrolls with cells and detects edges",
"type": "none"
}
],
"packageName": "@stonecrop/atable"
}
21 changes: 14 additions & 7 deletions common/reviews/api/atable.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,23 +117,25 @@ rowModified?: boolean;
modal: Ref< {
colIndex?: number;
event?: string;
height?: number;
left?: number;
parent?: HTMLElement;
rowIndex?: number;
top?: number;
visible?: boolean;
width?: string;
width?: number;
component?: string;
componentProps?: Record<string, any>;
}, TableModal_2 | {
colIndex?: number;
event?: string;
height?: number;
left?: number;
parent?: HTMLElement;
rowIndex?: number;
top?: number;
visible?: boolean;
width?: string;
width?: number;
component?: string;
componentProps?: Record<string, any>;
}>;
Expand Down Expand Up @@ -219,23 +221,25 @@ rowModified?: boolean;
modal: Ref< {
colIndex?: number;
event?: string;
height?: number;
left?: number;
parent?: HTMLElement;
rowIndex?: number;
top?: number;
visible?: boolean;
width?: string;
width?: number;
component?: string;
componentProps?: Record<string, any>;
}, TableModal_2 | {
colIndex?: number;
event?: string;
height?: number;
left?: number;
parent?: HTMLElement;
rowIndex?: number;
top?: number;
visible?: boolean;
width?: string;
width?: number;
component?: string;
componentProps?: Record<string, any>;
}>;
Expand Down Expand Up @@ -321,23 +325,25 @@ rowModified?: boolean;
modal: Ref< {
colIndex?: number;
event?: string;
height?: number;
left?: number;
parent?: HTMLElement;
rowIndex?: number;
top?: number;
visible?: boolean;
width?: string;
width?: number;
component?: string;
componentProps?: Record<string, any>;
}, TableModal_2 | {
colIndex?: number;
event?: string;
height?: number;
left?: number;
parent?: HTMLElement;
rowIndex?: number;
top?: number;
visible?: boolean;
width?: string;
width?: number;
component?: string;
componentProps?: Record<string, any>;
}>;
Expand Down Expand Up @@ -398,12 +404,13 @@ export type TableDisplay = {
export type TableModal = {
colIndex?: number;
event?: string;
height?: number;
left?: number;
parent?: HTMLElement;
rowIndex?: number;
top?: number;
visible?: boolean;
width?: string;
width?: number;
component?: string;
componentProps?: Record<string, any>;
};
Expand Down
20 changes: 13 additions & 7 deletions docs/atable/atable.createtablestore.md

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion docs/atable/atable.tablemodal.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ Table modal definition.
export type TableModal = {
colIndex?: number;
event?: string;
height?: number;
left?: number;
parent?: HTMLElement;
rowIndex?: number;
top?: number;
visible?: boolean;
width?: string;
width?: number;
component?: string;
componentProps?: Record<string, any>;
};
Expand Down
Loading