passwordField.tsx 2.1 KB

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