Skip to content

Commit

Permalink
NumberInput: Allow to pass in a locale for improved number formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
gu-stav committed Aug 29, 2022
1 parent 170877d commit fb61d61
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
9 changes: 6 additions & 3 deletions packages/strapi-design-system/src/NumberInput/NumberInput.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState, useRef } from 'react';
import React, { useState, useRef, useMemo } from 'react';
import PropTypes from 'prop-types';
import CarretDown from '@strapi/icons/CarretDown';
import styled from 'styled-components';
Expand Down Expand Up @@ -36,6 +36,7 @@ export const NumberInput = React.forwardRef(
error,
label,
labelAction,
locale: defaultLocale,
id,
onValueChange,
value,
Expand All @@ -49,8 +50,9 @@ export const NumberInput = React.forwardRef(
// inputValue should ALWAYS be a string. value should ALWAYS stay a number
const [inputValue, setInputValue] = useState(value === undefined || value === null ? INITIAL_VALUE : String(value));
const generatedId = useId('numberinput', id);
const numberParserRef = useRef(new NumberParser(getDefaultLocale()));
const numberFormaterRef = useRef(new NumberFormatter(getDefaultLocale(), { maximumSignificantDigits: 21 }));
const locale = useMemo(() => defaultLocale || getDefaultLocale(), [defaultLocale]);
const numberParserRef = useRef(new NumberParser(locale));
const numberFormaterRef = useRef(new NumberFormatter(locale, { maximumSignificantDigits: 21 }));

const handleChange = (e) => {
const nextValue = e.target.value;
Expand Down Expand Up @@ -242,6 +244,7 @@ NumberInput.propTypes = {
id: PropTypes.string,
label: PropTypes.string,
labelAction: PropTypes.element,
locale: PropTypes.string,
name: PropTypes.string.isRequired,
onValueChange: PropTypes.func.isRequired,
required: PropTypes.bool,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ NumberInputs can be used without a label.
<Canvas>
<Story name="withoutLabel">
{() => {
const [content, setContent] = useState();
const [content, setContent] = useState(0);
return (
<Box padding={10}>
<NumberInput
Expand Down Expand Up @@ -289,6 +289,31 @@ NumberInputs are always found in forms. They give users the possibility to add o
</Story>
</Canvas>

## With locale

<Canvas>
<Story name="locale">
{() => {
const [content, setContent] = useState();
return (
<Box padding={10}>
<NumberInput
placeholder="This is a content placeholder"
label="Content"
name="content"
locale="de"
onValueChange={(value) => setContent(value)}
value={content}
/>
<Box as="p" padding={4} background="neutral100">
<Typography>{`The value is ${content}`}</Typography>
</Box>
</Box>
);
}}
</Story>
</Canvas>

## Props

<ArgsTable of={NumberInput} />

0 comments on commit fb61d61

Please sign in to comment.