mirror of
https://github.com/CorentinTh/it-tools.git
synced 2025-04-27 01:56:15 -04:00
parent
e876d03608
commit
374caa176e
2 changed files with 32 additions and 2 deletions
|
@ -1,7 +1,8 @@
|
||||||
import { useRouteQuery } from '@vueuse/router';
|
import { useRouteQuery } from '@vueuse/router';
|
||||||
import { computed } from 'vue';
|
import { computed } from 'vue';
|
||||||
|
import { useStorage } from '@vueuse/core';
|
||||||
|
|
||||||
export { useQueryParam };
|
export { useQueryParam, useQueryParamOrStorage };
|
||||||
|
|
||||||
const transformers = {
|
const transformers = {
|
||||||
number: {
|
number: {
|
||||||
|
@ -33,3 +34,31 @@ function useQueryParam<T>({ name, defaultValue }: { name: string; defaultValue:
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function useQueryParamOrStorage<T>({ name, storageName, defaultValue }: { name: string; storageName: string; defaultValue?: T }) {
|
||||||
|
const type = typeof defaultValue;
|
||||||
|
const transformer = transformers[type as keyof typeof transformers] ?? transformers.string;
|
||||||
|
|
||||||
|
const storageRef = useStorage(storageName, defaultValue);
|
||||||
|
const storageDefaultValue = storageRef.value ?? defaultValue;
|
||||||
|
|
||||||
|
const proxy = useRouteQuery(name, transformer.toQuery(storageDefaultValue as never));
|
||||||
|
|
||||||
|
const ref = computed<T>({
|
||||||
|
get() {
|
||||||
|
return transformer.fromQuery(proxy.value) as unknown as T;
|
||||||
|
},
|
||||||
|
set(value) {
|
||||||
|
proxy.value = transformer.toQuery(value as never);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
watch(
|
||||||
|
ref,
|
||||||
|
(newValue) => {
|
||||||
|
storageRef.value = newValue;
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
return ref;
|
||||||
|
}
|
||||||
|
|
|
@ -9,9 +9,10 @@ import {
|
||||||
import { withDefaultOnError } from '@/utils/defaults';
|
import { withDefaultOnError } from '@/utils/defaults';
|
||||||
import { booleanToHumanReadable } from '@/utils/boolean';
|
import { booleanToHumanReadable } from '@/utils/boolean';
|
||||||
import { useValidation } from '@/composable/validation';
|
import { useValidation } from '@/composable/validation';
|
||||||
|
import { useQueryParamOrStorage } from '@/composable/queryParams';
|
||||||
|
|
||||||
const rawPhone = ref('');
|
const rawPhone = ref('');
|
||||||
const defaultCountryCode = ref(getDefaultCountryCode());
|
const defaultCountryCode = useQueryParamOrStorage({ name: 'country', storageName: 'phone-parser:country', defaultValue: getDefaultCountryCode() });
|
||||||
const validation = useValidation({
|
const validation = useValidation({
|
||||||
source: rawPhone,
|
source: rawPhone,
|
||||||
rules: [
|
rules: [
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue