bootstrap-vue-next
Version:
BootstrapVueNext is an early and lovely component library for Vue 3 & Nuxt 3 based on Bootstrap 5 and Typescript.
125 lines (124 loc) • 4.08 kB
JavaScript
import { ref, inject, computed, onMounted, onActivated, nextTick } from "vue";
import { u as useAriaInvalid } from "./useAriaInvalid-BTUbGj3Y.mjs";
import { u as useId } from "./useId-BmZnPXsS.mjs";
import { u as useFocus } from "./index-CMqRvrZx.mjs";
import { n as formGroupPluginKey } from "./keys-DUCx0SNv.mjs";
import { u as useStateClass } from "./useStateClass-BGbSLWFN.mjs";
import { u as useToNumber, r as useDebounceFn } from "./index-CaguhSuF.mjs";
const normalizeInput = (v, modelModifiers) => {
if (v === null) return;
let update = v;
if (modelModifiers.number && typeof update === "string" && update !== "") {
const parsed = Number.parseFloat(update);
update = Number.isNaN(parsed) ? update : parsed;
}
return update;
};
const useFormInput = (props, input, modelValue, modelModifiers) => {
var _a;
const forceUpdateKey = ref(0);
const computedId = useId(() => props.id, "input");
const debounceNumber = useToNumber(() => props.debounce ?? 0);
const debounceMaxWaitNumber = useToNumber(() => props.debounceMaxWait ?? NaN);
const formGroupData = (_a = inject(formGroupPluginKey, null)) == null ? void 0 : _a(computedId);
const computedState = computed(
() => props.state !== void 0 ? props.state : (formGroupData == null ? void 0 : formGroupData.state.value) ?? null
);
const computedAriaInvalid = useAriaInvalid(() => props.ariaInvalid, computedState);
const stateClass = useStateClass(computedState);
const internalUpdateModelValue = useDebounceFn(
(value) => {
modelValue.value = value;
},
() => modelModifiers.lazy === true ? 0 : debounceNumber.value,
{ maxWait: () => modelModifiers.lazy === true ? NaN : debounceMaxWaitNumber.value }
);
const updateModelValue = (value, force = false) => {
if (modelModifiers.lazy === true && force === false) return;
internalUpdateModelValue(value);
};
const { focused } = useFocus(input, {
initialValue: props.autofocus
});
const _formatValue = (value, evt, force = false) => {
if (props.formatter !== void 0 && (!props.lazyFormatter || force)) {
return props.formatter(value, evt);
}
return value;
};
onMounted(() => {
var _a2;
if (input.value) {
input.value.value = ((_a2 = modelValue.value) == null ? void 0 : _a2.toString()) ?? "";
}
});
onActivated(() => {
nextTick(() => {
if (props.autofocus) {
focused.value = true;
}
});
});
const onInput = (evt) => {
const { value } = evt.target;
const formattedValue = _formatValue(value, evt);
if (evt.defaultPrevented) {
evt.preventDefault();
return;
}
const nextModel = formattedValue;
updateModelValue(nextModel);
};
const onChange = (evt) => {
const { value } = evt.target;
const formattedValue = _formatValue(value, evt);
if (evt.defaultPrevented) {
evt.preventDefault();
return;
}
const nextModel = formattedValue;
if (modelValue.value !== nextModel) {
updateModelValue(formattedValue, true);
}
};
const onBlur = (evt) => {
if (!modelModifiers.lazy && !props.lazyFormatter && !modelModifiers.trim) return;
const { value } = evt.target;
const formattedValue = _formatValue(value, evt, true);
const nextModel = modelModifiers.trim ? formattedValue.trim() : formattedValue;
const needsForceUpdate = nextModel.length !== formattedValue.length;
if (modelValue.value !== nextModel) {
updateModelValue(formattedValue, true);
}
if (modelModifiers.trim && needsForceUpdate) {
forceUpdateKey.value = forceUpdateKey.value + 1;
}
};
const focus = () => {
if (!props.disabled) {
focused.value = true;
}
};
const blur = () => {
if (!props.disabled) {
focused.value = false;
}
};
return {
input,
computedId,
computedAriaInvalid,
onInput,
onChange,
onBlur,
focus,
blur,
forceUpdateKey,
stateClass
};
};
export {
normalizeInput as n,
useFormInput as u
};
//# sourceMappingURL=useFormInput-D-gxhL95.mjs.map