Skip to content

Commit 5e92f28

Browse files
Initial tests.
1 parent e3cd28e commit 5e92f28

File tree

10 files changed

+989
-141
lines changed

10 files changed

+989
-141
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"typescript.tsdk": "node_modules\\typescript\\lib"
3+
}

package-lock.json

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

package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"author": "ReactWay",
1313
"license": "AGPLv3",
1414
"devDependencies": {
15+
"@types/fbemitter": "^2.0.32",
1516
"@types/html-webpack-plugin": "3.2.0",
1617
"@types/node": "11.11.3",
1718
"@types/react": "16.8.8",
@@ -21,14 +22,18 @@
2122
"cross-env": "5.2.0",
2223
"html-webpack-plugin": "3.2.0",
2324
"html-webpack-root-plugin": "0.10.0",
25+
"simplr-tslint": "^1.0.0-alpha.14",
26+
"tslint": "^5.14.0",
27+
"tslint-language-service": "^0.9.9",
2428
"typescript": "3.4.0-rc",
29+
"webpack": "^4.29.6",
2530
"webpack-cli": "3.3.0",
2631
"webpack-dev-server": "3.2.1"
2732
},
2833
"dependencies": {
2934
"babel-loader": "8.0.5",
35+
"fbemitter": "^2.1.1",
3036
"react": "16.8.4",
31-
"react-dom": "16.8.4",
32-
"webpack": "4.29.6"
37+
"react-dom": "16.8.4"
3338
}
3439
}

src/components/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export * from "./test"
1+
export * from "./test";

src/components/text-field.tsx

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import React, { useContext, useEffect, useState } from "react";
2+
import { FieldContext } from "../contexts/field-context";
3+
4+
export interface FieldValues {
5+
defaultValue?: string;
6+
initialValue?: string;
7+
currentValue?: string;
8+
}
9+
10+
export interface TextFieldProps extends FieldValues {
11+
name: string;
12+
}
13+
14+
const useFieldValues = (values: FieldValues) => {
15+
console.info("useFieldValues");
16+
const [defaultValue, setDefaultValue] = useState(values.defaultValue || "");
17+
let [initialValue, setInitialValue] = useState(values.initialValue);
18+
if (initialValue == null) {
19+
initialValue = defaultValue;
20+
}
21+
22+
const [currentValue, setCurrentValue] = useState(initialValue);
23+
24+
return { currentValue, setCurrentValue };
25+
};
26+
27+
export const TextField = (props: TextFieldProps) => {
28+
const { currentValue, setCurrentValue } = useFieldValues({
29+
defaultValue: props.defaultValue,
30+
initialValue: props.initialValue,
31+
currentValue: props.currentValue,
32+
});
33+
34+
const onChange = (event: React.ChangeEvent<HTMLInputElement>) => {
35+
// store.setCurrentValue(event.target.value);
36+
setCurrentValue(event.target.value);
37+
};
38+
39+
const onFocus = (event: React.FocusEvent<HTMLInputElement>) => {
40+
console.debug(event.target);
41+
};
42+
43+
return (
44+
<input
45+
type="text"
46+
value={currentValue}
47+
onChange={onChange}
48+
onFocus={onFocus}
49+
/>
50+
);
51+
};

src/contexts/field-context.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { createContext } from "react";
2+
import { FieldStore } from "../stores/field-store";
3+
4+
export interface FieldContextObject {
5+
store: FieldStore;
6+
}
7+
8+
export const FieldContext = createContext<FieldContextObject>({
9+
store: new FieldStore(),
10+
});

src/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React, { useState } from "react";
22
import ReactDOM from "react-dom";
33
import { Test } from "./components";
4+
import { TextField } from "./components/text-field";
45

56
const App = () => {
67
const [value, setValue] = useState("");
@@ -17,7 +18,7 @@ const App = () => {
1718
<div>
1819
<label>
1920
Imported:
20-
<Test />
21+
<TextField name="firstName" initialValue="Test" />
2122
</label>
2223
</div>
2324
</>

src/stores/field-store.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { EventEmitter } from "fbemitter";
2+
3+
const CHANGE_EVENT = "change";
4+
5+
export class FieldStore extends EventEmitter {
6+
public initialValue: string;
7+
public defaultValue: string;
8+
public currentValue: string;
9+
10+
/**
11+
*
12+
*/
13+
constructor(defaultValue: string = "", initialValue?: string) {
14+
super();
15+
16+
if (initialValue == null) {
17+
this.initialValue = defaultValue;
18+
this.currentValue = defaultValue;
19+
} else {
20+
this.initialValue = initialValue;
21+
this.currentValue = initialValue;
22+
}
23+
24+
this.defaultValue = defaultValue;
25+
}
26+
27+
public setCurrentValue(value: string): void {
28+
this.currentValue = value;
29+
this.emit(CHANGE_EVENT);
30+
}
31+
}

tsconfig.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
"target": "es5",
44
"jsx": "react",
55
"strict": true,
6-
"esModuleInterop": true
6+
"esModuleInterop": true,
7+
"plugins": [
8+
{
9+
"name": "tslint-language-service",
10+
"alwaysShowRuleFailuresAsWarnings": true,
11+
"ignoreDefinitionFiles": true
12+
}
13+
]
714
}
815
}

tslint.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "simplr-tslint"
3+
}

0 commit comments

Comments
 (0)