|
1 | 1 | import { InputProps } from '@heroui/input'; |
2 | 2 | import StyledInput from '@shared/StyledInput'; |
| 3 | +import { useState } from 'react'; |
3 | 4 | import { Controller, useFormContext } from 'react-hook-form'; |
4 | | -import { RiClipboardLine } from 'react-icons/ri'; |
| 5 | +import { RiCheckLine, RiClipboardLine, RiFileCopyLine } from 'react-icons/ri'; |
5 | 6 | import Label from './Label'; |
| 7 | +import SecretValueToggle from './jobs/SecretValueToggle'; |
6 | 8 |
|
7 | 9 | interface Props extends InputProps { |
8 | 10 | name: string; |
9 | 11 | label: string; |
10 | 12 | placeholder: string; |
11 | 13 | isOptional?: boolean; |
12 | | - displayPasteIcon?: boolean; |
13 | 14 | onBlur?: () => void; |
14 | 15 | onPasteValue?: (value: string) => void; |
15 | 16 | customLabel?: React.ReactNode; |
| 17 | + endContent?: 'copy' | 'paste'; |
| 18 | + hasSecretValue?: boolean; |
16 | 19 | } |
17 | 20 |
|
18 | 21 | export default function InputWithLabel({ |
19 | 22 | name, |
20 | 23 | label, |
21 | 24 | placeholder, |
22 | 25 | isOptional, |
23 | | - displayPasteIcon, |
24 | 26 | customLabel, |
| 27 | + endContent, |
| 28 | + hasSecretValue, |
25 | 29 | ...props |
26 | 30 | }: Props) { |
27 | 31 | const { control } = useFormContext(); |
28 | 32 |
|
| 33 | + const [copied, setCopied] = useState(false); |
| 34 | + const [isFieldSecret, setFieldSecret] = useState(!!hasSecretValue); |
| 35 | + |
29 | 36 | return ( |
30 | 37 | <div className="col w-full gap-2"> |
31 | 38 | {customLabel ? customLabel : <Label value={label} isOptional={isOptional} />} |
32 | 39 |
|
33 | | - <Controller |
34 | | - name={name} |
35 | | - control={control} |
36 | | - render={({ field, fieldState }) => { |
37 | | - if (name === 'deployment.deploymentType.crUsername') { |
38 | | - console.log({ field, fieldState }); |
39 | | - } |
| 40 | + <div className="flex gap-3"> |
| 41 | + {hasSecretValue && ( |
| 42 | + <SecretValueToggle |
| 43 | + isSecret={isFieldSecret} |
| 44 | + onClick={() => { |
| 45 | + setFieldSecret((previous) => !previous); |
| 46 | + }} |
| 47 | + /> |
| 48 | + )} |
40 | 49 |
|
41 | | - return ( |
42 | | - <StyledInput |
43 | | - placeholder={placeholder} |
44 | | - value={field.value ?? ''} |
45 | | - onChange={(e) => { |
46 | | - const value = e.target.value; |
47 | | - field.onChange(value); |
48 | | - }} |
49 | | - onBlur={() => { |
50 | | - field.onBlur(); |
| 50 | + <Controller |
| 51 | + name={name} |
| 52 | + control={control} |
| 53 | + render={({ field, fieldState }) => { |
| 54 | + if (name === 'deployment.deploymentType.crUsername') { |
| 55 | + console.log({ field, fieldState }); |
| 56 | + } |
51 | 57 |
|
52 | | - if (props.onBlur) { |
53 | | - props.onBlur(); |
54 | | - } |
55 | | - }} |
56 | | - onPaste={(e) => { |
57 | | - const pastedText = e.clipboardData?.getData('text') ?? ''; |
| 58 | + return ( |
| 59 | + <StyledInput |
| 60 | + placeholder={placeholder} |
| 61 | + value={field.value ?? ''} |
| 62 | + onChange={(e) => { |
| 63 | + const value = e.target.value; |
| 64 | + field.onChange(value); |
| 65 | + }} |
| 66 | + onBlur={() => { |
| 67 | + field.onBlur(); |
58 | 68 |
|
59 | | - if (props.onPasteValue) { |
60 | | - props.onPasteValue(pastedText); |
61 | | - } |
62 | | - }} |
63 | | - isInvalid={!!fieldState.error} |
64 | | - errorMessage={fieldState.error?.message} |
65 | | - endContent={ |
66 | | - displayPasteIcon ? ( |
67 | | - <div |
68 | | - className="cursor-pointer hover:opacity-60" |
69 | | - onClick={async () => { |
70 | | - try { |
71 | | - const clipboardText = await navigator.clipboard.readText(); |
72 | | - field.onChange(clipboardText); |
| 69 | + if (props.onBlur) { |
| 70 | + props.onBlur(); |
| 71 | + } |
| 72 | + }} |
| 73 | + onPaste={(e) => { |
| 74 | + const pastedText = e.clipboardData?.getData('text') ?? ''; |
| 75 | + |
| 76 | + if (props.onPasteValue) { |
| 77 | + props.onPasteValue(pastedText); |
| 78 | + } |
| 79 | + }} |
| 80 | + isInvalid={!!fieldState.error} |
| 81 | + errorMessage={fieldState.error?.message} |
| 82 | + endContent={ |
| 83 | + endContent === 'paste' ? ( |
| 84 | + <div |
| 85 | + className="cursor-pointer hover:opacity-60" |
| 86 | + onClick={async () => { |
| 87 | + try { |
| 88 | + const clipboardText = await navigator.clipboard.readText(); |
| 89 | + field.onChange(clipboardText); |
73 | 90 |
|
74 | | - if (props.onPasteValue) { |
75 | | - props.onPasteValue(clipboardText); |
| 91 | + if (props.onPasteValue) { |
| 92 | + props.onPasteValue(clipboardText); |
| 93 | + } |
| 94 | + } catch (error) { |
| 95 | + console.error('Failed to read clipboard:', error); |
76 | 96 | } |
77 | | - } catch (error) { |
78 | | - console.error('Failed to read clipboard:', error); |
79 | | - } |
80 | | - }} |
81 | | - > |
82 | | - <RiClipboardLine className="text-lg text-slate-600" /> |
83 | | - </div> |
84 | | - ) : undefined |
85 | | - } |
86 | | - {...props} |
87 | | - /> |
88 | | - ); |
89 | | - }} |
90 | | - /> |
| 97 | + }} |
| 98 | + > |
| 99 | + <RiClipboardLine className="text-lg text-slate-600" /> |
| 100 | + </div> |
| 101 | + ) : endContent === 'copy' ? ( |
| 102 | + <div |
| 103 | + className="cursor-pointer text-lg text-slate-600 hover:opacity-60" |
| 104 | + onClick={async () => { |
| 105 | + try { |
| 106 | + if (copied) { |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + navigator.clipboard.writeText(field.value); |
| 111 | + setCopied(true); |
| 112 | + setTimeout(() => { |
| 113 | + setCopied(false); |
| 114 | + }, 1000); |
| 115 | + } catch (error) { |
| 116 | + console.error('Failed to copy to clipboard:', error); |
| 117 | + } |
| 118 | + }} |
| 119 | + > |
| 120 | + {copied ? <RiCheckLine /> : <RiFileCopyLine />} |
| 121 | + </div> |
| 122 | + ) : undefined |
| 123 | + } |
| 124 | + type={isFieldSecret ? 'password' : 'text'} |
| 125 | + {...props} |
| 126 | + /> |
| 127 | + ); |
| 128 | + }} |
| 129 | + /> |
| 130 | + </div> |
91 | 131 | </div> |
92 | 132 | ); |
93 | 133 | } |
0 commit comments