Skip to content

Commit

Permalink
Add type prop to Input (#163)
Browse files Browse the repository at this point in the history
  • Loading branch information
moroshko authored Oct 7, 2020
1 parent 3708579 commit 8b638de
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/components/Input.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import { mergeProps } from "../utils/component";
import Field from "./internal/Field";
import InternalInput from "./internal/InternalInput";

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

const DEFAULT_PROPS = {
type: InternalInput.DEFAULT_PROPS.type,
variant: InternalInput.DEFAULT_PROPS.variant,
color: InternalInput.DEFAULT_PROPS.color,
disabled: false,
Expand All @@ -27,6 +28,7 @@ const DEFAULT_PROPS = {
},
};

Input.TYPES = TYPES;
Input.VARIANTS = VARIANTS;
Input.COLORS = COLORS;
Input.DEFAULT_PROPS = DEFAULT_PROPS;
Expand All @@ -37,6 +39,7 @@ function Input(props) {
DEFAULT_PROPS,
{},
{
type: (type) => TYPES.includes(type),
variant: (variant) => VARIANTS.includes(variant),
numericPrefix: (numericPrefix) =>
typeof numericPrefix === "string" && numericPrefix.length > 0,
Expand All @@ -53,6 +56,7 @@ function Input(props) {
);
const {
name,
type,
variant,
numericPrefix,
numericSuffix,
Expand Down Expand Up @@ -105,6 +109,7 @@ function Input(props) {
<InternalInput
id={label ? inputId : null}
name={name}
type={type}
variant={variant}
numericPrefix={numericPrefix}
numericSuffix={numericSuffix}
Expand All @@ -128,6 +133,7 @@ function Input(props) {

Input.propTypes = {
name: PropTypes.string.isRequired,
type: PropTypes.oneOf(TYPES),
variant: PropTypes.oneOf(VARIANTS),
numericPrefix: PropTypes.string,
numericSuffix: PropTypes.string,
Expand Down
8 changes: 8 additions & 0 deletions src/components/Input.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ describe("Input", () => {
});
});

it("type password", () => {
render(<FormWithInput label="New credit limit" type="password" />);

const input = screen.getByLabelText("New credit limit");

expect(input).toHaveAttribute("type", "password");
});

it("numeric variant", () => {
render(<FormWithInput label="New credit limit" variant="numeric" />);

Expand Down
8 changes: 7 additions & 1 deletion src/components/internal/InternalInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import useBackground from "../../hooks/useBackground";
import useResponsivePropsCSS from "../../hooks/useResponsivePropsCSS";
import { mergeProps } from "../../utils/component";

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

const NUMERIC_REGEX = /^\d*$/;

const DEFAULT_PROPS = {
type: "text",
variant: "text",
color: "grey.t05",
disabled: false,
Expand All @@ -20,6 +22,7 @@ const DEFAULT_PROPS = {
__internal__focus: false,
};

InternalInput.TYPES = TYPES;
InternalInput.VARIANTS = VARIANTS;
InternalInput.COLORS = COLORS;
InternalInput.NUMERIC_REGEX = NUMERIC_REGEX;
Expand All @@ -31,6 +34,7 @@ function InternalInput(props) {
DEFAULT_PROPS,
{},
{
type: (type) => TYPES.includes(type),
variant: (variant) => VARIANTS.includes(variant),
numericPrefix: (numericPrefix) =>
typeof numericPrefix === "string" && numericPrefix.length > 0,
Expand All @@ -48,6 +52,7 @@ function InternalInput(props) {
name,
parentName,
id,
type,
placeholder,
variant,
numericPrefix,
Expand Down Expand Up @@ -104,7 +109,7 @@ function InternalInput(props) {
name={name}
data-parent-name={parentName}
placeholder={placeholder}
type="text"
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",
Expand Down Expand Up @@ -132,6 +137,7 @@ InternalInput.propTypes = {
name: PropTypes.string.isRequired,
parentName: PropTypes.string,
id: PropTypes.string,
type: PropTypes.oneOf(TYPES),
placeholder: PropTypes.string,
variant: PropTypes.oneOf(VARIANTS),
numericPrefix: PropTypes.string,
Expand Down
16 changes: 15 additions & 1 deletion website/src/pages/components/input/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import RadioGroupSetting, {
import { formatCode, nonDefaultProps } from "../../../utils/formatting";

const { useTheme, Input } = allDesignSystem;
const { VARIANTS, COLORS, DEFAULT_PROPS } = Input;
const { TYPES, VARIANTS, COLORS, DEFAULT_PROPS } = Input;
const scope = allDesignSystem;

const typeOptions = getRadioOptions(TYPES);
const variantOptions = getRadioOptions(VARIANTS);
const colorOptions = getRadioOptions(COLORS);
const isOptionalOptions = getCheckboxOptions();
Expand All @@ -20,6 +21,7 @@ const isDisabledOptions = getCheckboxOptions();

function InputPage() {
const theme = useTheme();
const [type, setType] = useState(DEFAULT_PROPS.type);
const [variant, setVariant] = useState(DEFAULT_PROPS.variant);
const [color, setColor] = useState(DEFAULT_PROPS.color);
const [optional, setIsOptional] = useState(DEFAULT_PROPS.optional);
Expand All @@ -43,6 +45,11 @@ function InputPage() {
prop: "name",
value: variant === "numeric" ? "newCreditLimit" : "name",
},
{
prop: "type",
value: type,
defaultValue: DEFAULT_PROPS.type,
},
{
prop: "variant",
value: variant,
Expand Down Expand Up @@ -110,6 +117,13 @@ function InputPage() {
}}
>
<RadioGroupSetting
heading="Type"
options={typeOptions}
selectedValue={type}
setSelectedValue={setType}
/>
<RadioGroupSetting
css={{ marginLeft: theme.space[13] }}
heading="Variant"
options={variantOptions}
selectedValue={variant}
Expand Down
9 changes: 9 additions & 0 deletions website/src/pages/kitchen-sink/components/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import KitchenSinkForm from "../../../components/kitchen-sink/KitchenSinkForm";

function FormWithInput({
initialValue = "",
type,
placeholder,
variant,
numericPrefix,
Expand All @@ -25,6 +26,7 @@ function FormWithInput({
>
<Input
name="name"
type={type}
placeholder={placeholder}
variant={variant}
numericPrefix={numericPrefix}
Expand All @@ -42,6 +44,7 @@ function FormWithInput({

FormWithInput.propTypes = {
initialValue: PropTypes.string,
type: PropTypes.oneOf(Input.TYPES),
placeholder: PropTypes.string,
variant: PropTypes.oneOf(Input.VARIANTS),
numericPrefix: PropTypes.string,
Expand Down Expand Up @@ -79,6 +82,12 @@ function KitchenSinkInput() {

<FormWithInput label="White focus" __internal__focus />

<FormWithInput
label="Password"
type="password"
initialValue="Some value"
/>

<FormWithInput label="With placeholder" placeholder="Placeholder" />

<FormWithInput label="With value" initialValue="Some value" />
Expand Down

1 comment on commit 8b638de

@vercel
Copy link

@vercel vercel bot commented on 8b638de Oct 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.