6
6
*/
7
7
8
8
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' ;
11
11
12
12
//TODO adjust Opacity when focus, Blur
13
13
type PressEvent = { || } ;
14
14
type ButtonProps = { |
15
15
title : string ,
16
16
onPress : ( event ? : PressEvent ) => mixed ,
17
17
onClick : ( event ?: SyntheticMouseEvent < HTMLButtonElement > ) => mixed ,
18
+ //handleOnClick:(event?:SyntheticMouseEvent<HTMLButtonElement>)=>mixed,
18
19
touchSoundDisabled ?: ?boolean ,
19
20
color ?: ?string ,
20
21
/**
@@ -60,54 +61,58 @@ type ButtonProps = {|
60
61
* If true, disable all interactions for this component.
61
62
*/
62
63
disabled ?: ?boolean ,
63
-
64
+ getDimension ?: ( ) => mixed ,
64
65
/**
65
66
* Used to locate this view in end-to-end tests.
66
67
*/
67
68
testID ?: ?string ,
68
69
| } ;
69
70
70
- function renderButton ( props : ButtonProps , apeContext :CanvasComponentContext , parentLayout ) {
71
+ function renderButton (
72
+ props : ButtonProps ,
73
+ apeContext : CanvasComponentContext ,
74
+ parentLayout
75
+ ) {
71
76
const { ctx} = apeContext ;
72
77
73
78
// If is relative and x and y haven't be processed, don't render
74
79
// start drawing the canvas
75
- console . log ( '[PROPS]' , props )
80
+ console . log ( '[PROPS]' , props ) ;
76
81
const { title, color} = props ;
77
- if ( ! title ) {
78
- throw Error ( " Title required!" )
82
+ if ( ! title ) {
83
+ throw Error ( ' Title required!' ) ;
79
84
}
80
85
const borderRadius = ButtonDefaults . containerStyle . borderRadius ;
81
86
const backgroundColor = ButtonDefaults . containerStyle . backgroundColor ;
82
87
let x = 40 ;
83
88
let y = 300 ;
89
+
84
90
const textWidth = ctx . measureText ( title ) . width ;
85
- let width = textWidth * 1.5 ;
91
+ let width = textWidth * 1.5 ;
86
92
let height = ButtonDefaults . containerStyle . height ;
87
93
let globalStyle = {
88
94
width : width ,
89
95
height : height ,
90
96
color : color ,
91
97
borderRadius : borderRadius ,
92
- backgroundColor :color ,
98
+ backgroundColor : color ,
93
99
lineWidth : 0 ,
94
100
borderColor : 'transparent' ,
95
101
} ;
96
102
const resetStyle = newStyle => {
97
103
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 ) ;
107
104
} ;
105
+ // const redrawButton = ctx => {
106
+ // // TODO reset Style on focus
107
+ // let newStyle = {
108
+ // lineWidth: 2,
109
+ // borderColor: '#ccc',
110
+ // };
111
+ // resetStyle(newStyle);
112
+ // };
108
113
109
114
ctx . beginPath ( ) ;
110
- ctx . fillStyle = color || ButtonDefaults . containerStyle . backgroundColor
115
+ ctx . fillStyle = color || ButtonDefaults . containerStyle . backgroundColor ;
111
116
ctx . moveTo ( x , y ) ;
112
117
/**
113
118
* Top Right Radius
@@ -139,40 +144,40 @@ function renderButton(props: ButtonProps, apeContext:CanvasComponentContext, par
139
144
ctx . lineWidth = globalStyle . lineWidth ;
140
145
ctx . strokeStyle = globalStyle . borderColor ;
141
146
ctx . stroke ( ) ;
142
- ctx . fillStyle = ButtonDefaults . textStyle . color ;
147
+ ctx . fillStyle = ButtonDefaults . textStyle . color ;
143
148
144
149
//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` ;
147
152
ctx . font = newSize + ' ' + fontArgs [ fontArgs . length - 1 ] ;
148
153
149
- // ctx.textAlign = 'center';
154
+ // ctx.textAlign = 'center';
150
155
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 ) ;
153
158
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
+ /**
176
181
* We need to remove addEventListeners from the renderButton
177
182
* function because this function runs for each state/prop update.
178
183
* It will keep creating/refreshing listeners for every render.
@@ -181,15 +186,11 @@ We can keep this way, if we run this addEventListener
181
186
once by checking if the listener already exist.
182
187
Note onClick will need to share scope with this function to work properly.
183
188
*/
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);
189
192
}
190
193
191
-
192
-
193
194
export default function createButtonInstance ( props : ButtonProps ) : mixed {
194
195
return {
195
196
type : 'Button' ,
0 commit comments