-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiInput.svelte
More file actions
271 lines (250 loc) · 7.97 KB
/
Copy pathMultiInput.svelte
File metadata and controls
271 lines (250 loc) · 7.97 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
<script lang="ts">
import { _ } from 'svelte-i18n'
import {
getCountryCallingCode,
parsePhoneNumberFromString,
type CountryCode,
} from 'libphonenumber-js/mobile'
import closeIcon from '$lib/assets/images/google-icons/close.svg'
interface props {
translation_key: string
value?: string
deleteAction?: () => void
isConfirming?: boolean
}
let {
translation_key,
value = $bindable(''),
deleteAction,
isConfirming = false,
}: props = $props()
// So we have a unique id for the label-input pair so we can handle multiple inputs correctly in a list even with multiple recipients
const randomId = Math.random().toString(36).substring(2, 15)
let showingValue = $state('')
let selectedCountry: CountryCode = $state('NL')
let phoneInputEl: HTMLInputElement | null = $state(null)
let phoneTouched = $state(false)
// Parse via libphonenumber so local-format inputs (e.g. "0612345678") are
// normalised to E.164 ("+31612345678"). Yivi stores mobile numbers in
// E.164, so anything we send that isn't canonical fails the identity
// match at decrypt time (tracked in cryptify#39).
let parsedPhone = $derived(
showingValue.length === 0
? null
: parsePhoneNumberFromString(showingValue, selectedCountry)
)
let phoneValid = $derived(
showingValue.length === 0 || (parsedPhone?.isValid() ?? false)
)
$effect(() => {
if (phoneInputEl) {
phoneInputEl.setCustomValidity(
phoneValid ? '' : 'Invalid phone number'
)
}
})
// Yivi discloses dates as DD-MM-YYYY, but <input type="date"> uses YYYY-MM-DD.
// These two helpers keep the stored `value` in Yivi's format so the IBE
// identity derived during decryption matches the one used during encryption.
function dateToHtml(ddmmyyyy: string): string {
if (!ddmmyyyy) return ''
const p = ddmmyyyy.split('-')
return p.length === 3 ? `${p[2]}-${p[1]}-${p[0]}` : ddmmyyyy
}
function dateFromHtml(yyyymmdd: string): string {
if (!yyyymmdd) return ''
const p = yyyymmdd.split('-')
return p.length === 3 ? `${p[2]}-${p[1]}-${p[0]}` : yyyymmdd
}
const allowedCountries = [
'at',
'be',
'bg',
'cy',
'dk',
'de',
'ee',
'fi',
'fr',
'gr',
'hu',
'ie',
'is',
'it',
'hr',
'lv',
'lt',
'li',
'lu',
'mt',
'mc',
'nl',
'no',
'pl',
'pt',
'ro',
'si',
'sk',
'es',
'cz',
'gb',
'se',
'ch',
]
function getCountryPrefix(countryCode: string): string {
const country = countryCode.toUpperCase() as CountryCode
return '+' + getCountryCallingCode(country)
}
$effect(() => {
if (
translation_key ===
'filesharing.attributes.pbdf.sidn-pbdf.mobilenumber.mobilenumber'
) {
// Always emit canonical E.164 so the encrypted policy matches what
// Yivi discloses at decrypt time. If the input is not yet parseable
// (partial typing, invalid number) emit an empty string so the
// downstream form stays in the "no recipient attribute" state
// rather than committing a malformed value.
value = parsedPhone?.isValid() ? parsedPhone.number : ''
}
})
</script>
<div class="input-wrapper">
<label for={randomId}>
{$_(translation_key)}
<span class="optional-text"
>({$_('filesharing.attributes.optional')})</span
>
</label>
<div class="optional-value" class:removed-del-border={isConfirming}>
{#if translation_key === 'filesharing.attributes.pbdf.sidn-pbdf.mobilenumber.mobilenumber'}
<select
bind:value={selectedCountry}
class="pg-input phone-select"
class:is-confirming-bg={isConfirming}
disabled={isConfirming}
>
{#each allowedCountries as country (country)}
<option value={country.toUpperCase() as CountryCode}>
{country.toUpperCase()}
{getCountryPrefix(country)}
</option>
{/each}
</select>
<input
bind:this={phoneInputEl}
id={randomId}
class="pg-input"
class:is-confirming-bg={isConfirming}
class:phone-invalid={!phoneValid && phoneTouched}
disabled={isConfirming}
type="tel"
placeholder={$_(translation_key + '.placeholder')}
bind:value={showingValue}
onblur={() => {
phoneTouched = true
}}
/>
{:else if translation_key === 'filesharing.attributes.pbdf.gemeente.personalData.dateofbirth'}
<input
id={randomId}
class="pg-input"
class:is-confirming-bg={isConfirming}
disabled={isConfirming}
type="date"
value={dateToHtml(value)}
oninput={(e) => {
value = dateFromHtml((e.target as HTMLInputElement).value)
}}
/>
{:else}
<input
id={randomId}
class="pg-input"
class:is-confirming-bg={isConfirming}
disabled={isConfirming}
type="text"
placeholder={$_(translation_key + '.placeholder')}
bind:value
/>
{/if}
{#if deleteAction}
<button
class:hidden={isConfirming}
class="btn-delete invert"
onclick={deleteAction}
>
<img
style="width: 14px; height: 14px;"
src={closeIcon}
alt="remove optional attribute"
/>
</button>
{/if}
</div>
{#if !phoneValid && phoneTouched && translation_key === 'filesharing.attributes.pbdf.sidn-pbdf.mobilenumber.mobilenumber'}
<p class="phone-error">{$_('filesharing.attributes.phoneInvalid')}</p>
{/if}
</div>
<style>
.input-wrapper:last-child {
margin-bottom: 0;
}
select {
font-family: var(--pg-font-family);
}
.phone-select {
width: auto;
min-width: 6.5rem;
}
label {
font-family: var(--pg-font-family);
font-size: var(--pg-font-size-xs);
font-weight: var(--pg-font-weight-extrabold);
color: var(--pg-text);
display: block;
}
.optional-text {
font-weight: var(--pg-font-weight-regular);
color: var(--pg-text-secondary);
}
.btn-delete {
all: unset;
aspect-ratio: 1 / 1;
border-radius: var(--pg-border-radius-md);
margin: 4px;
margin-left: 0px;
display: flex;
align-items: center;
justify-content: center;
transition: all 0.2s ease;
background-color: transparent;
height: 80%;
cursor: pointer;
}
.btn-delete:hover {
background-color: var(--pg-soft-background);
}
.btn-delete:focus-visible {
outline: 2px solid var(--pg-primary);
outline-offset: 2px;
}
.removed-del-border {
border-radius: var(--pg-border-radius-md);
}
.optional-value {
display: flex;
align-items: center;
gap: 0.5rem;
height: 40px;
}
.phone-invalid {
border-color: var(--pg-error, #e53e3e) !important;
}
.phone-error {
font-size: var(--pg-font-size-xs);
color: var(--pg-error, #e53e3e);
margin: 0.25rem 0 0 0;
font-family: var(--pg-font-family);
}
</style>