File size: 8,832 Bytes
4d70170 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 |
<script lang="ts">
import type {
UnwrapNestedRefs,
} from 'vue'
import {
computed,
defineComponent,
inject,
reactive,
ref,
watch,
} from 'vue'
export default defineComponent({
inheritAttrs: false,
props: {
iconLeft: {
type: String,
default: null,
},
iconRight: {
type: String,
default: null,
},
loadingLeft: {
type: Boolean,
default: false,
},
loadingRight: {
type: Boolean,
default: false,
},
placeholder: {
type: String,
default: undefined,
},
selectAll: {
type: Boolean,
default: false,
},
status: {
type: String,
default: undefined,
},
suggestion: {
type: [String, Number],
default: null,
},
type: {
type: String,
default: 'text',
},
modelValue: {
type: [String, Number],
required: true,
},
},
emits: ['blur', 'focus', 'update:modelValue'],
setup(props, { emit }) {
const vueFormField = inject<{
data: UnwrapNestedRefs<{
focused: boolean
status: string | null
}>
}>('VueFormField', null)
const model = computed({
get: () => props.modelValue,
set: (value: string | number) => {
emit('update:modelValue', value)
},
})
const input = ref<HTMLInputElement | HTMLTextAreaElement | null>(null)
const focused = ref(false)
const select = reactive({
autoSelect: false,
selectAllTimer: null,
})
const showSuggestion = computed(
() =>
props.suggestion !== null
&& props.suggestion !== model.value
&& focused.value
&& model.value,
)
watch(
() => focused.value,
(value) => {
if (vueFormField) {
vueFormField.data.focused = value
}
},
{
immediate: true,
},
)
watch(
() => props.status,
(value) => {
if (vueFormField) {
vueFormField.data.status = value
}
},
{
immediate: true,
},
)
const focus = () => {
input.value?.focus()
}
const autoSelectAll = () => {
if (props.selectAll && select.autoSelect) {
const inputElement = input.value
requestAnimationFrame(() => {
inputElement.setSelectionRange(0, inputElement.value.length)
clearTimeout(select.selectAllTimer)
select.selectAllTimer = setTimeout(() => {
select.autoSelect = false
}, 500)
})
}
}
const onBlur = (event: FocusEvent) => {
focused.value = false
select.autoSelect = false
emit('blur', event)
}
const onFocus = (event: FocusEvent) => {
if (!focused.value) {
clearTimeout(select.selectAllTimer)
select.autoSelect = true
}
focused.value = true
autoSelectAll()
emit('focus', event)
}
const onKeyTab = (event: KeyboardEvent) => {
if (showSuggestion.value) {
model.value = props.suggestion
event.preventDefault()
event.stopPropagation()
}
}
return {
input,
focused,
showSuggestion,
model,
focus,
onBlur,
onFocus,
onKeyTab,
}
},
})
</script>
<template>
<div
class="vue-ui-input"
:class="[
`type-${type}`,
{
focused,
'show-suggestion': showSuggestion,
[`status-${status}`]: status,
},
$attrs.class,
]"
@click="focus()"
>
<div class="content">
<VueLoadingIndicator
v-if="loadingLeft"
class="small left"
/>
<VueIcon
v-else-if="iconLeft"
:icon="iconLeft"
class="input-icon left"
/>
<slot name="left" />
<div class="input-wrapper">
<component
:is="type === 'textarea' ? type : 'input'"
ref="input"
class="input"
:type="type"
:value.prop="modelValue"
:placeholder="placeholder"
v-bind="$attrs"
@input="model = $event.currentTarget.value"
@focus="onFocus"
@blur="onBlur"
@keydown.tab="onKeyTab"
/>
<input
v-if="showSuggestion"
class="input suggestion"
:value="suggestion"
disabled
>
</div>
<slot name="right" />
<VueIcon
v-if="iconRight"
:icon="iconRight"
class="input-icon right"
/>
<VueLoadingIndicator
v-if="loadingRight"
class="small right"
/>
<!-- Focus animation -->
<div class="border" />
</div>
</div>
</template>
<style lang="stylus">
@import '~@vue/ui/src/style/imports'
colors($color)
> .content
> .border
background $color
&.focused
> .content
> .vue-ui-loading-indicator
.animation
border-right-color $color
border-bottom-color $color
> .input-icon
svg
fill rgba($color, .8)
.vue-ui-input
$lightened = theme('colors.gray.500')
display inline-block
vertical-align middle
box-sizing border-box
width auto
min-width 200px
> .content
h-box()
box-center()
padding 0 10px
border solid 1px $vue-ui-primary-100
color $vue-ui-gray-800
border-radius $br
transition background .3s
position relative
.vue-ui-dark-mode &
border-color theme('colors.gray.700')
color $vue-ui-white
> .input-wrapper
position relative
width 0
flex auto 1 1
> .input
position relative
z-index 1
font-family inherit
font-size 14px
line-height 14px
color @color
padding 0
width 100%
display block
border none
background transparent
.vue-ui-dark-mode &
color $vue-ui-white
&:not(textarea)
height 30px
&::placeholder
color $lightened
.vue-ui-dark-mode &
color $vue-ui-gray-300
// Disable noisy browser styles
outline none
&::-moz-focus-inner
border 0
> textarea.input
padding 8px 10px
resize vertical
min-height 30px
box-sizing border-box
line-height 18px
> .suggestion
position absolute
z-index 0
top 0
left 0
overflow hidden
white-space nowrap
text-overflow ellipsis
color $lightened
pointer-events none
> .input-icon
&.left
margin-right 6px
&.right
margin-left 6px
svg
fill $lightened
transition fill .3s
> .vue-ui-loading-indicator
&.left
margin-right 8px
&.right
margin-left 8px
.animation
border-right-color $lightened
border-bottom-color $lightened
> .border
position absolute
bottom -1px
left 30%
right @left
opacity 0
height 2px
pointer-events none
transition left .15s, right .15s, opacity .15s
&.type-textarea
> .content
padding 0
&:not(.flat)
> .content
background $vue-ui-white
.vue-ui-dark-mode &
background $vue-ui-gray-900
&.show-suggestion
> .content > .input-wrapper > .input
&::placeholder
color transparent
// Colors
colors($vue-ui-primary-500)
&.accent
colors($vue-ui-accent-500)
.vue-ui-dark-mode &
colors($vue-ui-accent-300)
&.danger,
&.status-danger
colors($vue-ui-danger-500)
&.warning,
&.status-warning
colors($vue-ui-warning-500)
&.info,
&.status-info
colors($vue-ui-info-500)
&.success,
&.status-success
colors($vue-ui-primary-500)
&.focused
&:not(.flat)
> .content
> .border
left 0
right @left
opacity 1
&.round
> .content > .border
display none
&.flat
> .content
border-color transparent
> .border
display none
&.big
> .content
padding 0 14px
> .input-wrapper
> .input
font-size 16px
&:not(textarea)
height 42px
> textarea.input
padding 14px 0
> .input-icon
width 20px
height @width
&.left
margin-right 10px
&.right
margin-left 10px
&.round
> .content
border-radius 17px
// Big button
&.big
> .content
border-radius 22px
&:not(.disabled)
cursor text
&.disabled
opacity .5
.vue-ui-dropdown-content > &
min-width 200px
padding 0 4px 4px
.vue-ui-high-contrast &
> .content
border-width 2px
border-style dashed
background $md-black !important
</style>
|