forked from ant-design/pro-components
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(field): support new valueType
second
(ant-design#1177)
* feat(field): support new valueType `second` * fix test
- Loading branch information
1 parent
faafadd
commit 3c45102
Showing
7 changed files
with
268 additions
and
35 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,65 @@ | ||
import React from 'react'; | ||
import { InputNumber } from 'antd'; | ||
import { ProFieldFC } from '../../index'; | ||
|
||
export type FieldDigitProps = { | ||
text: number; | ||
}; | ||
|
||
/** | ||
* 格式化秒 | ||
* @param result | ||
* @returns {string} | ||
*/ | ||
export function formatSecond(result: number) { | ||
let formatText = ''; | ||
const h = Math.floor((result / 3600) % 24); | ||
const m = Math.floor((result / 60) % 60); | ||
const s = Math.floor(result % 60); | ||
formatText = `${s}秒`; | ||
if (m > 0) { | ||
formatText = `${m}分钟${formatText}`; | ||
} | ||
if (h > 0) { | ||
formatText = `${h}小时${formatText}`; | ||
} | ||
return formatText; | ||
} | ||
|
||
/** | ||
* 格式化秒 | ||
* @param FieldSecond | ||
*/ | ||
const Second: ProFieldFC<FieldDigitProps> = ( | ||
{ text, mode: type, render, renderFormItem, fieldProps, ...rest }, | ||
ref, | ||
) => { | ||
if (type === 'read') { | ||
const secondText = formatSecond(Number(text) as number); | ||
const dom = <span ref={ref}>{secondText}</span>; | ||
if (render) { | ||
return render(text, { mode: type, ...fieldProps }, dom); | ||
} | ||
return dom; | ||
} | ||
if (type === 'edit' || type === 'update') { | ||
const dom = ( | ||
<InputNumber | ||
ref={ref} | ||
min={0} | ||
style={{ | ||
width: '100%', | ||
}} | ||
{...rest} | ||
{...fieldProps} | ||
/> | ||
); | ||
if (renderFormItem) { | ||
return renderFormItem(text, { mode: type, ...fieldProps }, dom); | ||
} | ||
return dom; | ||
} | ||
return null; | ||
}; | ||
|
||
export default React.forwardRef(Second); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -283,6 +283,7 @@ describe('Field', () => { | |
'progress', | ||
'percent', | ||
'digit', | ||
'second', | ||
'code', | ||
'jsonCode', | ||
'rate', | ||
|
Oops, something went wrong.