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

[aform] adate refactor #98

Merged
merged 30 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
9bd2a6e
wip: adate refactor
agritheory Sep 4, 2023
a4c955f
wip: datepicker styling, v-model wip
agritheory Sep 5, 2023
efb13a9
fix: minor fixes
Sep 6, 2023
0bfaacf
fix: add changelogs
Sep 6, 2023
31b351a
wip: datepicker, v-model WIP, test
agritheory Sep 11, 2023
4b97850
wip: adatepicker tests
agritheory Sep 11, 2023
147fcf2
fix: adatepicker emits
Sep 12, 2023
2664afd
fix: remove extra dev dependencies
Sep 13, 2023
65db57a
fix: update changelogs
Apr 17, 2024
cbc936e
Merge remote-tracking branch 'upstream/development' into adate_refactor
Apr 19, 2024
55670f0
test: update tests
Apr 19, 2024
e660d13
fix: add changelogs
Apr 19, 2024
fd60ab2
Merge remote-tracking branch 'upstream/development' into adate_refactor
Apr 23, 2024
961e30b
fix: cleanup ADatepicker component
Apr 23, 2024
44dc06f
fix: add missing prop for ADate
Apr 23, 2024
1c36c92
fix: add changelogs
Apr 23, 2024
1649272
fix: update story styling
Apr 23, 2024
5354c21
Merge remote-tracking branch 'upstream/development' into adate_refactor
Apr 23, 2024
93ca6a4
fix: bump version to 0.2.7
Apr 23, 2024
b1f85fe
fix: minor cleanup
Apr 23, 2024
77f3780
test: fix datepicker test
Apr 23, 2024
7ec76e5
styled today, selected, and cursor for datepicker.
crabinak Apr 23, 2024
c85a05e
fix: spawn datepicker on click
Apr 25, 2024
1a3b9fd
fix: minor cleanup
Apr 25, 2024
3946e70
Merge remote-tracking branch 'upstream/development' into adate_refactor
Apr 26, 2024
6b43842
fix: remove obsolete changelogs
Apr 26, 2024
09cce10
Merge branch 'development' into adate_refactor
Alchez May 6, 2024
40b13dd
fix: use focus composable to conditionally apply keyboard navigation
May 6, 2024
9681a2a
fix: add changelogs
May 6, 2024
97938b9
ci: add build artefacts
May 6, 2024
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
2 changes: 1 addition & 1 deletion aform/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@stonecrop/aform",
"version": "0.2.6",
"version": "0.2.7",
"license": "MIT",
"type": "module",
"author": {
Expand Down
331 changes: 74 additions & 257 deletions aform/src/components/form/ADate.vue
Original file line number Diff line number Diff line change
@@ -1,277 +1,94 @@
<template>
<div
v-if="!readonly"
:event="event"
:colIndex="colIndex"
:rowIndex="rowIndex"
:tableid="tableid"
class="adate"
tabindex="0"
ref="adatepicker">
<table>
<tr>
<td @click="previousMonth" :tabindex="-1">&lt;</td>
<th colspan="5">{{ monthAndYear }}</th>
<td @click="nextMonth" :tabindex="-1">&gt;</td>
</tr>
<tr v-for="rowNo in numberOfRows" :key="rowNo">
<!-- TODO: (style) remove inline styling and replace with theme package -->
<td
v-for="colNo in numberOfColumns"
:key="(rowNo - 1) * numberOfColumns + colNo"
:contenteditable="false"
:spellcheck="false"
:tabindex="0"
:style="{
border: isSelectedDate(currentDates[(rowNo - 1) * numberOfColumns + colNo])
? '2px solid var(--focus-cell-outline)'
: 'none',
borderBottomColor: isTodaysDate(currentDates[(rowNo - 1) * numberOfColumns + colNo])
? 'var(--focus-cell-outline)'
: 'none',
}"
@click.prevent.stop="selectDate($event, (rowNo - 1) * numberOfColumns + colNo)"
:class="{
todaysdate: isTodaysDate(currentDates[(rowNo - 1) * numberOfColumns + colNo]),
selecteddate: isSelectedDate(currentDates[(rowNo - 1) * numberOfColumns + colNo]),
}">
{{ new Date(currentDates[(rowNo - 1) * numberOfColumns + colNo]).getDate() }}
</td>
</tr>
</table>
<div>
<input
ref="dateRef"
type="date"
:id="uuid"
:disabled="readonly"
:required="required"
:value="inputDate"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@agritheory I had to move the map from v-model to value since the two-way sync was causing some issues with the v-model setup in the story. Let's discuss this change.

