parent
							
								
									e876d03608
								
							
						
					
					
						commit
						374caa176e
					
				| @ -1,7 +1,8 @@ | ||||
| import { useRouteQuery } from '@vueuse/router'; | ||||
| import { computed } from 'vue'; | ||||
| import { useStorage } from '@vueuse/core'; | ||||
| 
 | ||||
| export { useQueryParam }; | ||||
| export { useQueryParam, useQueryParamOrStorage }; | ||||
| 
 | ||||
| const transformers = { | ||||
|   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 { booleanToHumanReadable } from '@/utils/boolean'; | ||||
| import { useValidation } from '@/composable/validation'; | ||||
| import { useQueryParamOrStorage } from '@/composable/queryParams'; | ||||
| 
 | ||||
| const rawPhone = ref(''); | ||||
| const defaultCountryCode = ref(getDefaultCountryCode()); | ||||
| const defaultCountryCode = useQueryParamOrStorage({ name: 'country', storageName: 'phone-parser:country', defaultValue: getDefaultCountryCode() }); | ||||
| const validation = useValidation({ | ||||
|   source: rawPhone, | ||||
|   rules: [ | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user