passwordField.tsx 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import * as React from 'react';
  2. import {Context} from 'app/components/forms/form';
  3. import InputField from 'app/components/forms/inputField';
  4. import FormState from 'app/components/forms/state';
  5. type Props = InputField['props'] & {
  6. hasSavedValue?: boolean;
  7. prefix: string;
  8. formState?: typeof FormState[keyof typeof FormState];
  9. };
  10. type State = InputField['state'] & {
  11. editing: boolean;
  12. };
  13. // TODO(dcramer): im not entirely sure this is working correctly with
  14. // value propagation in all scenarios
  15. export default class PasswordField extends InputField<Props, State> {
  16. static defaultProps = {
  17. ...InputField.defaultProps,
  18. hasSavedValue: false,
  19. prefix: '',
  20. };
  21. constructor(props: Props, context: Context) {
  22. super(props, context);
  23. this.state = {...this.state, editing: false};
  24. }
  25. UNSAFE_componentWillReceiveProps(nextProps: Props) {
  26. // close edit mode after successful save
  27. // TODO(dcramer): this needs to work with this.context.form
  28. if (
  29. this.props.formState &&
  30. this.props.formState === FormState.SAVING &&
  31. nextProps.formState === FormState.READY
  32. ) {
  33. this.setState({
  34. editing: false,
  35. });
  36. }
  37. }
  38. getType() {
  39. return 'password';
  40. }
  41. cancelEdit = (e: React.MouseEvent<HTMLAnchorElement>) => {
  42. e.preventDefault();
  43. this.setState(
  44. {
  45. editing: false,
  46. },
  47. () => {
  48. this.setValue('');
  49. }
  50. );
  51. };
  52. startEdit = (e: React.MouseEvent<HTMLAnchorElement>) => {
  53. e.preventDefault();
  54. this.setState({
  55. editing: true,
  56. });
  57. };
  58. getField() {
  59. if (!this.props.hasSavedValue) {
  60. return super.getField();
  61. }
  62. if (this.state.editing) {
  63. return (
  64. <div className="form-password editing">
  65. <div>{super.getField()}</div>
  66. <div>
  67. <a onClick={this.cancelEdit}>Cancel</a>
  68. </div>
  69. </div>
  70. );
  71. } else {
  72. return (
  73. <div className="form-password saved">
  74. <span>
  75. {this.props.prefix + new Array(21 - this.props.prefix.length).join('*')}
  76. </span>
  77. {!this.props.disabled && <a onClick={this.startEdit}>Edit</a>}
  78. </div>
  79. );
  80. }
  81. }
  82. }