@click="showPicker" />
<label :for="uuid">{{ label }}</label>
<p v-show="validation.errorMessage" v-html="validation.errorMessage"></p>
</div>
</template>

<script setup lang="ts">
import { computed, inject, nextTick, onMounted, ref, watch } from 'vue'

import { TableDataStore } from '@stonecrop/atable'
import { defaultKeypressHandlers, useKeyboardNav } from '@stonecrop/utilities'

const props = defineProps<{
colIndex?: number
rowIndex?: number
tableid?: string
event?: Event
indent?: number
readonly?: boolean
}>()

const tableData = inject<TableDataStore>(props.tableid)

const numberOfRows = 6
const numberOfColumns = 7
const todaysDate = new Date()

const selectedDate = ref<Date>()
const currentMonth = ref<number>()
const currentYear = ref<number>()
const currentDates = ref<number[]>([])
// const width = ref('')

onMounted(async () => {
let cellDate = tableData.cellData<string | number | Date>(props.colIndex, props.rowIndex)
if (cellDate) {
if (!(cellDate instanceof Date)) {
cellDate = new Date(cellDate)
}

selectedDate.value = cellDate
currentMonth.value = selectedDate.value.getMonth()
currentYear.value = selectedDate.value.getFullYear()
} else {
currentMonth.value = todaysDate.getMonth()
currentYear.value = todaysDate.getFullYear()
}

renderMonth()
await nextTick()

const $selectedDate = document.getElementsByClassName('selecteddate')
if ($selectedDate.length > 0) {
;($selectedDate[0] as HTMLElement).focus()
} else {
const $todaysDate = document.getElementsByClassName('todaysdate')
if ($todaysDate.length > 0) {
;($todaysDate[0] as HTMLElement).focus()
}
}
})

watch([currentMonth, currentYear], () => {
renderMonth()
})

const renderMonth = () => {
currentDates.value = []
const firstOfMonth = new Date(currentYear.value, currentMonth.value, 1)
const monthStartWeekday = firstOfMonth.getDay()
const calendarStartDay = firstOfMonth.setDate(firstOfMonth.getDate() - monthStartWeekday)
for (let dayIndex of Array(43).keys()) {
currentDates.value.push(calendarStartDay + dayIndex * 86400000)
}
}

const previousYear = () => {
currentYear.value -= 1
}

const nextYear = () => {
currentYear.value += 1
}

