Skip to content

Commit f6cbae9

Browse files
authored
Merge pull request #34 from revolist/568-revogrid
#568 Enhance Datepicker Positioning Based on Screen Edge
2 parents d23a51e + ebbc287 commit f6cbae9

File tree

8 files changed

+120
-32
lines changed

8 files changed

+120
-32
lines changed

lib/editor.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ export class ColumnEditor implements EditorBase {
5353
);
5454
}
5555

56+
private isDate(value: any): value is Date {
57+
return Object.prototype.toString.call(value) === '[object Date]' && !isNaN(value.getTime());
58+
}
59+
5660
disconnectedCallback() {
5761
this.calendar?.hide();
5862
this.revoFloat?.remove();
@@ -66,6 +70,9 @@ export class ColumnEditor implements EditorBase {
6670
const model = this.editCell.model || {};
6771
val = model[this.editCell?.prop] || '';
6872
}
73+
if (this.isDate(val)) {
74+
val = val.toISOString().split('T')[0];
75+
}
6976

7077
return h('div', { class: 'revo-holder' }, [
7178
h(
@@ -83,10 +90,17 @@ export class ColumnEditor implements EditorBase {
8390
...this.data.column,
8491
ref: (e: HTMLDuetDatePickerElement) => (this.calendar = e),
8592
value: val,
93+
8694
onDuetChange: ({
8795
detail: { value, valueAsDate },
8896
}: CustomEvent<DateChangeEvent>) =>
8997
this.saveCallback(this.data.column.valueAsDate ? valueAsDate : value),
98+
onDuetOpen: () => {
99+
const { bottom } = this.revoFloat?.getBoundingClientRect() || {};
100+
const { clientHeight } = document.body;
101+
const position = bottom + 300 > clientHeight ? 'top' : 'bottom';
102+
this.calendar?.setAttribute('position', position);
103+
},
90104
}),
91105
],
92106
),

lib/renderer.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,10 @@ const svg = `<svg aria-hidden="true" viewBox="0 0 21 21" xmlns="http://www.w3.or
2222

2323
export const ColumnRenderer = (
2424
h: HyperFunc<VNode>,
25-
{ model, prop }: ColumnDataSchemaModel,
26-
): VNode[] => {
27-
let val = model[prop];
25+
{ value }: ColumnDataSchemaModel,
26+
) => {
2827
return [
29-
h('div', { class: { 'cell-value-wrapper': true } }, [val]),
28+
h('div', { class: { 'cell-value-wrapper': true } }, value?.toString()),
3029
h('button', {
3130
class: { calendar: true },
3231
innerHTML: svg,

lib/style.css

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
font-size: 14px;
1212
}
1313

14+
.revo-float duet-date-picker[position="top"] .duet-date__dialog {
15+
top: inherit;
16+
bottom: 100%;
17+
}
18+
1419
.revo-float .duet-date {
1520
height: 100%;
1621
display: flex;
@@ -51,9 +56,8 @@ revogr-data .calendar {
5156
opacity: .5;
5257
text-align: center;
5358
background-color: transparent;
54-
55-
svg {
56-
width: 14px;
57-
vertical-align: middle;
58-
}
59+
}
60+
revogr-data .calendar svg {
61+
width: 14px;
62+
vertical-align: middle;
5963
}

package-lock.json

Lines changed: 24 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@
5151
"license": "MIT",
5252
"devDependencies": {
5353
"@duetds/date-picker": "^1.4.0",
54-
"@revolist/revogrid": "^4.10.1",
54+
"@faker-js/faker": "^9.0.1",
55+
"@revolist/revogrid": "^4.10.8",
5556
"@types/lodash": "^4.14.149",
5657
"lodash": "4.17.20",
5758
"typescript": "^5.2.2",

src/index.ts

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,28 @@
11
import './style.css';
22
import { defineCustomElements } from '@revolist/revogrid/loader';
33
import Plugin from '@revolist/revogrid-column-date';
4+
import { makeData } from './makeData';
45

56
defineCustomElements();
67

78
const app = document.querySelector('#app');
89
const grid = document.createElement('revo-grid');
910

1011
const COLUMN_TYPE_DATE = 'date';
11-
12+
grid.theme = 'compact';
1213
// define columns
1314
grid.columns = [
14-
{ name: 'A', prop: 'name', size: 250 },
15+
{ name: 'Last Name', prop: 'lastName', size: 200 },
1516
{
16-
name: 'B',
17-
prop: 'date',
17+
name: 'Birthday',
18+
prop: 'birthday',
1819
size: 150,
20+
valueAsDate: true,
1921
// provide column type format
2022
columnType: COLUMN_TYPE_DATE,
2123
},
2224
];
23-
grid.source = [
24-
{
25-
name: 'Mark',
26-
date: '2020-08-24',
27-
},
28-
{
29-
name: 'Kate',
30-
date: '2020-08-24',
31-
},
32-
];
33-
25+
grid.source = makeData(120);
3426
// define formats
3527
grid.columnTypes = {
3628
[COLUMN_TYPE_DATE]: new Plugin(),

src/makeData.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { faker } from '@faker-js/faker'
2+
3+
export type Person = {
4+
avatar: string
5+
firstName: string
6+
lastName: string
7+
age: number
8+
visits: number
9+
progress: number
10+
birthday: string
11+
status: 'relationship' | 'complicated' | 'single'
12+
subRows?: Person[]
13+
}
14+
15+
const range = (len: number) => {
16+
const arr: number[] = []
17+
for (let i = 0; i < len; i++) {
18+
arr.push(i)
19+
}
20+
return arr
21+
}
22+
23+
const newPerson = (): Person => {
24+
return {
25+
avatar: faker.image.avatar(),
26+
firstName: faker.person.firstName(),
27+
lastName: faker.person.lastName(),
28+
age: faker.number.int(40),
29+
visits: faker.number.int(1000),
30+
progress: faker.number.int(100),
31+
birthday: faker.date.birthdate().toISOString().split('T')[0],
32+
status: faker.helpers.shuffle<Person['status']>([
33+
'relationship',
34+
'complicated',
35+
'single',
36+
])[0]!,
37+
}
38+
}
39+
40+
export function makeData(...lens: number[]) {
41+
const makeDataLevel = (depth = 0): Person[] => {
42+
const len = lens[depth]!
43+
return range(len).map((): Person => {
44+
return {
45+
...newPerson(),
46+
subRows: lens[depth + 1] ? makeDataLevel(depth + 1) : undefined,
47+
}
48+
})
49+
}
50+
51+
return makeDataLevel()
52+
}

src/style.css

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
html {
1+
html, body {
22
height: 100%;
33
width: 100%;
44
padding: 0;
@@ -12,3 +12,11 @@ html {
1212
width: 10px;
1313
height: 10px;
1414
}
15+
16+
#app {
17+
height: 100%;
18+
}
19+
20+
revo-grid {
21+
height: 100%;
22+
}

0 commit comments

Comments
 (0)