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

[core] feat(NumericInput): support localization #4388

Merged
merged 20 commits into from
Nov 16, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
applied suggested adjustments
  • Loading branch information
hunterxyz committed Oct 16, 2020
commit 1860be75c8b0a4282b39b149d35ad179894a664f
16 changes: 9 additions & 7 deletions packages/core/src/components/forms/numericInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,11 @@ export class NumericInput extends AbstractPureComponent2<HTMLInputProps & INumer
}

private static getValue(props: INumericInputProps, stateValue: string) {
if (props.value != null) {
return props.value.toString();
} else {
if (props.value === null || props.value === undefined) {
return stateValue;
}

return props.value.toString();
}

private static roundAndClampValue(
Expand Down Expand Up @@ -331,13 +331,14 @@ export class NumericInput extends AbstractPureComponent2<HTMLInputProps & INumer
const didMaxChange = this.props.max !== prevProps.max;
const didBoundsChange = didMinChange || didMaxChange;
const didLocaleChange = this.props.locale !== prevProps.locale;
const didValueChange = this.state.value !== prevState.value;

if ((didBoundsChange && this.state.value !== prevState.value) || didLocaleChange) {
if ((didBoundsChange && didValueChange) || didLocaleChange) {
// we clamped the value due to a bounds change, so we should fire the change callback
const valueAsString = parseStringToStringNumber(prevState.value, prevProps.locale);
const localizedValue = toLocaleString(parseFloat(valueAsString), this.props.locale);

this.props.onValueChange?.(Number(valueAsString), localizedValue, this.inputElement);
this.props.onValueChange?.(+valueAsString, localizedValue, this.inputElement);
}
}

Expand Down Expand Up @@ -390,8 +391,9 @@ export class NumericInput extends AbstractPureComponent2<HTMLInputProps & INumer
const { intent, max, min, locale } = this.props;
const value = Number(parseStringToStringNumber(this.state.value, locale));
const disabled = this.props.disabled || this.props.readOnly;
const isIncrementDisabled = max !== undefined && +value >= max;
const isDecrementDisabled = min !== undefined && +value <= min;
const isIncrementDisabled = max !== undefined && value >= max;
const isDecrementDisabled = min !== undefined && value <= min;

return (
<ButtonGroup className={Classes.FIXED} key="button-group" vertical={true}>
<Button
Expand Down
15 changes: 7 additions & 8 deletions packages/core/src/components/forms/numericInputUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ function getDecimalSeparator(locale: string) {
return result && result[1] || ".";
}

export function toLocaleString(num: number, locale?: string) {
locale = locale || "en-US";

export function toLocaleString(num: number, locale: string = "en-US") {
return sanitizeNumericInput(num.toLocaleString(locale), locale);
}

Expand All @@ -48,14 +46,15 @@ export function getValueOrEmptyValue(value: number | string = "") {
}

function transformLocalizedNumberToStringNumber(character: string, locale: string) {
adidahiya marked this conversation as resolved.
Show resolved Hide resolved
const charactersMap = new Array(10).fill("").map((_value, index) => index.toLocaleString(locale));
const jsNumber = charactersMap.indexOf(character);
const jsNumber = new Array(10).fill("")
.map(value => value.toLocaleString(locale))
.indexOf(character);

if (jsNumber !== -1) {
return jsNumber;
} else {
if (jsNumber === -1) {
return character;
}

return jsNumber;
}

export function parseStringToStringNumber(value: number | string, locale: string | undefined): string {
adidahiya marked this conversation as resolved.
Show resolved Hide resolved
Expand Down