Skip to content

Commit 407fa0c

Browse files
authored
Creates abstract button for code sharing (#77)
1 parent 76e0426 commit 407fa0c

File tree

7 files changed

+101
-43
lines changed

7 files changed

+101
-43
lines changed

package-lock.json

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"react": "^16.9.0"
3232
},
3333
"devDependencies": {
34-
"@nodegui/nodegui": "^0.6.7",
34+
"@nodegui/nodegui": "^0.6.8",
3535
"@types/node": "^12.0.10",
3636
"prettier": "^1.18.2",
3737
"react": "^16.9.0",
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { QIcon, QSize } from "@nodegui/nodegui";
2+
import { ViewProps, setViewProps } from "../View/RNView";
3+
import { QAbstractButton } from "@nodegui/nodegui";
4+
5+
/**
6+
* The Button component provides ability to add and manipulate native button widgets. It is based on
7+
* [NodeGui's QPushButton](https://docs.nodegui.org/docs/api/QPushButton).
8+
* ## Example
9+
* ```javascript
10+
* import React from "react";
11+
* import { Renderer, Button, Window } from "@nodegui/react-nodegui";
12+
* const App = () => {
13+
* return (
14+
* <Window>
15+
* <Button style={buttonStyle} text={"Hello World"} />
16+
* </Window>
17+
* );
18+
* };
19+
* const buttonStyle = `
20+
* color: white;
21+
* `;
22+
* Renderer.render(<App />);
23+
*
24+
* ```
25+
*/
26+
export interface AbstractButtonProps extends ViewProps {
27+
/**
28+
* Sets the given text to the button. [QPushButton: setText](https://docs.nodegui.org/docs/api/QPushButton#buttonsettexttext)
29+
*/
30+
text?: string;
31+
/**
32+
* Sets an icon in the button. [QPushButton: setIcon](https://docs.nodegui.org/docs/api/QPushButton#buttonseticonicon)
33+
*/
34+
icon?: QIcon;
35+
/**
36+
* Sets an icon size in the button. [QPushButton: setIconSize](https://docs.nodegui.org/docs/api/QPushButton#buttonseticonsize)
37+
*/
38+
iconSize?: QSize;
39+
}
40+
41+
export const setAbstractButtonProps = (
42+
widget: QAbstractButton,
43+
newProps: AbstractButtonProps,
44+
oldProps: AbstractButtonProps
45+
) => {
46+
const setter: AbstractButtonProps = {
47+
set text(buttonText: string) {
48+
widget.setText(buttonText);
49+
},
50+
set icon(icon: QIcon) {
51+
widget.setIcon(icon);
52+
},
53+
set iconSize(iconSize: QSize) {
54+
widget.setIconSize(iconSize);
55+
}
56+
};
57+
Object.assign(setter, newProps);
58+
setViewProps(widget, newProps, oldProps);
59+
};

src/components/Button/RNButton.ts

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
import { QIcon, QPushButton, NodeWidget } from "@nodegui/nodegui";
2-
import { ViewProps, setViewProps } from "../View/RNView";
1+
import { QPushButton, NodeWidget } from "@nodegui/nodegui";
2+
import {
3+
AbstractButtonProps,
4+
setAbstractButtonProps
5+
} from "../AbstractComponents/RNAbstractButton";
36
import { RNWidget } from "../config";
47
import { throwUnsupported } from "../../utils/helpers";
58

@@ -24,19 +27,11 @@ import { throwUnsupported } from "../../utils/helpers";
2427
*
2528
* ```
2629
*/
27-
export interface ButtonProps extends ViewProps {
28-
/**
29-
* Sets the given text to the button. [QPushButton: setText](https://docs.nodegui.org/docs/api/QPushButton#buttonsettexttext)
30-
*/
31-
text?: string;
30+
export interface ButtonProps extends AbstractButtonProps {
3231
/**
3332
* Sets whether the button border is raised. [QPushButton: setFlat](https://docs.nodegui.org/docs/api/QPushButton#buttonsetflatisflat)
3433
*/
3534
flat?: boolean;
36-
/**
37-
* Sets an icon in the button. [QPushButton: setIcon](https://docs.nodegui.org/docs/api/QPushButton#buttonseticonicon)
38-
*/
39-
icon?: QIcon;
4035
}
4136

4237
const setButtonProps = (
@@ -45,18 +40,12 @@ const setButtonProps = (
4540
oldProps: ButtonProps
4641
) => {
4742
const setter: ButtonProps = {
48-
set text(buttonText: string) {
49-
widget.setText(buttonText);
50-
},
5143
set flat(isFlat: boolean) {
5244
widget.setFlat(isFlat);
53-
},
54-
set icon(icon: QIcon) {
55-
widget.setIcon(icon);
5645
}
5746
};
5847
Object.assign(setter, newProps);
59-
setViewProps(widget, newProps, oldProps);
48+
setAbstractButtonProps(widget, newProps, oldProps);
6049
};
6150

6251
/**

src/components/CheckBox/RNCheckBox.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
import { ViewProps, setViewProps } from "../View/RNView";
21
import { QCheckBox, NodeWidget } from "@nodegui/nodegui";
32
import { RNWidget } from "../config";
43
import { throwUnsupported } from "../../utils/helpers";
4+
import {
5+
AbstractButtonProps,
6+
setAbstractButtonProps
7+
} from "../AbstractComponents/RNAbstractButton";
58

69
/**
710
* The CheckBox component provides ability to add and manipulate native button widgets. It is based on
@@ -24,11 +27,7 @@ import { throwUnsupported } from "../../utils/helpers";
2427
*
2528
* ```
2629
*/
27-
export interface CheckBoxProps extends ViewProps {
28-
/**
29-
* Sets the given text to the checkbox.. [QCheckBox: setText](https://docs.nodegui.org/docs/api/QCheckBox/#checkboxsettexttext)
30-
*/
31-
text?: string;
30+
export interface CheckBoxProps extends AbstractButtonProps {
3231
/**
3332
* This property holds whether the button is checked. [QCheckBox: setChecked](https://docs.nodegui.org/docs/api/QCheckBox/#checkboxsetcheckedcheck)
3433
*/
@@ -41,15 +40,12 @@ const setCheckBoxProps = (
4140
oldProps: CheckBoxProps
4241
) => {
4342
const setter: CheckBoxProps = {
44-
set text(checkboxText: string) {
45-
widget.setText(checkboxText);
46-
},
4743
set checked(isChecked: boolean) {
4844
widget.setChecked(isChecked);
4945
}
5046
};
5147
Object.assign(setter, newProps);
52-
setViewProps(widget, newProps, oldProps);
48+
setAbstractButtonProps(widget, newProps, oldProps);
5349
};
5450

5551
/**

src/components/RadioButton/RNRadioButton.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { QRadioButton, NodeWidget } from "@nodegui/nodegui";
2-
import { ViewProps, setViewProps } from "../View/RNView";
32
import { RNWidget } from "../config";
43
import { throwUnsupported } from "../../utils/helpers";
4+
import {
5+
setAbstractButtonProps,
6+
AbstractButtonProps
7+
} from "../AbstractComponents/RNAbstractButton";
58

6-
export interface RadioButtonProps extends ViewProps {
7-
text?: string;
9+
export interface RadioButtonProps extends AbstractButtonProps {
10+
// More to be added
811
}
912

1013
const setRadioButtonProps = (
@@ -13,12 +16,10 @@ const setRadioButtonProps = (
1316
oldProps: RadioButtonProps
1417
) => {
1518
const setter: RadioButtonProps = {
16-
set text(checkboxText: string) {
17-
widget.setText(checkboxText);
18-
}
19+
// more setters to be added
1920
};
2021
Object.assign(setter, newProps);
21-
setViewProps(widget, newProps, oldProps);
22+
setAbstractButtonProps(widget, newProps, oldProps);
2223
};
2324

2425
/**

src/components/View/RNView.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import {
55
CursorShape,
66
NodeWidget,
77
QIcon,
8-
FlexLayout,
9-
WidgetAttribute
8+
FlexLayout
109
} from "@nodegui/nodegui";
1110
import { RNWidget, RNProps } from "../config";
1211

@@ -112,13 +111,19 @@ export interface ViewProps extends RNProps {
112111
* <View attributes={{Qt::WA_Disabled: true}} />
113112
*/
114113
attributes?: WidgetAttributesMap;
114+
115+
/**
116+
* Prop to set the Widget flags. example:
117+
* <View windowFlags={{Qt::SplashScreen: true}} />
118+
*/
119+
windowFlags?: WindowFlagsMap;
115120
}
116121

117122
/**
118123
* @ignore
119124
*/
120125
export const setViewProps = (
121-
widget: RNView,
126+
widget: QWidget,
122127
newProps: ViewProps,
123128
oldProps: ViewProps
124129
) => {
@@ -134,7 +139,6 @@ export const setViewProps = (
134139
console.warn("Both styleSheet and inlineStyle can't be used together");
135140
}
136141
widget.setInlineStyle(inlineStyle);
137-
console.log(inlineStyle);
138142
},
139143
set geometry(geometry: Geometry) {
140144
widget.setGeometry(
@@ -214,6 +218,11 @@ export const setViewProps = (
214218
Object.entries(attributesMap).forEach(([attribute, value]) => {
215219
widget.setAttribute(Number(attribute), value);
216220
});
221+
},
222+
set windowFlags(windowFlagsMap: WindowFlagsMap) {
223+
Object.entries(windowFlagsMap).forEach(([flag, value]) => {
224+
widget.setWindowFlag(Number(flag), value);
225+
});
217226
}
218227
};
219228
Object.assign(setter, newProps);
@@ -285,3 +294,7 @@ interface ListenerMap {
285294
type WidgetAttributesMap = {
286295
[key: number]: boolean;
287296
};
297+
298+
type WindowFlagsMap = {
299+
[key: number]: boolean;
300+
};

0 commit comments

Comments
 (0)