Skip to content

Commit

Permalink
refactor(VInput): convert to typescript (#5852)
Browse files Browse the repository at this point in the history
* refactor(VInput): convert to typescript

* style(VInput): remove unnecessary type declaration

* style: removed empty lines

* style(VInput): move type declaration

* style(VInput): improve typings

* style(VInput): reorder genDefaultSlot children

* style: revert changes made to genControl style
  • Loading branch information
johnleider committed Dec 14, 2018
1 parent 16afd16 commit 2a81ad0
Show file tree
Hide file tree
Showing 14 changed files with 99 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ export default {
VSelect.methods.clearableCallback.call(this)
},
genInput () {
const input = VTextField.methods.genInput.call(this)
const input = VTextField.options.methods.genInput.call(this)

input.data.attrs.role = 'combobox'
input.data.domProps.value = this.internalSearch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,30 @@ import {
kebabCase
} from '../../util/helpers'
import { deprecate } from '../../util/console'
import mixins, { ExtractVue } from '../../util/mixins'

/* @vue/component */
export default {
name: 'v-input',
// Types
import Vue, { VNode, VNodeData } from 'vue'
interface options extends Vue {
/* eslint-disable-next-line camelcase */
$_modelEvent: string
}

mixins: [
Colorable,
Themeable,
Validatable
],
export default mixins<options &
/* eslint-disable indent */
ExtractVue<[
typeof Colorable,
typeof Themeable,
typeof Validatable
]>
/* eslint-enable indent */
>(
Colorable,
Themeable,
Validatable
/* @vue/component */
).extend({
name: 'v-input',

props: {
appendIcon: String,
Expand All @@ -40,18 +54,25 @@ export default {
hideDetails: Boolean,
hint: String,
label: String,
loading: Boolean,
persistentHint: Boolean,
prependIcon: String,
/** @deprecated */
prependIconCb: Function
prependIconCb: Function,
value: { required: false }
},

data: vm => ({
hasMouseDown: false
}),
data () {
return {
attrsInput: {},
lazyValue: this.value as any,
hasMouseDown: false
}
},

computed: {
classesInput () {
classes: () => ({}),
classesInput (): object {
return {
...this.classes,
'v-input--has-state': this.hasState,
Expand Down Expand Up @@ -84,7 +105,7 @@ export default {
get () {
return this.lazyValue
},
set (val) {
set (val: any) {
this.lazyValue = val
this.$emit(this.$_modelEvent, val)
}
Expand All @@ -100,6 +121,12 @@ export default {
}
},

watch: {
value (val) {
this.lazyValue = val
}
},

beforeCreate () {
// v-radio-group needs to emit a different event
// https://github.com/vuetifyjs/vuetify/issues/4752
Expand Down Expand Up @@ -129,26 +156,30 @@ export default {
]
},
// TODO: remove shouldDeprecate (2.0), used for clearIcon
genIcon (type, cb, shouldDeprecate = true) {
const icon = this[`${type}Icon`]
genIcon (
type: string,
cb?: (e: Event) => void,
shouldDeprecate = true
) {
const icon = (this as any)[`${type}Icon`]
const eventName = `click:${kebabCase(type)}`
cb = cb || this[`${type}IconCb`]
cb = cb || (this as any)[`${type}IconCb`]

if (shouldDeprecate && type && cb) {
deprecate(`:${type}-icon-cb`, `@${eventName}`, this)
}

const data = {
const data: VNodeData = {
props: {
color: this.validationState,
dark: this.dark,
disabled: this.disabled,
light: this.light
},
on: !(this.$listeners[eventName] || cb)
? null
? undefined
: {
click: e => {
click: (e: Event) => {
e.preventDefault()
e.stopPropagation()

Expand All @@ -157,7 +188,7 @@ export default {
},
// Container has mouseup event that will
// trigger menu open if enclosed
mouseup: e => {
mouseup: (e: Event) => {
e.preventDefault()
e.stopPropagation()
}
Expand Down Expand Up @@ -217,7 +248,11 @@ export default {
}
})
},
genSlot (type, location, slot) {
genSlot (
type: string,
location: string,
slot: (VNode | VNode[])[]
) {
if (!slot.length) return null

const ref = `${type}-${location}`
Expand All @@ -230,8 +265,8 @@ export default {
genPrependSlot () {
const slot = []

if (this.$slots['prepend']) {
slot.push(this.$slots['prepend'])
if (this.$slots.prepend) {
slot.push(this.$slots.prepend)
} else if (this.prependIcon) {
slot.push(this.genIcon('prepend'))
}
Expand All @@ -245,32 +280,32 @@ export default {
// 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'])
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) {
onClick (e: Event) {
this.$emit('click', e)
},
onMouseDown (e) {
onMouseDown (e: Event) {
this.hasMouseDown = true
this.$emit('mousedown', e)
},
onMouseUp (e) {
onMouseUp (e: Event) {
this.hasMouseDown = false
this.$emit('mouseup', e)
}
},

render (h) {
render (h): VNode {
return h('div', this.setTextColor(this.validationState, {
staticClass: 'v-input',
attrs: this.attrsInput,
'class': this.classesInput
}), this.genContent())
}
}
})
4 changes: 2 additions & 2 deletions packages/vuetify/src/components/VOverflowBtn/VOverflowBtn.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export default {
: VSelect.methods.genCommaSelection.call(this, item, index, last)
},
genInput () {
const input = VTextField.methods.genInput.call(this)
const input = VTextField.options.methods.genInput.call(this)

input.data.domProps.value = this.editable ? this.internalSearch : ''
input.data.attrs.readonly = !this.isAnyValueAllowed
Expand All @@ -69,7 +69,7 @@ export default {
genLabel () {
if (this.editable && this.isFocused) return null

const label = VTextField.methods.genLabel.call(this)
const label = VTextField.options.methods.genLabel.call(this)

if (!label) return label

Expand Down
2 changes: 1 addition & 1 deletion packages/vuetify/src/components/VRadioGroup/VRadio.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export default {
// We can't actually use the mixin directly because
// it's made for standalone components, but its
// genInput method is exactly what we need
return Selectable.methods.genInput.call(this, ...args)
return Selectable.options.methods.genInput.call(this, ...args)
},
genLabel () {
return this.$createElement(VLabel, {
Expand Down
8 changes: 3 additions & 5 deletions packages/vuetify/src/components/VRadioGroup/VRadioGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@ import {
} from '../../mixins/registrable'

/* @vue/component */
export default {
export default VInput.extend({
name: 'v-radio-group',

extends: VInput,

mixins: [
Comparable,
RegistrableProvide('radio')
Expand Down Expand Up @@ -88,7 +86,7 @@ export default {
attrs: {
role: 'radiogroup'
}
}, VInput.methods.genDefaultSlot.call(this))
}, VInput.options.methods.genDefaultSlot.call(this))
},
onRadioChange (value) {
if (this.disabled) return
Expand Down Expand Up @@ -131,4 +129,4 @@ export default {
if (index > -1) this.radios.splice(index, 1)
}
}
}
})
10 changes: 5 additions & 5 deletions packages/vuetify/src/components/VRangeSlider/VRangeSlider.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default {
classes () {
return Object.assign({}, {
'v-input--range-slider': true
}, VSlider.computed.classes.call(this))
}, VSlider.options.computed.classes.call(this))
},
internalValue: {
get () {
Expand Down Expand Up @@ -67,7 +67,7 @@ export default {
return this.internalValue.some(v => v !== this.min) || this.alwaysDirty
},
trackFillStyles () {
const styles = VSlider.computed.trackFillStyles.call(this)
const styles = VSlider.options.computed.trackFillStyles.call(this)
const fillPercent = Math.abs(this.inputWidth[0] - this.inputWidth[1])

styles.width = `calc(${fillPercent}% - ${this.trackPadding}px)`
Expand All @@ -80,7 +80,7 @@ export default {
this.internalValue[0]
) return 0

return VSlider.computed.trackPadding.call(this)
return VSlider.options.computed.trackPadding.call(this)
}
},

Expand All @@ -91,13 +91,13 @@ export default {
},
genInput () {
return createRange(2).map(i => {
const input = VSlider.methods.genInput.call(this)
const input = VSlider.options.methods.genInput.call(this)

input.data.attrs.value = this.internalValue[i]

input.data.on.focus = e => {
this.activeThumb = i
VSlider.methods.onFocus.call(this, e)
VSlider.options.methods.onFocus.call(this, e)
}

return input
Expand Down
6 changes: 3 additions & 3 deletions packages/vuetify/src/components/VSelect/VSelect.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export default {
return this.filterDuplicates(this.cachedItems.concat(this.items))
},
classes () {
return Object.assign({}, VTextField.computed.classes.call(this), {
return Object.assign({}, VTextField.options.computed.classes.call(this), {
'v-select': true,
'v-select--chips': this.hasChips,
'v-select--chips--small': this.smallChips,
Expand Down Expand Up @@ -384,7 +384,7 @@ export default {
]
},
genInput () {
const input = VTextField.methods.genInput.call(this)
const input = VTextField.options.methods.genInput.call(this)

input.data.domProps.value = null
input.data.attrs.readonly = true
Expand Down Expand Up @@ -607,7 +607,7 @@ export default {
}
}

VTextField.methods.onMouseUp.call(this, e)
VTextField.options.methods.onMouseUp.call(this, e)
},
onScroll () {
if (!this.isMenuActive) {
Expand Down
6 changes: 2 additions & 4 deletions packages/vuetify/src/components/VSlider/VSlider.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,11 @@ import { consoleWarn } from '../../util/console'
import Loadable from '../../mixins/loadable'

/* @vue/component */
export default {
export default VInput.extend({
name: 'v-slider',

directives: { ClickOutside },

extends: VInput,

mixins: [Loadable],

props: {
Expand Down Expand Up @@ -520,4 +518,4 @@ export default {
this.internalValue = value
}
}
}
})

0 comments on commit 2a81ad0

Please sign in to comment.