2019-04-16 18:52:31 +03:00
|
|
|
/*
|
|
|
|
Copyright 2019 New Vector Ltd
|
2020-05-14 21:33:17 +03:00
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
2019-04-16 18:52:31 +03:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 16:55:57 +03:00
|
|
|
/* eslint-disable babel/no-invalid-this */
|
2020-05-14 21:33:17 +03:00
|
|
|
import React from "react";
|
|
|
|
import classNames from "classnames";
|
2019-04-23 16:55:57 +03:00
|
|
|
|
2020-05-14 22:19:15 +03:00
|
|
|
type Data = Pick<IFieldState, "value" | "allowEmpty">;
|
2020-05-14 21:33:17 +03:00
|
|
|
|
2020-09-21 16:35:35 +03:00
|
|
|
interface IRule<T, D = void> {
|
2020-05-14 21:33:17 +03:00
|
|
|
key: string;
|
|
|
|
final?: boolean;
|
2020-09-21 16:35:35 +03:00
|
|
|
skip?(this: T, data: Data, derivedData: D): boolean;
|
|
|
|
test(this: T, data: Data, derivedData: D): boolean | Promise<boolean>;
|
|
|
|
valid?(this: T, derivedData: D): string;
|
|
|
|
invalid?(this: T, derivedData: D): string;
|
2020-05-14 21:33:17 +03:00
|
|
|
}
|
|
|
|
|
2020-09-21 16:35:35 +03:00
|
|
|
interface IArgs<T, D = void> {
|
|
|
|
rules: IRule<T, D>[];
|
|
|
|
description(this: T, derivedData: D): React.ReactChild;
|
2020-11-10 21:29:28 +03:00
|
|
|
hideDescriptionIfValid?: boolean;
|
2020-09-21 16:35:35 +03:00
|
|
|
deriveData?(data: Data): Promise<D>;
|
2020-05-14 21:33:17 +03:00
|
|
|
}
|
|
|
|
|
2020-05-14 22:19:15 +03:00
|
|
|
export interface IFieldState {
|
2020-05-14 21:33:17 +03:00
|
|
|
value: string;
|
|
|
|
focused: boolean;
|
|
|
|
allowEmpty: boolean;
|
|
|
|
}
|
2019-04-16 18:52:31 +03:00
|
|
|
|
2020-05-14 22:19:15 +03:00
|
|
|
export interface IValidationResult {
|
|
|
|
valid?: boolean;
|
|
|
|
feedback?: React.ReactChild;
|
|
|
|
}
|
|
|
|
|
2019-04-16 18:52:31 +03:00
|
|
|
/**
|
|
|
|
* Creates a validation function from a set of rules describing what to validate.
|
2020-05-14 21:33:17 +03:00
|
|
|
* Generic T is the "this" type passed to the rule methods
|
2019-04-16 18:52:31 +03:00
|
|
|
*
|
2019-04-17 13:39:11 +03:00
|
|
|
* @param {Function} description
|
|
|
|
* Function that returns a string summary of the kind of value that will
|
|
|
|
* meet the validation rules. Shown at the top of the validation feedback.
|
2020-11-09 04:07:15 +03:00
|
|
|
* @param {Boolean} hideDescriptionIfValid
|
|
|
|
* If true, don't show the description if the validation passes validation.
|
2020-09-21 16:35:35 +03:00
|
|
|
* @param {Function} deriveData
|
|
|
|
* Optional function that returns a Promise to an object of generic type D.
|
|
|
|
* The result of this Promise is passed to rule methods `skip`, `test`, `valid`, and `invalid`.
|
|
|
|
* Useful for doing calculations per-value update once rather than in each of the above rule methods.
|
2019-04-16 18:52:31 +03:00
|
|
|
* @param {Object} rules
|
|
|
|
* An array of rules describing how to check to input value. Each rule in an object
|
|
|
|
* and may have the following properties:
|
|
|
|
* - `key`: A unique ID for the rule. Required.
|
2020-02-29 04:00:57 +03:00
|
|
|
* - `skip`: A function used to determine whether the rule should even be evaluated.
|
2019-04-18 23:33:37 +03:00
|
|
|
* - `test`: A function used to determine the rule's current validity. Required.
|
2019-04-17 13:39:11 +03:00
|
|
|
* - `valid`: Function returning text to show when the rule is valid. Only shown if set.
|
|
|
|
* - `invalid`: Function returning text to show when the rule is invalid. Only shown if set.
|
2020-02-29 04:00:57 +03:00
|
|
|
* - `final`: A Boolean if true states that this rule will only be considered if all rules before it returned valid.
|
2019-04-16 18:52:31 +03:00
|
|
|
* @returns {Function}
|
|
|
|
* A validation function that takes in the current input value and returns
|
|
|
|
* the overall validity and a feedback UI that can be rendered for more detail.
|
|
|
|
*/
|
2020-11-09 04:16:38 +03:00
|
|
|
export default function withValidation<T = undefined, D = void>({
|
2020-11-09 04:16:58 +03:00
|
|
|
description, hideDescriptionIfValid, deriveData, rules,
|
2020-11-09 04:16:38 +03:00
|
|
|
}: IArgs<T, D>) {
|
2020-05-14 22:19:15 +03:00
|
|
|
return async function onValidate({ value, focused, allowEmpty = true }: IFieldState): Promise<IValidationResult> {
|
2019-04-18 23:33:37 +03:00
|
|
|
if (!value && allowEmpty) {
|
2019-04-16 18:52:31 +03:00
|
|
|
return {
|
|
|
|
valid: null,
|
|
|
|
feedback: null,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-09-21 16:35:35 +03:00
|
|
|
const data = { value, allowEmpty };
|
|
|
|
const derivedData = deriveData ? await deriveData(data) : undefined;
|
|
|
|
|
2019-04-16 18:52:31 +03:00
|
|
|
const results = [];
|
|
|
|
let valid = true;
|
|
|
|
if (rules && rules.length) {
|
|
|
|
for (const rule of rules) {
|
2019-04-18 23:33:37 +03:00
|
|
|
if (!rule.key || !rule.test) {
|
2019-04-16 18:52:31 +03:00
|
|
|
continue;
|
|
|
|
}
|
2020-02-29 03:31:56 +03:00
|
|
|
|
2020-02-29 04:00:57 +03:00
|
|
|
if (!valid && rule.final) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-09-21 16:35:35 +03:00
|
|
|
if (rule.skip && rule.skip.call(this, data, derivedData)) {
|
2020-02-29 03:31:56 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-04-23 16:55:57 +03:00
|
|
|
// We're setting `this` to whichever component holds the validation
|
2019-04-19 00:53:46 +03:00
|
|
|
// function. That allows rules to access the state of the component.
|
2020-09-21 16:35:35 +03:00
|
|
|
const ruleValid = await rule.test.call(this, data, derivedData);
|
2019-04-16 18:52:31 +03:00
|
|
|
valid = valid && ruleValid;
|
|
|
|
if (ruleValid && rule.valid) {
|
|
|
|
// If the rule's result is valid and has text to show for
|
|
|
|
// the valid state, show it.
|
2020-09-21 16:35:35 +03:00
|
|
|
const text = rule.valid.call(this, derivedData);
|
2019-04-23 16:55:57 +03:00
|
|
|
if (!text) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-04-16 18:52:31 +03:00
|
|
|
results.push({
|
|
|
|
key: rule.key,
|
|
|
|
valid: true,
|
2019-04-23 16:55:57 +03:00
|
|
|
text,
|
2019-04-16 18:52:31 +03:00
|
|
|
});
|
|
|
|
} else if (!ruleValid && rule.invalid) {
|
|
|
|
// If the rule's result is invalid and has text to show for
|
|
|
|
// the invalid state, show it.
|
2020-09-21 16:35:35 +03:00
|
|
|
const text = rule.invalid.call(this, derivedData);
|
2019-04-23 16:55:57 +03:00
|
|
|
if (!text) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-04-16 18:52:31 +03:00
|
|
|
results.push({
|
|
|
|
key: rule.key,
|
|
|
|
valid: false,
|
2019-04-23 16:55:57 +03:00
|
|
|
text,
|
2019-04-16 18:52:31 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-16 20:12:13 +03:00
|
|
|
// Hide feedback when not focused
|
|
|
|
if (!focused) {
|
|
|
|
return {
|
|
|
|
valid,
|
|
|
|
feedback: null,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-04-16 18:52:31 +03:00
|
|
|
let details;
|
|
|
|
if (results && results.length) {
|
|
|
|
details = <ul className="mx_Validation_details">
|
|
|
|
{results.map(result => {
|
|
|
|
const classes = classNames({
|
|
|
|
"mx_Validation_detail": true,
|
|
|
|
"mx_Validation_valid": result.valid,
|
|
|
|
"mx_Validation_invalid": !result.valid,
|
|
|
|
});
|
|
|
|
return <li key={result.key} className={classes}>
|
|
|
|
{result.text}
|
|
|
|
</li>;
|
|
|
|
})}
|
|
|
|
</ul>;
|
|
|
|
}
|
|
|
|
|
2019-04-17 13:39:11 +03:00
|
|
|
let summary;
|
2020-11-09 04:07:15 +03:00
|
|
|
if (description && (details || !hideDescriptionIfValid)) {
|
2019-04-23 16:55:57 +03:00
|
|
|
// We're setting `this` to whichever component holds the validation
|
|
|
|
// function. That allows rules to access the state of the component.
|
2020-09-21 16:35:35 +03:00
|
|
|
const content = description.call(this, derivedData);
|
2019-04-23 16:55:57 +03:00
|
|
|
summary = <div className="mx_Validation_description">{content}</div>;
|
2019-04-17 13:39:11 +03:00
|
|
|
}
|
|
|
|
|
2019-04-19 00:53:46 +03:00
|
|
|
let feedback;
|
|
|
|
if (summary || details) {
|
|
|
|
feedback = <div className="mx_Validation">
|
|
|
|
{summary}
|
|
|
|
{details}
|
|
|
|
</div>;
|
|
|
|
}
|
2019-04-16 18:52:31 +03:00
|
|
|
|
|
|
|
return {
|
|
|
|
valid,
|
|
|
|
feedback,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|