-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathEditInputField.vue
216 lines (194 loc) · 4.87 KB
/
EditInputField.vue
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
<template>
<fieldset>
<label :id="inputLabelId">
{{ fieldLabel }}
</label>
<textarea
v-if="
props.fieldType === 'textarea' || props.fieldType === 'markdown'
"
ref="inputField"
v-model="content"
:placeholder="placeholder"
:aria-labelledby="inputLabelId"
:aria-placeholder="placeholder"
@input="handleInput"
@keydown="keyDown"
@keyup="handleSuggestionsPopupKeyUp"
@focus="handleSuggestionsPopupFocus"
@blur="handleSuggestionsPopupBlur"
@mouseup="handleSuggestionsPopupMouseUp"
/>
<div v-else>
<slot />
<input
v-if="!hide"
ref="inputField"
v-model="content"
:type="props.fieldType"
:placeholder="placeholder"
:aria-labelledby="inputLabelId"
:aria-placeholder="placeholder"
@input="handleInput"
@keydown="keyDown"
@keyup="handleSuggestionsPopupKeyUp"
@focus="handleSuggestionsPopupFocus"
@blur="handleSuggestionsPopupBlur"
@mouseup="handleSuggestionsPopupMouseUp"
/>
</div>
<SuggestionsPopup
v-if="suggestionsPopupVisible"
ref="suggestionsPopupElement"
v-bind="suggestionsData"
:options="filteredSuggestionOptions"
@suggestions-selected="handleSuggestionsPopupSelectedEvent"
/>
</fieldset>
</template>
<script setup>
import { getCurrentInstance, ref, watch } from 'vue';
import SuggestionsPopup from '../Modals/SuggestionsPopup.vue';
import useSuggestionPopup from '../../composables/useSuggestionsPopup';
const log = getCurrentInstance().proxy.$log;
const emit = defineEmits(['input']);
const props = defineProps({
fieldLabel: {
type: String,
default: '',
},
fieldType: {
type: String,
default: '',
},
hide: {
type: Boolean,
default: false,
required: false,
},
placeholder: {
type: String,
default: '',
},
suggestionOptions: {
type: Array,
default: () => [],
},
// Value (passed in v-model)
// eslint-disable-next-line vue/require-prop-types
value: {
type: String,
default: '',
required: true,
},
});
// Template refs
/**
* @type {import('vue').Ref<HTMLElement | null>}
*/
const inputField = ref(null);
const suggestionsData = ref(null);
/**
* @type {import('vue').Ref<string>}
*/
const content = ref(props.value);
/**
* Unique identifier used for identifying the input label for accessibility reasons.
* @type {import('vue').Ref<string>}
*/
const inputLabelId = ref(`input-label-${Math.floor(Math.random() * 10000000)}`);
// deconstruct composable
const {
suggestionsPopupVisible,
filteredSuggestionOptions,
suggestionsPopupElement,
handleSuggestionsPopupKeyUp,
handleSuggestionsPopupKeyDown,
handleSuggestionsPopupFocus,
handleSuggestionsPopupBlur,
handleSuggestionsPopupMouseUp,
handleSuggestionsPopupSelectedEvent,
} = useSuggestionPopup(suggestionsData, null, emit, log, props);
watch(
() => props.value,
(newValue) => {
content.value = newValue;
},
);
const handleInput = () => {
emit('input', content.value);
};
const keyDown = (e) => {
// Redirect to suggestions handler if in suggestion mode
if (suggestionsPopupVisible) {
handleSuggestionsPopupKeyDown(e);
}
};
</script>
<script>
export default {
name: 'EditInputField',
};
</script>
<style scoped>
fieldset {
display: flex;
flex-direction: column;
margin-bottom: 1em;
}
@media (min-width: 1200px) {
fieldset {
flex-direction: row;
}
}
fieldset > * {
margin: 0;
}
fieldset > label {
display: inline-block;
width: 10em;
height: 34px;
font-weight: bold;
line-height: 17px;
vertical-align: top;
}
fieldset > div,
fieldset > textarea {
width: revert;
flex: 1;
}
fieldset > div > input {
width: 100%;
}
fieldset input[type='number'] {
width: 5em;
flex-grow: 0;
}
fieldset > textarea {
min-height: 10em;
resize: vertical;
}
fieldset > .editor {
min-height: 10em;
flex: 1;
border-radius: 2px;
resize: vertical;
}
/*
Hack to overwrite the heavy-handed global unscoped styles of Nextcloud core
that cause our markdown editor CodeMirror to behave strangely on mobile
See: https://github.com/nextcloud/cookbook/issues/908
*/
.editor:deep(div[contenteditable='true']) {
width: revert;
min-height: revert;
padding: revert;
border: revert;
border-radius: revert;
margin: revert;
background-color: revert;
color: revert;
font-size: revert;
outline: revert;
}
</style>