Bladeren bron

Show/Hide password

Pushkar Anand 4 jaren geleden
bovenliggende
commit
5ce89b27ef
3 gewijzigde bestanden met toevoegingen van 84 en 11 verwijderingen
  1. 38 11
      src/components/SingleInputForm.tsx
  2. 23 0
      src/components/icons/Visibility.tsx
  3. 23 0
      src/components/icons/VisibilityOff.tsx

+ 38 - 11
src/components/SingleInputForm.tsx

@@ -4,6 +4,9 @@ import { Form } from 'react-bootstrap';
 import { Formik, FormikHelpers } from 'formik';
 import { Formik, FormikHelpers } from 'formik';
 import * as Yup from 'yup';
 import * as Yup from 'yup';
 import SubmitButton from './SubmitButton';
 import SubmitButton from './SubmitButton';
+import styled from 'styled-components';
+import Visibility from './icons/Visibility';
+import VisibilityOff from './icons/VisibilityOff';
 
 
 interface formValues {
 interface formValues {
     passphrase: string;
     passphrase: string;
@@ -15,8 +18,24 @@ interface Props {
     buttonText: string;
     buttonText: string;
 }
 }
 
 
+const Group = styled.div`
+    display: flex;
+`;
+
+const Button = styled.button`
+    background: transparent;
+    border: none;
+    width: 36px;
+    height: 36px;
+    margin-left: -36px;
+    display: flex;
+    align-items: center;
+`;
+
 export default function SingleInputForm(props: Props) {
 export default function SingleInputForm(props: Props) {
     const [loading, SetLoading] = useState(false);
     const [loading, SetLoading] = useState(false);
+    const [showPassword, setShowPassword] = useState(false);
+
     const submitForm = async (
     const submitForm = async (
         values: formValues,
         values: formValues,
         { setFieldError }: FormikHelpers<formValues>,
         { setFieldError }: FormikHelpers<formValues>,
@@ -40,17 +59,25 @@ export default function SingleInputForm(props: Props) {
             }) => (
             }) => (
                 <Form noValidate onSubmit={handleSubmit}>
                 <Form noValidate onSubmit={handleSubmit}>
                     <Form.Group>
                     <Form.Group>
-                        <Form.Control
-                            type={props.fieldType}
-                            placeholder={props.placeholder}
-                            value={values.passphrase}
-                            onChange={handleChange('passphrase')}
-                            isInvalid={Boolean(
-                                touched.passphrase && errors.passphrase,
-                            )}
-                            disabled={loading}
-                            autoFocus
-                        />
+                        <Group>
+                            <Form.Control
+                                type={showPassword ? 'text' : props.fieldType}
+                                placeholder={props.placeholder}
+                                value={values.passphrase}
+                                onChange={handleChange('passphrase')}
+                                isInvalid={Boolean(
+                                    touched.passphrase && errors.passphrase,
+                                )}
+                                disabled={loading}
+                                autoFocus
+                            />
+                            {
+                                props.fieldType === 'password' &&
+                                <Button type='button' onClick={() => setShowPassword(!showPassword)}>
+                                    {showPassword ? <VisibilityOff /> : <Visibility />}
+                                </Button>
+                            }
+                        </Group>
                         <Form.Control.Feedback type="invalid">
                         <Form.Control.Feedback type="invalid">
                             {errors.passphrase}
                             {errors.passphrase}
                         </Form.Control.Feedback>
                         </Form.Control.Feedback>

+ 23 - 0
src/components/icons/Visibility.tsx

@@ -0,0 +1,23 @@
+import React from 'react';
+export default function Visibility(props) {
+    return (
+        <div >
+            <svg
+                xmlns="http://www.w3.org/2000/svg"
+                height={props.height}
+                viewBox={props.viewBox}
+                width={props.width}
+                fill="#000000">
+                <path d="M0 0h24v24H0z" fill="none"/><path d="M12 4.5C7 4.5 2.73 7.61 1 12c1.73 4.39 6 7.5 11 7.5s9.27-3.11 11-7.5c-1.73-4.39-6-7.5-11-7.5zM12 17c-2.76 0-5-2.24-5-5s2.24-5 5-5 5 2.24 5 5-2.24 5-5 5zm0-8c-1.66 0-3 1.34-3 3s1.34 3 3 3 3-1.34 3-3-1.34-3-3-3z"/>
+            </svg>
+        </div>
+
+    );
+}
+
+Visibility.defaultProps = {
+    height: 24,
+    width: 24,
+    viewBox: '0 0 24 24',
+    open: false,
+};

+ 23 - 0
src/components/icons/VisibilityOff.tsx

@@ -0,0 +1,23 @@
+import React from 'react';
+export default function VisibilityOff(props) {
+    return (
+        <div >
+            <svg
+                xmlns="http://www.w3.org/2000/svg"
+                height={props.height}
+                viewBox={props.viewBox}
+                width={props.width}
+                fill="#000000">
+                <path d="M0 0h24v24H0zm0 0h24v24H0zm0 0h24v24H0zm0 0h24v24H0z" fill="none"/><path d="M12 7c2.76 0 5 2.24 5 5 0 .65-.13 1.26-.36 1.83l2.92 2.92c1.51-1.26 2.7-2.89 3.43-4.75-1.73-4.39-6-7.5-11-7.5-1.4 0-2.74.25-3.98.7l2.16 2.16C10.74 7.13 11.35 7 12 7zM2 4.27l2.28 2.28.46.46C3.08 8.3 1.78 10.02 1 12c1.73 4.39 6 7.5 11 7.5 1.55 0 3.03-.3 4.38-.84l.42.42L19.73 22 21 20.73 3.27 3 2 4.27zM7.53 9.8l1.55 1.55c-.05.21-.08.43-.08.65 0 1.66 1.34 3 3 3 .22 0 .44-.03.65-.08l1.55 1.55c-.67.33-1.41.53-2.2.53-2.76 0-5-2.24-5-5 0-.79.2-1.53.53-2.2zm4.31-.78l3.15 3.15.02-.16c0-1.66-1.34-3-3-3l-.17.01z"/>
+            </svg>
+        </div>
+
+    );
+}
+
+VisibilityOff.defaultProps = {
+    height: 24,
+    width: 24,
+    viewBox: '0 0 24 24',
+    open: false,
+};