Skip to content

Commit e39c1fb

Browse files
Adding error handling and added truncate functionality
1 parent 5eadc7b commit e39c1fb

File tree

6 files changed

+157
-38
lines changed

6 files changed

+157
-38
lines changed

src/plugin/VInlineSelect.vue

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,22 @@ function toggleField() {
197197
198198
199199
// ------------------------------------------------ Check for errors //
200-
const internalErrorMessages = computed(() => {
200+
const internalErrors = ref();
201+
const internalErrorMessages = computed(() => internalErrors.value);
202+
203+
watch(() => showField.value, () => {
204+
if (showField.value) {
205+
checkInternalErrors();
206+
}
207+
});
208+
209+
watch(() => modelValue.value, () => {
210+
if (showField.value) {
211+
checkInternalErrors();
212+
}
213+
});
214+
215+
function checkInternalErrors() {
201216
const response = useCheckForErrors({
202217
required: settings.required,
203218
rules: settings.rules,
@@ -206,8 +221,9 @@ const internalErrorMessages = computed(() => {
206221
207222
error.value = response.errors;
208223
224+
internalErrors.value = response.results;
209225
return response.results;
210-
});
226+
}
211227
212228
213229
// ------------------------------------------------ Save the value / Emit update //
@@ -259,8 +275,7 @@ onUnmounted(() => {
259275
</script>
260276

261277
<style lang="scss" scoped>
262-
:deep(.v-input__append),
263-
:deep(.v-field__append-inner) {
278+
:deep(.v-input__append) {
264279
padding: 0 !important;
265280
}
266281

src/plugin/VInlineTextField.vue

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ import {
9292
useCheckForErrors,
9393
useSaveValue,
9494
useToggleField,
95+
useTruncateText,
9596
} from './composables/methods';
9697
import {
9798
useFieldContainerClass,
@@ -121,6 +122,15 @@ let originalValue = modelValue.value;
121122
const displayValue = computed(() => {
122123
if (modelValue.value) {
123124
empty.value = false;
125+
126+
if (settings.truncateLength) {
127+
return useTruncateText({
128+
length: settings.truncateLength,
129+
suffix: settings.truncateSuffix,
130+
text: modelValue.value,
131+
});
132+
}
133+
124134
return modelValue.value;
125135
}
126136
@@ -184,7 +194,22 @@ function toggleField() {
184194
185195
186196
// ------------------------------------------------ Check for errors //
187-
const internalErrorMessages = computed(() => {
197+
const internalErrors = ref();
198+
const internalErrorMessages = computed(() => internalErrors.value);
199+
200+
watch(() => showField.value, () => {
201+
if (showField.value) {
202+
checkInternalErrors();
203+
}
204+
});
205+
206+
watch(() => modelValue.value, () => {
207+
if (showField.value) {
208+
checkInternalErrors();
209+
}
210+
});
211+
212+
function checkInternalErrors() {
188213
const response = useCheckForErrors({
189214
required: settings.required,
190215
rules: settings.rules,
@@ -193,8 +218,9 @@ const internalErrorMessages = computed(() => {
193218
194219
error.value = response.errors;
195220
221+
internalErrors.value = response.results;
196222
return response.results;
197-
});
223+
}
198224
199225
200226
// ------------------------------------------------ Save the value / Emit update //
@@ -251,11 +277,17 @@ onUnmounted(() => {
251277
</script>
252278

253279
<style lang="scss" scoped>
254-
:deep(.v-input__append),
255-
:deep(.v-field__append-inner) {
280+
:deep(.v-input__append) {
256281
padding: 0 !important;
257282
}
258283
284+
.truncate {
285+
overflow: hidden;
286+
text-overflow: ellipsis;
287+
white-space: nowrap;
288+
width: 250px;
289+
}
290+
259291
.icons-container {
260292
height: 100%;
261293
}

src/plugin/VInlineTextarea.vue

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ import {
9191
useCheckForErrors,
9292
useSaveValue,
9393
useToggleField,
94+
useTruncateText,
9495
} from './composables/methods';
9596
import {
9697
useFieldContainerClass,
@@ -120,6 +121,15 @@ let originalValue = modelValue.value;
120121
const displayValue = computed(() => {
121122
if (modelValue.value) {
122123
empty.value = false;
124+
125+
if (settings.truncateLength) {
126+
return useTruncateText({
127+
length: settings.truncateLength,
128+
suffix: settings.truncateSuffix,
129+
text: modelValue.value,
130+
});
131+
}
132+
123133
return modelValue.value;
124134
}
125135
@@ -183,7 +193,22 @@ function toggleField() {
183193
184194
185195
// ------------------------------------------------ Check for errors //
186-
const internalErrorMessages = computed(() => {
196+
const internalErrors = ref();
197+
const internalErrorMessages = computed(() => internalErrors.value);
198+
199+
watch(() => showField.value, () => {
200+
if (showField.value) {
201+
checkInternalErrors();
202+
}
203+
});
204+
205+
watch(() => modelValue.value, () => {
206+
if (showField.value) {
207+
checkInternalErrors();
208+
}
209+
});
210+
211+
function checkInternalErrors() {
187212
const response = useCheckForErrors({
188213
required: settings.required,
189214
rules: settings.rules,
@@ -192,8 +217,9 @@ const internalErrorMessages = computed(() => {
192217
193218
error.value = response.errors;
194219
220+
internalErrors.value = response.results;
195221
return response.results;
196-
});
222+
}
197223
198224
199225
// ------------------------------------------------ Save the value / Emit update //
@@ -245,8 +271,7 @@ onUnmounted(() => {
245271
</script>
246272

247273
<style lang="scss" scoped>
248-
:deep(.v-input__append),
249-
:deep(.v-field__append-inner) {
274+
:deep(.v-input__append) {
250275
padding: 0 !important;
251276
}
252277

src/plugin/components/SaveFieldButtons.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
>
1616
<v-icon
1717
v-if="!loading && !settings.hideSaveIcon"
18-
:color="hasErrors ? 'danger' : settings.saveIconColor"
18+
:color="hasErrors ? 'error' : settings.saveIconColor"
1919
:icon="saveIcon"
2020
/>
2121
<v-icon

src/plugin/composables/methods.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import {
22
FieldValue,
3+
UseCheckForErrors,
34
UseSaveValue,
45
UseToggleField,
6+
UseTruncateText
57
} from '@/types';
68
import axios from 'axios';
79

@@ -16,7 +18,7 @@ function buildResponseItem(item: object, name: string, value: FieldValue) {
1618

1719

1820
// ------------------------------------------------ Composables //
19-
const useCheckForErrors = (options) => {
21+
const useCheckForErrors: UseCheckForErrors = (options) => {
2022
const { required, rules } = options;
2123
let { value } = options;
2224
value = unref(value);
@@ -39,7 +41,6 @@ const useCheckForErrors = (options) => {
3941
const result = handler(value);
4042

4143
if (result === true) continue;
42-
console.log(result);
4344
if (typeof result !== 'string') {
4445
console.warn(`${result} is not a valid value. Rule functions must return boolean true or a string.`);
4546

@@ -58,7 +59,6 @@ const useCheckForErrors = (options) => {
5859
};
5960
};
6061

61-
6262
const useSaveValue: UseSaveValue = async (options) => {
6363
const { settings, emit, name, value } = options;
6464
const submitData = buildResponseItem(settings.item as object, name, value);
@@ -118,9 +118,23 @@ const useToggleField: UseToggleField = (options) => {
118118
};
119119
};
120120

121+
const useTruncateText: UseTruncateText = (options) => {
122+
const { length = 0 } = options;
123+
let { suffix, text } = options;
124+
text = text.toString();
125+
suffix = suffix || '...';
126+
127+
if (text.length > length) {
128+
return `${text.substring(0, length)}${suffix}`;
129+
}
130+
131+
return text;
132+
};
133+
121134

122135
export {
123136
useCheckForErrors,
124137
useSaveValue,
125138
useToggleField,
139+
useTruncateText,
126140
};

0 commit comments

Comments
 (0)