Skip to content

Commit

Permalink
Refactor to classes to reduce boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
a7ul committed Sep 1, 2019
1 parent 05fab78 commit 22b0f1f
Show file tree
Hide file tree
Showing 15 changed files with 369 additions and 390 deletions.
55 changes: 25 additions & 30 deletions src/components/Button/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { registerComponent } from "../config";
import { QPushButton, QIcon } from "@nodegui/nodegui";
import { QPushButton, QIcon, NodeWidget } from "@nodegui/nodegui";
import { Fiber } from "react-reconciler";
import { ViewProps, setProps as setViewProps } from "../View";

import { registerComponent, ComponentConfig } from "../config";
interface ButtonProps extends ViewProps {
text?: string;
flat?: boolean;
Expand All @@ -28,35 +28,30 @@ const setProps = (
setViewProps(widget, newProps, oldProps);
};

export const Button = registerComponent<ButtonProps>({
id: "button",
getContext() {
return {};
},
shouldSetTextContent: () => {
return false;
},
createInstance: newProps => {
class ButtonConfig extends ComponentConfig {
id = "button";
shouldSetTextContent(nextProps: object): boolean {
return true;
}
createInstance(
newProps: object,
rootInstance: Set<NodeWidget>,
context: any,
workInProgress: Fiber
): NodeWidget {
const widget = new QPushButton();
setProps(widget, newProps, {});
return widget;
},
finalizeInitialChildren: () => {
return false;
},
commitMount: (instance, newProps, internalInstanceHandle) => {
return;
},
prepareUpdate: (
instance,
oldProps,
newProps,
rootContainerInstance,
hostContext
) => {
return true;
},
commitUpdate: (instance, updatePayload, oldProps, newProps, finishedWork) => {
}
commitUpdate(
instance: NodeWidget,
updatePayload: any,
oldProps: object,
newProps: object,
finishedWork: Fiber
): void {
setProps(instance as QPushButton, newProps, oldProps);
}
});
}

export const Button = registerComponent<ButtonProps>(new ButtonConfig());
52 changes: 24 additions & 28 deletions src/components/CheckBox/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { registerComponent } from "../config";
import { QCheckBox } from "@nodegui/nodegui";
import { QCheckBox, NodeWidget } from "@nodegui/nodegui";
import { Fiber } from "react-reconciler";
import { ViewProps, setProps as setViewProps } from "../View";
import { registerComponent, ComponentConfig } from "../config";

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

export const CheckBox = registerComponent<CheckBoxProps>({
id: "checkbox",
getContext() {
return {};
},
shouldSetTextContent: () => {
class CheckBoxConfig extends ComponentConfig {
id = "checkbox";
shouldSetTextContent(nextProps: object): boolean {
return true;
},
createInstance: newProps => {
}
createInstance(
newProps: object,
rootInstance: Set<NodeWidget>,
context: any,
workInProgress: Fiber
): NodeWidget {
const widget = new QCheckBox();
setProps(widget, newProps, {});
return widget;
},
finalizeInitialChildren: () => {
return false;
},
commitMount: (instance, newProps, internalInstanceHandle) => {
return;
},
prepareUpdate: (
instance,
oldProps,
newProps,
rootContainerInstance,
hostContext
) => {
return true;
},
commitUpdate: (instance, updatePayload, oldProps, newProps, finishedWork) => {
}
commitUpdate(
instance: NodeWidget,
updatePayload: any,
oldProps: object,
newProps: object,
finishedWork: Fiber
): void {
setProps(instance as QCheckBox, newProps, oldProps);
}
});
}

export const CheckBox = registerComponent<CheckBoxProps>(new CheckBoxConfig());
52 changes: 24 additions & 28 deletions src/components/Dial/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { registerComponent } from "../config";
import { QDial } from "@nodegui/nodegui";
import { Fiber } from "react-reconciler";
import { QDial, NodeWidget } from "@nodegui/nodegui";
import { ViewProps, setProps as setViewProps } from "../View";
import { registerComponent, ComponentConfig } from "../config";

export interface DialProps extends ViewProps {
notchesVisible?: boolean;
Expand Down Expand Up @@ -28,35 +29,30 @@ export const setProps = (
setViewProps(widget, newProps, oldProps);
};

export const Dial = registerComponent<DialProps>({
id: "dial",
getContext() {
return {};
},
shouldSetTextContent: () => {
class DialConfig extends ComponentConfig {
id = "dial";
shouldSetTextContent(nextProps: object): boolean {
return true;
},
createInstance: newProps => {
}
createInstance(
newProps: object,
rootInstance: Set<NodeWidget>,
context: any,
workInProgress: Fiber
): NodeWidget {
const widget = new QDial();
setProps(widget, newProps, {});
return widget;
},
finalizeInitialChildren: () => {
return false;
},
commitMount: (instance, newProps, internalInstanceHandle) => {
return;
},
prepareUpdate: (
instance,
oldProps,
newProps,
rootContainerInstance,
hostContext
) => {
return true;
},
commitUpdate: (instance, updatePayload, oldProps, newProps, finishedWork) => {
}
commitUpdate(
instance: NodeWidget,
updatePayload: any,
oldProps: object,
newProps: object,
finishedWork: Fiber
): void {
setProps(instance as QDial, newProps, oldProps);
}
});
}

export const Dial = registerComponent<DialProps>(new DialConfig());
59 changes: 30 additions & 29 deletions src/components/Image/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { registerComponent } from "../config";
import { QPixmap, QLabelEvents, AspectRatioMode } from "@nodegui/nodegui";
import {
QPixmap,
QLabelEvents,
AspectRatioMode,
NodeWidget
} from "@nodegui/nodegui";
import { Fiber } from "react-reconciler";
import { registerComponent, ComponentConfig } from "../config";
import { ImageLabel } from "./ImageLabel";
import { TextProps, setProps as setTextProps } from "../Text";
interface ImageProps extends TextProps {
Expand Down Expand Up @@ -30,39 +36,34 @@ const setProps = (
setTextProps(widget, newProps, oldProps);
};

export const Image = registerComponent<ImageProps>({
id: "image",
getContext() {
return {};
},
shouldSetTextContent: () => {
return false;
},
createInstance: newProps => {
class ImageConfig extends ComponentConfig {
id = "image";
shouldSetTextContent(nextProps: object): boolean {
return true;
}
createInstance(
newProps: object,
rootInstance: Set<NodeWidget>,
context: any,
workInProgress: Fiber
): NodeWidget {
const widget = new ImageLabel();
setProps(widget, newProps, {});
widget.addEventListener(QLabelEvents.Resize, () => {
const size = widget.size();
widget.scalePixmap(size.width, size.height);
});
return widget;
},
finalizeInitialChildren: () => {
return false;
},
commitMount: (instance, newProps, internalInstanceHandle) => {
return;
},
prepareUpdate: (
instance,
oldProps,
newProps,
rootContainerInstance,
hostContext
) => {
return true;
},
commitUpdate: (instance, updatePayload, oldProps, newProps, finishedWork) => {
}
commitUpdate(
instance: NodeWidget,
updatePayload: any,
oldProps: object,
newProps: object,
finishedWork: Fiber
): void {
setProps(instance as ImageLabel, newProps, oldProps);
}
});
}

export const Image = registerComponent<ImageProps>(new ImageConfig());
53 changes: 24 additions & 29 deletions src/components/LineEdit/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { registerComponent } from "../config";
import { QLineEdit } from "@nodegui/nodegui";
import { QLineEdit, NodeWidget } from "@nodegui/nodegui";
import { Fiber } from "react-reconciler";
import { ViewProps, setProps as setViewProps } from "../View";

import { registerComponent, ComponentConfig } from "../config";
interface LineEditProps extends ViewProps {
children?: string;
text?: string;
Expand Down Expand Up @@ -29,35 +29,30 @@ const setProps = (
setViewProps(widget, newProps, oldProps);
};

export const LineEdit = registerComponent<LineEditProps>({
id: "linedit",
getContext() {
return {};
},
shouldSetTextContent: () => {
class LineEditConfig extends ComponentConfig {
id = "linedit";
shouldSetTextContent(nextProps: object): boolean {
return true;
},
createInstance: newProps => {
}
createInstance(
newProps: object,
rootInstance: Set<NodeWidget>,
context: any,
workInProgress: Fiber
): NodeWidget {
const widget = new QLineEdit();
setProps(widget, newProps, {});
return widget;
},
finalizeInitialChildren: () => {
return false;
},
commitMount: (instance, newProps, internalInstanceHandle) => {
return;
},
prepareUpdate: (
instance,
oldProps,
newProps,
rootContainerInstance,
hostContext
) => {
return true;
},
commitUpdate: (instance, updatePayload, oldProps, newProps, finishedWork) => {
}
commitUpdate(
instance: NodeWidget,
updatePayload: any,
oldProps: object,
newProps: object,
finishedWork: Fiber
): void {
setProps(instance as QLineEdit, newProps, oldProps);
}
});
}

export const LineEdit = registerComponent<LineEditProps>(new LineEditConfig());
Loading

0 comments on commit 22b0f1f

Please sign in to comment.