File size: 4,356 Bytes
0bfe2e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import { cn } from '../core/styling';
import * as React from 'react';
import {
  BasicField,
  BasicFieldOptions,
  extractBasicFieldProps,
} from '../basic-field';
import {
  extractInputPartProps,
  InputAddon,
  InputAnatomy,
  InputContainer,
  InputIcon,
  InputStyling,
} from '../input';
import * as PasswordToggleField from '@radix-ui/react-password-toggle-field';
import { LuEye, LuEyeClosed } from 'react-icons/lu';

/* -------------------------------------------------------------------------------------------------
 * PasswordInput
 * -----------------------------------------------------------------------------------------------*/

export type PasswordInputProps = Omit<
  React.ComponentPropsWithRef<'input'>,
  'size' | 'type' | 'autoComplete'
> &
  InputStyling &
  BasicFieldOptions & {
    /**
     * Callback invoked when the value changes. Returns the string value.
     */
    onValueChange?: (value: string) => void;
    /**
     * The autoComplete attribute for the password input.
     * @default "current-password"
     */
    autoComplete?: 'current-password' | 'new-password';
  };

export const PasswordInput = React.forwardRef<
  HTMLInputElement,
  PasswordInputProps
>((props, ref) => {
  const [props1, basicFieldProps] = extractBasicFieldProps<PasswordInputProps>(
    props,
    React.useId()
  );

  const [
    {
      size,
      intent,
      leftAddon,
      leftIcon,
      rightAddon,
      rightIcon,
      className,
      onValueChange,
      onChange,
      autoComplete = 'current-password',
      ...rest
    },
    {
      inputContainerProps,
      leftAddonProps,
      leftIconProps,
      rightAddonProps,
      rightIconProps,
    },
  ] = extractInputPartProps<PasswordInputProps>({
    ...props1,
    size: props1.size ?? 'md',
    intent: props1.intent ?? 'basic',
    leftAddon: props1.leftAddon,
    leftIcon: props1.leftIcon,
    rightAddon: props1.rightAddon,
    rightIcon: props1.rightIcon,
  });

  const handleOnChange = React.useCallback(
    (e: React.ChangeEvent<HTMLInputElement>) => {
      onValueChange?.(e.target.value);
      onChange?.(e);
    },
    [onValueChange, onChange]
  );

  return (
    <BasicField {...basicFieldProps}>
      <InputContainer {...inputContainerProps}>
        <InputAddon {...leftAddonProps} />
        <InputIcon {...leftIconProps} />

        <div className="relative flex-1">
          <PasswordToggleField.Root>
            <PasswordToggleField.Input
              id={basicFieldProps.id}
              name={basicFieldProps.name}
              className={cn(
                'form-input',
                InputAnatomy.root({
                  size,
                  intent,
                  hasError: !!basicFieldProps.error,
                  isDisabled: !!basicFieldProps.disabled,
                  isReadonly: !!basicFieldProps.readonly,
                  hasRightAddon: !!rightAddon,
                  hasRightIcon: !!rightIcon,
                  hasLeftAddon: !!leftAddon,
                  hasLeftIcon: !!leftIcon,
                }),
                className
              )}
              disabled={basicFieldProps.disabled || basicFieldProps.readonly}
              data-disabled={basicFieldProps.disabled}
              data-readonly={basicFieldProps.readonly}
              aria-readonly={basicFieldProps.readonly}
              required={basicFieldProps.required}
              onChange={handleOnChange}
              autoComplete={autoComplete}
              {...rest}
              ref={ref}
            />
            <PasswordToggleField.Toggle
              className={cn(
                'absolute right-0 top-0 h-full px-3',
                'text-muted-foreground hover:text-foreground',
                'focus:outline-none focus:ring-0',
                'disabled:opacity-50 disabled:pointer-events-none',
                'bg-transparent'
              )}
            >
              <PasswordToggleField.Icon
                visible={<LuEye className="h-6 w-6" />}
                hidden={<LuEyeClosed className="h-6 w-6" />}
              />
            </PasswordToggleField.Toggle>
          </PasswordToggleField.Root>
        </div>

        <InputAddon {...rightAddonProps} />
        <InputIcon {...rightIconProps} />
      </InputContainer>
    </BasicField>
  );
});

PasswordInput.displayName = 'PasswordInput';