-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
34 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,33 @@ | ||
<script setup lang="ts"> | ||
import { ref } from 'vue' | ||
import { useMinimum } from '@vueuse/math' | ||
const other = ref(10) | ||
const value = useMinimum(0, other) | ||
const min1 = ref(10) | ||
const min2 = ref(20) | ||
const result = useMinimum(min1, min2) | ||
</script> | ||
|
||
<template> | ||
<div> | ||
<div> | ||
other: | ||
<input v-model="other" type="number"> | ||
<button @click="other--"> | ||
other -= 1 | ||
min1: | ||
<input v-model="min1" type="number"> | ||
<button @click="min1--"> | ||
min1 -= 1 | ||
</button> | ||
<button @click="other++"> | ||
other += 1 | ||
<button @click="min1++"> | ||
min1 += 1 | ||
</button> | ||
</div> | ||
value:{{ value }} | ||
<div> | ||
<button @click="value--"> | ||
value -= 1 | ||
min2: | ||
<input v-model="min2" type="number"> | ||
<button @click="min2--"> | ||
min2 -= 1 | ||
</button> | ||
<button @click="value++"> | ||
value += 1 | ||
<button @click="min2++"> | ||
min2 += 1 | ||
</button> | ||
</div> | ||
result:{{ result }} | ||
</div> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,18 @@ | ||
import type { Ref } from 'vue-demi' | ||
import { computed, ref } from 'vue-demi' | ||
import type { MaybeComputedRef, MaybeRef } from '@vueuse/shared' | ||
import { computed } from 'vue-demi' | ||
import type { MaybeComputedRef } from '@vueuse/shared' | ||
import { resolveUnref } from '@vueuse/shared' | ||
|
||
/** | ||
* Reactively get useMinimum of values. | ||
* Reactively get minimum of values. | ||
* | ||
* @see https://vueuse.org/useMinimum | ||
* @param value number | ||
* @param {...number[]} values | ||
* | ||
* @param values | ||
*/ | ||
export function useMinimum(defaultValue: MaybeRef<number>, ...mins: MaybeComputedRef<number>[]): Ref<number> { | ||
const _value = ref(defaultValue) | ||
return computed<number>({ | ||
get() { | ||
return _value.value = Math.min(_value.value, ...mins.map(value => resolveUnref(value))) | ||
}, | ||
set(value) { | ||
_value.value = Math.min(value, ...mins.map(value => resolveUnref(value))) | ||
}, | ||
}) | ||
export function useMinimum(...mins: MaybeComputedRef<number>[]) { | ||
return computed<number>(() => | ||
Math.min(...mins.map(value => resolveUnref(value))), | ||
) | ||
} | ||
|
||
export const useMin = useMinimum | ||
// alias | ||
export { useMinimum as useMin } |