Skip to content

Commit 0875e2b

Browse files
committed
remove onClick from the Button render
1 parent 0faeb16 commit 0875e2b

File tree

6 files changed

+103
-69
lines changed

6 files changed

+103
-69
lines changed

packages/app/src/Clock.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class Clock extends React.Component {
5151
<Text style={styles.time}>
5252
{this.state.time}
5353
</Text>
54-
54+
5555
{/*<Text style={styles.time}>
5656
{this.state.time}
5757
</Text>

packages/app/src/Sidebar.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ const styles = StyleSheet.create({
2828
lineHeight: 40,
2929
backgroundColor: 'orange',
3030
},
31-
3231
});
3332

3433
class Item extends React.Component {
@@ -61,19 +60,21 @@ class Sidebar extends Component {
6160
<View style={styles.sidebar}>
6261
{/*<View style={styles.container}>*/}
6362
<Button
64-
color={"#000"}
65-
title="Click Me 🚀"
66-
onClick = {()=>{alert('hi')}}
63+
color={'#000'}
64+
title="Click Me 🚀"
65+
onClick={() => {
66+
alert('hi');
67+
}}
6768
/>
69+
6870
<FocusableItem
6971
focusKey="sidebar-item-1"
7072
text="Rio de Janeiro"
7173
idx={120}
7274
/>
7375
<FocusableItem focusKey="sidebar-item-2" text="Kyoto" idx={160} />
7476
<FocusableItem focusKey="sidebar-item-3" text="Stockholm" idx={200} />
75-
76-
77+
7778
{/*</View>*/}
7879
</View>
7980
);

packages/react-ape/renderer/elements/Button.js

Lines changed: 55 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@
66
*/
77

88
import {ButtonDefaults} from '../constants';
9-
import {trackMousePosition,isMouseInside} from '../utils'
10-
import type {CanvasComponentContext} from '../types'
9+
import {trackMousePosition, isMouseInside} from '../utils';
10+
import type {CanvasComponentContext} from '../types';
1111

1212
//TODO adjust Opacity when focus, Blur
1313
type PressEvent = {||};
1414
type ButtonProps = {|
1515
title: string,
1616
onPress: (event?: PressEvent) => mixed,
1717
onClick: (event?: SyntheticMouseEvent<HTMLButtonElement>) => mixed,
18+
//handleOnClick:(event?:SyntheticMouseEvent<HTMLButtonElement>)=>mixed,
1819
touchSoundDisabled?: ?boolean,
1920
color?: ?string,
2021
/**
@@ -60,54 +61,58 @@ type ButtonProps = {|
6061
* If true, disable all interactions for this component.
6162
*/
6263
disabled?: ?boolean,
63-
64+
getDimension?: () => mixed,
6465
/**
6566
* Used to locate this view in end-to-end tests.
6667
*/
6768
testID?: ?string,
6869
|};
6970

70-
function renderButton(props: ButtonProps, apeContext:CanvasComponentContext, parentLayout) {
71+
function renderButton(
72+
props: ButtonProps,
73+
apeContext: CanvasComponentContext,
74+
parentLayout
75+
) {
7176
const {ctx} = apeContext;
7277

7378
// If is relative and x and y haven't be processed, don't render
7479
// start drawing the canvas
75-
console.log('[PROPS]',props)
80+
console.log('[PROPS]', props);
7681
const {title, color} = props;
77-
if(!title){
78-
throw Error("Title required!")
82+
if (!title) {
83+
throw Error('Title required!');
7984
}
8085
const borderRadius = ButtonDefaults.containerStyle.borderRadius;
8186
const backgroundColor = ButtonDefaults.containerStyle.backgroundColor;
8287
let x = 40;
8388
let y = 300;
89+
8490
const textWidth = ctx.measureText(title).width;
85-
let width = textWidth * 1.5;
91+
let width = textWidth * 1.5;
8692
let height = ButtonDefaults.containerStyle.height;
8793
let globalStyle = {
8894
width: width,
8995
height: height,
9096
color: color,
9197
borderRadius: borderRadius,
92-
backgroundColor:color,
98+
backgroundColor: color,
9399
lineWidth: 0,
94100
borderColor: 'transparent',
95101
};
96102
const resetStyle = newStyle => {
97103
globalStyle = {...globalStyle, newStyle};
98-
99-
};
100-
const redrawButton = ctx => {
101-
// TODO reset Style on focus
102-
let newStyle = {
103-
lineWidth: 2,
104-
borderColor: '#ccc',
105-
};
106-
resetStyle(newStyle);
107104
};
105+
// const redrawButton = ctx => {
106+
// // TODO reset Style on focus
107+
// let newStyle = {
108+
// lineWidth: 2,
109+
// borderColor: '#ccc',
110+
// };
111+
// resetStyle(newStyle);
112+
// };
108113

109114
ctx.beginPath();
110-
ctx.fillStyle = color || ButtonDefaults.containerStyle.backgroundColor
115+
ctx.fillStyle = color || ButtonDefaults.containerStyle.backgroundColor;
111116
ctx.moveTo(x, y);
112117
/**
113118
* Top Right Radius
@@ -139,40 +144,40 @@ function renderButton(props: ButtonProps, apeContext:CanvasComponentContext, par
139144
ctx.lineWidth = globalStyle.lineWidth;
140145
ctx.strokeStyle = globalStyle.borderColor;
141146
ctx.stroke();
142-
ctx.fillStyle = ButtonDefaults.textStyle.color;
147+
ctx.fillStyle = ButtonDefaults.textStyle.color;
143148

144149
//set the fontSize
145-
const fontArgs = ctx.font.split(' ');
146-
const newSize =` ${ButtonDefaults.textStyle.fontSize}px`;
150+
const fontArgs = ctx.font.split(' ');
151+
const newSize = ` ${ButtonDefaults.textStyle.fontSize}px`;
147152
ctx.font = newSize + ' ' + fontArgs[fontArgs.length - 1];
148153

149-
// ctx.textAlign = 'center';
154+
// ctx.textAlign = 'center';
150155

151-
// ctx.fillText(title, 400 / 2, y + height / 2,textWidth);
152-
ctx.fillText(title , (x + textWidth /2.5), y + height /2);
156+
// ctx.fillText(title, 400 / 2, y + height / 2,textWidth);
157+
ctx.fillText(title, x + textWidth / 2.5, y + height / 2);
153158
ctx.closePath();
154-
155-
const onClick = (event: SyntheticMouseEvent<HTMLButtonElement>) => {
156-
const rect = {
157-
x,
158-
y,
159-
height,
160-
width,
161-
};
162-
const mousePosition = trackMousePosition(ctx.canvas, event);
163-
if (isMouseInside(mousePosition, rect)) {
164-
redrawButton(ctx);
165-
if (props.onClick && typeof props.onClick === 'function') {
166-
props.onClick(event);
167-
}
168-
}
169-
};
170-
171-
172-
173-
174-
// TODO:
175-
/**
159+
// if(props.handleOnClick){
160+
// onClick()
161+
// }
162+
163+
// const onClick = (event: SyntheticMouseEvent<HTMLButtonElement>) => {
164+
// const rect = {
165+
// x,
166+
// y,
167+
// height,
168+
// width,
169+
// };
170+
// const mousePosition = trackMousePosition(ctx.canvas, event);
171+
// if (isMouseInside(mousePosition, rect)) {
172+
// redrawButton(ctx);
173+
// if (props.onClick && typeof props.onClick === 'function') {
174+
// props.onClick(event);
175+
// }
176+
// }
177+
// };
178+
179+
// TODO:
180+
/**
176181
* We need to remove addEventListeners from the renderButton
177182
* function because this function runs for each state/prop update.
178183
* It will keep creating/refreshing listeners for every render.
@@ -181,15 +186,11 @@ We can keep this way, if we run this addEventListener
181186
once by checking if the listener already exist.
182187
Note onClick will need to share scope with this function to work properly.
183188
*/
184-
ctx.canvas.addEventListener('click', onClick, false);
185-
ctx.canvas.addEventListener('focus', redrawButton);
186-
ctx.canvas.addEventListener('blur', redrawButton);
187-
188-
189+
// ctx.canvas.addEventListener('click', onClick, false);
190+
// ctx.canvas.addEventListener('focus', redrawButton);
191+
// ctx.canvas.addEventListener('blur', redrawButton);
189192
}
190193

191-
192-
193194
export default function createButtonInstance(props: ButtonProps): mixed {
194195
return {
195196
type: 'Button',

packages/react-ape/renderer/reactApeComponent.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import Text from './elements/Text';
1111
import View from './elements/View';
1212
import Button from './elements/Button';
1313
import {CustomComponents} from '../modules/Register';
14+
import {trackMousePosition, isMouseInside} from './utils';
1415

1516
const CHILDREN = 'children';
1617
const STYLE = 'style';
@@ -32,6 +33,37 @@ const ReactApeComponent = {
3233
);
3334
});
3435

36+
const tag = '[CREATE_ELEMENT]';
37+
38+
if (type === 'Button') {
39+
const {ctx} = apeContextGlobal;
40+
ctx.canvas.addEventListener(
41+
'click',
42+
event => {
43+
props.onClick(event);
44+
},
45+
false
46+
);
47+
}
48+
/**
49+
* Handling click button event
50+
* @param {*} event
51+
*/
52+
const onClick = (event: SyntheticMouseEvent<HTMLButtonElement>) => {
53+
const rect = {
54+
x,
55+
y,
56+
height,
57+
width,
58+
};
59+
const mousePosition = trackMousePosition(ctx.canvas, event);
60+
if (isMouseInside(mousePosition, rect)) {
61+
//redrawButton(ctx);
62+
if (props.onClick && typeof props.onClick === 'function') {
63+
props.onClick(event);
64+
}
65+
}
66+
};
3567
const COMPONENTS = {
3668
...customDict,
3769
Image: Image(props),

packages/react-ape/renderer/reactApeRenderer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ const ReactApeFiber = reconciler({
116116

117117
if (diff) {
118118
const parentLayout = element.parentLayout || element.getParentLayout();
119-
if (type === 'Text' ) {
119+
if (type === 'Text') {
120120
parentLayout.resetLayout(); // View needs to be reset on text updates
121121
}
122122

packages/react-ape/renderer/utils.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,21 @@
66
export function unsafeCreateUniqueId(): string {
77
return (Math.random() * 10e18 + Date.now()).toString(36);
88
}
9-
type MouseEventType={|
10-
x:number,
11-
y:number
12-
|}
13-
export function trackMousePosition(canvas, event):MouseEventType {
9+
type MouseEventType = {|
10+
x: number,
11+
y: number,
12+
|};
13+
export function trackMousePosition(canvas, event): MouseEventType {
1414
return {
1515
x: event.clientX - canvas.offsetLeft,
1616
y: event.clientY - canvas.offsetTop,
1717
};
1818
}
19-
export const isMouseInside = (pos, rect):boolean => {
19+
export const isMouseInside = (pos, rect): boolean => {
2020
return (
2121
pos.x > rect.x &&
2222
pos.x < rect.x + rect.width &&
2323
pos.y < rect.y + rect.height &&
2424
pos.y > rect.y
2525
);
26-
};
26+
};

0 commit comments

Comments
 (0)