Skip to content

Commit 9319f06

Browse files
committed
update Button-test.js
1 parent c117509 commit 9319f06

File tree

4 files changed

+60
-49
lines changed

4 files changed

+60
-49
lines changed

packages/app/src/Sidebar.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class Sidebar extends Component {
4141
<Text>Rio de Janeiro</Text>
4242
<Text>Kyoto</Text>
4343
<Text>Stockholm</Text>
44-
<Button onClick={()=>alert('hi')} title="PRESS ME" />
44+
<Button onClick={() => alert('hi')} title="PRESS ME" />
4545
</View>
4646
</View>
4747
);

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

Lines changed: 45 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,12 @@
77

88
import {ButtonDefaults} from '../constants';
99

10-
11-
1210
//TODO adjust Opacity when focus, Blur
1311
type PressEvent = {||};
1412
type ButtonProps = {|
1513
title: string,
1614
onPress: (event?: PressEvent) => mixed,
17-
onClick: (event?: SyntheticMouseEvent<T>) => mixed,
15+
onClick: (event?: SyntheticMouseEvent<HTMLButtonElement>) => mixed,
1816
touchSoundDisabled?: ?boolean,
1917
color?: ?string,
2018
/**
@@ -69,10 +67,10 @@ type ButtonProps = {|
6967

7068
function renderButton(props: ButtonProps, apeContext, parentLayout) {
7169
//
72-
console.log('[RENDER]')
70+
console.log('[RENDER]');
7371
const {spatialGeometry = {x: 0, y: 0}} = parentLayout;
7472
const {ctx} = apeContext;
75-
73+
7674
// If is relative and x and y haven't be processed, don't render
7775
if (!spatialGeometry) return null;
7876
// start drawing the canvas
@@ -85,28 +83,27 @@ function renderButton(props: ButtonProps, apeContext, parentLayout) {
8583
let height = ButtonDefaults.containerStyle.height;
8684
let globalStyle = {
8785
width: width,
88-
height:height,
86+
height: height,
8987
color: color,
90-
borderRadius:borderRadius,
88+
borderRadius: borderRadius,
9189
backgroundColor,
92-
lineWidth:0,
93-
borderColor: 'transparent'
94-
}
95-
const resetStyle = (newStyle)=>{
96-
globalStyle = {...globalStyle,newStyle}
97-
console.log('style)))))', globalStyle)
98-
}
99-
const redrawButton = (ctx)=>{
90+
lineWidth: 0,
91+
borderColor: 'transparent',
92+
};
93+
const resetStyle = newStyle => {
94+
globalStyle = {...globalStyle, newStyle};
95+
console.log('style)))))', globalStyle);
96+
};
97+
const redrawButton = ctx => {
10098
// TODO reset Style on focus
101-
let newStyle ={
102-
lineWidth:2,
103-
borderColor:'#ccc'
104-
}
99+
let newStyle = {
100+
lineWidth: 2,
101+
borderColor: '#ccc',
102+
};
105103
//let prevStyle = globalStyle
106-
resetStyle(newStyle)
104+
resetStyle(newStyle);
105+
};
107106

108-
}
109-
110107
ctx.beginPath();
111108
ctx.fillStyle = backgroundColor;
112109
ctx.moveTo(x, y);
@@ -137,8 +134,8 @@ const resetStyle = (newStyle)=>{
137134
ctx.quadraticCurveTo(x, y, x + borderRadius, y);
138135

139136
ctx.fill();
140-
ctx.lineWidth = globalStyle.lineWidth
141-
ctx.strokeStyle = globalStyle.borderColor
137+
ctx.lineWidth = globalStyle.lineWidth;
138+
ctx.strokeStyle = globalStyle.borderColor;
142139
ctx.stroke();
143140
ctx.fillStyle = color || ButtonDefaults.textStyle.color;
144141
ctx.font = `${ButtonDefaults.textStyle.fontSize} Helvetica`;
@@ -147,41 +144,41 @@ const resetStyle = (newStyle)=>{
147144
ctx.fillText(title, x + width / 2, y + height / 2);
148145
ctx.closePath();
149146

150-
const onClick = (event:SyntheticMouseEvent<HTMLButtonElement>)=>{
151-
const rect ={
147+
const onClick = (event: SyntheticMouseEvent<HTMLButtonElement>) => {
148+
const rect = {
152149
x,
153150
y,
154151
height,
155-
width
152+
width,
153+
};
154+
const mousePosition = trackMousePosition(ctx.canvas, event);
155+
if (isInside(mousePosition, rect)) {
156+
redrawButton(ctx);
157+
if (props.onClick && typeof props.onClick === 'function') {
158+
props.onClick(event);
159+
}
156160
}
157-
const mousePosition = trackMousePosition(ctx.canvas,event)
158-
if(isInside(mousePosition,rect)){
159-
redrawButton(ctx)
160-
if(props.onClick && typeof props.onClick === "function"){
161-
props.onClick(event)
162-
}
163-
164-
}
165-
166-
}
161+
};
167162
function trackMousePosition(canvas, event) {
168163
return {
169164
x: event.clientX - canvas.offsetLeft,
170-
y: event.clientY - canvas.offsetTop
165+
y: event.clientY - canvas.offsetTop,
171166
};
172167
}
173-
const isInside=(pos,rect)=>{
174-
return pos.x > rect.x && pos.x < rect.x+rect.width && pos.y < rect.y+rect.height && pos.y > rect.y
175-
}
176-
177-
ctx.canvas.addEventListener('click',onClick,false)
178-
ctx.canvas.addEventListener('focus',redrawButton)
179-
ctx.canvas.addEventListener('blur',redrawButton)
180-
181-
// events
168+
const isInside = (pos, rect) => {
169+
return (
170+
pos.x > rect.x &&
171+
pos.x < rect.x + rect.width &&
172+
pos.y < rect.y + rect.height &&
173+
pos.y > rect.y
174+
);
175+
};
182176

183-
177+
ctx.canvas.addEventListener('click', onClick, false);
178+
ctx.canvas.addEventListener('focus', redrawButton);
179+
ctx.canvas.addEventListener('blur', redrawButton);
184180

181+
// events
185182
}
186183

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

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ describe('Button', () => {
2525
lineTo: jest.fn(),
2626
quadraticCurveTo: jest.fn(),
2727
font: 'Helvetica',
28+
canvas:{
29+
addEventListener:jest.fn()
30+
}
31+
2832
},
2933
};
3034

@@ -54,6 +58,8 @@ describe('Button', () => {
5458
expect(font).toEqual(`${ButtonDefaults.textStyle.fontSize} Helvetica`);
5559
expect(quadraticCurveTo.mock.calls.length).toEqual(4);
5660
expect(fillStyle).toBe(color);
61+
expect(Button).toMatchSnapshot()
62+
5763
});
5864
});
5965
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Button Call the button with Props Should render properly 1`] = `
4+
Object {
5+
"render": [Function],
6+
"type": "Button",
7+
}
8+
`;

0 commit comments

Comments
 (0)