Commit 3afc3fb4 authored by 田翔's avatar 田翔

fix: 时间增加时间段

parent 09c894a0
{
"name": "panda-xform",
"version": "5.9.22",
"description": "5.9.22 数值形态优化",
"version": "5.9.23",
"description": "5.9.23 时间增加时间段",
"keywords": [
"panda-xform"
],
......
import React, { useMemo } from 'react'
import moment from 'moment'
const dateType = ['date', 'dateTime', 'year', 'month', 'week', 'quarter', 'datePeriod']
const DateView = (props) => {
const { value, format } = props
const valueShow = useMemo(() => {
if (value) {
const isValid = moment(value)._isValid
if (isValid) {
if (format === dateType[0]) {
return moment(value).format('YYYY-MM-DD')
} else if (format === dateType[1]) {
return moment(value).format('YYYY-MM-DD HH:mm')
} else if (format === dateType[2]) {
return moment(value).format('YYYY-MM')
} else if (format === dateType[3]) {
return moment(value).format('YYYY')
} else if (format === dateType[4]) {
return `${moment(value).format('YYYY')}${moment(value).week()}周`
} else if (format === dateType[5]) {
return `${moment(value).format('YYYY')}${moment(value).quarter()}季度`
} else if (format === dateType[6]) {
return `${moment(value).format('YYYY-MM-DD')} ${moment(value).format('HH:mm') >= '12:00' ? '下午' : '上午'}`
}
}
}
return value
}, [value])
return (
<div>{valueShow}</div>
)
}
export default DateView
\ No newline at end of file
......@@ -11,6 +11,7 @@ import CoordView from './components/CoordView'
import SelectView from './components/SelectView'
import ValueEdit from './components/ValueEdit'
import IDCard from './components/IDCard'
import DateView from './components/DateView'
// import IconPack from '../../../components/IconPack'
import { isObject, isArray, getFieldInfo } from '../../../../utils'
......@@ -154,7 +155,10 @@ const TablePack = (props, ref) => {
if (['RadioButton', 'CheckBox'].includes(widget)) {
return <SelectView {...props} />
}
if(widget === 'IDCard'){
if (['DateTime'].includes(widget)) {
return <DateView {...props} />
}
if (widget === 'IDCard') {
return <IDCard {...props} />
}
return <Text align={alignment} text={value} />
......@@ -272,6 +276,9 @@ const TablePack = (props, ref) => {
if (['RadioButton', 'CheckBox'].includes(widget)) {
return <SelectView {...props} />
}
if (['DateTime'].includes(widget)) {
return <DateView {...props} />
}
return <Text text={value} />
}
},
......
......@@ -1774,9 +1774,9 @@ const dateWidgets = [
format: {
title: '日期格式',
type: 'string',
enum: ['date', 'dateTime', 'year', 'month', 'week', 'quarter'],
enum: ['date', 'dateTime', 'year', 'month', 'week', 'quarter', 'datePeriod'],
default: 'date',
enumNames: ['日期', '日期时间', '日期年份', '日期月份', '日期周', '日期季度'],
enumNames: ['日期', '日期时间', '日期年份', '日期月份', '日期周', '日期季度', '日期时段'],
},
options: {
title: '选项',
......
// 自定义时间组件
import React, { useEffect } from 'react'
import { DatePicker } from 'antd'
import moment from 'moment'
import React, { useEffect, useMemo, useState } from 'react'
import { DatePicker, Select } from 'antd'
import moment, { months } from 'moment'
import locale from 'antd/lib/date-picker/locale/zh_CN'
const DateTime = (props) => {
......@@ -9,6 +9,14 @@ const DateTime = (props) => {
const { value, onChange, schema, addons } = props
const { disabled, format, presetValue, placeholder, options, currentDate, defaultCurrent } = schema
const period = useMemo(() => {
let hour = moment().hour()
if (value && moment(value)._isValid) {
hour = moment(value).hour()
}
return hour < 12 ? '上午' : '下午'
}, [value])
useEffect(() => {
if (addons) {
let value = presetValue || (options === '默认为当前时间' ? moment(new Date()).format('YYYY-MM-DD HH:mm:ss') : '')
......@@ -28,12 +36,25 @@ const DateTime = (props) => {
const dateChange = (date, dateStr) => {
if (date && date._d) {
onChange(moment(date._d).format('YYYY-MM-DD HH:mm:ss'))
if (format === 'datePeriod') {
onChange(moment(date._d).format('YYYY-MM-DD 09:00:00'))
} else {
onChange(moment(date._d).format('YYYY-MM-DD HH:mm:ss'))
}
} else {
onChange('')
}
}
const selectChange = (date) => {
const isValid = moment(value)._isValid
if (isValid) {
onChange(`${moment(value).format('YYYY-MM-DD')} ${date === '上午' ? '09:00:00' : '13:30:00'}`)
} else {
onChange(`${moment().format('YYYY-MM-DD')} ${date === '上午' ? '09:00:00' : '13:30:00'}`)
}
}
const isValid = moment(value)._isValid
const timeOtions = format === 'dateTime' ?
{
......@@ -50,18 +71,32 @@ const DateTime = (props) => {
}
return (
<DatePicker
disabledDate={disabledDate}
disabled={disabled}
placeholder={disabled ? (placeholder || '') : (placeholder || '点击选择日期')}
value={isValid ? moment(value) : null}
onChange={dateChange}
showNow
{...timeOtions}
style={{ width: '100%' }}
locale={locale}
/>
);
<div style={{ display: 'flex' }}>
<DatePicker
disabledDate={disabledDate}
disabled={disabled}
placeholder={disabled ? (placeholder || '') : (placeholder || '点击选择日期')}
value={isValid ? moment(value) : null}
onChange={dateChange}
showNow
{...timeOtions}
style={{ width: '100%' }}
locale={locale}
/>
{
format === 'datePeriod' && (
<Select
value={period}
style={{ width: '70px' }}
onChange={(value) => selectChange(value)}
>
<Select.Option value='上午'>上午</Select.Option>
<Select.Option value='下午'>下午</Select.Option>
</Select>
)
}
</div>
)
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment