Skip to content

Commit 6aeaeb6

Browse files
committed
adding onClick event to Button
1 parent 5ed0465 commit 6aeaeb6

File tree

2 files changed

+69
-23
lines changed

2 files changed

+69
-23
lines changed

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

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type PressEvent = {||};
1212
type ButtonProps = {|
1313
title: string,
1414
onPress: (event?: PressEvent) => mixed,
15+
onClick: (event?: PressEvent) => mixed,
1516
touchSoundDisabled?: ?boolean,
1617
color?: ?string,
1718
/**
@@ -65,7 +66,7 @@ type ButtonProps = {|
6566
|};
6667

6768
function renderButton(props: ButtonProps, apeContext, parentLayout) {
68-
const {spatialGeometry = {x: 0, y: 0}, relativeIndex} = parentLayout;
69+
const {spatialGeometry = {x: 0, y: 0}} = parentLayout;
6970
const {ctx} = apeContext;
7071
// If is relative and x and y haven't be processed, don't render
7172
if (!spatialGeometry) return null;
@@ -77,10 +78,11 @@ function renderButton(props: ButtonProps, apeContext, parentLayout) {
7778
let y = spatialGeometry.y || 0;
7879
let width = x + y;
7980
let height = ButtonDefaults.containerStyle.height;
80-
// if(parentLayout && parentLayout.style){
81-
// width = parentLayout.style.width
82-
// height = parentLayout.style.height
83-
// }
81+
82+
// ctx.addEventListener('click',()=>{
83+
// alert('hi')
84+
// })
85+
8486
ctx.beginPath();
8587
ctx.fillStyle = backgroundColor;
8688
ctx.moveTo(x, y);
@@ -119,6 +121,50 @@ function renderButton(props: ButtonProps, apeContext, parentLayout) {
119121
ctx.fillText(title, x + width / 2, y + height / 2);
120122
//ctx.fillText('Start', x + height /2, y + height /2 );
121123
ctx.closePath();
124+
125+
//const canvas = document.getElementById('root')
126+
//const context = canvas.getContext('2d')
127+
const onClick = (event: SyntheticEvent<HTMLButtonElement>) => {
128+
const rect = {
129+
x,
130+
y,
131+
height,
132+
width,
133+
};
134+
// const clientX = event.clientX - ctx.canvas.offsetLeft
135+
// const clientY = event.clientY - ctx.canvas.offsetTop
136+
const mousePosition = getMousePos(ctx.canvas, event);
137+
138+
// console.log('mouse',x,y)
139+
console.log('rect', rect);
140+
console.log('mouse', mousePosition);
141+
if (isInside(mousePosition, rect)) {
142+
alert('hi');
143+
} else {
144+
//alert('not inside')
145+
}
146+
};
147+
function getMousePos(canvas, event) {
148+
var rect = canvas.getBoundingClientRect();
149+
return {
150+
x: event.clientX - rect.left,
151+
y: event.clientY - rect.top,
152+
};
153+
}
154+
const isInside = (pos, rect) => {
155+
return (
156+
pos.x > rect.x &&
157+
pos.x < rect.x + rect.width &&
158+
pos.y < rect.y + rect.heigth &&
159+
pos.y > rect.y
160+
);
161+
};
162+
const redrawButton = () => {};
163+
ctx.canvas.addEventListener('click', onClick, false);
164+
ctx.canvas.addEventListener('focus', redrawButton);
165+
ctx.canvas.addEventListener('blur', redrawButton);
166+
167+
// events
122168
}
123169

124170
export default function createButtonInstance(props: ButtonProps): mixed {

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

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import React from 'react';
22
import renderer from 'react-test-renderer';
3-
import {ButtonDefaults} from '../../constants'
3+
import {ButtonDefaults} from '../../constants';
44
import CreateButtonInstance from '../Button';
55

66
describe('Button', () => {
77
describe('Call the button with Props', () => {
88
it('Should render properly', () => {
9-
const title = 'Press Me'
10-
const color = '#f8a978'
11-
const x = 40
12-
const y = 20
13-
const width = x + y
14-
const height = ButtonDefaults.containerStyle.height
9+
const title = 'Press Me';
10+
const color = '#f8a978';
11+
const x = 40;
12+
const y = 20;
13+
const width = x + y;
14+
const height = ButtonDefaults.containerStyle.height;
1515
const props = {title, color};
1616
const apeContext = {
1717
ctx: {
@@ -43,17 +43,17 @@ describe('Button', () => {
4343
font,
4444
} = apeContext.ctx;
4545
expect(beginPath.mock.calls.length).toBe(1);
46-
expect(beginPath).toBeCalledWith()
47-
expect(closePath).toBeCalledWith()
48-
expect(stroke).toBeCalledWith()
49-
expect(moveTo).toBeCalledWith(x,y)
50-
expect(lineTo.mock.calls.length).toEqual(4)
51-
expect(fill.mock.calls.length).toBe(1)
52-
expect(fillText.mock.calls.length).toBe(1)
53-
expect(fillText).toBeCalledWith(title,x + width/2,y+height/2)
54-
expect(font).toEqual(`${ButtonDefaults.textStyle.fontSize} Helvetica`)
55-
expect(quadraticCurveTo.mock.calls.length).toEqual(4)
56-
expect(fillStyle).toBe(color)
46+
expect(beginPath).toBeCalledWith();
47+
expect(closePath).toBeCalledWith();
48+
expect(stroke).toBeCalledWith();
49+
expect(moveTo).toBeCalledWith(x, y);
50+
expect(lineTo.mock.calls.length).toEqual(4);
51+
expect(fill.mock.calls.length).toBe(1);
52+
expect(fillText.mock.calls.length).toBe(1);
53+
expect(fillText).toBeCalledWith(title, x + width / 2, y + height / 2);
54+
expect(font).toEqual(`${ButtonDefaults.textStyle.fontSize} Helvetica`);
55+
expect(quadraticCurveTo.mock.calls.length).toEqual(4);
56+
expect(fillStyle).toBe(color);
5757
});
5858
});
5959
});

0 commit comments

Comments
 (0)