const previousMonth = () => {
if (currentMonth.value == 0) {
currentMonth.value = 11
previousYear()
} else {
currentMonth.value -= 1
import { ref } from 'vue'

withDefaults(
defineProps<{
label: string
required?: boolean
readonly?: boolean
uuid?: string
validation?: Record<string, any>
}>(),
{
validation: () => ({ errorMessage: '&nbsp;' }),
}
}
)

const nextMonth = () => {
if (currentMonth.value == 11) {
currentMonth.value = 0
nextYear()
} else {
currentMonth.value += 1
}
}
const inputDate = defineModel<string | number | Date>()
const dateRef = ref<HTMLInputElement | null>(null)

const isTodaysDate = (day: string | number | Date) => {
if (currentMonth.value !== todaysDate.getMonth()) {
return
const showPicker = () => {
if (dateRef.value) {
dateRef.value.showPicker()
}
return todaysDate.toDateString() === new Date(day).toDateString()
}

const isSelectedDate = (day: string | number | Date) => {
return new Date(day).toDateString() === new Date(selectedDate.value).toDateString()
}

const selectDate = (event: Event, currentIndex: number) => {
selectedDate.value = new Date(currentDates.value[currentIndex])
updateData()
// TODO: (typing) figure out a way to close datepicker
// context.refs.adatepicker.destroy()
}

const updateData = () => {
// TODO: check proper date format to feed back (assuming number for now)
tableData.setCellData(props.rowIndex, props.colIndex, selectedDate.value.getTime())
}

// const dayWidth = computed(() => {
// const widthValue = Number(width.value.replace('px', ''))
// return `${widthValue / (numberOfColumns - 1)}px`
// })

const monthAndYear = computed(() => {
return new Date(currentYear.value, currentMonth.value, 1).toLocaleDateString(undefined, {
year: 'numeric',
month: 'long',
})
})

// setup keyboard navigation
useKeyboardNav([
{
parent: 'table.adate',
selectors: 'td',
handlers: {
...defaultKeypressHandlers,
...{
'keydown.pageup': previousMonth,
'keydown.shift.pageup': previousYear,
'keydown.pagedown': nextMonth,
'keydown.shift.pagedown': nextYear,
},
},
},
])
</script>

<style scoped>
@import '@/theme/aform.css';

.adate {
border: 2px solid var(--focus-cell-outline);
div {
min-width: 40ch;
border: 1px solid transparent;
padding: 0rem;
margin: 0rem;
margin-right: 1ch;
}

input {
width: calc(100% - 1ch);
outline: 1px solid transparent;
border: 1px solid var(--input-border-color);
padding: 1ch 0.5ch 0.5ch 1ch;
margin: calc(1.15rem / 2) 0 0 0;
min-height: 1.15rem;
border-radius: 0.25rem;
}

p,
label {
color: var(--input-label-color);
display: block;
min-height: 1.15rem;
padding: 0rem;
margin: 0rem;
border: 1px solid transparent;
margin-bottom: 0.25rem;
}

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

label {
z-index: 2;
font-size: 80%;
position: absolute;
z-index: 100;
font-size: var(--table-font-size);
display: inline-table;
background-color: var(--row-color-zebra-light);
color: var(--cell-text-color);
outline: none;
width: calc(100% - 4px);
}

.adate tr {
height: 1.15rem;
text-align: center;
vertical-align: middle;
}

.adate td {
border: 2px solid transparent;
min-width: 2.25ch; /* this doesn't zoom correctly */
}

.adate td:hover {
border: 2px solid var(--focus-cell-outline);
}

.adate td {
border: 1px;
border-style: solid;
border-color: var(--cell-border-color);
border-radius: 0px;
box-sizing: border-box;
margin: 0px;
outline: none;
box-shadow: none;
color: var(--cell-text-color);
text-overflow: ellipsis;
overflow: hidden;
padding-left: 0.5ch;
padding-right: 0.5ch;
}

.adate td:focus,
.adate td:focus-within {
background-color: var(--focus-cell-background);
outline-width: 2px;
outline-style: solid;
outline-color: var(--focus-cell-outline);
box-shadow: none;
overflow: hidden;
min-height: 1.15em;
max-height: 1.15em;
overflow: hidden;
}

button {
background-color: var(--row-color-zebra-light);
border: none;
padding: 0px;
margin: 0px;
color: var(--cell-text-color);
outline: none;
font-size: var(--table-font-size);
}

.dateheader {
font-weight: 700;
display: flex;
align-items: center;
justify-content: space-between;
background: white;
margin: calc(-1.5rem - calc(2.15rem / 2)) 0 0 1ch;
padding: 0 0.25ch 0 0.25ch;
}

.adate .todaysdate {
border-bottom-color: var(--focus-cell-outline);
input:focus {
border: 1px solid var(--input-active-border-color);
}

.adate .selecteddate {
border: 2px solid var(--focus-cell-outline);
input:focus + label {
color: var(--input-active-label-color);
}
</style>
Loading
Loading