password.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import Field from './_field';
  2. import { Input, NeoButton } from 'app/design/controls'
  3. import { View } from 'app/design/view'
  4. import { useState } from 'react';
  5. import { appSetting } from 'app/lib/util';
  6. import { useFormField } from 'app/lib/form/use-form-field';
  7. export default function FormFieldPassword(props) {
  8. const [isVisible, setIsVisible] = useState(true)
  9. const {
  10. name,
  11. field,
  12. placeholder,
  13. placeholderTextColor,
  14. focused,
  15. onFocus,
  16. onBlur,
  17. inputRef,
  18. returnKeyProps,
  19. isAdaptiveLabel,
  20. inputId,
  21. errorId,
  22. invalid,
  23. } = useFormField(props);
  24. const eyeIcons = appSetting('forms', 'password_eye_button')?.image;
  25. const eyeIcon = isVisible ? eyeIcons?.visible : eyeIcons?.hidden;
  26. return (
  27. <Field {...props} value={field.value} focused={focused} isAdaptiveLabel={isAdaptiveLabel}>
  28. <View className="relative w-full">
  29. <Input
  30. ref={inputRef}
  31. id={inputId}
  32. nativeID={inputId}
  33. textContentType="password"
  34. autoComplete="current-password"
  35. placeholderTextColor={placeholderTextColor}
  36. placeholder={placeholder}
  37. secureTextEntry={isVisible}
  38. name={name}
  39. onChangeText={field.onChange}
  40. onFocus={onFocus}
  41. onBlur={onBlur}
  42. value={field.value}
  43. aria-label={props.caption}
  44. aria-invalid={invalid || undefined}
  45. aria-describedby={errorId}
  46. {...returnKeyProps}
  47. />
  48. {eyeIcon ? (
  49. <View className="absolute right-1.5 top-1/2 -translate-y-1/2 justify-center items-center z-20">
  50. <NeoButton
  51. image={eyeIcon}
  52. style="borderless"
  53. controlSize="small"
  54. hitSlop={{ top: 8, right: 8, bottom: 8, left: 8 }}
  55. accessibilityLabel={isVisible ? 'Show password' : 'Hide password'}
  56. onPress={() => { setIsVisible(!isVisible) }}
  57. />
  58. </View>
  59. ) : null}
  60. </View>
  61. </Field>
  62. );
  63. }