Next.js template
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PasswordField.js 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import React, { useState } from 'react';
  2. import PropTypes from 'prop-types';
  3. import BaseInputField from './BaseInputField';
  4. import PasswordStrength from './PasswordStrength';
  5. const PasswordField = ({
  6. field,
  7. form,
  8. label,
  9. placeholder,
  10. disabled,
  11. shouldTestPasswordStrength,
  12. autoFocus,
  13. ...props
  14. }) => {
  15. const [passwordValue, setPasswordValue] = useState('');
  16. const [isCapsLockOn, setIsCapsLockOn] = useState(false);
  17. const onChange = (e) => {
  18. if (shouldTestPasswordStrength) {
  19. const { value } = e.target;
  20. setPasswordValue(value);
  21. }
  22. field.onChange(e);
  23. };
  24. const onKeyDown = (keyEvent) => {
  25. if (keyEvent.getModifierState('CapsLock')) {
  26. setIsCapsLockOn(true);
  27. } else {
  28. setIsCapsLockOn(false);
  29. }
  30. };
  31. return (
  32. <div className="c-password">
  33. <BaseInputField
  34. type="password"
  35. label={label}
  36. placeholder={placeholder}
  37. disabled={disabled}
  38. form={form}
  39. field={field}
  40. {...props}
  41. onChange={onChange}
  42. autoFocus={autoFocus}
  43. onKeyDown={onKeyDown}
  44. isCapsLockOn={isCapsLockOn}
  45. />
  46. {shouldTestPasswordStrength && (
  47. <PasswordStrength
  48. passwordValue={passwordValue}
  49. shouldTestPasswordStrength
  50. />
  51. )}
  52. </div>
  53. );
  54. };
  55. PasswordField.propTypes = {
  56. field: PropTypes.shape({
  57. onChange: PropTypes.func,
  58. }),
  59. form: PropTypes.shape({}),
  60. label: PropTypes.oneOfType([PropTypes.string, PropTypes.shape({})]),
  61. placeholder: PropTypes.string,
  62. disabled: PropTypes.bool,
  63. shouldTestPasswordStrength: PropTypes.bool,
  64. autoFocus: PropTypes.bool,
  65. };
  66. export default PasswordField;