Skip to content

Commit af6061f

Browse files
author
Martynas Žilinskas
authored
DOM components (#60)
* Added password field. * Added email field. * Added search field. * Added TextArea component. * Updated Numeric modifier. * Core field NormalizeValue, ParseValue and FormatValue props callbacks will be called before components. * Added DefaultModifiers and DefaultNormalizers to core field. Updated value helpers to filter out duplications. * Fixed onChange props interface errors. * Fixed import. * Externals fix webpack in simplr-forms-dom. * Webpack externals fix in simplr-forms-dom. * Number is still WIP. * Removed onFocus, onBlur from CoreFieldProps. * Added HTMLElementProps for extending. Added onFocus and onBlur to DomFieldProps. * Updated Email field. Comment out DefaultModifiers in Number field. * Added hidden field. * Added Select and checkbox fields. * Added GetHTMLProps. * Added FieldId in Field child context. * Added FieldId getter in base-container. * Added OnFocus, OnBlur and IsDisabled in Radio button. * Added children to DomFieldProps. * Changed FormEventHandler -> ChangeEventHandler. * Fixed return types, prop types. * Fixed checkbox: value -> checked. * Fixed GetHTMLProps in BaseDomField and Radio button. * Added Element property to all fields. Closes #38
1 parent 9bae54a commit af6061f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1539
-102
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,4 +255,5 @@ yarn.lock
255255
packages/simplr-forms-test
256256

257257
dist
258-
@types
258+
@types
259+
package-lock.json

.travis.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
language: node_js
2+
notifications:
3+
email:
4+
on_success: never
5+
on_failure: change
6+
node_js:
7+
- stable
8+
before_install:
9+
- "npm install -g npm@^4.6.1"
10+
- "npm install -g typescript@^2.3"
11+
script:
12+
- npm run bootstrap
13+
- npm run tools-build
14+
- npm run source-build
15+
- npm test

package.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
{
22
"name": "simplr-forms",
33
"private": true,
4-
"scripts": {},
4+
"scripts": {
5+
"bootstrap": "lerna bootstrap",
6+
"tools-build": "lerna run gulp-build",
7+
"source-build": "lerna run build",
8+
"test": "lerna run test-tsc"
9+
},
510
"devDependencies": {
6-
"lerna": "2.0.0-rc.1",
7-
"typescript": "2.3.0"
11+
"lerna": "2.0.0-rc.2"
812
}
913
}

packages/simplr-forms-dom/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88
"types": "index.d.ts",
99
"author": "simplrjs <[email protected]> (https://github.com/simplrjs)",
1010
"scripts": {
11-
"build": "webpack && npm run tslint",
11+
"build": "gulp",
1212
"watch": "webpack -w",
13-
"tslint": "tslint --config ./tslint.json --project . --exclude __tests__/**/* && echo TsLint test successfully passed.",
1413
"uglifyjs": "uglifyjs ./dist/simplr-forms-dom.js -o ./dist/simplr-forms-dom.min.js --compress --mangle",
1514
"release": "npm run build && npm run uglifyjs",
1615
"test": "jest",
16+
"test-tslint": "tslint --config ./tslint.json --project . --exclude ./__tests__/* && echo TsLint test successfully passed.",
17+
"test-tsc": "tsc -p . --noEmit",
1718
"test-watch": "jest --watchAll",
1819
"test-coverage": "npm test -- --coverage",
1920
"prepublishOnly": "npm run build",

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

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,20 @@ export interface BaseDomFieldState extends BaseFieldState {
1313

1414
}
1515

16-
export abstract class BaseDomField<TProps extends DomFieldProps, TState extends BaseDomFieldState>
16+
export abstract class BaseDomField<TProps extends DomFieldProps, TState extends BaseDomFieldState, TUnderlyingElement = any>
1717
extends BaseField<TProps, TState> {
18-
protected OnFocus = (event: React.FocusEvent<HTMLInputElement>): void => {
19-
const props = this.props as FieldProps;
18+
public Element: TUnderlyingElement | undefined;
19+
20+
protected OnFocus = (event: React.FocusEvent<any>): void => {
21+
const props = this.props as DomFieldProps;
2022
if (props.onFocus != null) {
2123
props.onFocus(event);
2224
}
23-
2425
this.Focus();
2526
}
2627

27-
protected OnBlur = (event: React.FocusEvent<HTMLInputElement>): void => {
28-
const props = this.props as FieldProps;
28+
protected OnBlur = (event: React.FocusEvent<any>): void => {
29+
const props = this.props as DomFieldProps;
2930
if (props.onBlur != null) {
3031
props.onBlur(event);
3132
}
@@ -35,13 +36,39 @@ export abstract class BaseDomField<TProps extends DomFieldProps, TState extends
3536

3637
protected get FieldTemplate(): DomFieldTemplateCallback | undefined {
3738
const formProps = this.FormStore.GetState().Form.Props as FormProps;
39+
if (this.props.template != null) {
40+
return this.props.template;
41+
}
42+
3843
if (formProps.template) {
3944
return formProps.template;
4045
}
46+
}
4147

42-
if (this.props.template != null) {
43-
return this.props.template;
44-
}
48+
protected GetHTMLProps(props: DomFieldProps): {} {
49+
const {
50+
defaultValue,
51+
destroyOnUnmount,
52+
disabled,
53+
formatValue,
54+
initialValue,
55+
name,
56+
normalizeValue,
57+
onBlur,
58+
onFocus,
59+
parseValue,
60+
template,
61+
validationType,
62+
value,
63+
children,
64+
...restProps
65+
} = props;
66+
67+
return restProps;
68+
}
69+
70+
protected SetElementRef = (element: TUnderlyingElement): void => {
71+
this.Element = element;
4572
}
4673

4774
public abstract renderField(): JSX.Element | null;
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import * as React from "react";
2+
import { FieldValue } from "simplr-forms/contracts";
3+
import { DomFieldProps } from "../contracts/field";
4+
5+
import { BaseDomField, BaseDomFieldState } from "../abstractions/base-dom-field";
6+
import { FieldOnChangeCallback } from "../contracts/field";
7+
import {
8+
HTMLElementProps
9+
} from "../contracts/field";
10+
import {
11+
FormProps
12+
} from "../contracts/form";
13+
14+
export type CheckBoxOnChangeCallback = FieldOnChangeCallback<HTMLInputElement>;
15+
16+
/**
17+
* Override the differences between extended interfaces.
18+
*/
19+
export interface CheckBoxProps extends DomFieldProps, HTMLElementProps<HTMLInputElement> {
20+
name: string;
21+
onFocus?: React.FocusEventHandler<HTMLInputElement>;
22+
onBlur?: React.FocusEventHandler<HTMLInputElement>;
23+
onChange?: CheckBoxOnChangeCallback;
24+
25+
defaultValue?: boolean;
26+
initialValue?: boolean;
27+
value?: boolean;
28+
ref?: React.Ref<CheckBox>;
29+
}
30+
31+
export class CheckBox extends BaseDomField<CheckBoxProps, BaseDomFieldState, HTMLInputElement> {
32+
protected GetValueFromEvent(event: React.ChangeEvent<HTMLInputElement>): FieldValue {
33+
return event.currentTarget.checked;
34+
}
35+
36+
protected OnChangeHandler: React.ChangeEventHandler<HTMLInputElement> = (event) => {
37+
this.OnValueChange(this.GetValueFromEvent(event));
38+
39+
const newValue = this.FormStore.GetField(this.FieldId).Value;
40+
41+
if (this.props.onChange != null) {
42+
event.persist();
43+
this.props.onChange(event, newValue, this.FieldId, this.FormId);
44+
}
45+
46+
const formStoreState = this.FormStore.GetState();
47+
const formProps = formStoreState.Form.Props as FormProps;
48+
if (formProps.onChange != null) {
49+
event.persist();
50+
formProps.onChange(event, newValue, this.FieldId, this.FormId);
51+
}
52+
}
53+
54+
protected get RawDefaultValue(): boolean {
55+
if (this.props.defaultValue != null) {
56+
return this.props.defaultValue;
57+
}
58+
return false;
59+
}
60+
61+
renderField(): JSX.Element | null {
62+
return <input
63+
ref={this.SetElementRef}
64+
type="checkbox"
65+
name={this.FieldId}
66+
checked={this.Value}
67+
onChange={this.OnChangeHandler}
68+
disabled={this.Disabled}
69+
onFocus={this.OnFocus}
70+
onBlur={this.OnBlur}
71+
{...this.GetHTMLProps(this.props) }
72+
/>;
73+
}
74+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import * as React from "react";
2+
import { FieldValue } from "simplr-forms/contracts";
3+
import { DomFieldProps } from "../contracts/field";
4+
5+
import { BaseDomField, BaseDomFieldState } from "../abstractions/base-dom-field";
6+
import { FieldOnChangeCallback } from "../contracts/field";
7+
import {
8+
HTMLElementProps
9+
} from "../contracts/field";
10+
import {
11+
FormProps
12+
} from "../contracts/form";
13+
14+
export type EmailOnChangeCallback = FieldOnChangeCallback<HTMLInputElement>;
15+
16+
/**
17+
* Override the differences between extended interfaces.
18+
*/
19+
export interface EmailProps extends DomFieldProps, HTMLElementProps<HTMLInputElement> {
20+
name: string;
21+
onFocus?: React.FocusEventHandler<HTMLInputElement>;
22+
onBlur?: React.FocusEventHandler<HTMLInputElement>;
23+
onChange?: EmailOnChangeCallback;
24+
25+
defaultValue?: FieldValue;
26+
value?: FieldValue;
27+
ref?: React.Ref<Email>;
28+
}
29+
30+
export class Email extends BaseDomField<EmailProps, BaseDomFieldState, HTMLInputElement> {
31+
protected GetValueFromEvent(event: React.ChangeEvent<HTMLInputElement>): FieldValue {
32+
return event.currentTarget.value;
33+
}
34+
35+
protected OnChangeHandler: React.ChangeEventHandler<HTMLInputElement> = (event) => {
36+
this.OnValueChange(this.GetValueFromEvent(event));
37+
38+
const newValue = this.FormStore.GetField(this.FieldId).Value;
39+
40+
if (this.props.onChange != null) {
41+
event.persist();
42+
this.props.onChange(event, newValue, this.FieldId, this.FormId);
43+
}
44+
45+
const formStoreState = this.FormStore.GetState();
46+
const formProps = formStoreState.Form.Props as FormProps;
47+
if (formProps.onChange != null) {
48+
event.persist();
49+
formProps.onChange(event, newValue, this.FieldId, this.FormId);
50+
}
51+
}
52+
53+
protected get RawDefaultValue(): string {
54+
if (this.props.defaultValue != null) {
55+
return this.props.defaultValue;
56+
}
57+
return "";
58+
}
59+
60+
renderField(): JSX.Element | null {
61+
return <input
62+
ref={this.SetElementRef}
63+
type="email"
64+
name={this.FieldId}
65+
value={this.Value}
66+
onChange={this.OnChangeHandler}
67+
disabled={this.Disabled}
68+
onFocus={this.OnFocus}
69+
onBlur={this.OnBlur}
70+
{...this.GetHTMLProps(this.props) }
71+
/>;
72+
}
73+
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,20 @@ export class FieldsArray extends BaseFieldsArray<FieldsArrayProps, FieldsArraySt
2222
this.Element = element;
2323
}
2424

25-
protected HTMLProps(): {} {
25+
protected GetHTMLProps(props: FieldsArrayProps): {} {
2626
const {
2727
name,
2828
index,
2929
destroyOnUnmount,
3030
children,
31-
...rest } = this.props;
32-
return rest;
31+
...restProps } = this.props;
32+
return restProps;
3333
}
3434

3535
render(): JSX.Element | null {
3636
return <div
3737
ref={this.setElementRef}
38-
{...this.HTMLProps() }
38+
{...this.GetHTMLProps(this.props) }
3939
>
4040
{this.props.children}
4141
</div>;

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,19 @@ export class FieldsGroup extends BaseFieldsGroup<FieldsGroupProps, FieldsGroupSt
2222
this.Element = element;
2323
}
2424

25-
protected HTMLProps(): {} {
25+
protected GetHTMLProps(props: FieldsGroupProps): {} {
2626
const {
2727
name,
2828
destroyOnUnmount,
2929
children,
30-
...rest } = this.props;
31-
return rest;
30+
...restProps } = this.props;
31+
return restProps;
3232
}
3333

3434
render(): JSX.Element | null {
3535
return <div
3636
ref={this.setElementRef}
37-
{...this.HTMLProps() }
37+
{...this.GetHTMLProps(this.props) }
3838
>
3939
{this.props.children}
4040
</div>;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import * as React from "react";
2+
import { BaseField, BaseFieldState } from "simplr-forms";
3+
import { FieldProps, FieldValue } from "simplr-forms/contracts";
4+
5+
export interface HiddenProps extends FieldProps {
6+
defaultValue: FieldValue;
7+
value: FieldValue;
8+
}
9+
10+
export class Hidden extends BaseField<HiddenProps, BaseFieldState> {
11+
protected get IsControlled(): boolean {
12+
return true;
13+
}
14+
15+
protected get RawDefaultValue(): FieldValue {
16+
return this.props.defaultValue;
17+
}
18+
19+
render(): null {
20+
return null;
21+
}
22+
}

0 commit comments

Comments
 (0)