This repository has been archived by the owner on Dec 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFrmrTextInput.coffee
173 lines (141 loc) · 4.97 KB
/
FrmrTextInput.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# Copyright (c) 2018 Natalie Marleny
# Casing - UI framework for Framer
# License: MIT
# URL: https://github.com/nataliemarleny/Casing
# Modified code, originally from: https://github.com/ajimix/Input-Framer
# Thank you ajimix for the amazing code - you rock!
# Extends the LayerStyle class which does the pixel ratio calculations in framer
_inputStyle =
Object.assign({}, Framer.LayerStyle,
calculatePixelRatio = (layer, value) ->
(value * layer.context.pixelMultiplier) + "px"
fontSize: (layer) ->
calculatePixelRatio(layer, layer._properties.fontSize)
lineHeight: (layer) ->
(layer._properties.lineHeight) + "em"
padding: (layer) ->
{ pixelMultiplier } = layer.context
padding = []
paddingValue = layer._properties.padding
# Check if we have a single number as integer
if Number.isInteger(paddingValue)
return calculatePixelRatio(layer, paddingValue)
# If we have multiple values they come as string (e.g. "1 2 3 4")
paddingValues = layer._properties.padding.split(" ")
switch paddingValues.length
when 4
padding.top = parseFloat(paddingValues[0])
padding.right = parseFloat(paddingValues[1])
padding.bottom = parseFloat(paddingValues[2])
padding.left = parseFloat(paddingValues[3])
when 3
padding.top = parseFloat(paddingValues[0])
padding.right = parseFloat(paddingValues[1])
padding.bottom = parseFloat(paddingValues[2])
padding.left = parseFloat(paddingValues[1])
when 2
padding.top = parseFloat(paddingValues[0])
padding.right = parseFloat(paddingValues[1])
padding.bottom = parseFloat(paddingValues[0])
padding.left = parseFloat(paddingValues[1])
else
padding.top = parseFloat(paddingValues[0])
padding.right = parseFloat(paddingValues[0])
padding.bottom = parseFloat(paddingValues[0])
padding.left = parseFloat(paddingValues[0])
# Return as 4-value string (e.g "1px 2px 3px 4px")
"#{padding.top * pixelMultiplier}px #{padding.right * pixelMultiplier}px #{padding.bottom * pixelMultiplier}px #{padding.left * pixelMultiplier}px"
)
class exports.FrmrTextInput extends Layer
constructor: (options = {}) ->
_.defaults options,
width: Screen.width / 2
height: 60
backgroundColor: "white"
fontSize: 30
lineHeight: 1
padding: 10
text: ""
placeholder: ""
type: "text"
autoCorrect: true
autoComplete: true
autoCapitalize: true
spellCheck: true
autofocus: false
textColor: "#000"
fontFamily: "-apple-system"
fontWeight: "500"
tabIndex: 0
textarea: false
enabled: true
super options
# Add additional properties
@_properties.fontSize = options.fontSize
@_properties.lineHeight = options.lineHeight
@_properties.padding = options.padding
@placeholderColor = options.placeholderColor if options.placeholderColor?
@input = document.createElement if options.textarea then 'textarea' else 'input'
@input.id = "input-#{_.now()}"
# Add styling to the input element
_.assign @input.style,
width: _inputStyle["width"](@)
height: _inputStyle["height"](@)
fontSize: _inputStyle["fontSize"](@)
lineHeight: _inputStyle["lineHeight"](@)
outline: "none"
border: "none"
backgroundColor: options.backgroundColor
padding: _inputStyle["padding"](@)
fontFamily: options.fontFamily
color: options.textColor
fontWeight: options.fontWeight
_.assign @input,
value: options.text
type: options.type
placeholder: options.placeholder
@input.setAttribute "tabindex", options.tabindex
@input.setAttribute "autocorrect", if options.autoCorrect then "on" else "off"
@input.setAttribute "autocomplete", if options.autoComplete then "on" else "off"
@input.setAttribute "autocapitalize", if options.autoCapitalize then "on" else "off"
@input.setAttribute "spellcheck", if options.spellCheck then "on" else "off"
if not options.enabled
@input.setAttribute "disabled", true
if options.autofocus
@input.setAttribute "autofocus", true
@form = document.createElement "form"
@form.appendChild @input
@_element.appendChild @form
@backgroundColor = "transparent"
@updatePlaceholderColor options.placeholderColor if @placeholderColor
@define "style",
get: -> @input.style
set: (value) ->
_.extend @input.style, value
@define "value",
get: -> @input.value
set: (value) ->
@input.value = value
updatePlaceholderColor: (color) ->
@placeholderColor = color
if @pageStyle?
document.head.removeChild @pageStyle
@pageStyle = document.createElement "style"
@pageStyle.type = "text/css"
css = "##{@input.id}::-webkit-input-placeholder { color: #{@placeholderColor}; }"
@pageStyle.appendChild(document.createTextNode css)
document.head.appendChild @pageStyle
focus: () ->
@input.focus()
unfocus: () ->
@input.blur()
onFocus: (cb) ->
@input.addEventListener "focus", ->
cb.apply(@)
onUnfocus: (cb) ->
@input.addEventListener "blur", ->
cb.apply(@)
disable: () ->
@input.setAttribute "disabled", true
enable: () =>
@input.removeAttribute "disabled", true