This commit is contained in:
darenhsu
2022-07-17 13:16:16 +08:00
parent 84759556ff
commit befd344ab0
28070 changed files with 4008428 additions and 1 deletions
+126
View File
@@ -0,0 +1,126 @@
@import './_variables.scss'
/* Theme */
+theme(v-input) using ($material)
color: map-deep-get($material, 'text', 'primary')
input,
textarea
color: map-deep-get($material, 'text', 'primary')
input::placeholder,
textarea::placeholder
color: map-deep-get($material, 'text', 'disabled')
&--is-disabled
color: map-deep-get($material, 'text', 'disabled')
input,
textarea
color: map-deep-get($material, 'text', 'disabled')
.v-input
align-items: flex-start
display: flex
flex: 1 1 auto
font-size: $input-font-size
letter-spacing: $input-letter-spacing
max-width: 100%
text-align: $input-text-align
.v-progress-linear
top: calc(100% - 1px)
left: 0
input
max-height: $input-max-height
input,
textarea
// Remove Firefox red outline
&:invalid
box-shadow: none
&:focus,
&:active
outline: none
.v-label
height: $input-label-height
line-height: $input-label-letter-spacing
&__append-outer,
&__prepend-outer
display: inline-flex
margin-bottom: 4px
margin-top: 4px
line-height: 1
.v-icon
user-select: none
&__append-outer
+ltr()
margin-left: $input-prepend-append-outer-margin
+rtl()
margin-right: $input-prepend-append-outer-margin
&__prepend-outer
+ltr()
margin-right: $input-prepend-append-outer-margin
+rtl()
margin-left: $input-prepend-append-outer-margin
&__control
display: flex
flex-direction: column
height: auto
flex-grow: 1
flex-wrap: wrap
min-width: 0
width: 100% // For IE11
&__icon
align-items: center
display: inline-flex
height: $input-icon-height
flex: 1 0 auto
justify-content: center
min-width: $input-icon-min-width
width: $input-icon-width
&--clear
border-radius: 50%
.v-icon--disabled
visibility: hidden
&__slot
align-items: center
color: inherit
display: flex
margin-bottom: $input-slot-margin-bottom
min-height: inherit
position: relative
transition: $primary-transition
width: 100%
&--dense > .v-input__control > .v-input__slot
margin-bottom: $input-dense-slot-margin-bottom
&--is-disabled:not(.v-input--is-readonly)
pointer-events: none
&--is-loading > .v-input__control > .v-input__slot
&:before,
&:after
display: none
&--hide-details > .v-input__control > .v-input__slot
margin-bottom: 0
&--has-state
&.error--text .v-label
animation: v-shake .6s map-get($transition, 'swing')
+318
View File
@@ -0,0 +1,318 @@
// Styles
import './VInput.sass'
// Components
import VIcon from '../VIcon'
import VLabel from '../VLabel'
import VMessages from '../VMessages'
// Mixins
import BindsAttrs from '../../mixins/binds-attrs'
import Validatable from '../../mixins/validatable'
// Utilities
import {
convertToUnit,
getSlot,
kebabCase,
} from '../../util/helpers'
import mergeData from '../../util/mergeData'
// Types
import { VNode, VNodeData, PropType } from 'vue'
import mixins from '../../util/mixins'
import { InputValidationRule } from 'vuetify/types'
const baseMixins = mixins(
BindsAttrs,
Validatable,
)
interface options extends InstanceType<typeof baseMixins> {
/* eslint-disable-next-line camelcase */
$_modelEvent: string
}
/* @vue/component */
export default baseMixins.extend<options>().extend({
name: 'v-input',
inheritAttrs: false,
props: {
appendIcon: String,
backgroundColor: {
type: String,
default: '',
},
dense: Boolean,
height: [Number, String],
hideDetails: [Boolean, String] as PropType<boolean | 'auto'>,
hint: String,
id: String,
label: String,
loading: Boolean,
persistentHint: Boolean,
prependIcon: String,
value: null as any as PropType<any>,
},
data () {
return {
lazyValue: this.value,
hasMouseDown: false,
}
},
computed: {
classes (): object {
return {
'v-input--has-state': this.hasState,
'v-input--hide-details': !this.showDetails,
'v-input--is-label-active': this.isLabelActive,
'v-input--is-dirty': this.isDirty,
'v-input--is-disabled': this.isDisabled,
'v-input--is-focused': this.isFocused,
// <v-switch loading>.loading === '' so we can't just cast to boolean
'v-input--is-loading': this.loading !== false && this.loading != null,
'v-input--is-readonly': this.isReadonly,
'v-input--dense': this.dense,
...this.themeClasses,
}
},
computedId (): string {
return this.id || `input-${this._uid}`
},
hasDetails (): boolean {
return this.messagesToDisplay.length > 0
},
hasHint (): boolean {
return !this.hasMessages &&
!!this.hint &&
(this.persistentHint || this.isFocused)
},
hasLabel (): boolean {
return !!(this.$slots.label || this.label)
},
// Proxy for `lazyValue`
// This allows an input
// to function without
// a provided model
internalValue: {
get (): any {
return this.lazyValue
},
set (val: any) {
this.lazyValue = val
this.$emit(this.$_modelEvent, val)
},
},
isDirty (): boolean {
return !!this.lazyValue
},
isLabelActive (): boolean {
return this.isDirty
},
messagesToDisplay (): string[] {
if (this.hasHint) return [this.hint]
if (!this.hasMessages) return []
return this.validations.map((validation: string | InputValidationRule) => {
if (typeof validation === 'string') return validation
const validationResult = validation(this.internalValue)
return typeof validationResult === 'string' ? validationResult : ''
}).filter(message => message !== '')
},
showDetails (): boolean {
return this.hideDetails === false || (this.hideDetails === 'auto' && this.hasDetails)
},
},
watch: {
value (val) {
this.lazyValue = val
},
},
beforeCreate () {
// v-radio-group needs to emit a different event
// https://github.com/vuetifyjs/vuetify/issues/4752
this.$_modelEvent = (this.$options.model && this.$options.model.event) || 'input'
},
methods: {
genContent () {
return [
this.genPrependSlot(),
this.genControl(),
this.genAppendSlot(),
]
},
genControl () {
return this.$createElement('div', {
staticClass: 'v-input__control',
}, [
this.genInputSlot(),
this.genMessages(),
])
},
genDefaultSlot () {
return [
this.genLabel(),
this.$slots.default,
]
},
genIcon (
type: string,
cb?: (e: Event) => void,
extraData: VNodeData = {}
) {
const icon = (this as any)[`${type}Icon`]
const eventName = `click:${kebabCase(type)}`
const hasListener = !!(this.listeners$[eventName] || cb)
const data = mergeData({
attrs: {
'aria-label': hasListener ? kebabCase(type).split('-')[0] + ' icon' : undefined,
color: this.validationState,
dark: this.dark,
disabled: this.isDisabled,
light: this.light,
},
on: !hasListener
? undefined
: {
click: (e: Event) => {
e.preventDefault()
e.stopPropagation()
this.$emit(eventName, e)
cb && cb(e)
},
// Container has g event that will
// trigger menu open if enclosed
mouseup: (e: Event) => {
e.preventDefault()
e.stopPropagation()
},
},
}, extraData)
return this.$createElement('div', {
staticClass: `v-input__icon`,
class: type ? `v-input__icon--${kebabCase(type)}` : undefined,
}, [
this.$createElement(
VIcon,
data,
icon
),
])
},
genInputSlot () {
return this.$createElement('div', this.setBackgroundColor(this.backgroundColor, {
staticClass: 'v-input__slot',
style: { height: convertToUnit(this.height) },
on: {
click: this.onClick,
mousedown: this.onMouseDown,
mouseup: this.onMouseUp,
},
ref: 'input-slot',
}), [this.genDefaultSlot()])
},
genLabel () {
if (!this.hasLabel) return null
return this.$createElement(VLabel, {
props: {
color: this.validationState,
dark: this.dark,
disabled: this.isDisabled,
focused: this.hasState,
for: this.computedId,
light: this.light,
},
}, this.$slots.label || this.label)
},
genMessages () {
if (!this.showDetails) return null
return this.$createElement(VMessages, {
props: {
color: this.hasHint ? '' : this.validationState,
dark: this.dark,
light: this.light,
value: this.messagesToDisplay,
},
attrs: {
role: this.hasMessages ? 'alert' : null,
},
scopedSlots: {
default: props => getSlot(this, 'message', props),
},
})
},
genSlot (
type: string,
location: string,
slot: (VNode | VNode[])[]
) {
if (!slot.length) return null
const ref = `${type}-${location}`
return this.$createElement('div', {
staticClass: `v-input__${ref}`,
ref,
}, slot)
},
genPrependSlot () {
const slot = []
if (this.$slots.prepend) {
slot.push(this.$slots.prepend)
} else if (this.prependIcon) {
slot.push(this.genIcon('prepend'))
}
return this.genSlot('prepend', 'outer', slot)
},
genAppendSlot () {
const slot = []
// Append icon for text field was really
// an appended inner icon, v-text-field
// will overwrite this method in order to obtain
// backwards compat
if (this.$slots.append) {
slot.push(this.$slots.append)
} else if (this.appendIcon) {
slot.push(this.genIcon('append'))
}
return this.genSlot('append', 'outer', slot)
},
onClick (e: Event) {
this.$emit('click', e)
},
onMouseDown (e: Event) {
this.hasMouseDown = true
this.$emit('mousedown', e)
},
onMouseUp (e: Event) {
this.hasMouseDown = false
this.$emit('mouseup', e)
},
},
render (h): VNode {
return h('div', this.setTextColor(this.validationState, {
staticClass: 'v-input',
class: this.classes,
}), this.genContent())
},
})
+272
View File
@@ -0,0 +1,272 @@
import VInput from '../VInput'
import {
mount,
MountOptions,
Wrapper,
} from '@vue/test-utils'
describe('VInput.ts', () => {
type Instance = InstanceType<typeof VInput>
let mountFunction: (options?: MountOptions<Instance>) => Wrapper<Instance>
beforeEach(() => {
mountFunction = (options?: MountOptions<Instance>) => {
return mount(VInput, {
// https://github.com/vuejs/vue-test-utils/issues/1130
sync: false,
...options,
})
}
})
it('should have hint', () => {
const wrapper = mountFunction({
propsData: {
hint: 'foo',
},
})
expect(wrapper.vm.hasHint).toBe(false)
wrapper.setProps({ persistentHint: true })
expect(wrapper.vm.hasHint).toBe(true)
wrapper.setProps({ persistentHint: false })
expect(wrapper.vm.hasHint).toBe(false)
wrapper.setData({ isFocused: true })
expect(wrapper.vm.hasHint).toBe(true)
})
it('should emit an input update', () => {
const wrapper = mountFunction()
const input = jest.fn()
wrapper.vm.$on('input', input)
expect(wrapper.vm.lazyValue).toBeUndefined()
wrapper.vm.internalValue = 'foo'
expect(input).toHaveBeenCalledWith('foo')
expect(wrapper.vm.lazyValue).toBe('foo')
})
it('should generate append and prepend slots', () => {
const el = slot => ({
render: h => h('div', slot),
})
const wrapper = mountFunction({
slots: { append: [el('append')] },
})
const wrapper2 = mountFunction({
slots: { prepend: [el('prepend')] },
})
expect(wrapper.html()).toMatchSnapshot()
expect(wrapper2.html()).toMatchSnapshot()
})
it('should generate an icon and match snapshot', async () => {
const wrapper = mountFunction({
propsData: {
prependIcon: 'list',
},
})
expect(wrapper.html()).toMatchSnapshot()
wrapper.setProps({
prependIcon: undefined,
appendIcon: 'list',
})
await wrapper.vm.$nextTick()
expect(wrapper.html()).toMatchSnapshot()
})
it('should not generate input details', () => {
const wrapper = mountFunction({
propsData: {
hideDetails: true,
},
})
expect(wrapper.vm.genMessages()).toBeNull()
expect(wrapper.html()).toMatchSnapshot()
})
it('should invoke callback', () => {
const cb = jest.fn()
const wrapper = mountFunction({
propsData: {
prependIcon: 'list',
appendIcon: 'search',
},
listeners: {
'click:prepend': cb,
'click:append': cb,
},
})
const click = jest.fn()
wrapper.vm.$on('click', click)
const prepend = wrapper.findAll('.v-icon').at(0)
const append = wrapper.findAll('.v-icon').at(1)
const slot = wrapper.find('.v-input__slot')
prepend.trigger('click')
expect(cb).toHaveBeenCalledTimes(1)
append.trigger('click')
expect(cb).toHaveBeenCalledTimes(2)
expect(click).not.toHaveBeenCalled()
slot.trigger('click')
expect(click).toHaveBeenCalled()
})
it('should accept a custom height', async () => {
const wrapper = mountFunction()
const inputWrapper = wrapper.find('.v-input__slot')
expect(inputWrapper.element.style.height).toBe('')
expect(wrapper.vm.height).toBeUndefined()
wrapper.setProps({ height: 10 })
await wrapper.vm.$nextTick()
expect(inputWrapper.element.style.height).toBe('10px')
wrapper.setProps({ height: '20px' })
await wrapper.vm.$nextTick()
expect(inputWrapper.element.style.height).toBe('20px')
})
it('should update lazyValue when value is updated', async () => {
const wrapper = mountFunction({
propsData: {
value: 'foo',
},
})
expect(wrapper.vm.lazyValue).toBe('foo')
wrapper.setProps({ value: 'bar' })
await wrapper.vm.$nextTick()
expect(wrapper.vm.lazyValue).toBe('bar')
})
it('should call the correct event for different click locations', () => {
const onClick = jest.fn()
const onMouseDown = jest.fn()
const onMouseUp = jest.fn()
const wrapper = mountFunction({
methods: {
onClick,
onMouseDown,
onMouseUp,
},
})
const slot = wrapper.find('.v-input__slot')
wrapper.trigger('click')
wrapper.trigger('mousedown')
wrapper.trigger('mouseup')
slot.trigger('click')
slot.trigger('mousedown')
slot.trigger('mouseup')
expect(onClick).toHaveBeenCalledTimes(1)
expect(onMouseDown).toHaveBeenCalledTimes(1)
expect(onMouseUp).toHaveBeenCalledTimes(1)
})
it('should be in an error state', async () => {
const wrapper = mountFunction({
propsData: { error: true },
})
expect(wrapper.html()).toMatchSnapshot()
wrapper.setProps({ errorMessages: 'required', error: false })
await wrapper.vm.$nextTick()
expect(wrapper.html()).toMatchSnapshot()
})
it('should hide messages if no messages and hide-details is auto', () => {
const wrapper = mountFunction({
propsData: {
hideDetails: 'auto',
},
})
expect(wrapper.vm.genMessages()).toBeNull()
wrapper.setProps({ error: true })
expect(wrapper.vm.genMessages()).toBeNull()
wrapper.setProps({ errorMessages: 'required' })
expect(wrapper.vm.genMessages()).not.toBeNull()
})
it('should be disabled', () => {
const wrapper = mountFunction()
expect(wrapper.vm.isInteractive).toBe(true)
wrapper.setProps({ disabled: true })
expect(wrapper.vm.isInteractive).toBe(false)
wrapper.setProps({
disabled: false,
readonly: true,
})
expect(wrapper.vm.isInteractive).toBe(false)
wrapper.setProps({ readonly: false })
expect(wrapper.vm.isInteractive).toBe(true)
})
it('should render a label', () => {
const wrapper = mountFunction({
propsData: { label: 'foo' },
})
expect(wrapper.vm.hasLabel).toBe(true)
expect(wrapper.html()).toMatchSnapshot()
const wrapper2 = mountFunction({
slots: {
label: [{ render: h => h('div', 'foo') }],
},
})
expect(wrapper2.html()).toMatchSnapshot()
})
it('should apply theme to label, counter, messages and icons', () => {
const wrapper = mountFunction({
propsData: {
label: 'foo',
hint: 'bar',
persistentHint: true,
light: true,
prependIcon: 'prepend',
appendIcon: 'append',
},
})
expect(wrapper.html()).toMatchSnapshot()
})
it('should not apply attrs to element', () => {
const wrapper = mountFunction({
propsData: {
foo: 'bar',
},
})
expect(wrapper.html()).toMatchSnapshot()
expect(wrapper.attributes()).not.toHaveProperty('foobar')
})
})
@@ -0,0 +1,244 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`VInput.ts should apply theme to label, counter, messages and icons 1`] = `
<div class="v-input theme--light">
<div class="v-input__prepend-outer">
<div class="v-input__icon v-input__icon--prepend">
<i aria-hidden="true"
class="v-icon notranslate material-icons theme--light"
>
prepend
</i>
</div>
</div>
<div class="v-input__control">
<div class="v-input__slot">
<label for="input-68"
class="v-label theme--light"
style="left: 0px; position: relative;"
>
foo
</label>
</div>
<div class="v-messages theme--light">
<span name="message-transition"
tag="div"
class="v-messages__wrapper"
>
<div class="v-messages__message">
bar
</div>
</span>
</div>
</div>
<div class="v-input__append-outer">
<div class="v-input__icon v-input__icon--append">
<i aria-hidden="true"
class="v-icon notranslate material-icons theme--light"
>
append
</i>
</div>
</div>
</div>
`;
exports[`VInput.ts should be in an error state 1`] = `
<div class="v-input v-input--has-state theme--light error--text">
<div class="v-input__control">
<div class="v-input__slot">
</div>
<div class="v-messages theme--light error--text">
<span name="message-transition"
tag="div"
class="v-messages__wrapper"
>
</span>
</div>
</div>
</div>
`;
exports[`VInput.ts should be in an error state 2`] = `
<div class="v-input v-input--has-state theme--light error--text">
<div class="v-input__control">
<div class="v-input__slot">
</div>
<div class="v-messages theme--light error--text"
role="alert"
>
<span name="message-transition"
tag="div"
class="v-messages__wrapper"
>
<div class="v-messages__message">
required
</div>
</span>
</div>
</div>
</div>
`;
exports[`VInput.ts should generate an icon and match snapshot 1`] = `
<div class="v-input theme--light">
<div class="v-input__prepend-outer">
<div class="v-input__icon v-input__icon--prepend">
<i aria-hidden="true"
class="v-icon notranslate material-icons theme--light"
>
list
</i>
</div>
</div>
<div class="v-input__control">
<div class="v-input__slot">
</div>
<div class="v-messages theme--light">
<span name="message-transition"
tag="div"
class="v-messages__wrapper"
>
</span>
</div>
</div>
</div>
`;
exports[`VInput.ts should generate an icon and match snapshot 2`] = `
<div class="v-input theme--light">
<div class="v-input__control">
<div class="v-input__slot">
</div>
<div class="v-messages theme--light">
<span name="message-transition"
tag="div"
class="v-messages__wrapper"
>
</span>
</div>
</div>
<div class="v-input__append-outer">
<div class="v-input__icon v-input__icon--append">
<i aria-hidden="true"
class="v-icon notranslate material-icons theme--light"
>
list
</i>
</div>
</div>
</div>
`;
exports[`VInput.ts should generate append and prepend slots 1`] = `
<div class="v-input theme--light">
<div class="v-input__control">
<div class="v-input__slot">
</div>
<div class="v-messages theme--light">
<span name="message-transition"
tag="div"
class="v-messages__wrapper"
>
</span>
</div>
</div>
<div class="v-input__append-outer">
<div>
append
</div>
</div>
</div>
`;
exports[`VInput.ts should generate append and prepend slots 2`] = `
<div class="v-input theme--light">
<div class="v-input__prepend-outer">
<div>
prepend
</div>
</div>
<div class="v-input__control">
<div class="v-input__slot">
</div>
<div class="v-messages theme--light">
<span name="message-transition"
tag="div"
class="v-messages__wrapper"
>
</span>
</div>
</div>
</div>
`;
exports[`VInput.ts should not apply attrs to element 1`] = `
<div class="v-input theme--light">
<div class="v-input__control">
<div class="v-input__slot">
</div>
<div class="v-messages theme--light">
<span name="message-transition"
tag="div"
class="v-messages__wrapper"
>
</span>
</div>
</div>
</div>
`;
exports[`VInput.ts should not generate input details 1`] = `
<div class="v-input v-input--hide-details theme--light">
<div class="v-input__control">
<div class="v-input__slot">
</div>
</div>
</div>
`;
exports[`VInput.ts should render a label 1`] = `
<div class="v-input theme--light">
<div class="v-input__control">
<div class="v-input__slot">
<label for="input-59"
class="v-label theme--light"
style="left: 0px; position: relative;"
>
foo
</label>
</div>
<div class="v-messages theme--light">
<span name="message-transition"
tag="div"
class="v-messages__wrapper"
>
</span>
</div>
</div>
</div>
`;
exports[`VInput.ts should render a label 2`] = `
<div class="v-input theme--light">
<div class="v-input__control">
<div class="v-input__slot">
<label for="input-63"
class="v-label theme--light"
style="left: 0px; position: relative;"
>
<div>
foo
</div>
</label>
</div>
<div class="v-messages theme--light">
<span name="message-transition"
tag="div"
class="v-messages__wrapper"
>
</span>
</div>
</div>
</div>
`;
+14
View File
@@ -0,0 +1,14 @@
@import '../../styles/styles.sass';
$input-font-size: 16px !default;
$input-letter-spacing: normal !default;
$input-text-align: left !default;
$input-max-height: 32px !default;
$input-label-height: 20px !default;
$input-label-letter-spacing: 20px !default;
$input-prepend-append-outer-margin: 9px !default;
$input-icon-height: 24px !default;
$input-icon-min-width: 24px !default;
$input-icon-width: 24px !default;
$input-slot-margin-bottom: 8px !default;
$input-dense-slot-margin-bottom: 4px !default;
+4
View File
@@ -0,0 +1,4 @@
import VInput from './VInput'
export { VInput }
export default VInput