-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathQuestionShort.vue
More file actions
289 lines (259 loc) Β· 7.22 KB
/
Copy pathQuestionShort.vue
File metadata and controls
289 lines (259 loc) Β· 7.22 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
<!--
- SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
- SPDX-License-Identifier: AGPL-3.0-or-later
-->
<template>
<Question
v-bind="questionProps"
:titlePlaceholder="answerType.titlePlaceholder"
:warningInvalid="answerType.warningInvalid"
:errorMessage="errorMessage"
v-on="commonListeners">
<div class="question__content">
<input
ref="input"
:aria-labelledby="titleId"
:aria-describedby="description ? descriptionId : undefined"
:aria-errormessage="hasError ? errorId : undefined"
:aria-invalid="hasError ? 'true' : undefined"
:placeholder="submissionInputPlaceholder"
:disabled="!readOnly"
:name="name || undefined"
:required="isRequired"
:value="values[0]"
class="question__input"
dir="auto"
:maxlength="maxStringLengths.answerText"
minlength="1"
:type="validationObject.inputType"
:step="validationObject.inputType === 'number' ? 'any' : undefined"
@invalid.prevent="validate"
@input="onInput"
@keydown.enter.exact.prevent="onKeydownEnter" />
<NcActions
v-if="!readOnly"
:id="validationTypeMenuId"
v-model:open="isValidationTypeMenuOpen"
:aria-label="
t('forms', 'Input types (currently: {type})', {
type: validationObject.label,
})
"
:container="`#${validationTypeMenuId}`"
class="validation-type-menu__toggle"
variant="tertiary-no-background">
<template #icon>
<NcIconSvgWrapper :svg="validationObject.icon" />
</template>
<NcActionRadio
v-for="(
validationTypeObject, validationTypeName
) in validationTypes"
:key="validationTypeName"
:modelValue="validationType"
:name="`${id}_validationMenu`"
:value="validationTypeName"
@update:modelValue="onChangeValidationType(validationTypeName)">
{{ validationTypeObject.label }}
</NcActionRadio>
<NcActionInput
v-if="validationType === 'regex'"
ref="regexInput"
:label="t('forms', 'Regular expression for input validation')"
:modelValue="validationRegex"
@input="onInputRegex"
@submit="onSubmitRegex">
<template #icon>
<NcIconSvgWrapper :svg="IconRegex" />
</template>
/^[a-z]{3}$/i
<!-- ^ Some example RegExp for the placeholder text -->
</NcActionInput>
</NcActions>
</div>
<template #insert>
<slot name="insert" />
</template>
</Question>
</template>
<script>
import IconRegex from '@material-symbols/svg-400/outlined/regular_expression.svg?raw'
import debounce from 'debounce'
import NcActionInput from '@nextcloud/vue/components/NcActionInput'
import NcActionRadio from '@nextcloud/vue/components/NcActionRadio'
import NcActions from '@nextcloud/vue/components/NcActions'
import NcIconSvgWrapper from '@nextcloud/vue/components/NcIconSvgWrapper'
import Question from './Question.vue'
import QuestionMixin from '../../mixins/QuestionMixin.js'
import { INPUT_DEBOUNCE_MS } from '../../models/Constants.ts'
import validationTypes from '../../models/ValidationTypes.js'
import { splitRegex, validateExpression } from '../../utils/RegularExpression.js'
export default {
name: 'QuestionShort',
components: {
NcIconSvgWrapper,
NcActions,
NcActionInput,
NcActionRadio,
Question,
},
mixins: [QuestionMixin],
emits: ['update:values'],
setup() {
return {
IconRegex,
}
},
data() {
return {
validationTypes,
isValidationTypeMenuOpen: false,
}
},
computed: {
submissionInputPlaceholder() {
if (!this.readOnly) {
return (
this.validationObject.createPlaceholder
|| this.answerType.createPlaceholder
)
}
return (
this.validationObject.submitPlaceholder
|| this.answerType.submitPlaceholder
)
},
/**
* Current user input validation type
*/
validationObject() {
return validationTypes[this.validationType]
},
/**
* Name of the current validation type, fallsback to 'text'
*/
validationType() {
return this.extraSettings?.validationType || 'text'
},
/**
* Id of the validation type menu
*/
validationTypeMenuId() {
return 'q' + this.index + '__validation_menu'
},
/**
* The regular expression
*/
validationRegex() {
return this.extraSettings?.validationRegex || ''
},
},
methods: {
async validate() {
/** @type {HTMLInputElement} */
const input = this.$refs.input
const value = input.value
// Clear the previous custom error before checking native validity.
input.setCustomValidity('')
if (this.isRequired && input.validity.valueMissing) {
this.errorMessage = t('forms', 'You must answer this question')
return false
}
const isCustomValid =
!value
|| this.validationObject.validate(
value,
splitRegex(this.validationRegex),
)
if (!input.validity.valid || !isCustomValid) {
input.setCustomValidity(this.validationObject.errorMessage)
this.errorMessage = this.validationObject.errorMessage
return false
}
this.errorMessage = null
return true
},
onInput: debounce(async function () {
/** @type {HTMLInputElement} */
const input = this.$refs.input
const value = input.value
this.$emit('update:values', [value])
this.validate()
}, INPUT_DEBOUNCE_MS),
/**
* Change input type
*
* @param {string} validationType new input type
*/
onChangeValidationType(validationType) {
if (validationType === 'regex') {
// Make sure to also submit a regex (even if empty)
this.onExtraSettingsChange({
validationType,
validationRegex: this.validationRegex,
})
} else {
// For all other types except regex we close the menu (for regex we keep it open to allow entering a regex)
this.isValidationTypeMenuOpen = false
this.onExtraSettingsChange({
validationType:
validationType === 'text' ? undefined : validationType,
})
}
},
/**
* Validate and save regex if valid
*
* Ensures the regex is enclosed with delimters, as required for PCRE,
* and regex is only using modifiers supported by JS *and* PHP
*
* @param {InputEvent|SubmitEvent} event input event
* @return {boolean} true if the regex is valid
*/
onInputRegex(event) {
if (event?.isComposing) {
return false
}
const input = this.$refs.regexInput.$el.querySelector('input')
const validationRegex = input.value
// remove potential previous validity
input.setCustomValidity('')
if (!validateExpression(validationRegex)) {
input.setCustomValidity(t('forms', 'Invalid regular expression'))
return false
}
this.onExtraSettingsChange({ validationRegex })
return true
},
/**
* Same as `onInputRegex` but for convinience also closes the menu
*
* @param {SubmitEvent} event regex submit event
*/
onSubmitRegex(event) {
if (this.onInputRegex(event)) {
this.isValidationTypeMenuOpen = false
}
},
},
}
</script>
<style lang="scss" scoped>
.question__input {
width: 100%;
min-height: var(--default-clickable-area);
&:disabled {
width: calc(100% - var(--default-clickable-area)) !important;
margin-inline-start: -12px;
}
}
.validation-type-menu__toggle {
position: relative;
inset-inline-end: calc(4px + var(--default-clickable-area));
inset-block-start: 4px;
}
:deep(input:invalid) {
// nextcloud/server#36548
border-color: var(--color-error) !important;
}
</style>