|
1 | 1 | <script setup lang="ts"> |
2 | | -import { getCurrentInstance, onMounted } from '@vue/runtime-core' |
| 2 | +import { computed, ref, toRef, watch } from '@vue/runtime-core' |
| 3 | +import { onInputData } from '../composables/input' |
| 4 | +import { isKeyDataEvent } from '../input/types' |
| 5 | +import { useInterval } from '../composables/utils' |
| 6 | +import { useFocus } from '../focus/Focusable' |
3 | 7 |
|
4 | | -const instance = getCurrentInstance() |
| 8 | +const emit = defineEmits<{ |
| 9 | + (event: 'update:modelValue', value: string): void |
| 10 | +}>() |
5 | 11 |
|
6 | | -console.log(!!instance) |
7 | | -debugger |
| 12 | +const props = withDefaults( |
| 13 | + defineProps<{ |
| 14 | + modelValue?: string |
| 15 | + disabled?: boolean |
| 16 | + label?: string |
| 17 | + minWidth?: number |
| 18 | + type?: 'text' | 'password' |
| 19 | + }>(), |
| 20 | + { |
| 21 | + minWidth: 10, |
| 22 | + type: 'text', |
| 23 | + } |
| 24 | +) |
8 | 25 |
|
9 | | -onMounted(() => { |
10 | | - console.log(!!instance) |
11 | | - debugger |
| 26 | +const { active, disabled } = useFocus({ |
| 27 | + // @ts-expect-error: vue bug? |
| 28 | + disabled: toRef(props, 'disabled'), |
| 29 | +}) |
| 30 | +
|
| 31 | +watch(active, () => { |
| 32 | + if (active.value) { |
| 33 | + restartBlinking() |
| 34 | + } |
| 35 | +}) |
| 36 | +
|
| 37 | +// used for fallback value when no v-model |
| 38 | +const localText = ref('') |
| 39 | +const text = computed({ |
| 40 | + get() { |
| 41 | + return props.modelValue ?? localText.value |
| 42 | + }, |
| 43 | + set(value) { |
| 44 | + if (props.modelValue == null) { |
| 45 | + localText.value = value |
| 46 | + } else { |
| 47 | + emit('update:modelValue', value) |
| 48 | + } |
| 49 | + }, |
| 50 | +}) |
| 51 | +const cursorPosition = ref(text.value.length) |
| 52 | +const displayedValue = computed(() => { |
| 53 | + const textValue = |
| 54 | + props.type === 'text' ? text.value : '*'.repeat(text.value.length) |
| 55 | + if (showCursorBlock.value && active.value) { |
| 56 | + return ( |
| 57 | + textValue.slice(0, cursorPosition.value) + |
| 58 | + FULL_BLOCK + |
| 59 | + textValue.slice(cursorPosition.value + 1) + |
| 60 | + (cursorPosition.value >= textValue.length ? '' : ' ') |
| 61 | + ) |
| 62 | + } |
| 63 | + return textValue + ' ' |
| 64 | +}) |
| 65 | +
|
| 66 | +const FULL_BLOCK = '\u{2588}' // '█' |
| 67 | +const showCursorBlock = ref(true) |
| 68 | +
|
| 69 | +const { restart } = useInterval(() => { |
| 70 | + showCursorBlock.value = !showCursorBlock.value |
| 71 | +}, 700) |
| 72 | +// allows to always show the cursor while moving it |
| 73 | +function restartBlinking() { |
| 74 | + restart() |
| 75 | + showCursorBlock.value = true |
| 76 | +} |
| 77 | +
|
| 78 | +onInputData(({ event }) => { |
| 79 | + if (active.value && !disabled.value) { |
| 80 | + if (isKeyDataEvent(event)) { |
| 81 | + switch (event.key) { |
| 82 | + // cursor moving |
| 83 | + case 'ArrowLeft': |
| 84 | + cursorPosition.value = Math.max(0, cursorPosition.value - 1) |
| 85 | + // TODO: handle alt, ctrl |
| 86 | + restartBlinking() |
| 87 | + break |
| 88 | + case 'ArrowRight': |
| 89 | + cursorPosition.value = Math.min( |
| 90 | + text.value.length, |
| 91 | + cursorPosition.value + 1 |
| 92 | + ) |
| 93 | + restartBlinking() |
| 94 | + break |
| 95 | +
|
| 96 | + case 'Backspace': |
| 97 | + if (cursorPosition.value > 0) { |
| 98 | + text.value = |
| 99 | + text.value.slice(0, cursorPosition.value - 1) + |
| 100 | + text.value.slice(cursorPosition.value) |
| 101 | + cursorPosition.value-- |
| 102 | + } |
| 103 | + break |
| 104 | + case 'e': |
| 105 | + case 'E': |
| 106 | + if (event.ctrlKey) { |
| 107 | + cursorPosition.value = text.value.length |
| 108 | + restartBlinking() |
| 109 | + break |
| 110 | + } |
| 111 | +
|
| 112 | + case 'a': |
| 113 | + case 'A': |
| 114 | + if (event.ctrlKey) { |
| 115 | + cursorPosition.value = 0 |
| 116 | + restartBlinking() |
| 117 | + break |
| 118 | + } |
| 119 | +
|
| 120 | + case 'u': |
| 121 | + case 'U': |
| 122 | + if (event.ctrlKey) { |
| 123 | + text.value = text.value.slice(cursorPosition.value) |
| 124 | + cursorPosition.value = 0 |
| 125 | + restartBlinking() |
| 126 | + break |
| 127 | + } |
| 128 | +
|
| 129 | + case 'k': |
| 130 | + case 'K': |
| 131 | + if (event.ctrlKey) { |
| 132 | + text.value = text.value.slice(0, cursorPosition.value) |
| 133 | + restartBlinking() |
| 134 | + break |
| 135 | + } |
| 136 | +
|
| 137 | + default: |
| 138 | + if ( |
| 139 | + event.key.length === 1 && |
| 140 | + !event.altKey && |
| 141 | + !event.ctrlKey && |
| 142 | + !event.metaKey |
| 143 | + ) { |
| 144 | + text.value = |
| 145 | + text.value.slice(0, cursorPosition.value) + |
| 146 | + event.key + |
| 147 | + text.value.slice(cursorPosition.value) |
| 148 | + cursorPosition.value++ |
| 149 | + } |
| 150 | + break |
| 151 | + } |
| 152 | + } |
| 153 | + } |
12 | 154 | }) |
13 | 155 | </script> |
14 | 156 |
|
15 | | -<template> |
16 | | - <TuiText dimmed>Label: </TuiText> |
17 | | - <TuiText>value</TuiText> |
| 157 | +<template borderStyle="round"> |
| 158 | + <Box> |
| 159 | + <Box alignItems="center"> |
| 160 | + <!-- Could also be a title prop in Box --> |
| 161 | + <Text dimmed>{{ label }} </Text> |
| 162 | + </Box> |
| 163 | + <Box |
| 164 | + borderStyle="round" |
| 165 | + :borderColor="disabled ? 'gray' : active ? 'blue' : undefined" |
| 166 | + :backgroundColor="active ? 'blue' : undefined" |
| 167 | + :height="3" |
| 168 | + :minWidth="minWidth" |
| 169 | + > |
| 170 | + <Text :dimmed="disabled">{{ displayedValue }}</Text> |
| 171 | + </Box> |
| 172 | + </Box> |
18 | 173 | </template> |
0 commit comments