-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.html
More file actions
274 lines (218 loc) · 13 KB
/
index.html
File metadata and controls
274 lines (218 loc) · 13 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Alpine JS Number Plugin</title>
<link rel="apple-touch-icon" sizes="180x180" href="./icon/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="./icon/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="./icon/favicon-16x16.png">
<link rel="manifest" href="/icon/site.webmanifest">
<script src="https://cdn.tailwindcss.com"></script>
<script defer src="./dist/number.min.js"></script>
<script
defer
src="https://unpkg.com/[email protected]/dist/cdn.min.js"
></script>
</head>
<body class="p-4 md:p-12 flex flex-col items-center">
<div class="max-w-3xl">
<div class="flex justify-between">
<!-- Logo -->
<div class="h-12 w-12 bg-gray-100 rounded-lg flex justify-center items-end p-3 text-5xl font-black shadow-md mb-4 border border-gray-600">,.</div>
<!-- Github -->
<a href="https://github.com/boptom/alpinejs-number">
<svg aria-hidden="true" class="size-7" fill="currentColor" viewBox="0 0 24 24"><path clip-rule="evenodd" d="M12 2C6.477 2 2 6.484 2 12.017c0 4.425 2.865 8.18 6.839 9.504.5.092.682-.217.682-.483 0-.237-.008-.868-.013-1.703-2.782.605-3.369-1.343-3.369-1.343-.454-1.158-1.11-1.466-1.11-1.466-.908-.62.069-.608.069-.608 1.003.07 1.531 1.032 1.531 1.032.892 1.53 2.341 1.088 2.91.832.092-.647.35-1.088.636-1.338-2.22-.253-4.555-1.113-4.555-4.951 0-1.093.39-1.988 1.029-2.688-.103-.253-.446-1.272.098-2.65 0 0 .84-.27 2.75 1.026A9.564 9.564 0 0112 6.844c.85.004 1.705.115 2.504.337 1.909-1.296 2.747-1.027 2.747-1.027.546 1.379.202 2.398.1 2.651.64.7 1.028 1.595 1.028 2.688 0 3.848-2.339 4.695-4.566 4.943.359.309.678.92.678 1.855 0 1.338-.012 2.419-.012 2.747 0 .268.18.58.688.482A10.019 10.019 0 0022 12.017C22 6.484 17.522 2 12 2z" fill-rule="evenodd"></path></svg>
</a>
</div>
<h1 class="font-bold mb-2">Alpine JS Number Plugin</h1>
<p class="mb-10 border-l-4 pl-2">Enhanced Numeric Input for <a href="https://alpinejs.dev/" class="underline">Alpine.js</a>: Simplifying Number Formatting and Currency Handling.</p>
<div x-data class="flex flex-col space-y-16">
<!-- Installation -->
<div>
<h2 class="font-semibold mb-2">Installation</h2>
<p class="text-gray-600">With a CDN</p>
<div class="font-mono rounded-lg p-3 bg-gray-100 my-6 overflow-auto">
<pre class="max-w-12">
<script
defer
src="https://unpkg.com/alpinejs-number@latest/dist/number.min.js"
></script>
<script
defer
src="https://unpkg.com/[email protected]/dist/cdn.min.js">
</script></pre>
</div>
<p class="text-gray-600 mt-10">With a Package Manager</p>
<div class="font-mono rounded-lg p-3 bg-gray-100 my-6 overflow-auto">
<pre class="max-w-12">
yarn add -D alpinejs-number
npm install -D alpinejs-number</pre>
</div>
<div class="font-mono rounded-lg p-3 bg-gray-100 my-6 overflow-auto">
<pre class="max-w-12">
import Alpine from 'alpinejs'
import number from 'alpinejs-number'
Alpine.plugin(number)
Alpine.start()</pre>
</div>
</div>
<!-- Basic Usage -->
<div>
<h2 class="font-semibold mb-2">Basic Usage</h2>
<p class="text-gray-600">Use <span class="bg-gray-100">x-number</span> directive on an input to auto format the number.</p>
<div class="font-mono rounded-lg p-3 bg-gray-100 my-6">
<input <span class="font-bold text-red-500">x-number</span> <span class="hidden md:inline">class="border rounded px-2 py-1"</span> <span class="hidden sm:inline">value="1000.99"</span> />
</div>
<label for="basic" class="mr-2">Amount:</label>
<input x-number class="border rounded px-2 py-1 text-right w-32" value="1000.99" id="basic" />
</div>
<!-- Precision -->
<div>
<h2 class="font-semibold mb-2">Precision</h2>
<p class="text-gray-600">Precision can be set with a with a number modifier.</p>
<div class="font-mono rounded-lg p-3 bg-gray-100 my-6">
<input x-number<span class="font-bold text-red-500">.4</span> <span class="hidden md:inline">class="border rounded px-2 py-1"</span> <span class="hidden sm:inline">value="1000.9999"</span> />
</div>
<label for="prefix" class="mr-2">Amount:</label>
<input x-number.4 class="border rounded px-2 py-1 text-right w-32" value="1000.9999" id="prefix" />
</div>
<!-- Prefix -->
<div id="prefix">
<h2 class="font-semibold mb-2">Prefix</h2>
<p class="text-gray-600">The expression between quotes is the prefix.</p>
<p class="text-gray-600">Note: numbers, minus and decimal point cannot be used in the prefix.</p>
<div class="font-mono rounded-lg p-3 bg-gray-100 my-6">
<input x-number=<span class="font-bold text-red-500">"$"</span> <span class="hidden md:inline">class="border rounded px-2 py-1"</span> <span class="hidden sm:inline">value="1000.99"</span> />
</div>
<label for="prefix-input" class="mr-2">Amount:</label>
<input x-number="$" class="border rounded px-2 py-1 text-right w-32" value="1000.99" id="prefix-input" />
</div>
<!-- Separators -->
<div>
<h2 class="font-semibold mb-2">Override Thousands Separator and Decimal Point</h2>
<p class="text-gray-600">
The thousands separator and the decimal point can be specified in the string between quotes.
Simply separate the strings with a vertical bar.
The first string is the <a href="#prefix" class="underline">prefix</a>, the second is the thousands separator, and the third is the
decimal point.
</p>
<div class="font-mono rounded-lg p-3 bg-gray-100 my-6">
<input x-number="€<span class="font-bold text-red-500">|.|,</span>" <span class="hidden md:inline">class="border rounded px-2 py-1"</span> <span class="hidden sm:inline">value="1000,99"</span> />
</div>
<label for="separators" class="mr-2">Amount in Euros:</label>
<input x-number="€|.|," class="border rounded px-2 py-1 text-right w-32" value="1000,99" id="separators" />
</div>
<!-- Suffix -->
<div>
<h2 class="font-semibold mb-2">Suffix</h2>
<p class="text-gray-600">
A suffix can be specified within the quotes too. It is the fourth string in the list.
</p>
<div class="font-mono rounded-lg p-3 bg-gray-100 my-6">
<input x-number.0="|,|.|<span class="font-bold text-red-500">¢</span>" <span class="hidden md:inline">class="border rounded px-2 py-1"</span> <span class="hidden sm:inline">value="99"</span> />
</div>
<label for="separators" class="mr-2">Cents:</label>
<input x-number.0="|,|.|¢" class="border rounded px-2 py-1 text-right w-32" value="99" id="separators" />
</div>
<!-- Alternate Suffix -->
<div>
<p class="text-gray-600">A suffix can also be added with a string after a colon.</p>
<p class="text-gray-600">Note: special characters and uppercase characters cannot be used here.</p>
<div class="font-mono rounded-lg p-3 bg-gray-100 my-6">
<input x-number:<span class="font-bold text-red-500">x</span> <span class="hidden md:inline">class="border rounded px-2 py-1"</span> <span class="hidden sm:inline">value="1.5"</span> />
</div>
<label for="alternate-suffix" class="mr-2">Multiplier:</label>
<input x-number:x class="border rounded px-2 py-1 text-right w-32" value="1.5" id="alternate-suffix" />
</div>
<!-- Unsigned -->
<div>
<h2 class="font-semibold mb-2">Unsigned</h2>
<p class="text-gray-600">Use the unsigned modifier to disable negative numbers.</p>
<div class="font-mono rounded-lg p-3 bg-gray-100 my-6">
<input x-number<span class="font-bold text-red-500">.unsigned</span> <span class="hidden md:inline">class="border rounded px-2 py-1"</span> <span class="hidden sm:inline">value="1000.99"</span> />
</div>
<p class="text-gray-600 mb-2">Try typing a negative number into this box.</p>
<label for="unsigned" class="mr-2">Amount:</label>
<input x-number.unsigned class="border rounded px-2 py-1 text-right w-32" value="1000.9999" id="unsigned" />
</div>
<!-- toNumber -->
<div>
<h2 class="font-semibold mb-2">To Number Magic</h2>
<p class="text-gray-600"><span class="bg-gray-100">$toNumber</span> magic can be used to convert a number string to a number.</p>
<div class="font-mono rounded-lg p-3 bg-gray-100 my-6">
<div x-text="<span class="text-red-500">$toNumber('$1,234.99')</span>"></div>
</div>
<p class="text-gray-600 mb-2">Output:</p>
<div x-text="$toNumber('$1,234.99')" class="border rounded px-2 py-1 text-right w-32"></div>
<p class="text-gray-600 mt-10">A second argument can be used to pass the decimal character.</p>
<div class="font-mono rounded-lg p-3 bg-gray-100 my-6">
<div x-text="$toNumber('€1.234,99', <span class="text-red-500">','</span>)"></div>
</div>
<p class="text-gray-600 mb-2">Output:</p>
<div x-text="$toNumber('€1.234,99', ',')" class="border rounded px-2 py-1 text-right w-32"></div>
</div>
<!-- toFormatted -->
<div>
<h2 class="font-semibold mb-2">To Formatted Magic</h2>
<p class="text-gray-600"><span class="bg-gray-100">$toFormatted</span> magic can be used to convert a number to a formatted number string.</p>
<div class="font-mono rounded-lg p-3 bg-gray-100 my-6">
<div x-text="<span class="text-red-500">$toFormatted('1234000.99')</span>"></div>
</div>
<p class="text-gray-600 mb-2">Output:</p>
<div x-text="$toFormatted('$1234000.99')" class="border rounded px-2 py-1 text-right w-32"></div>
<p class="text-gray-600 mt-10">
The prefix, thousands separator, decimal character, precision and suffix
can be passed as arguments to the function.
</p>
<div class="font-mono rounded-lg p-3 bg-gray-100 my-6">
<div x-text="$toFormatted('1234.9999', <span class="text-red-500">'$', ',', '.', ' USD', 2</span>)"></div>
</div>
<p class="text-gray-600 mb-2">Output:</p>
<div x-text="$toFormatted('1234.999', '$', ',', '.', ' USD', 2)" class="border rounded px-2 py-1 text-right w-32"></div>
</div>
<div>
<h2 class="font-semibold mb-2">Form Example</h2>
<p class="text-gray-600">Alpine JS along with the magics can be utilized to transform inputs to numbers before submitting a form.</p>
<div class="font-mono rounded-lg p-3 bg-gray-100 my-6 overflow-auto">
<pre class="max-w-12">
<div x-data="{ amount: 10000, formData: {} }" class="space-y-4">
<form <span class="text-red-500">@submit.prevent="formData = { amount: $toNumber(amount) }"</span>>
<label for="form-example" class="mr-2">Amount:</label>
<input
x-number="$"
id="form-example"
class="border rounded px-2 py-1 text-right w-32"
x-model="amount"
required
/>
<button type="submit" class="border rounded px-2 py-1">Submit</button>
</form>
<pre
x-text="JSON.stringify(formData)"
class="bg-gray-100 p-4 rounded-lg"
></pre>
</div></pre>
</div>
<div x-data="{ amount: 10000, formData: {} }" class="space-y-4">
<form @submit.prevent="formData = { amount: $toNumber(amount) }">
<label for="form-example" class="mr-2">Amount:</label>
<input
x-number="$"
id="form-example"
class="border rounded px-2 py-1 text-right w-32"
x-model="amount"
required
/>
<button type="submit" class="border rounded px-2 py-1">Submit</button>
</form>
<pre
x-text="JSON.stringify(formData)"
class="bg-gray-100 p-4 rounded-lg"
></pre>
</div>
</div>
</div>
</body>
</html>