react-ui-validations
Advanced tools
Comparing version 1.10.3 to 1.11.0
@@ -6,2 +6,13 @@ # Change Log | ||
# [1.11.0](https://github.com/skbkontur/retail-ui/tree/master/packages/react-ui-validations/compare/react-ui-validations@1.10.3...react-ui-validations@1.11.0) (2022-11-25) | ||
### Features | ||
* add validation settings, make focus on warnings optional ([#3036](https://github.com/skbkontur/retail-ui/tree/master/packages/react-ui-validations/issues/3036)) ([65030c2](https://github.com/skbkontur/retail-ui/tree/master/packages/react-ui-validations/commit/65030c26a71e8539ba3d68a3960fee93cad216e0)) | ||
## [1.10.3](https://github.com/skbkontur/retail-ui/tree/master/packages/react-ui-validations/compare/react-ui-validations@1.10.2...react-ui-validations@1.10.3) (2022-11-09) | ||
@@ -8,0 +19,0 @@ |
@@ -30,2 +30,11 @@ // Generated by dts-bundle v0.7.3 | ||
} | ||
export enum FocusMode { | ||
'Errors' = 0, | ||
'ErrorsAndWarnings' = 1, | ||
'None' = 2 | ||
} | ||
export interface ValidationSettings { | ||
focusMode: FocusMode; | ||
} | ||
export type ValidateArgumentType = boolean | ValidationSettings; | ||
export interface ValidationContainerProps { | ||
@@ -45,3 +54,5 @@ children?: React.ReactNode; | ||
submit(withoutFocus?: boolean): Promise<void>; | ||
submit(validationSettings?: ValidationSettings): Promise<void>; | ||
validate(withoutFocus?: boolean): Promise<boolean>; | ||
validate(validationSettings?: ValidationSettings): Promise<boolean>; | ||
render(): JSX.Element; | ||
@@ -136,3 +147,3 @@ } | ||
import { ValidationWrapperInternal } from 'react-ui-validations/src/ValidationWrapperInternal'; | ||
import { ScrollOffset } from 'react-ui-validations/src/ValidationContainer'; | ||
import { ScrollOffset, ValidateArgumentType } from 'react-ui-validations/src/ValidationContainer'; | ||
export interface ValidationContextSettings { | ||
@@ -167,3 +178,3 @@ scrollOffset: ScrollOffset; | ||
getChildWrappersSortedByPosition(): ValidationWrapperInternal[]; | ||
validate(withoutFocus: boolean): Promise<boolean>; | ||
validate(withoutFocusOrValidationSettings: ValidateArgumentType): Promise<boolean>; | ||
render(): JSX.Element; | ||
@@ -170,0 +181,0 @@ } |
58
index.js
@@ -332,22 +332,25 @@ 'use strict'; | ||
}; | ||
ValidationContextWrapper.prototype.validate = function (withoutFocus) { | ||
ValidationContextWrapper.prototype.validate = function (withoutFocusOrValidationSettings) { | ||
var _a; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var childrenWrappersSortedByPosition, firstInvalid, invalid; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, Promise.all(this.childWrappers.map(function (x) { return x.processSubmit(); }))]; | ||
var focusMode, childrenWrappersSortedByPosition, firstError; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
focusMode = ValidationContextWrapper.getFocusMode(withoutFocusOrValidationSettings); | ||
return [4 /*yield*/, Promise.all(this.childWrappers.map(function (x) { return x.processSubmit(); }))]; | ||
case 1: | ||
_a.sent(); | ||
_b.sent(); | ||
childrenWrappersSortedByPosition = this.getChildWrappersSortedByPosition(); | ||
firstInvalid = childrenWrappersSortedByPosition.find(function (x) { return x.hasError() || x.hasWarning(); }); | ||
invalid = childrenWrappersSortedByPosition.some(function (x) { return x.hasError(); }); | ||
if (firstInvalid) { | ||
if (!withoutFocus) { | ||
firstInvalid.focus(); | ||
} | ||
firstError = childrenWrappersSortedByPosition.find(function (x) { return x.hasError(); }); | ||
if (focusMode === FocusMode.ErrorsAndWarnings) { | ||
(_a = childrenWrappersSortedByPosition.find(function (x) { return x.hasError() || x.hasWarning(); })) === null || _a === void 0 ? void 0 : _a.focus(); | ||
} | ||
if (focusMode === FocusMode.Errors) { | ||
firstError === null || firstError === void 0 ? void 0 : firstError.focus(); | ||
} | ||
if (this.props.onValidationUpdated) { | ||
this.props.onValidationUpdated(!firstInvalid); | ||
this.props.onValidationUpdated(!firstError); | ||
} | ||
return [2 /*return*/, !invalid]; | ||
return [2 /*return*/, !firstError]; | ||
} | ||
@@ -357,2 +360,11 @@ }); | ||
}; | ||
ValidationContextWrapper.getFocusMode = function (withoutFocusOrValidationSettings) { | ||
if (typeof withoutFocusOrValidationSettings === 'object' && 'focusMode' in withoutFocusOrValidationSettings) { | ||
return withoutFocusOrValidationSettings.focusMode; | ||
} | ||
else if (!withoutFocusOrValidationSettings) { | ||
return FocusMode.Errors; | ||
} | ||
return FocusMode.None; | ||
}; | ||
ValidationContextWrapper.prototype.render = function () { | ||
@@ -365,2 +377,8 @@ return (React__default["default"].createElement(ValidationContext.Provider, { value: this }, | ||
var FocusMode; | ||
(function (FocusMode) { | ||
FocusMode[FocusMode["Errors"] = 0] = "Errors"; | ||
FocusMode[FocusMode["ErrorsAndWarnings"] = 1] = "ErrorsAndWarnings"; | ||
FocusMode[FocusMode["None"] = 2] = "None"; | ||
})(FocusMode || (FocusMode = {})); | ||
var ValidationContainer = /** @class */ (function (_super) { | ||
@@ -375,4 +393,4 @@ __extends(ValidationContainer, _super); | ||
} | ||
ValidationContainer.prototype.submit = function (withoutFocus) { | ||
if (withoutFocus === void 0) { withoutFocus = false; } | ||
ValidationContainer.prototype.submit = function (withoutFocusOrValidationSettings) { | ||
if (withoutFocusOrValidationSettings === void 0) { withoutFocusOrValidationSettings = { focusMode: FocusMode.Errors }; } | ||
return __awaiter(this, void 0, void 0, function () { | ||
@@ -385,3 +403,3 @@ return __generator(this, function (_a) { | ||
} | ||
return [4 /*yield*/, this.childContext.validate(withoutFocus)]; | ||
return [4 /*yield*/, this.childContext.validate(withoutFocusOrValidationSettings)]; | ||
case 1: | ||
@@ -394,8 +412,8 @@ _a.sent(); | ||
}; | ||
ValidationContainer.prototype.validate = function (withoutFocus) { | ||
if (withoutFocus === void 0) { withoutFocus = false; } | ||
ValidationContainer.prototype.validate = function (withoutFocusOrValidationSettings) { | ||
if (withoutFocusOrValidationSettings === void 0) { withoutFocusOrValidationSettings = { focusMode: FocusMode.Errors }; } | ||
if (!this.childContext) { | ||
throw new Error('childContext is not defined'); | ||
} | ||
return this.childContext.validate(withoutFocus); | ||
return this.childContext.validate(withoutFocusOrValidationSettings); | ||
}; | ||
@@ -402,0 +420,0 @@ ValidationContainer.prototype.render = function () { |
{ | ||
"name": "react-ui-validations", | ||
"version": "1.10.3", | ||
"version": "1.11.0", | ||
"description": "Validations for @skbkontur/react-ui", | ||
@@ -5,0 +5,0 @@ "main": "./index.js", |
Sorry, the diff of this file is not supported yet
163501
1325