Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit cb2ce6f

Browse files
authored
Merge pull request #5403 from matrix-org/matthew/username-tooltip
hide some validation tooltips if fields are valid.
2 parents 0766519 + 36aa80f commit cb2ce6f

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

src/components/views/auth/RegistrationForm.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ export default class RegistrationForm extends React.Component {
250250

251251
validateEmailRules = withValidation({
252252
description: () => _t("Use an email address to recover your account"),
253+
hideDescriptionIfValid: true,
253254
rules: [
254255
{
255256
key: "required",
@@ -326,6 +327,7 @@ export default class RegistrationForm extends React.Component {
326327

327328
validatePhoneNumberRules = withValidation({
328329
description: () => _t("Other users can invite you to rooms using your contact details"),
330+
hideDescriptionIfValid: true,
329331
rules: [
330332
{
331333
key: "required",
@@ -356,6 +358,7 @@ export default class RegistrationForm extends React.Component {
356358

357359
validateUsernameRules = withValidation({
358360
description: () => _t("Use lowercase letters, numbers, dashes and underscores only"),
361+
hideDescriptionIfValid: true,
359362
rules: [
360363
{
361364
key: "required",

src/components/views/elements/Validation.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ interface IRule<T, D = void> {
3333
interface IArgs<T, D = void> {
3434
rules: IRule<T, D>[];
3535
description(this: T, derivedData: D): React.ReactChild;
36+
hideDescriptionIfValid?: boolean;
3637
deriveData?(data: Data): Promise<D>;
3738
}
3839

@@ -54,6 +55,8 @@ export interface IValidationResult {
5455
* @param {Function} description
5556
* Function that returns a string summary of the kind of value that will
5657
* meet the validation rules. Shown at the top of the validation feedback.
58+
* @param {Boolean} hideDescriptionIfValid
59+
* If true, don't show the description if the validation passes validation.
5760
* @param {Function} deriveData
5861
* Optional function that returns a Promise to an object of generic type D.
5962
* The result of this Promise is passed to rule methods `skip`, `test`, `valid`, and `invalid`.
@@ -71,7 +74,9 @@ export interface IValidationResult {
7174
* A validation function that takes in the current input value and returns
7275
* the overall validity and a feedback UI that can be rendered for more detail.
7376
*/
74-
export default function withValidation<T = undefined, D = void>({ description, deriveData, rules }: IArgs<T, D>) {
77+
export default function withValidation<T = undefined, D = void>({
78+
description, hideDescriptionIfValid, deriveData, rules,
79+
}: IArgs<T, D>) {
7580
return async function onValidate({ value, focused, allowEmpty = true }: IFieldState): Promise<IValidationResult> {
7681
if (!value && allowEmpty) {
7782
return {
@@ -156,7 +161,7 @@ export default function withValidation<T = undefined, D = void>({ description, d
156161
}
157162

158163
let summary;
159-
if (description) {
164+
if (description && (details || !hideDescriptionIfValid)) {
160165
// We're setting `this` to whichever component holds the validation
161166
// function. That allows rules to access the state of the component.
162167
const content = description.call(this, derivedData);

0 commit comments

Comments
 (0)