Skip to content

Commit 22b0f1f

Browse files
committed
Refactor to classes to reduce boilerplate
1 parent 05fab78 commit 22b0f1f

File tree

15 files changed

+369
-390
lines changed

15 files changed

+369
-390
lines changed

src/components/Button/index.ts

+25-30
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { registerComponent } from "../config";
2-
import { QPushButton, QIcon } from "@nodegui/nodegui";
1+
import { QPushButton, QIcon, NodeWidget } from "@nodegui/nodegui";
2+
import { Fiber } from "react-reconciler";
33
import { ViewProps, setProps as setViewProps } from "../View";
4-
4+
import { registerComponent, ComponentConfig } from "../config";
55
interface ButtonProps extends ViewProps {
66
text?: string;
77
flat?: boolean;
@@ -28,35 +28,30 @@ const setProps = (
2828
setViewProps(widget, newProps, oldProps);
2929
};
3030

31-
export const Button = registerComponent<ButtonProps>({
32-
id: "button",
33-
getContext() {
34-
return {};
35-
},
36-
shouldSetTextContent: () => {
37-
return false;
38-
},
39-
createInstance: newProps => {
31+
class ButtonConfig extends ComponentConfig {
32+
id = "button";
33+
shouldSetTextContent(nextProps: object): boolean {
34+
return true;
35+
}
36+
createInstance(
37+
newProps: object,
38+
rootInstance: Set<NodeWidget>,
39+
context: any,
40+
workInProgress: Fiber
41+
): NodeWidget {
4042
const widget = new QPushButton();
4143
setProps(widget, newProps, {});
4244
return widget;
43-
},
44-
finalizeInitialChildren: () => {
45-
return false;
46-
},
47-
commitMount: (instance, newProps, internalInstanceHandle) => {
48-
return;
49-
},
50-
prepareUpdate: (
51-
instance,
52-
oldProps,
53-
newProps,
54-
rootContainerInstance,
55-
hostContext
56-
) => {
57-
return true;
58-
},
59-
commitUpdate: (instance, updatePayload, oldProps, newProps, finishedWork) => {
45+
}
46+
commitUpdate(
47+
instance: NodeWidget,
48+
updatePayload: any,
49+
oldProps: object,
50+
newProps: object,
51+
finishedWork: Fiber
52+
): void {
6053
setProps(instance as QPushButton, newProps, oldProps);
6154
}
62-
});
55+
}
56+
57+
export const Button = registerComponent<ButtonProps>(new ButtonConfig());

src/components/CheckBox/index.ts

+24-28
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { registerComponent } from "../config";
2-
import { QCheckBox } from "@nodegui/nodegui";
1+
import { QCheckBox, NodeWidget } from "@nodegui/nodegui";
2+
import { Fiber } from "react-reconciler";
33
import { ViewProps, setProps as setViewProps } from "../View";
4+
import { registerComponent, ComponentConfig } from "../config";
45

56
interface CheckBoxProps extends ViewProps {
67
children?: string;
@@ -25,35 +26,30 @@ const setProps = (
2526
setViewProps(widget, newProps, oldProps);
2627
};
2728

28-
export const CheckBox = registerComponent<CheckBoxProps>({
29-
id: "checkbox",
30-
getContext() {
31-
return {};
32-
},
33-
shouldSetTextContent: () => {
29+
class CheckBoxConfig extends ComponentConfig {
30+
id = "checkbox";
31+
shouldSetTextContent(nextProps: object): boolean {
3432
return true;
35-
},
36-
createInstance: newProps => {
33+
}
34+
createInstance(
35+
newProps: object,
36+
rootInstance: Set<NodeWidget>,
37+
context: any,
38+
workInProgress: Fiber
39+
): NodeWidget {
3740
const widget = new QCheckBox();
3841
setProps(widget, newProps, {});
3942
return widget;
40-
},
41-
finalizeInitialChildren: () => {
42-
return false;
43-
},
44-
commitMount: (instance, newProps, internalInstanceHandle) => {
45-
return;
46-
},
47-
prepareUpdate: (
48-
instance,
49-
oldProps,
50-
newProps,
51-
rootContainerInstance,
52-
hostContext
53-
) => {
54-
return true;
55-
},
56-
commitUpdate: (instance, updatePayload, oldProps, newProps, finishedWork) => {
43+
}
44+
commitUpdate(
45+
instance: NodeWidget,
46+
updatePayload: any,
47+
oldProps: object,
48+
newProps: object,
49+
finishedWork: Fiber
50+
): void {
5751
setProps(instance as QCheckBox, newProps, oldProps);
5852
}
59-
});
53+
}
54+
55+
export const CheckBox = registerComponent<CheckBoxProps>(new CheckBoxConfig());

src/components/Dial/index.ts

+24-28
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { registerComponent } from "../config";
2-
import { QDial } from "@nodegui/nodegui";
1+
import { Fiber } from "react-reconciler";
2+
import { QDial, NodeWidget } from "@nodegui/nodegui";
33
import { ViewProps, setProps as setViewProps } from "../View";
4+
import { registerComponent, ComponentConfig } from "../config";
45

56
export interface DialProps extends ViewProps {
67
notchesVisible?: boolean;
@@ -28,35 +29,30 @@ export const setProps = (
2829
setViewProps(widget, newProps, oldProps);
2930
};
3031

31-
export const Dial = registerComponent<DialProps>({
32-
id: "dial",
33-
getContext() {
34-
return {};
35-
},
36-
shouldSetTextContent: () => {
32+
class DialConfig extends ComponentConfig {
33+
id = "dial";
34+
shouldSetTextContent(nextProps: object): boolean {
3735
return true;
38-
},
39-
createInstance: newProps => {
36+
}
37+
createInstance(
38+
newProps: object,
39+
rootInstance: Set<NodeWidget>,
40+
context: any,
41+
workInProgress: Fiber
42+
): NodeWidget {
4043
const widget = new QDial();
4144
setProps(widget, newProps, {});
4245
return widget;
43-
},
44-
finalizeInitialChildren: () => {
45-
return false;
46-
},
47-
commitMount: (instance, newProps, internalInstanceHandle) => {
48-
return;
49-
},
50-
prepareUpdate: (
51-
instance,
52-
oldProps,
53-
newProps,
54-
rootContainerInstance,
55-
hostContext
56-
) => {
57-
return true;
58-
},
59-
commitUpdate: (instance, updatePayload, oldProps, newProps, finishedWork) => {
46+
}
47+
commitUpdate(
48+
instance: NodeWidget,
49+
updatePayload: any,
50+
oldProps: object,
51+
newProps: object,
52+
finishedWork: Fiber
53+
): void {
6054
setProps(instance as QDial, newProps, oldProps);
6155
}
62-
});
56+
}
57+
58+
export const Dial = registerComponent<DialProps>(new DialConfig());

src/components/Image/index.ts

+30-29
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1-
import { registerComponent } from "../config";
2-
import { QPixmap, QLabelEvents, AspectRatioMode } from "@nodegui/nodegui";
1+
import {
2+
QPixmap,
3+
QLabelEvents,
4+
AspectRatioMode,
5+
NodeWidget
6+
} from "@nodegui/nodegui";
7+
import { Fiber } from "react-reconciler";
8+
import { registerComponent, ComponentConfig } from "../config";
39
import { ImageLabel } from "./ImageLabel";
410
import { TextProps, setProps as setTextProps } from "../Text";
511
interface ImageProps extends TextProps {
@@ -30,39 +36,34 @@ const setProps = (
3036
setTextProps(widget, newProps, oldProps);
3137
};
3238

33-
export const Image = registerComponent<ImageProps>({
34-
id: "image",
35-
getContext() {
36-
return {};
37-
},
38-
shouldSetTextContent: () => {
39-
return false;
40-
},
41-
createInstance: newProps => {
39+
class ImageConfig extends ComponentConfig {
40+
id = "image";
41+
shouldSetTextContent(nextProps: object): boolean {
42+
return true;
43+
}
44+
createInstance(
45+
newProps: object,
46+
rootInstance: Set<NodeWidget>,
47+
context: any,
48+
workInProgress: Fiber
49+
): NodeWidget {
4250
const widget = new ImageLabel();
4351
setProps(widget, newProps, {});
4452
widget.addEventListener(QLabelEvents.Resize, () => {
4553
const size = widget.size();
4654
widget.scalePixmap(size.width, size.height);
4755
});
4856
return widget;
49-
},
50-
finalizeInitialChildren: () => {
51-
return false;
52-
},
53-
commitMount: (instance, newProps, internalInstanceHandle) => {
54-
return;
55-
},
56-
prepareUpdate: (
57-
instance,
58-
oldProps,
59-
newProps,
60-
rootContainerInstance,
61-
hostContext
62-
) => {
63-
return true;
64-
},
65-
commitUpdate: (instance, updatePayload, oldProps, newProps, finishedWork) => {
57+
}
58+
commitUpdate(
59+
instance: NodeWidget,
60+
updatePayload: any,
61+
oldProps: object,
62+
newProps: object,
63+
finishedWork: Fiber
64+
): void {
6665
setProps(instance as ImageLabel, newProps, oldProps);
6766
}
68-
});
67+
}
68+
69+
export const Image = registerComponent<ImageProps>(new ImageConfig());

src/components/LineEdit/index.ts

+24-29
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { registerComponent } from "../config";
2-
import { QLineEdit } from "@nodegui/nodegui";
1+
import { QLineEdit, NodeWidget } from "@nodegui/nodegui";
2+
import { Fiber } from "react-reconciler";
33
import { ViewProps, setProps as setViewProps } from "../View";
4-
4+
import { registerComponent, ComponentConfig } from "../config";
55
interface LineEditProps extends ViewProps {
66
children?: string;
77
text?: string;
@@ -29,35 +29,30 @@ const setProps = (
2929
setViewProps(widget, newProps, oldProps);
3030
};
3131

32-
export const LineEdit = registerComponent<LineEditProps>({
33-
id: "linedit",
34-
getContext() {
35-
return {};
36-
},
37-
shouldSetTextContent: () => {
32+
class LineEditConfig extends ComponentConfig {
33+
id = "linedit";
34+
shouldSetTextContent(nextProps: object): boolean {
3835
return true;
39-
},
40-
createInstance: newProps => {
36+
}
37+
createInstance(
38+
newProps: object,
39+
rootInstance: Set<NodeWidget>,
40+
context: any,
41+
workInProgress: Fiber
42+
): NodeWidget {
4143
const widget = new QLineEdit();
4244
setProps(widget, newProps, {});
4345
return widget;
44-
},
45-
finalizeInitialChildren: () => {
46-
return false;
47-
},
48-
commitMount: (instance, newProps, internalInstanceHandle) => {
49-
return;
50-
},
51-
prepareUpdate: (
52-
instance,
53-
oldProps,
54-
newProps,
55-
rootContainerInstance,
56-
hostContext
57-
) => {
58-
return true;
59-
},
60-
commitUpdate: (instance, updatePayload, oldProps, newProps, finishedWork) => {
46+
}
47+
commitUpdate(
48+
instance: NodeWidget,
49+
updatePayload: any,
50+
oldProps: object,
51+
newProps: object,
52+
finishedWork: Fiber
53+
): void {
6154
setProps(instance as QLineEdit, newProps, oldProps);
6255
}
63-
});
56+
}
57+
58+
export const LineEdit = registerComponent<LineEditProps>(new LineEditConfig());

0 commit comments

Comments
 (0)