Password.tsx 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright 2017-2020 @polkadot/react-components authors & contributors
  2. // This software may be modified and distributed under the terms
  3. // of the Apache-2.0 license. See the LICENSE file for details.
  4. import { BareProps } from './types';
  5. import React, { useState } from 'react';
  6. import { MAX_PASS_LEN } from '@polkadot/ui-keyring/defaults';
  7. import { classes } from './util';
  8. import Button from './Button';
  9. import Input from './Input';
  10. interface Props extends BareProps {
  11. autoFocus?: boolean;
  12. children?: React.ReactNode;
  13. defaultValue?: any;
  14. help?: string;
  15. isDisabled?: boolean;
  16. isError?: boolean;
  17. isFull?: boolean;
  18. label?: string;
  19. labelExtra?: React.ReactNode;
  20. name?: string;
  21. onChange: (value: string) => void;
  22. onEnter?: () => void;
  23. onEscape?: () => void;
  24. tabIndex?: number;
  25. value: any;
  26. withLabel?: boolean;
  27. }
  28. function Password ({ autoFocus, children, className, defaultValue, help, isDisabled, isError, isFull, label, labelExtra, name, onChange, onEnter, onEscape, style, tabIndex, value, withLabel }: Props): React.ReactElement<Props> {
  29. const [isVisible, setIsVisible] = useState(false);
  30. const _toggleVisible = (): void => setIsVisible(!isVisible);
  31. return (
  32. <Input
  33. autoFocus={autoFocus}
  34. className={classes('ui--Password', className)}
  35. defaultValue={defaultValue}
  36. help={help}
  37. isAction
  38. isDisabled={isDisabled}
  39. isError={isError}
  40. isFull={isFull}
  41. label={label}
  42. labelExtra={labelExtra}
  43. maxLength={MAX_PASS_LEN}
  44. name={name}
  45. onChange={onChange}
  46. onEnter={onEnter}
  47. onEscape={onEscape}
  48. style={style}
  49. tabIndex={tabIndex}
  50. type={
  51. isVisible
  52. ? 'text'
  53. : 'password'
  54. }
  55. value={value}
  56. withLabel={withLabel}
  57. >
  58. <Button
  59. icon={
  60. isVisible
  61. ? 'hide'
  62. : 'unhide'
  63. }
  64. onClick={_toggleVisible}
  65. />
  66. {children}
  67. </Input>
  68. );
  69. }
  70. export default React.memo(Password);