mirror of
https://github.com/tusen-ai/naive-ui.git
synced 2025-01-24 12:45:18 +08:00
refactor(time-picker): use ref
This commit is contained in:
parent
502deff40b
commit
7f2ec0d3da
@ -7,7 +7,7 @@
|
|||||||
}"
|
}"
|
||||||
>
|
>
|
||||||
<n-input
|
<n-input
|
||||||
ref="tracked"
|
ref="trackedRef"
|
||||||
v-model:value="displayTimeString"
|
v-model:value="displayTimeString"
|
||||||
class="n-time-picker-input"
|
class="n-time-picker-input"
|
||||||
passively-activated
|
passively-activated
|
||||||
@ -41,7 +41,7 @@
|
|||||||
adjust-to
|
adjust-to
|
||||||
>
|
>
|
||||||
<div
|
<div
|
||||||
ref="offsetContainer"
|
ref="offsetContainerRef"
|
||||||
v-zindexable="{ enabled: active }"
|
v-zindexable="{ enabled: active }"
|
||||||
class="n-positioning-container"
|
class="n-positioning-container"
|
||||||
:class="{
|
:class="{
|
||||||
@ -50,7 +50,7 @@
|
|||||||
}"
|
}"
|
||||||
>
|
>
|
||||||
<div
|
<div
|
||||||
ref="tracking"
|
ref="trackingRef"
|
||||||
class="n-positioning-content"
|
class="n-positioning-content"
|
||||||
>
|
>
|
||||||
<transition
|
<transition
|
||||||
@ -59,7 +59,7 @@
|
|||||||
>
|
>
|
||||||
<div
|
<div
|
||||||
v-if="active"
|
v-if="active"
|
||||||
ref="panel"
|
ref="panelRef"
|
||||||
v-clickoutside="handleClickOutside"
|
v-clickoutside="handleClickOutside"
|
||||||
tabindex="0"
|
tabindex="0"
|
||||||
class="n-time-picker-selector"
|
class="n-time-picker-selector"
|
||||||
@ -78,7 +78,7 @@
|
|||||||
'n-time-picker-selector-time-row--transition-disabled': hourTransitionDisabled
|
'n-time-picker-selector-time-row--transition-disabled': hourTransitionDisabled
|
||||||
}"
|
}"
|
||||||
>
|
>
|
||||||
<n-scrollbar ref="hours" :theme="mergedTheme">
|
<n-scrollbar ref="hoursRef" :theme="mergedTheme">
|
||||||
<div
|
<div
|
||||||
v-for="hour in hours"
|
v-for="hour in hours"
|
||||||
:key="hour"
|
:key="hour"
|
||||||
@ -105,7 +105,7 @@
|
|||||||
'n-time-picker-selector-time-row--invalid': isMinuteInvalid
|
'n-time-picker-selector-time-row--invalid': isMinuteInvalid
|
||||||
}"
|
}"
|
||||||
>
|
>
|
||||||
<n-scrollbar ref="minutes" :theme="mergedTheme">
|
<n-scrollbar ref="minutesRef" :theme="mergedTheme">
|
||||||
<div
|
<div
|
||||||
v-for="minute in minutes"
|
v-for="minute in minutes"
|
||||||
:key="minute"
|
:key="minute"
|
||||||
@ -131,7 +131,7 @@
|
|||||||
'n-time-picker-selector-time-row--transition-disabled': secondTransitionDisabled,
|
'n-time-picker-selector-time-row--transition-disabled': secondTransitionDisabled,
|
||||||
}"
|
}"
|
||||||
>
|
>
|
||||||
<n-scrollbar ref="seconds" :theme="mergedTheme">
|
<n-scrollbar ref="secondsRef" :theme="mergedTheme">
|
||||||
<div
|
<div
|
||||||
v-for="second in seconds"
|
v-for="second in seconds"
|
||||||
:key="second"
|
:key="second"
|
||||||
@ -182,6 +182,7 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
|
import { ref } from 'vue'
|
||||||
import NLazyTeleport from '../../_base/lazy-teleport'
|
import NLazyTeleport from '../../_base/lazy-teleport'
|
||||||
import NScrollbar from '../../scrollbar'
|
import NScrollbar from '../../scrollbar'
|
||||||
import NInput from '../../input'
|
import NInput from '../../input'
|
||||||
@ -203,13 +204,10 @@ import getHours from 'date-fns/getHours'
|
|||||||
import getSeconds from 'date-fns/getSeconds'
|
import getSeconds from 'date-fns/getSeconds'
|
||||||
import { strictParse } from '../../_utils/component/datePicker'
|
import { strictParse } from '../../_utils/component/datePicker'
|
||||||
import keyboardDelegate from '../../_utils/delegate/keyboardDelegate'
|
import keyboardDelegate from '../../_utils/delegate/keyboardDelegate'
|
||||||
import { KEY_CODE } from '../../_utils'
|
|
||||||
import NBaseFocusDetector from '../../_base/focus-detector'
|
import NBaseFocusDetector from '../../_base/focus-detector'
|
||||||
import TimeIcon from '../../_icons/time-outline.vue'
|
import TimeIcon from '../../_icons/time-outline.vue'
|
||||||
import styles from './styles'
|
import styles from './styles'
|
||||||
import { useIsMounted } from '../../_utils/composition'
|
import { warn, call, useIsMounted, KEY_CODE } from '../../_utils'
|
||||||
import { warn } from '../../_utils/naive/warn'
|
|
||||||
import { call } from '../../_utils/vue'
|
|
||||||
|
|
||||||
const DEFAULT_FORMAT = 'HH:mm:ss'
|
const DEFAULT_FORMAT = 'HH:mm:ss'
|
||||||
|
|
||||||
@ -338,7 +336,14 @@ export default {
|
|||||||
},
|
},
|
||||||
setup () {
|
setup () {
|
||||||
return {
|
return {
|
||||||
isMounted: useIsMounted()
|
isMounted: useIsMounted(),
|
||||||
|
hoursRef: ref(null),
|
||||||
|
minutesRef: ref(null),
|
||||||
|
secondsRef: ref(null),
|
||||||
|
offsetContainerRef: ref(null),
|
||||||
|
trackingRef: ref(null),
|
||||||
|
trackedRef: ref(null),
|
||||||
|
panelRef: ref(null)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
data () {
|
data () {
|
||||||
@ -421,7 +426,7 @@ export default {
|
|||||||
watch: {
|
watch: {
|
||||||
computedTime (value) {
|
computedTime (value) {
|
||||||
this.refreshTimeString(value)
|
this.refreshTimeString(value)
|
||||||
this.$nextTick().then(this.scrollTimer)
|
this.$nextTick(this.scrollTimer)
|
||||||
},
|
},
|
||||||
active (value) {
|
active (value) {
|
||||||
if (this.isValueInvalid) {
|
if (this.isValueInvalid) {
|
||||||
@ -431,16 +436,16 @@ export default {
|
|||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
__placeableBody () {
|
__placeableBody () {
|
||||||
return this.$refs.panel
|
return this.panelRef
|
||||||
},
|
},
|
||||||
__placeableOffsetContainer () {
|
__placeableOffsetContainer () {
|
||||||
return this.$refs.offsetContainer
|
return this.offsetContainerRef
|
||||||
},
|
},
|
||||||
__placeableTracking () {
|
__placeableTracking () {
|
||||||
return this.$refs.tracking
|
return this.trackingRef
|
||||||
},
|
},
|
||||||
__placeableTracked () {
|
__placeableTracked () {
|
||||||
return this.$refs.tracked
|
return this.trackedRef
|
||||||
},
|
},
|
||||||
doChange (value) {
|
doChange (value) {
|
||||||
const {
|
const {
|
||||||
@ -491,7 +496,7 @@ export default {
|
|||||||
break
|
break
|
||||||
case KEY_CODE.TAB:
|
case KEY_CODE.TAB:
|
||||||
const shiftPressed = keyboardDelegate.getKeyboardStatus().shiftPressed
|
const shiftPressed = keyboardDelegate.getKeyboardStatus().shiftPressed
|
||||||
if (shiftPressed && e.target === this.$refs.panel) {
|
if (shiftPressed && e.target === this.panelRef) {
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
this.closeTimeSelector({
|
this.closeTimeSelector({
|
||||||
returnFocus: true,
|
returnFocus: true,
|
||||||
@ -503,7 +508,7 @@ export default {
|
|||||||
},
|
},
|
||||||
disableTransitionOneTick (unit) {
|
disableTransitionOneTick (unit) {
|
||||||
this[unit + 'TransitionDisabled'] = true
|
this[unit + 'TransitionDisabled'] = true
|
||||||
this.$nextTick().then(() => {
|
this.$nextTick(() => {
|
||||||
this[unit + 'TransitionDisabled'] = false
|
this[unit + 'TransitionDisabled'] = false
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
@ -572,7 +577,7 @@ export default {
|
|||||||
},
|
},
|
||||||
handleTimeInputBlur (e) {
|
handleTimeInputBlur (e) {
|
||||||
if (this.active) {
|
if (this.active) {
|
||||||
const panel = this.$refs.panel
|
const panel = this.panelRef
|
||||||
if (!(
|
if (!(
|
||||||
panel &&
|
panel &&
|
||||||
panel.contains(e.relatedTarget)
|
panel.contains(e.relatedTarget)
|
||||||
@ -600,29 +605,29 @@ export default {
|
|||||||
})
|
})
|
||||||
},
|
},
|
||||||
scrollTimer () {
|
scrollTimer () {
|
||||||
if (this.$refs.hours && this.$refs.hours.$el) {
|
if (this.hoursRef && this.hoursRef.$el) {
|
||||||
const hour = this.$refs.hours.$el.querySelector('.n-time-picker-selector-time-row__item--active')
|
const hour = this.hoursRef.$el.querySelector('.n-time-picker-selector-time-row__item--active')
|
||||||
if (hour) {
|
if (hour) {
|
||||||
this.$refs.hours.$refs.scrollContainer.scrollTo(0, hour.offsetTop)
|
this.hoursRef.scrollTo(0, hour.offsetTop)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (this.$refs.minutes && this.$refs.minutes.$el) {
|
if (this.minutesRef && this.minutesRef.$el) {
|
||||||
const minute = this.$refs.minutes.$el.querySelector('.n-time-picker-selector-time-row__item--active')
|
const minute = this.minutesRef.$el.querySelector('.n-time-picker-selector-time-row__item--active')
|
||||||
if (minute) {
|
if (minute) {
|
||||||
this.$refs.minutes.$refs.scrollContainer.scrollTo(0, minute.offsetTop)
|
this.minutesRef.scrollTo(0, minute.offsetTop)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (this.$refs.seconds && this.$refs.seconds.$el) {
|
if (this.secondsRef && this.secondsRef.$el) {
|
||||||
const second = this.$refs.seconds.$el.querySelector('.n-time-picker-selector-time-row__item--active')
|
const second = this.secondsRef.$el.querySelector('.n-time-picker-selector-time-row__item--active')
|
||||||
if (second) {
|
if (second) {
|
||||||
this.$refs.seconds.$refs.scrollContainer.scrollTo(0, second.offsetTop)
|
this.secondsRef.scrollTo(0, second.offsetTop)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
openTimeSelector () {
|
openTimeSelector () {
|
||||||
this.memorizedValue = this.value
|
this.memorizedValue = this.value
|
||||||
this.active = true
|
this.active = true
|
||||||
this.$nextTick().then(this.scrollTimer)
|
this.$nextTick(this.scrollTimer)
|
||||||
},
|
},
|
||||||
handleActivatorClick (e) {
|
handleActivatorClick (e) {
|
||||||
if (!this.active) {
|
if (!this.active) {
|
||||||
@ -630,7 +635,7 @@ export default {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
handleClickOutside (e) {
|
handleClickOutside (e) {
|
||||||
if (this.active && !this.$refs.tracked.$el.contains(e.target)) {
|
if (this.active && !this.trackedRef.$el.contains(e.target)) {
|
||||||
this.closeTimeSelector({
|
this.closeTimeSelector({
|
||||||
returnFocus: false,
|
returnFocus: false,
|
||||||
emitBlur: true
|
emitBlur: true
|
||||||
@ -644,7 +649,7 @@ export default {
|
|||||||
if (this.active) {
|
if (this.active) {
|
||||||
this.active = false
|
this.active = false
|
||||||
if (returnFocus) {
|
if (returnFocus) {
|
||||||
this.$refs.tracked.focus()
|
this.trackedRef.focus()
|
||||||
}
|
}
|
||||||
if (emitBlur) {
|
if (emitBlur) {
|
||||||
const {
|
const {
|
||||||
@ -691,7 +696,7 @@ export default {
|
|||||||
})
|
})
|
||||||
},
|
},
|
||||||
handleMenuFocus (e) {
|
handleMenuFocus (e) {
|
||||||
const panel = this.$refs.panel
|
const panel = this.panelRef
|
||||||
if (
|
if (
|
||||||
keyboardDelegate.tabPressed &&
|
keyboardDelegate.tabPressed &&
|
||||||
e.target === panel &&
|
e.target === panel &&
|
||||||
|
Loading…
Reference in New Issue
Block a user