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: 给numberInput组件增加dynamicDecimal属性,支持用户保留完整的小数位数 #1945

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
13 changes: 13 additions & 0 deletions packages/zent/__tests__/number-input.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ describe('NumberInput', () => {
expect(wrapper.state('value').cmp(new Decimal(0))).toBe(0);
});

it('dynamic decimal point precision', () => {
const wrapper = mount(
<NumberInput value={2.123} dynamicDecimal={true} integer={false} />
);
expect(wrapper.state('input')).toBe('2.123');
const input = wrapper.find('input');
input.simulate('blur');
input.instance().value = '4.1';
input.simulate('change');
input.simulate('blur');
expect(wrapper.state('input')).toBe('4.1');
});

it('change value within min and max ', () => {
let wrapper = mount(<NumberInput showStepper value={2} min={0} max={3} />);
wrapper.find('.zent-number-input-arrowup').simulate('click');
Expand Down
11 changes: 11 additions & 0 deletions packages/zent/src/form/demos/2.builtin-field.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ zh-CN:
tagText2: 书籍
tagText3: 旅行
ageText: 年龄
weight: 体重
colorText: 喜欢的颜色
dateRangeText: 身份证有效期
dateRangeValidationErrors: 请填写有效期
Expand Down Expand Up @@ -50,6 +51,7 @@ en-US:
tagText2: Book
tagText3: Travel
ageText: Age
weight: Weight
colorText: Favorite color
dateRangeText: Validity period
dateRangeValidationErrors: Please select the dateRange
Expand Down Expand Up @@ -139,6 +141,15 @@ function Component() {
showStepper: true,
}}
/>
<FormNumberInputField
name="weight"
label="{i18n.weight}:"
defaultValue={67.23}
props={{
integer: false,
dynamicDecimal: true,
}}
/>
<FormColorPickerField
name="color"
label="{i18n.colorText}:"
Expand Down
20 changes: 16 additions & 4 deletions packages/zent/src/number-input/NumberInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface INumberInputDecimalProps extends INumberInputCommonProps {
value?: string | number;
onChange?: (value: string) => void;
decimal?: number;
dynamicDecimal?: boolean;
onInput?: (value: string) => void;
min?: number | string;
}
Expand Down Expand Up @@ -121,7 +122,14 @@ function getStateFromProps(
max,
delta: Decimals.getDelta(props.decimal, props.step),
...(updateValueInState
? Decimals.normalizeValue(props.value, min, max, props.decimal)
? Decimals.normalizeValue(
props.value,
min,
max,
props.decimal,
false,
props.dynamicDecimal
)
: {}),
};
}
Expand All @@ -136,6 +144,7 @@ export class NumberInput extends Component<
type: 'number',
decimal: 0,
size: 'normal',
dynamicDecimal: false,
};

static contextType = DisabledContext;
Expand Down Expand Up @@ -218,14 +227,15 @@ export class NumberInput extends Component<
const { onBlur } = this.props;
onBlur?.(e);
} else {
const { onChange, decimal, showTooltip } = this.props;
const { onChange, decimal, showTooltip, dynamicDecimal } = this.props;
const { input, min, max } = this.state as INumberInputDecimalState;
const normalized = Decimals.normalizeValue(
input,
min,
max,
decimal,
showTooltip
showTooltip,
dynamicDecimal
);
onChange?.(normalized.input);
this.setState(normalized, () => {
Expand Down Expand Up @@ -363,7 +373,9 @@ export class NumberInput extends Component<
props.value,
nextState.min,
nextState.max,
props.decimal
props.decimal,
false,
props.dynamicDecimal
);
nextState.value = value;
nextState.input = input;
Expand Down
1 change: 1 addition & 0 deletions packages/zent/src/number-input/README_en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Default value type is string. Under integer mode, value type is number, with def
| integer | Integer mode | `boolean` | `false` | | No |
| decimal | Decimal | `number` | `0` | | No |
| step | Step used in stepper | `number` | | | No |
| dynamicDecimal | show decimal as input | `boolean` | `false` | | No |
| min | Minimum value in the range | `number` | | | No |
| max | Maximum value in the range | `number` | | | No |
| placeholder | Placeholder text | `string` | `''` | | No |
Expand Down
1 change: 1 addition & 0 deletions packages/zent/src/number-input/README_zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ group: 信息录入
| integer | 整数模式 | `boolean` | `false` | | 否 |
| decimal | 数值精度 | `number` | `0` | | 否 |
| step | 步进 | `number` | 整数模式为 `1`,小数模式根据精度而定 | | 否 |
| dynamicDecimal | 是否完整保存用户输入的小数位数 | `boolean` | `false` | | 否 |
| min | 数值范围最小值 | `number` | | | 否 |
| max | 数值范围最大值 | `number` | | | 否 |
| placeholder | 原生 placeholder 文案 | `string` | `''` | | 否 |
Expand Down
19 changes: 17 additions & 2 deletions packages/zent/src/number-input/decimal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,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)) {
Expand All @@ -42,7 +54,8 @@ export function normalizeValue(
min: Decimal | null,
max: Decimal | null,
decimalPlaces: number,
showTooltip?: boolean
showTooltip?: boolean,
dynamicDecimal?: boolean
): {
input: string;
value: Decimal;
Expand Down Expand Up @@ -93,7 +106,9 @@ export function normalizeValue(
}
const popState = pop && showTooltip ? { pop } : {};
return {
input: decimal.toFixed(decimalPlaces),
input: decimal.toFixed(
dynamicDecimal ? getDecimalsLength(decimal) : decimalPlaces
),
value: decimal,
...popState,
};
Expand Down
1 change: 1 addition & 0 deletions packages/zent/src/number-input/demos/2.decimal-point.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ ReactDOM.render(
step={1}
placeholder="{i18n.placehoder}"
/>
<NumberInput value={2.123} dynamicDecimal={true} placeholder="{i18n.placehoder}" integer={false} />
</div>,
mountNode
);
Expand Down