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

refactor(database): make Date-Picker component compatible with mobile devices #8846

Merged
merged 2 commits into from
Dec 3, 2024
Merged
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
121 changes: 89 additions & 32 deletions packages/affine/data-view/src/property-presets/date/cell-renderer.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import {
popMenu,
popupTargetFromElement,
} from '@blocksuite/affine-components/context-menu';
import { DatePicker } from '@blocksuite/affine-components/date-picker';
import { createLitPortal } from '@blocksuite/affine-components/portal';
import { unsafeCSSVarV2 } from '@blocksuite/affine-shared/theme';
import { IS_MOBILE } from '@blocksuite/global/env';
import { flip, offset } from '@floating-ui/dom';
import { signal } from '@preact/signals-core';
import { baseTheme } from '@toeverything/theme';
import { format } from 'date-fns/format';
import { css, html, unsafeCSS } from 'lit';
import { state } from 'lit/decorators.js';

import { BaseCellRenderer } from '../../core/property/index.js';
import { createFromBaseCellRenderer } from '../../core/property/renderer.js';
Expand Down Expand Up @@ -77,33 +83,81 @@ export class DateCellEditing extends BaseCellRenderer<number> {
{ once: true }
);
this._prevPortalAbortController = abortController;
const root = createLitPortal({
abortController,
closeOnClickAway: true,
computePosition: {
referenceElement: this,
placement: 'bottom',
middleware: [offset(10), flip()],
},
template: () => {
const datePicker = new DatePicker();
datePicker.value = this.value ?? Date.now();
datePicker.popup = true;
datePicker.onChange = (date: Date) => {
this.tempValue = date;
};
datePicker.onEscape = () => {
abortController.abort();
};
requestAnimationFrame(() => datePicker.focusDateCell());
return datePicker;
},
});
// TODO: use z-index from variable,
// for now the slide-layout-modal's z-index is `1001`
// the z-index of popover should be higher than it
// root.style.zIndex = 'var(--affine-z-index-popover)';
root.style.zIndex = '1002';
if (IS_MOBILE) {
popMenu(popupTargetFromElement(this), {
options: {
title: {
text: this.property.name$.value,
},
onClose: () => {
abortController.abort();
},
items: [
() =>
html`<div
style="
padding: 12px;
background-color: ${unsafeCSSVarV2('layer/background/primary')};
border-radius: 12px;
color: ${unsafeCSSVarV2('text/secondary')};
font-size: 17px;
line-height: 22px;
height: 46px;
"
>
${this.dateString}
</div>`,
() => {
const datePicker = new DatePicker();
datePicker.padding = 0;
datePicker.value = this.value ?? Date.now();
datePicker.onChange = (date: Date) => {
this.tempValue$.value = date;
};
datePicker.onEscape = () => {
abortController.abort();
};
requestAnimationFrame(() => datePicker.focusDateCell());
return html`<div
style="padding: 12px;background-color: ${unsafeCSSVarV2(
'layer/background/primary'
)};border-radius: 12px"
>
${datePicker}
</div>`;
},
],
},
});
} else {
const root = createLitPortal({
abortController,
closeOnClickAway: true,
computePosition: {
referenceElement: this,
placement: 'bottom',
middleware: [offset(10), flip()],
},
template: () => {
const datePicker = new DatePicker();
datePicker.value = this.value ?? Date.now();
datePicker.popup = true;
datePicker.onChange = (date: Date) => {
this.tempValue$.value = date;
};
datePicker.onEscape = () => {
abortController.abort();
};
requestAnimationFrame(() => datePicker.focusDateCell());
return datePicker;
},
});
// TODO: use z-index from variable,
// for now the slide-layout-modal's z-index is `1001`
// the z-index of popover should be higher than it
// root.style.zIndex = 'var(--affine-z-index-popover)';
root.style.zIndex = '1002';
}
};

private updateValue = () => {
Expand All @@ -113,14 +167,20 @@ export class DateCellEditing extends BaseCellRenderer<number> {
}

this.onChange(tempValue.getTime());
this.tempValue = undefined;
this.tempValue$.value = undefined;
};

tempValue$ = signal<Date>();

get dateString() {
const value = this.tempValue ?? this.value;
return value ? format(value, 'yyyy/MM/dd') : '';
}

get tempValue() {
return this.tempValue$.value;
}

override firstUpdated() {
this.openDatePicker();
}
Expand All @@ -138,9 +198,6 @@ export class DateCellEditing extends BaseCellRenderer<number> {
${this.dateString}
</div>`;
}

@state()
accessor tempValue: Date | undefined = undefined;
}

export const datePropertyConfig = datePropertyModelConfig.createPropertyMeta({
Expand Down
Loading