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: improve the interaction design of the DatetimePicker component #2674

Merged
merged 2 commits into from
May 23, 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
1 change: 1 addition & 0 deletions packages/neuron-ui/src/components/Send/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ const Send = () => {
<div className={styles.datetimePicker}>
<div className={styles.datetimeDialog}>
<DatetimePicker
confirmText={(time, display) => `${t('send.release-on')}${time == null ? '' : ` ${display}`}`}
onConfirm={(time: number) => {
updateTransactionOutput('date')(locktimeIndex)(`${time}`)
setLocktimeIndex(-1)
Expand Down
2 changes: 1 addition & 1 deletion packages/neuron-ui/src/styles/mixin.scss
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

@mixin dialog-confirm-button {
border: none;
width: 5.125rem;
min-width: 5.125rem;
height: 1.875rem;
background-color: var(--nervos-green);
color: #fff;
Expand Down
5 changes: 5 additions & 0 deletions packages/neuron-ui/src/widgets/Calendar/focusControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ export const useTableFocusControl = (
moveDate(calendarYear, calendarMonth, focusDate.getDate())
}, [calendarYear, calendarMonth, minDate?.toDateString(), maxDate?.toDateString()])

useEffect(() => {
setYear(value?.getFullYear() ?? new Date().getFullYear())
setMonth((value?.getMonth() ?? new Date().getMonth()) + 1)
}, [value?.toDateString()])

const onKeyDown = (e: KeyboardEvent<HTMLElement>) => {
const keyEventMap = {
Enter: () => onChange(focusDate),
Expand Down
17 changes: 5 additions & 12 deletions packages/neuron-ui/src/widgets/Calendar/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState, useEffect, useMemo, useCallback } from 'react'
import React, { useState, useMemo, useCallback } from 'react'
import { useTranslation } from 'react-i18next'
import {
getMonthCalendar,
Expand Down Expand Up @@ -71,11 +71,6 @@ const Calendar: React.FC<CalendarProps> = ({
const [month, setMonth] = useState(new Date().getMonth() + 1)
const [status, setStatus] = useState<'year' | 'month' | 'date'>('date')

useEffect(() => {
setYear(value?.getFullYear() ?? new Date().getFullYear())
setMonth((value?.getMonth() ?? new Date().getMonth()) + 1)
}, [value?.toDateString()])

const [uId] = useState(() => (+new Date()).toString(16).slice(-4))

const [t, { language }] = useTranslation()
Expand All @@ -87,12 +82,10 @@ const Calendar: React.FC<CalendarProps> = ({
const monthName = monthNames[month - 1]
const monthShortName = monthShortNames[month - 1]

const calendar = useMemo(() => getMonthCalendar(year, month, firstDayOfWeek, language), [
year,
month,
firstDayOfWeek,
language,
])
const calendar = useMemo(
() => getMonthCalendar(year, month, firstDayOfWeek, language),
[year, month, firstDayOfWeek, language]
)
function isDisabledTime(date: Date): boolean {
return !isDayInRange(date, { minDate, maxDate })
}
Expand Down
26 changes: 13 additions & 13 deletions packages/neuron-ui/src/widgets/DatetimePicker/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import Button from 'widgets/Button'
import { useTranslation } from 'react-i18next'
import styles from './datetimePicker.module.scss'

const SECONDS_PER_DAY = 24 * 3600 * 1000
let UTC: string | number = -new Date().getTimezoneOffset() / 60
if (UTC > 0) {
UTC = `UTC+${UTC}`
Expand All @@ -14,10 +13,7 @@ if (UTC > 0) {

export const formatDate = (datetime: Date) => {
const month = (datetime.getMonth() + 1).toString().padStart(2, '0')
const date = datetime
.getDate()
.toString()
.padStart(2, '0')
const date = datetime.getDate().toString().padStart(2, '0')
const year = datetime.getFullYear()
return `${month}/${date}/${year}`
}
Expand All @@ -28,14 +24,9 @@ export interface DatetimePickerProps {
notice?: string
onConfirm: (time: number) => void
onCancel: (e: React.MouseEvent<HTMLElement, MouseEvent>) => void
confirmText?: string | ((time?: number, display?: string) => string)
}
const DatetimePicker = ({
preset = new Date(Date.now() + SECONDS_PER_DAY),
onConfirm,
onCancel,
title,
notice,
}: DatetimePickerProps) => {
const DatetimePicker = ({ preset, onConfirm, onCancel, confirmText, title, notice }: DatetimePickerProps) => {
const [t] = useTranslation()
const [status, setStatus] = useState<'done' | 'edit'>('done')
const [display, setDisplay] = useState<string>(preset ? formatDate(new Date(+preset)) : '')
Expand Down Expand Up @@ -138,7 +129,16 @@ const DatetimePicker = ({
) : null}
<div className={styles.actions}>
<Button type="cancel" label={t('common.cancel')} onClick={onCancel} />
<Button type="submit" label={t('common.save')} onClick={onSubmit} disabled={disabled} />
<Button
type="submit"
label={
typeof confirmText === 'function'
? confirmText(selected?.getTime(), display)
: confirmText ?? t('common.save')
}
onClick={onSubmit}
disabled={disabled}
/>
</div>
</div>
</div>
Expand Down