Skip to content

Commit

Permalink
Add variant="decimal" to Input
Browse files Browse the repository at this point in the history
  • Loading branch information
moroshko committed Oct 21, 2020
1 parent 2d57cc7 commit 41c834c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
6 changes: 5 additions & 1 deletion src/components/Input.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { mergeProps } from "../utils/component";
import Field from "./internal/Field";
import InternalInput from "./internal/InternalInput";

const { TYPES, VARIANTS, COLORS, NUMERIC_REGEX } = InternalInput;
const { TYPES, VARIANTS, COLORS, NUMERIC_REGEX, DECIMAL_REGEX } = InternalInput;

const DEFAULT_PROPS = {
type: InternalInput.DEFAULT_PROPS.type,
Expand All @@ -24,6 +24,10 @@ const DEFAULT_PROPS = {
return "Only 0-9 are allowed";
}

if (variant === "decimal" && DECIMAL_REGEX.test(value) === false) {
return "Invalid";
}

return null;
},
};
Expand Down
10 changes: 10 additions & 0 deletions src/components/Input.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ describe("Input", () => {
expect(input).toHaveAttribute("pattern", "[0-9]*");
});

it("decimal variant", () => {
render(<FormWithInput label="Amount" variant="decimal" />);

const input = screen.getByLabelText("Amount");

expect(input).toHaveAttribute("type", "text");
expect(input).toHaveAttribute("inputmode", "decimal");
expect(input).not.toHaveAttribute("pattern");
});

it("with numericPrefix", () => {
render(
<FormWithInput
Expand Down
22 changes: 16 additions & 6 deletions src/components/internal/InternalInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import useResponsivePropsCSS from "../../hooks/useResponsivePropsCSS";
import { mergeProps } from "../../utils/component";

const TYPES = ["text", "password"];
const VARIANTS = ["text", "numeric"];
const VARIANTS = ["text", "numeric", "decimal"];
const COLORS = ["grey.t05", "white"];

const NUMERIC_REGEX = /^\d*$/;
const DECIMAL_REGEX = /^\d*(\.\d{2})?$/;

const DEFAULT_PROPS = {
type: "text",
Expand All @@ -26,6 +27,7 @@ InternalInput.TYPES = TYPES;
InternalInput.VARIANTS = VARIANTS;
InternalInput.COLORS = COLORS;
InternalInput.NUMERIC_REGEX = NUMERIC_REGEX;
InternalInput.DECIMAL_REGEX = DECIMAL_REGEX;
InternalInput.DEFAULT_PROPS = DEFAULT_PROPS;

function InternalInput(props) {
Expand Down Expand Up @@ -93,6 +95,18 @@ function InternalInput(props) {
},
[pasteAllowed]
);
const variantProps =
variant === "numeric"
? {
// See: https://technology.blog.gov.uk/2020/02/24/why-the-gov-uk-design-system-team-changed-the-input-type-for-numbers
inputMode: "numeric",
pattern: "[0-9]*",
}
: variant === "decimal"
? {
inputMode: "decimal",
}
: {};

return (
<div
Expand All @@ -110,11 +124,7 @@ function InternalInput(props) {
data-parent-name={parentName}
placeholder={placeholder}
type={type}
{...(variant === "numeric" && {
// See: https://technology.blog.gov.uk/2020/02/24/why-the-gov-uk-design-system-team-changed-the-input-type-for-numbers
inputMode: "numeric",
pattern: "[0-9]*",
})}
{...variantProps}
maxLength={maxLength}
disabled={disabled}
onPaste={onPaste}
Expand Down

0 comments on commit 41c834c

Please sign in to comment.