Skip to content

Commit 901e8df

Browse files
Merge pull request #30 from SimplrJS/feature/normalizers-and-modifiers
Normalizers and modifiers.
2 parents eea5999 + efa1a71 commit 901e8df

File tree

16 files changed

+168
-10
lines changed

16 files changed

+168
-10
lines changed

packages/simplr-forms-core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "Shared simplr-forms logic.",
55
"repository": "SimplrJS/simplr-forms",
66
"homepage": "https://github.com/SimplrJS/simplr-forms",
7-
"main": "main.js",
7+
"main": "index.js",
88
"types": "index.d.ts",
99
"author": "simplrjs <[email protected]> (https://github.com/simplrjs)",
1010
"scripts": {

packages/simplr-forms-core/src/abstractions/base-field.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,9 @@ export abstract class BaseField<TProps extends FieldProps, TState extends BaseFi
134134
*/
135135
protected get Value(): FieldValue {
136136
// If field is defined
137-
if (this.state != null && this.state.Field != null) {
137+
if (this.state != null && this.state.Value != null) {
138138
// Return its value
139-
return (this.state.Field as FieldStateRecord).Value;
139+
return this.state.Value;
140140
}
141141

142142
// Return default value
@@ -157,7 +157,6 @@ export abstract class BaseField<TProps extends FieldProps, TState extends BaseFi
157157
const parser = this.props.parseValue as FieldParseValueCallback;
158158
return parser(value);
159159
}
160-
161160
return ValueHelpers.ParseValue(
162161
React.Children.toArray(this.props.children) as Array<JSX.Element>,
163162
value
@@ -166,7 +165,7 @@ export abstract class BaseField<TProps extends FieldProps, TState extends BaseFi
166165

167166
protected FormatValue(value: FieldValue): FieldValue {
168167
if (this.props.formatValue != null) {
169-
const formatter = this.props.parseValue as FieldFormatValueCallback;
168+
const formatter = this.props.formatValue as FieldFormatValueCallback;
170169
return formatter(value);
171170
}
172171

@@ -177,8 +176,8 @@ export abstract class BaseField<TProps extends FieldProps, TState extends BaseFi
177176
}
178177

179178
protected NormalizeValue(value: FieldValue): FieldValue {
180-
if (this.props.formatValue != null) {
181-
const normalizer = this.props.parseValue as FieldNormalizeValueCallback;
179+
if (this.props.normalizeValue != null) {
180+
const normalizer = this.props.normalizeValue as FieldNormalizeValueCallback;
182181
return normalizer(value);
183182
}
184183

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./modifiers/index";
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import * as React from "react";
2+
import { Modifier } from "../contracts/value";
3+
import { FieldValue } from "../contracts/field";
4+
import { ValueOfType } from "../utils/value-helpers";
5+
6+
export abstract class BaseModifier<TProps, TState> extends React.Component<TProps, TState> implements Modifier {
7+
// Indentifier function
8+
static SimplrFormsCoreModifier() { }
9+
abstract Format(value: FieldValue): FieldValue;
10+
abstract Parse(value: FieldValue): FieldValue;
11+
render() {
12+
return null;
13+
}
14+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { FieldValue } from "../contracts/field";
2+
import { ValueOfType } from "../utils/value-helpers";
3+
import { BaseModifier } from "./base-modifier";
4+
5+
export interface CurrencyProps {
6+
symbol: string;
7+
}
8+
9+
const EMPTY_VALUE = "";
10+
11+
export class CurrencyModifier extends BaseModifier<CurrencyProps, {}> {
12+
private emptyFormattedValue = this.Format(EMPTY_VALUE);
13+
14+
Format(value: FieldValue): FieldValue {
15+
value = value !== EMPTY_VALUE ? value : 0;
16+
return `${value}${this.props.symbol}`;
17+
}
18+
Parse(value: FieldValue): FieldValue {
19+
if (value === this.emptyFormattedValue) {
20+
console.log(`Parser got an emptyFormattedValue.`);
21+
return 0;
22+
}
23+
if (ValueOfType<string>(value, CurrencyModifier.name, "string")) {
24+
const firstChar = value.substr(0, 1);
25+
let negative = false;
26+
if (firstChar === "-") {
27+
negative = true;
28+
value = value.substr(1);
29+
}
30+
const leadingMinus = negative ? "-" : "";
31+
const numValue = Number(leadingMinus + value.replace(/[^0-9\.]+/g, ""));
32+
return numValue;
33+
}
34+
}
35+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from "./base-modifier";
2+
export * from "./currency";
3+
export * from "./numeric";
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { FieldValue } from "../contracts/field";
2+
import { ValueOfType } from "../utils/value-helpers";
3+
import { BaseModifier } from "./base-modifier";
4+
5+
export class NumericModifier extends BaseModifier<{}, {}> {
6+
Format(value: FieldValue): FieldValue {
7+
if (value == null) {
8+
return "";
9+
}
10+
return value.toString();
11+
}
12+
Parse(value: FieldValue): FieldValue {
13+
return value.replace(/[^0-9\.]+/g, "");
14+
}
15+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./normalizers/index";
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 { FieldValue } from "../contracts/field";
3+
import { ValueOfType } from "../utils/value-helpers";
4+
import { BaseNormalizer } from "./base-normalizer";
5+
6+
export interface AlphanumericProps {
7+
allowSpaces?: boolean;
8+
}
9+
10+
export class AlphanumericNormalizer extends BaseNormalizer<AlphanumericProps, {}> {
11+
static defaultProps: AlphanumericProps = {
12+
allowSpaces: true
13+
};
14+
15+
Normalize(value: FieldValue): FieldValue {
16+
if (ValueOfType<string>(value, AlphanumericNormalizer.name, "string")) {
17+
const space = this.props.allowSpaces ? " " : "";
18+
const regex = new RegExp(`[^0-9a-zA-Z${space}]`);
19+
return value.replace(regex, "");
20+
}
21+
}
22+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import * as React from "react";
2+
import { Normalizer } from "../contracts/value";
3+
import { FieldValue } from "../contracts/field";
4+
import { ValueOfType } from "../utils/value-helpers";
5+
6+
export abstract class BaseNormalizer<TProps, TState> extends React.Component<TProps, TState> implements Normalizer {
7+
// Indentifier function
8+
static SimplrFormsCoreNormalizer() { }
9+
abstract Normalize(value: FieldValue): FieldValue;
10+
render() {
11+
return null;
12+
}
13+
}

0 commit comments

Comments
 (0)