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(time-picker): 新增自定义触发器(#2899) #2902

Closed
wants to merge 3 commits into from
Closed
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
5 changes: 5 additions & 0 deletions .changeset/curly-carrots-give.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hi-ui/hiui": patch
---

feat(time-picker): 新增自定义触发器
5 changes: 5 additions & 0 deletions .changeset/dull-years-listen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hi-ui/time-picker": minor
---

feat: 新增自定义触发器
94 changes: 56 additions & 38 deletions packages/ui/time-picker/src/TimePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export const TimePicker = forwardRef<HTMLDivElement | null, TimePickerProps>(
overlay,
size = 'md',
invalid = false,
customRender,
},
ref
) => {
Expand Down Expand Up @@ -257,49 +258,62 @@ export const TimePicker = forwardRef<HTMLDivElement | null, TimePickerProps>(

return (
<div ref={ref} role={role} className={cls}>
<div ref={setAttachEl} className={`${prefixCls}__input-wrapper`}>
<Input
size={size}
isFitContent={appearance === 'unset'}
ref={inputRef}
onValidChange={setIsInputValid}
disabled={inputReadonly || disabled}
type={type}
placeholders={placeholder}
prefix={prefixCls}
format={format}
hourStep={hourStep}
secondStep={secondStep}
minuteStep={minuteStep}
disabledHours={disabledHours}
disabledMinutes={disabledMinutes}
disabledSeconds={disabledSeconds}
value={cacheValue}
onChange={onCacheChange}
onFocus={() => {
showPopperRef.current = true
setShowPopper(true)
}}
/>
{customRender ? (
<div
className={`${prefixCls}__function-button`}
ref={setAttachEl}
onClick={() => {
showPopperRef.current = !showPopperRef.current
setShowPopper((pre) => !pre)
showPopperRef.current = true
setShowPopper(true)
}}
>
{showPopper ? (
<CloseCircleFilled
className={`${prefixCls}__close-button`}
onClick={() => {
onCacheChange(type === 'single' ? [''] : ['', ''])
}}
/>
) : (
<TimeOutlined />
)}
{typeof customRender === 'function' ? customRender(cacheValue) : customRender}
</div>
</div>
) : (
<div ref={setAttachEl} className={`${prefixCls}__input-wrapper`}>
<Input
size={size}
isFitContent={appearance === 'unset'}
ref={inputRef}
onValidChange={setIsInputValid}
disabled={inputReadonly || disabled}
type={type}
placeholders={placeholder}
prefix={prefixCls}
format={format}
hourStep={hourStep}
secondStep={secondStep}
minuteStep={minuteStep}
disabledHours={disabledHours}
disabledMinutes={disabledMinutes}
disabledSeconds={disabledSeconds}
value={cacheValue}
onChange={onCacheChange}
onFocus={() => {
showPopperRef.current = true
setShowPopper(true)
}}
/>
<div
className={`${prefixCls}__function-button`}
onClick={() => {
showPopperRef.current = !showPopperRef.current
setShowPopper((pre) => !pre)
}}
>
{showPopper ? (
<CloseCircleFilled
className={`${prefixCls}__close-button`}
onClick={() => {
onCacheChange(type === 'single' ? [''] : ['', ''])
}}
/>
) : (
<TimeOutlined />
)}
</div>
</div>
)}

<Popper
{...(overlay || {})}
unmountOnClose={false}
Expand Down Expand Up @@ -409,6 +423,10 @@ export interface TimePickerProps extends ExtendType {
* @default false
*/
invalid?: boolean
/**
* 自定义触发器
*/
customRender?: React.ReactNode | ((option: string[]) => React.ReactNode)
}

if (__DEV__) {
Expand Down
45 changes: 45 additions & 0 deletions packages/ui/time-picker/stories/custom-renser.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import React, { useState } from 'react'
import TimePicker from '../src'
import Input from '@hi-ui/input'

/**
* @title 新增自定义触发器
*/
export const CustomRender = () => {
const [basicValue, setBasicValue] = useState<string | string[]>(['12:00:00'])
const [rangeValue, setRangeValue] = useState<string | string[]>(['11:11:11', '12:00:00'])

return (
<>
<h1>CustomRender-Basic</h1>
<div className="time-picker-custom-render__wrap">
<TimePicker
placeholder={['请选择时间']}
value={basicValue}
onChange={(e) => {
console.log('basic-default', e)
setBasicValue(e)
}}
customRender={(data) => {
return <Input value={data[0]} readOnly placeholder="请选择" />
}}
/>
</div>
<h1>CustomRender-Range</h1>
<div className="time-picker-range-custom-render__wrap">
<TimePicker
value={rangeValue}
placeholder={['请选择开始时间', '请选择结束时间']}
onChange={(value) => {
console.log('range-value', value)
setRangeValue(value)
}}
type="range"
customRender={(data) => {
return <Input value={data.join('-')} readOnly placeholder="请选择" />
}}
/>
</div>
</>
)
}
1 change: 1 addition & 0 deletions packages/ui/time-picker/stories/index.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export * from './disabled.stories'
export * from './range.stories'
export * from './format.stories'
export * from './custom-disabled.stories'
export * from './custom-renser.stories'

export default {
title: 'Data Input/TimePicker',
Expand Down