-
Notifications
You must be signed in to change notification settings - Fork 379
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: 给numberInput组件增加dynamicDecimal属性,支持用户保留完整的小数位数 #1945
Open
davedavehong
wants to merge
6
commits into
youzan:master
Choose a base branch
from
davedavehong:feat/add-number-input-dynamic-decimal
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d781474
feat: 给numberInput组件增加dynamicDecimal属性,支持用户保留完整的小数位数
davedavehong 8df8252
feat: 改成让 decimal 属性支持 -1来实现动态小数位;在NumberInput中增加demo
davedavehong 3d4deae
feat: 删除dynamicDecimal字段的文档说明
davedavehong b55b721
feat: 优化demo位置;增加单测
davedavehong 3026ebc
feat: 调整为dynamicDecimal字段表示小数点位自适应
davedavehong 43e903b
feat: 调整文案
davedavehong File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -12,6 +12,9 @@ export function isDecimal(value: string): boolean { | |
return /^[-+]?\d*\.?\d*$/.test(value); | ||
} | ||
|
||
// 表示小数点位数取用户实际输入的小数点位数 | ||
const DYNAMIC_DECIMAL_SIGN = -1; | ||
|
||
export function getDelta(decimal: number, step?: number): Decimal { | ||
if (Number.isFinite(step)) { | ||
return new Decimal(step); | ||
|
@@ -20,6 +23,18 @@ export function getDelta(decimal: number, step?: number): Decimal { | |
return new Decimal(1).div(Math.pow(10, decimal)); | ||
} | ||
|
||
/** | ||
* 取小数点后数字长度 | ||
* @param decimal | ||
* @example (3.12) => 2 | ||
*/ | ||
function getDecimalsLength(decimal: Decimal) { | ||
const DecimalsRegexMatch = /\.(\d*)$/.exec(decimal.toString()); | ||
if (DecimalsRegexMatch) { | ||
return DecimalsRegexMatch[1].length; | ||
} | ||
return 0; | ||
} | ||
function fromPotential(v: number | string | undefined): Decimal | null { | ||
v = String(v); | ||
if (isDecimal(v)) { | ||
|
@@ -93,7 +108,11 @@ export function normalizeValue( | |
} | ||
const popState = pop && showTooltip ? { pop } : {}; | ||
return { | ||
input: decimal.toFixed(decimalPlaces), | ||
input: decimal.toFixed( | ||
decimalPlaces === DYNAMIC_DECIMAL_SIGN | ||
? getDecimalsLength(decimal) | ||
: decimalPlaces | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 逻辑收到 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
), | ||
value: decimal, | ||
...popState, | ||
}; | ||
|
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,21 @@ | ||
--- | ||
order: 8 | ||
zh-CN: | ||
title: 小数点位数取用户实际输入的小数点位数 | ||
placehoder: 请输入数字 | ||
|
||
en-US: | ||
title: dynamicDecimal | ||
placehoder: please enter number | ||
--- | ||
|
||
```jsx | ||
import { NumberInput } from 'zent'; | ||
|
||
ReactDOM.render( | ||
<div> | ||
<NumberInput value={2.13} decimal={-1} placeholder="{i18n.placehoder}" integer={false} /> | ||
</div>, | ||
mountNode | ||
); | ||
``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我觉得
Infinity
更合适