-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add InputTimeComp and fix some bugs (#434)
- Loading branch information
1 parent
6682548
commit cb95d39
Showing
8 changed files
with
167 additions
and
79 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { InputNumber, Select } from 'antd'; | ||
import { useState } from 'react'; | ||
|
||
interface InputTimeCompProps { | ||
onChange?: (value: number | null) => void; | ||
value?: number; | ||
} | ||
|
||
type UnitType = 'second' | 'minute' | 'hour'; | ||
|
||
const SELECT_OPTIONS = [ | ||
{ | ||
label: '秒', | ||
value: 'second', | ||
}, | ||
{ | ||
label: '分钟', | ||
value: 'minute', | ||
}, | ||
{ | ||
label: '小时', | ||
value: 'hour', | ||
}, | ||
]; | ||
|
||
export default function InputTimeComp({ onChange, value }: InputTimeCompProps) { | ||
const [unit, setUnit] = useState<UnitType>('minute'); | ||
|
||
const durationChange = (value: number | null) => { | ||
if (!value || unit === 'second') onChange?.(value); | ||
if (unit === 'minute') { | ||
onChange?.(value! * 60); | ||
} | ||
if (unit === 'hour') { | ||
onChange?.(value! * 3600); | ||
} | ||
}; | ||
const getValue = (value: number | undefined) => { | ||
if (!value) return value; | ||
if (unit === 'minute') { | ||
return Math.floor(value / 60); | ||
} | ||
if (unit === 'hour') { | ||
return Math.floor(value / 3600); | ||
} | ||
return value; | ||
}; | ||
|
||
return ( | ||
<InputNumber | ||
min={1} | ||
onChange={durationChange} | ||
value={getValue(value)} | ||
addonAfter={ | ||
<Select | ||
defaultValue={'minute'} | ||
onChange={(val: UnitType) => setUnit(val)} | ||
options={SELECT_OPTIONS} | ||
/> | ||
} | ||
/> | ||
); | ||
} |
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
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
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
Oops, something went wrong.