Skip to content

Commit f027f59

Browse files
committedJul 10, 2023
Update checkbox and switch to use truthy composable
1 parent d29de32 commit f027f59

File tree

4 files changed

+57
-11
lines changed

4 files changed

+57
-11
lines changed
 

‎src/plugin/VInlineCheckbox.vue

+9-5
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,18 @@
55
>
66
<div
77
v-if="!showField && !settings.fieldOnly"
8-
class="v-inline-fields--container-display-container"
98
:class="displayContainerClass"
109
>
1110
<div :class="displaySelectionControlClasses">
1211
<div class="v-selection-control__wrapper">
1312
<div
1413
v-if="icons"
15-
class="v-inline-fields--container-display-container-value-icons"
1614
:class="displayValueClass"
1715
:style="displayValueStyle"
1816
@click="toggleField"
1917
>
2018
<BooleanIcons
21-
v-model="displayValue"
19+
v-model="truthyModelValue"
2220
:icon-false="settings.iconFalse"
2321
:icon-false-color="settings.iconFalseColor"
2422
:icon-false-title="settings.iconFalseTitle"
@@ -47,7 +45,6 @@
4745
>
4846
<v-checkbox
4947
v-bind="bindingSettings"
50-
v-model="modelValue"
5148
:color="settings.color"
5249
:density="settings.density"
5350
:disabled="loadingProp"
@@ -56,8 +53,9 @@
5653
:false-value="settings.falseValue"
5754
:hide-details="settings.hideDetails"
5855
:label="settings.label"
56+
:model-value="truthyModelValue"
5957
:true-icon="theTrueIcon"
60-
:value="settings.trueValue"
58+
:true-value="settings.trueValue"
6159
@update:model-value="saveValue"
6260
>
6361
<!-- Pass on all scoped slots -->
@@ -116,6 +114,7 @@ import {
116114
BooleanIcons,
117115
SaveFieldButtons,
118116
} from './components/index';
117+
import { useTruthyModelValue } from './composables/helpers';
119118
import { useToggleField } from './composables/methods';
120119
import { useGetIcon } from './composables/icons';
121120
import {
@@ -185,6 +184,11 @@ const displayValue = computed(() => {
185184
return modelValue.value == settings.trueValue;
186185
});
187186
187+
const truthyModelValue = computed(() => useTruthyModelValue({
188+
modelValue,
189+
trueValue: settings.trueValue,
190+
}));
191+
188192
189193
// ------------------------------------------------ Class & Styles //
190194
const inlineFieldsContainerClass = computed(() => useInlineFieldsContainerClass({

‎src/plugin/VInlineCustomField.vue

+2-2
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ const bindingDisplay = computed(() => {
152152
displayPrependInnerIconColor: props.displayPrependInnerIconColor,
153153
displayPrependInnerIconSize: props.displayPrependInnerIconSize,
154154
displayValue: displayValue.value,
155-
empty,
156-
error,
155+
empty: empty.value,
156+
error: error.value,
157157
field: 'v-text-field',
158158
underlineColor: settings.underlineColor,
159159
underlineStyle: settings.underlineStyle,

‎src/plugin/VInlineSwitch.vue

+9-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
@click="toggleField"
1717
>
1818
<BooleanIcons
19-
v-model="displayValue"
19+
v-model="truthyModelValue"
2020
:icon-false="settings.iconFalse"
2121
:icon-false-color="settings.iconFalseColor"
2222
:icon-false-title="settings.iconFalseTitle"
@@ -45,7 +45,6 @@
4545
>
4646
<v-switch
4747
v-bind="bindingSettings"
48-
v-model="modelValue"
4948
:color="settings.color"
5049
:density="settings.density"
5150
:disabled="loadingProp"
@@ -55,7 +54,8 @@
5554
:hide-details="settings.hideDetails"
5655
:label="settings.label"
5756
:loading="loadingProp"
58-
:value="settings.trueValue"
57+
:model-value="truthyModelValue"
58+
:true-value="settings.trueValue"
5959
@update:model-value="saveValue"
6060
>
6161
<!-- Pass on all scoped slots -->
@@ -104,6 +104,7 @@ import {
104104
import { IconOptions, useTheme } from 'vuetify';
105105
import { switchProps } from './utils/props';
106106
import { BooleanIcons } from './components/index';
107+
import { useTruthyModelValue } from './composables/helpers';
107108
import { useToggleField } from './composables/methods';
108109
import { useGetIcon } from './composables/icons';
109110
import {
@@ -166,6 +167,11 @@ const displayValue = computed(() => {
166167
return modelValue.value == settings.trueValue;
167168
});
168169
170+
const truthyModelValue = computed(() => useTruthyModelValue({
171+
modelValue,
172+
trueValue: settings.trueValue,
173+
}));
174+
169175
170176
// ------------------------------------------------ Class & Styles //
171177
const inlineFieldsContainerClass = computed(() => useInlineFieldsContainerClass({

‎src/plugin/composables/helpers.ts

+37-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { UseConvertToUnit } from '@/types';
1+
import {
2+
UseConvertToUnit,
3+
UseTruthyModelValue,
4+
} from '@/types';
25

36

47
/**
@@ -17,3 +20,36 @@ export const useConvertToUnit: UseConvertToUnit = (options) => {
1720

1821
return `${Number(str)}${unit}`;
1922
};
23+
24+
25+
export const useTruthyModelValue: UseTruthyModelValue = (options) => {
26+
const { modelValue, trueValue } = options;
27+
28+
const value = unref(modelValue);
29+
30+
if (value?.toLowerCase?.() === 'true') {
31+
return true;
32+
}
33+
34+
if (value === '1') {
35+
return true;
36+
}
37+
38+
if (value == '1') {
39+
return true;
40+
}
41+
42+
if (value === true) {
43+
return true;
44+
}
45+
46+
if (value == trueValue) {
47+
return true;
48+
}
49+
50+
if (value === trueValue) {
51+
return true;
52+
}
53+
54+
return false;
55+
};

0 commit comments

Comments
 (0)
Please sign in to comment.