Skip to content

Commit

Permalink
Merge pull request #147 from AppQuality/password-input
Browse files Browse the repository at this point in the history
feat: Handle password input
  • Loading branch information
iacopolea authored Dec 19, 2023
2 parents a552e85 + 8935496 commit 4584235
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 11 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
"@types/react-transition-group": "^4.4.1",
"@types/styled-components": "^5.1.10",
"auto": "^10.29.2",
"babel-loader": "^8.1.0",
"chromatic": "^5.9.0",
"copyfiles": "^2.4.1",
"cross-env": "^7.0.3",
Expand Down
5 changes: 3 additions & 2 deletions src/stories/button/Button.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import styled, { DefaultTheme } from "styled-components";
import getButtonSize from "./getButtonSize";
import getButtonStyle from "./getButtonStyle";
import { ButtonComponent } from "./_component";
import { ButtonProps } from "./_types";
import getButtonSize from "./getButtonSize";
import getButtonStyle from "./getButtonStyle";

export const Button = styled(ButtonComponent)(
({
Expand All @@ -13,6 +13,7 @@ export const Button = styled(ButtonComponent)(
flat = false,
squared = false,
variant = false,
alt,
}: ButtonProps & { theme: DefaultTheme }) => {
let styles = getButtonStyle(type, flat, disabled, variant, theme);
styles += getButtonSize(size, squared, theme);
Expand Down
2 changes: 2 additions & 0 deletions src/stories/button/_component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ export const ButtonComponent = ({
flat,
squared,
variant,
alt,
children,
...props
}: ButtonProps) => {
let Component = as;
return (
<Component
alt={alt}
disabled={disabled}
type={htmlType}
className={className}
Expand Down
1 change: 1 addition & 0 deletions src/stories/button/_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@ export type ButtonProps = BaseProps & {
* Optional form prop to submit a form somewhere else
*/
form?: string;
alt?: string;
};
11 changes: 10 additions & 1 deletion src/stories/form/input/Input.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Meta, Story } from "@storybook/react";
import Input from "./Input";
import { Story, Meta } from "@storybook/react";

export default {
title: "Inputs",
Expand Down Expand Up @@ -30,3 +30,12 @@ export const ReadonlyInput: Story = () => (
disabled={true}
/>
);

export const PasswordInput: Story = () => (
<Input
id="input"
type="password"
placeholder="Placeholder"
disabled={false}
/>
);
29 changes: 21 additions & 8 deletions src/stories/form/input/Input.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Search } from "react-bootstrap-icons";
import { useState } from "react";
import styled from "styled-components";
import { InvalidFocusStyle, InvalidInputStyle } from "../_style";
import PasswordIcon from "./inputIcons/PasswordIcon";
import SearchIcon from "./inputIcons/SearchIcon";

const Input = ({
id,
Expand All @@ -13,6 +15,7 @@ const Input = ({
extra,
onChange,
autocomplete = true,
i18n,
}: {
id: string;
type: string;
Expand All @@ -24,23 +27,27 @@ const Input = ({
isInvalid?: boolean;
extra?: any;
onChange?: (val: string) => void;
i18n?: {
showPassword?: string;
hidePassword?: string;
};
}) => {
const [currentType, setType] = useState(type);
return (
<StyledInput type={type} isInvalid={isInvalid} className={className}>
<input
id={id}
type={type}
type={currentType}
placeholder={placeholder}
disabled={disabled}
value={value}
autoComplete={autocomplete ? null : "off"}
onChange={(e) => onChange && onChange(e.target.value)}
{...extra}
/>
{type === "search" && (
<span className="input-group-text">
<Search />
</span>
{type === "search" && <SearchIcon />}
{type === "password" && (
<PasswordIcon i18n={i18n} type={currentType} setType={setType} />
)}
</StyledInput>
);
Expand All @@ -52,12 +59,18 @@ export const StyledInput = styled.div<{ type: string; isInvalid?: boolean }>`
flex-wrap: wrap;
align-items: stretch;
width: 100%;
.input-group-text {
.input-group-text,
.input-group-button {
position: absolute;
right: 15px;
top: 29%;
display: flex;
align-items: center;
height: 100%;
color: ${(props) => props.theme.variants.primary};
}
.input-group-text {
top: 29%;
}
input[type="search"] {
-webkit-appearance: none;
Expand Down
43 changes: 43 additions & 0 deletions src/stories/form/input/inputIcons/PasswordIcon.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { EyeFill, EyeSlash } from "react-bootstrap-icons";
import { Button } from "../../../button/Button";

const PasswordIcon = ({
type,
setType,
i18n,
}: {
type: string;
setType: (value: string) => void;
i18n?: {
showPassword?: string;
hidePassword?: string;
};
}) => {
return (
<span className={`input-group-button`}>
{type === "text" ? (
<Button
alt={i18n && i18n.hidePassword ? i18n.hidePassword : "Hide password"}
size="sm"
htmlType="button"
type="light"
onClick={() => setType("password")}
>
<EyeSlash />
</Button>
) : (
<Button
alt={i18n && i18n.showPassword ? i18n.showPassword : "Show password"}
size="sm"
htmlType="button"
type="light"
onClick={() => setType("text")}
>
<EyeFill />
</Button>
)}
</span>
);
};

export default PasswordIcon;
11 changes: 11 additions & 0 deletions src/stories/form/input/inputIcons/SearchIcon.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Search } from "react-bootstrap-icons";

const SearchIcon = () => {
return (
<span className="input-group-text">
<Search />
</span>
);
};

export default SearchIcon;

0 comments on commit 4584235

Please sign in to comment.