-
-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
8 changed files
with
292 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import React, { useEffect, useState } from 'react'; | ||
import Input, { InputProps } from '@uiw/react-input'; | ||
import Popover, { PopoverProps } from '@uiw/react-popover'; | ||
import DatePicker, { DatePickerProps } from '@uiw/react-date-picker'; | ||
import Icon from '@uiw/react-icon'; | ||
import { IProps } from '@uiw/utils'; | ||
import formatter from '@uiw/formatter'; | ||
import './style/date-input-range.less'; | ||
|
||
export interface DateInputRangeProps<V> extends IProps, Omit<InputProps, 'onChange' | 'value'> { | ||
popoverProps?: PopoverProps; | ||
datePickerProps?: DatePickerProps; | ||
value?: Array<V>; | ||
format?: string; | ||
allowClear?: boolean; | ||
bodyStyle?: object; | ||
onChange?: (selectedDate?: Date, dateRange?: Array<Date | undefined>) => void; | ||
} | ||
|
||
export function DateInputRange<V extends string | Date>(props: DateInputRangeProps<V>) { | ||
const { | ||
prefixCls = 'w-dateinputrange', | ||
bodyStyle = undefined, | ||
className, | ||
popoverProps, | ||
datePickerProps, | ||
allowClear = true, | ||
format = 'YYYY/MM/DD', | ||
onChange, | ||
value, | ||
...inputProps | ||
} = props; | ||
|
||
const [dateRange, setDateRange] = useState<Array<Date | undefined>>([]); | ||
useEffect(() => { | ||
let valueTemp: Array<Date> = []; | ||
const propsValue = value; | ||
if (Array.isArray(propsValue) && !!propsValue?.length) { | ||
propsValue.forEach((date, index) => { | ||
valueTemp[index] = typeof propsValue[index] === 'string' ? new Date(date) : (date as Date); | ||
}); | ||
} | ||
setDateRange(valueTemp); | ||
}, [JSON.stringify(value)]); | ||
|
||
function handleChange(cdate: Date | undefined, idx?: number) { | ||
const changeValue = [...dateRange]; | ||
changeValue[idx!] = cdate; | ||
setDateRange(changeValue); | ||
onChange && onChange(cdate, changeValue); | ||
} | ||
|
||
return ( | ||
<div | ||
className={[`${prefixCls}-contents`, `${prefixCls}-inner`].filter(Boolean).join(' ').trim()} | ||
style={{ width: 300, ...bodyStyle }} | ||
> | ||
<Popover | ||
trigger="focus" | ||
placement="bottomLeft" | ||
autoAdjustOverflow | ||
{...popoverProps} | ||
content={ | ||
<DatePicker | ||
date={dateRange[0]} | ||
className={`${prefixCls}-popover`} | ||
{...datePickerProps} | ||
onChange={(selectedDate) => handleChange(selectedDate, 0)} | ||
/> | ||
} | ||
> | ||
<Input | ||
placeholder="请选择日期" | ||
readOnly | ||
{...(inputProps as InputProps)} | ||
// onChange={(v) => console.log('v', v)} | ||
value={dateRange[0] ? formatter(format, dateRange[0]) : ''} | ||
className={[prefixCls, className].filter(Boolean).join(' ').trim()} | ||
/> | ||
</Popover> | ||
|
||
<Icon type="swap-right" verticalAlign="baseline" style={{ fontSize: 21, top: -1, margin: '0px 8px 0px 5px' }} /> | ||
|
||
<Popover | ||
trigger="focus" | ||
placement="bottomLeft" | ||
autoAdjustOverflow | ||
{...popoverProps} | ||
content={ | ||
<DatePicker | ||
date={dateRange[1]} | ||
className={`${prefixCls}-popover`} | ||
{...datePickerProps} | ||
onChange={(selectedDate) => handleChange(selectedDate, 1)} | ||
/> | ||
} | ||
> | ||
<Input | ||
placeholder="请选择日期" | ||
readOnly | ||
{...(inputProps as InputProps)} | ||
value={dateRange[1] ? formatter(format, dateRange[1]) : ''} | ||
// onChange={(v) => console.log('v2', v)} | ||
className={[prefixCls, className].filter(Boolean).join(' ').trim()} | ||
/> | ||
</Popover> | ||
{allowClear && dateRange.length > 0 && ( | ||
<Icon className={`${prefixCls}-close-btn`} color="#ccc" onClick={() => setDateRange([])} type="close" /> | ||
)} | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
@dateinputrange-prefix: ~'w-dateinputrange'; | ||
|
||
.@{dateinputrange-prefix} { | ||
&-contents { | ||
display: flex; | ||
justify-content: space-between; | ||
input { | ||
box-shadow: none; | ||
padding: 0px; | ||
height: 20px; | ||
} | ||
|
||
.w-input-inner { | ||
&:hover { | ||
box-shadow: none !important; | ||
} | ||
|
||
&:focus { | ||
box-shadow: none !important; | ||
} | ||
} | ||
} | ||
|
||
&-inner { | ||
display: flex; | ||
// overflow: hidden; | ||
justify-content: space-between; | ||
outline: none; | ||
border: none; | ||
align-items: center; | ||
border-radius: 3px; | ||
box-shadow: 0 0 0 0 rgba(19, 124, 189, 0), 0 0 0 0 rgba(19, 124, 189, 0), inset 0 0 0 1px rgba(16, 22, 26, 0.15), | ||
inset 0 1px 1px rgba(16, 22, 26, 0.2); | ||
box-sizing: border-box; | ||
background: #fff; | ||
min-height: 30px; | ||
margin: 0 !important; | ||
padding: 3px 10px 3px 10px; | ||
vertical-align: middle; | ||
line-height: 30px; | ||
color: #393e48; | ||
font-weight: 400; | ||
font-size: inherit; | ||
transition: box-shadow 0.3s cubic-bezier(0.4, 1, 0.75, 0.9); | ||
appearance: none; | ||
|
||
&:focus { | ||
box-shadow: 0 0 0 1px #393e48, 0 0 0 3px rgba(57, 62, 72, 0.17), inset 0 1px 1px rgba(16, 22, 26, 0.2); | ||
} | ||
|
||
&:hover { | ||
box-shadow: 0 0 0 1px #6e6e6e, 0 0 0 3px rgba(57, 62, 72, 0), inset 0 1px 1px rgba(16, 22, 26, 0.2); | ||
} | ||
|
||
&:focus&:hover { | ||
box-shadow: 0 0 0 1px #6e6e6e, 0 0 0 3px rgba(57, 62, 72, 0.17), inset 0 1px 1px rgba(16, 22, 26, 0.2); | ||
} | ||
|
||
&:disabled { | ||
box-shadow: none; | ||
background: #dddddd; | ||
opacity: 0.75; | ||
color: #a5a5a5; | ||
cursor: not-allowed; | ||
resize: none; | ||
} | ||
} | ||
} |
Oops, something went wrong.