Enhance TimeEdit Component with Context Integration and Dynamic Date Format
- Added useContext to integrate ApiServerContext for accessing user settings. - Implemented dynamic dateTimeFormat in the TimeEdit component, improving flexibility based on user preferences. - Refactored date picker to utilize the new format, enhancing user experience and consistency.
This commit is contained in:
parent
c29034ff27
commit
098fec3b2e
@ -1,8 +1,10 @@
|
|||||||
import { useState, useRef } from 'react'
|
import { useState, useRef, useContext } from 'react'
|
||||||
import PropTypes from 'prop-types'
|
import PropTypes from 'prop-types'
|
||||||
import { DatePicker, Input } from 'antd'
|
import { DatePicker, Input } from 'antd'
|
||||||
import dayjs from 'dayjs'
|
import dayjs from 'dayjs'
|
||||||
import FunctionIcon from '../../Icons/FunctionIcon'
|
import FunctionIcon from '../../Icons/FunctionIcon'
|
||||||
|
import { ApiServerContext } from '../context/ApiServerContext'
|
||||||
|
import { DEFAULT_DATE_TIME_FORMAT } from '../../../database/Settings'
|
||||||
|
|
||||||
const OPERATOR_KEYS = ['+', '-']
|
const OPERATOR_KEYS = ['+', '-']
|
||||||
|
|
||||||
@ -70,21 +72,17 @@ function applyTimeExpr(fallbackBase, expr) {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
const TimeEdit = ({
|
const TimeEdit = ({ value, onChange, onBlur, disabled, ...rest }) => {
|
||||||
value,
|
|
||||||
onChange,
|
|
||||||
onBlur,
|
|
||||||
disabled,
|
|
||||||
...rest
|
|
||||||
}) => {
|
|
||||||
const [isExprMode, setIsExprMode] = useState(false)
|
const [isExprMode, setIsExprMode] = useState(false)
|
||||||
const [exprValue, setExprValue] = useState('')
|
const [exprValue, setExprValue] = useState('')
|
||||||
const inputRef = useRef(null)
|
const inputRef = useRef(null)
|
||||||
|
const { userSettings } = useContext(ApiServerContext) || {}
|
||||||
|
const dateTimeFormat =
|
||||||
|
userSettings?.appearance?.dateTimeFormat || DEFAULT_DATE_TIME_FORMAT
|
||||||
|
|
||||||
const dayjsValue =
|
const dayjsValue =
|
||||||
value == null ? null : dayjs.isDayjs(value) ? value : dayjs(value)
|
value == null ? null : dayjs.isDayjs(value) ? value : dayjs(value)
|
||||||
const pickerValue =
|
const pickerValue = dayjsValue && dayjsValue.isValid() ? dayjsValue : null
|
||||||
dayjsValue && dayjsValue.isValid() ? dayjsValue : null
|
|
||||||
|
|
||||||
const switchToExprMode = (initialValue) => {
|
const switchToExprMode = (initialValue) => {
|
||||||
setIsExprMode(true)
|
setIsExprMode(true)
|
||||||
@ -162,6 +160,7 @@ const TimeEdit = ({
|
|||||||
showTime
|
showTime
|
||||||
style={{ width: '100%' }}
|
style={{ width: '100%' }}
|
||||||
value={pickerValue}
|
value={pickerValue}
|
||||||
|
format={dateTimeFormat}
|
||||||
onChange={onChange}
|
onChange={onChange}
|
||||||
onKeyDown={handleDatePickerKeyDown}
|
onKeyDown={handleDatePickerKeyDown}
|
||||||
disabled={disabled}
|
disabled={disabled}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user