Skip to content

Commit c752cd1

Browse files
Merge pull request #104 from SimplrJS/master
Master to dev (fixing tree)
2 parents fca8a93 + 7226260 commit c752cd1

File tree

14 files changed

+163
-25
lines changed

14 files changed

+163
-25
lines changed

common/config/rush/npm-shrinkwrap.json

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/react-forms-dom/src/abstractions/base-dom-field.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export type BaseDomFieldState = BaseFieldState;
1111

1212
export abstract class BaseDomField<TProps extends DomFieldProps, TState extends BaseDomFieldState, TUnderlyingElement = any>
1313
extends BaseField<TProps, TState> {
14-
public Element: TUnderlyingElement | undefined;
14+
public Element: TUnderlyingElement | null;
1515

1616
protected OnFocus = (event: React.FocusEvent<any>): void => {
1717
const props = this.props as DomFieldProps;
@@ -63,7 +63,7 @@ export abstract class BaseDomField<TProps extends DomFieldProps, TState extends
6363
return restProps;
6464
}
6565

66-
protected SetElementRef = (element: TUnderlyingElement | undefined): void => {
66+
protected SetElementRef = (element: TUnderlyingElement | null): void => {
6767
this.Element = element;
6868
}
6969

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import * as Immutable from "immutable";
2+
import { BaseContainer, BaseContainerParentContext } from "@simplr/react-forms";
3+
import { FormStore } from "@simplr/react-forms/stores";
4+
import { FormError, FieldValidationStatus } from "@simplr/react-forms/contracts";
5+
6+
export type BaseErrorsContainerProps = {};
7+
8+
export type FieldErrors = Immutable.Map<string, FormError>;
9+
10+
export interface BaseErrorsContainerState {
11+
FieldErrors: FieldErrors;
12+
FormError?: FormError;
13+
}
14+
15+
export abstract class BaseErrorsContainer<TProps extends BaseErrorsContainerProps, TState extends BaseErrorsContainerState>
16+
extends BaseContainer<TProps, TState> {
17+
public state: TState = {
18+
FieldErrors: Immutable.Map<string, FormError>()
19+
} as TState;
20+
21+
protected OnStoreUpdated(): void {
22+
const storeState = this.FormStore.GetState();
23+
24+
if (!storeState.HasError) {
25+
this.setState(state => {
26+
state.FieldErrors = Immutable.Map<string, FormError>();
27+
state.FormError = undefined;
28+
return state;
29+
});
30+
return;
31+
}
32+
const formError = storeState.Form.Error;
33+
const fieldsWithError = storeState
34+
.FieldsValidationStatuses
35+
.filter(x => x != null && x === FieldValidationStatus.HasError)
36+
.keySeq()
37+
.toArray();
38+
39+
const fieldsErrors: { [id: string]: FormError } = {};
40+
41+
fieldsWithError.forEach(fieldId => {
42+
const fieldError = storeState.Fields.get(fieldId).Error;
43+
if (fieldError != null) {
44+
fieldsErrors[fieldId] = fieldError;
45+
}
46+
});
47+
48+
this.setState(state => {
49+
state.FormError = formError;
50+
state.FieldErrors = Immutable.Map<string, FormError>(fieldsErrors);
51+
52+
return state;
53+
});
54+
}
55+
56+
public abstract render(): JSX.Element | null;
57+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from "./base-dom-field";
2+
export * from "./base-errors-container";
23
export * from "./base-form-button";
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import * as React from "react";
2+
import { FormStore } from "@simplr/react-forms/stores";
3+
import { FormError, FieldValidationStatus } from "@simplr/react-forms/contracts";
4+
5+
import {
6+
BaseErrorsContainer,
7+
BaseErrorsContainerState,
8+
FieldErrors
9+
} from "../abstractions/base-errors-container";
10+
11+
export type ErrorsTemplate = (fieldErrors: FieldErrors, formError: FormError | undefined, formStore: FormStore) => JSX.Element | null;
12+
13+
export interface ErrorsContainerProps {
14+
template: ErrorsTemplate;
15+
}
16+
17+
export type ErrorsContainerState = BaseErrorsContainerState;
18+
19+
export class ErrorsContainer extends BaseErrorsContainer<ErrorsContainerProps, ErrorsContainerState> {
20+
public render(): JSX.Element | null {
21+
return this.props.template(this.state.FieldErrors, this.state.FormError, this.FormStore);
22+
}
23+
}

packages/react-forms-dom/src/components/fields-array.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ interface Dictionary {
1414
}
1515

1616
export class FieldsArray extends BaseFieldsArray<FieldsArrayProps, FieldsArrayState> {
17-
public Element: HTMLDivElement | undefined;
17+
public Element: HTMLDivElement | null;
1818

19-
protected SetElementRef = (element: HTMLDivElement | undefined): void => {
19+
protected SetElementRef = (element: HTMLDivElement | null): void => {
2020
this.Element = element;
2121
}
2222

packages/react-forms-dom/src/components/fields-group.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ interface Dictionary {
1414
}
1515

1616
export class FieldsGroup extends BaseFieldsGroup<FieldsGroupProps, FieldsGroupState> {
17-
public Element: HTMLDivElement | undefined;
17+
public Element: HTMLDivElement | null;
1818

19-
protected SetElementRef = (element: HTMLDivElement | undefined): void => {
19+
protected SetElementRef = (element: HTMLDivElement | null): void => {
2020
this.Element = element;
2121
}
2222

packages/react-forms-dom/src/components/form.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import { BaseForm } from "@simplr/react-forms";
44
import { FormProps } from "../contracts/form";
55

66
export class Form extends BaseForm<FormProps, {}> {
7-
public Element: HTMLFormElement | undefined;
7+
public Element: HTMLFormElement | null;
88

9-
protected SetElementRef = (element: HTMLFormElement | undefined) => {
9+
protected SetElementRef = (element: HTMLFormElement | null) => {
1010
this.Element = element;
1111
if (this.FormStore != null && element != null) {
1212
this.FormStore.SetFormSubmitCallback(() => {

packages/react-forms-dom/src/components/radio.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export interface RadioState {
3434
export type RadioParentContext = RadioGroupChildContext & BaseContainerParentContext;
3535

3636
export class Radio extends BaseContainer<RadioProps, RadioState> {
37-
public Element: HTMLInputElement | undefined;
37+
public Element: HTMLInputElement | null;
3838
public state: RadioState = {};
3939
public context: RadioParentContext;
4040

@@ -139,7 +139,7 @@ export class Radio extends BaseContainer<RadioProps, RadioState> {
139139
return otherProps;
140140
}
141141

142-
protected SetElementRef = (element: HTMLInputElement | undefined) => {
142+
protected SetElementRef = (element: HTMLInputElement | null) => {
143143
this.Element = element;
144144
}
145145

packages/react-forms-dom/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export * from "./components/reset";
44
export * from "./components/clear";
55
export * from "./components/fields-group";
66
export * from "./components/fields-array";
7+
export * from "./components/errors-container";
78

89
// Fields
910
export * from "./components/text";

0 commit comments

Comments
 (0)