|
| 1 | +/* |
| 2 | +Copyright 2021 The Matrix.org Foundation C.I.C. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +import React, { PureComponent, RefCallback, RefObject } from "react"; |
| 18 | +import { replaceableComponent } from "../../../utils/replaceableComponent"; |
| 19 | +import Field, { IInputProps } from "../elements/Field"; |
| 20 | +import withValidation, { IFieldState, IValidationResult } from "../elements/Validation"; |
| 21 | +import { _t, _td } from "../../../languageHandler"; |
| 22 | + |
| 23 | +interface IProps extends Omit<IInputProps, "onValidate"> { |
| 24 | + id?: string; |
| 25 | + fieldRef?: RefCallback<Field> | RefObject<Field>; |
| 26 | + autoComplete?: string; |
| 27 | + value: string; |
| 28 | + password: string; // The password we're confirming |
| 29 | + |
| 30 | + labelRequired?: string; |
| 31 | + labelInvalid?: string; |
| 32 | + |
| 33 | + onChange(ev: React.FormEvent<HTMLElement>); |
| 34 | + onValidate?(result: IValidationResult); |
| 35 | +} |
| 36 | + |
| 37 | +@replaceableComponent("views.auth.EmailField") |
| 38 | +class PassphraseConfirmField extends PureComponent<IProps> { |
| 39 | + static defaultProps = { |
| 40 | + label: _td("Confirm password"), |
| 41 | + labelRequired: _td("Confirm password"), |
| 42 | + labelInvalid: _td("Passwords don't match"), |
| 43 | + }; |
| 44 | + |
| 45 | + private validate = withValidation({ |
| 46 | + rules: [ |
| 47 | + { |
| 48 | + key: "required", |
| 49 | + test: ({ value, allowEmpty }) => allowEmpty || !!value, |
| 50 | + invalid: () => _t(this.props.labelRequired), |
| 51 | + }, |
| 52 | + { |
| 53 | + key: "match", |
| 54 | + test: ({ value }) => !value || value === this.props.password, |
| 55 | + invalid: () => _t(this.props.labelInvalid), |
| 56 | + }, |
| 57 | + ], |
| 58 | + }); |
| 59 | + |
| 60 | + private onValidate = async (fieldState: IFieldState) => { |
| 61 | + const result = await this.validate(fieldState); |
| 62 | + if (this.props.onValidate) { |
| 63 | + this.props.onValidate(result); |
| 64 | + } |
| 65 | + |
| 66 | + return result; |
| 67 | + }; |
| 68 | + |
| 69 | + render() { |
| 70 | + return <Field |
| 71 | + id={this.props.id} |
| 72 | + ref={this.props.fieldRef} |
| 73 | + type="password" |
| 74 | + label={_t(this.props.label)} |
| 75 | + autoComplete={this.props.autoComplete} |
| 76 | + value={this.props.value} |
| 77 | + onChange={this.props.onChange} |
| 78 | + onValidate={this.onValidate} |
| 79 | + />; |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +export default PassphraseConfirmField; |
0 commit comments