@@ -26,9 +26,11 @@ type TimeRangeSelectorProps = {
2626 onTimeButtonChange : ( time : TimeRangeButton ) => void
2727 labelSize ?: 'small' | 'default'
2828 bindRef ?: any
29+ hideBtns ?: TimeRangeButton [ ]
30+ defaultTimeButton ?: TimeRangeButton
2931}
3032const TimeRangeSelector = ( props : TimeRangeSelectorProps ) => {
31- const { initialTimeButton, initialDatePickerValue, onTimeRangeChange, hideTitle, onTimeButtonChange, labelSize = 'default' , bindRef } = props
33+ const { initialTimeButton, initialDatePickerValue, onTimeRangeChange, hideTitle, onTimeButtonChange, labelSize = 'default' , bindRef, hideBtns = [ ] , defaultTimeButton = 'hour' } = props
3234 const [ timeButton , setTimeButton ] = useState ( initialTimeButton || '' ) ;
3335 const [ datePickerValue , setDatePickerValue ] = useState < RangeValue > ( initialDatePickerValue || [ null , null ] ) ;
3436 useEffect ( ( ) => {
@@ -37,7 +39,7 @@ const TimeRangeSelector = (props: TimeRangeSelectorProps) => {
3739 }
3840 } , [ bindRef ] )
3941 // 根据选择的时间范围计算开始和结束时间
40- const calculateTimeRange = ( curBtn : 'hour' | 'day' | 'threeDays' | 'sevenDays' ) => {
42+ const calculateTimeRange = ( curBtn : TimeRangeButton ) => {
4143 const currentSecond = Math . floor ( Date . now ( ) / 1000 ) ; // 当前秒级时间戳
4244 let startMin = currentSecond - 60 * 60
4345 switch ( curBtn ) {
@@ -75,15 +77,15 @@ const TimeRangeSelector = (props: TimeRangeSelectorProps) => {
7577 calculateTimeRange ( e . target . value ) ;
7678 } ;
7779 const reset = ( ) => {
78- setTimeButton ( 'hour' )
79- calculateTimeRange ( 'hour' )
80+ setTimeButton ( defaultTimeButton )
81+ calculateTimeRange ( defaultTimeButton )
8082 setDatePickerValue ( null )
8183 }
8284
8385 // 处理日期选择器的变化
8486 const handleDatePickerChange = ( dates : RangeValue ) => {
85- setTimeButton ( dates ? '' : 'hour' )
86- onTimeButtonChange ?.( dates ? '' : 'hour' )
87+ setTimeButton ( dates ? '' : defaultTimeButton )
88+ onTimeButtonChange ?.( dates ? '' : defaultTimeButton )
8789 setDatePickerValue ( dates ) ;
8890 if ( dates && Array . isArray ( dates ) && dates . length === 2 ) {
8991 const [ startDate , endDate ] = dates ;
@@ -94,7 +96,7 @@ const TimeRangeSelector = (props: TimeRangeSelectorProps) => {
9496 }
9597 }
9698 if ( ! dates ) {
97- calculateTimeRange ( 'hour' )
99+ calculateTimeRange ( defaultTimeButton )
98100 }
99101 } ;
100102
@@ -109,10 +111,10 @@ const TimeRangeSelector = (props: TimeRangeSelectorProps) => {
109111 < div className = "flex flex-nowrap items-center pt-btnybase mr-btnybase" >
110112 { ! hideTitle && < label className = { `whitespace-nowrap ` } > { $t ( '时间' ) } :</ label > }
111113 < Radio . Group className = "whitespace-nowrap" value = { timeButton } onChange = { handleRadioChange } buttonStyle = "solid" >
112- < Radio . Button value = "hour" > { $t ( '近1小时' ) } </ Radio . Button >
113- < Radio . Button value = "day" > { $t ( '近24小时' ) } </ Radio . Button >
114- < Radio . Button value = "threeDays" > { $t ( '近3天' ) } </ Radio . Button >
115- < Radio . Button className = "rounded-e-none" value = "sevenDays" > { $t ( '近7天' ) } </ Radio . Button >
114+ { hideBtns ?. length && hideBtns . includes ( 'hour' ) ? null : < Radio . Button value = "hour" > { $t ( '近1小时' ) } </ Radio . Button > }
115+ { hideBtns ?. length && hideBtns . includes ( 'day' ) ? null : < Radio . Button value = "day" > { $t ( '近24小时' ) } </ Radio . Button > }
116+ { hideBtns ?. length && hideBtns . includes ( 'threeDays' ) ? null : < Radio . Button value = "threeDays" > { $t ( '近3天' ) } </ Radio . Button > }
117+ { hideBtns ?. length && hideBtns . includes ( 'sevenDays' ) ? null : < Radio . Button className = "rounded-e-none" value = "sevenDays" > { $t ( '近7天' ) } </ Radio . Button > }
116118 </ Radio . Group >
117119 < DatePicker . RangePicker
118120 value = { datePickerValue }
0 commit comments