2018-03-19 02:16:27 +00:00
|
|
|
|
2017-12-07 04:31:12 +00:00
|
|
|
import omit from 'omit.js'
|
|
|
|
import inputProps from './inputProps'
|
|
|
|
import calculateNodeHeight from './calculateNodeHeight'
|
2018-01-12 08:10:41 +00:00
|
|
|
import hasProp from '../_util/props-util'
|
2017-12-07 04:31:12 +00:00
|
|
|
|
|
|
|
function onNextFrame (cb) {
|
|
|
|
if (window.requestAnimationFrame) {
|
|
|
|
return window.requestAnimationFrame(cb)
|
|
|
|
}
|
|
|
|
return window.setTimeout(cb, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
function clearNextFrameAction (nextFrameId) {
|
|
|
|
if (window.cancelAnimationFrame) {
|
|
|
|
window.cancelAnimationFrame(nextFrameId)
|
|
|
|
} else {
|
|
|
|
window.clearTimeout(nextFrameId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function fixControlledValue (value) {
|
|
|
|
if (typeof value === 'undefined' || value === null) {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
2018-04-08 13:17:20 +00:00
|
|
|
name: 'ATextarea',
|
2017-12-07 04:31:12 +00:00
|
|
|
props: {
|
|
|
|
...inputProps,
|
|
|
|
autosize: [Object, Boolean],
|
|
|
|
},
|
|
|
|
model: {
|
|
|
|
prop: 'value',
|
|
|
|
event: 'change.value',
|
|
|
|
},
|
|
|
|
data () {
|
|
|
|
const { value, defaultValue } = this.$props
|
|
|
|
return {
|
2017-12-27 10:15:11 +00:00
|
|
|
stateValue: fixControlledValue(!hasProp(this, 'value') ? defaultValue : value),
|
2017-12-07 04:31:12 +00:00
|
|
|
nextFrameActionId: undefined,
|
|
|
|
textareaStyles: {},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
},
|
|
|
|
watch: {
|
|
|
|
value (val) {
|
|
|
|
this.stateValue = fixControlledValue(val)
|
|
|
|
if (this.nextFrameActionId) {
|
|
|
|
clearNextFrameAction(this.nextFrameActionId)
|
|
|
|
}
|
|
|
|
this.nextFrameActionId = onNextFrame(this.resizeTextarea)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
mounted () {
|
2018-06-10 02:26:03 +00:00
|
|
|
this.$nextTick(() => {
|
|
|
|
this.resizeTextarea()
|
|
|
|
if (this.autoFocus) {
|
|
|
|
this.focus()
|
|
|
|
}
|
|
|
|
})
|
2017-12-07 04:31:12 +00:00
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
handleKeyDown (e) {
|
|
|
|
if (e.keyCode === 13) {
|
|
|
|
this.$emit('pressEnter', e)
|
|
|
|
}
|
|
|
|
this.$emit('keydown', e)
|
|
|
|
},
|
|
|
|
resizeTextarea () {
|
|
|
|
const { autosize } = this.$props
|
|
|
|
if (!autosize || !this.$refs.textArea) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const minRows = autosize ? autosize.minRows : null
|
|
|
|
const maxRows = autosize ? autosize.maxRows : null
|
|
|
|
const textareaStyles = calculateNodeHeight(this.$refs.textArea, false, minRows, maxRows)
|
|
|
|
this.textareaStyles = textareaStyles
|
|
|
|
},
|
|
|
|
|
|
|
|
getTextAreaClassName () {
|
|
|
|
const { prefixCls, disabled } = this.$props
|
|
|
|
return {
|
|
|
|
[prefixCls]: true,
|
|
|
|
[`${prefixCls}-disabled`]: disabled,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
handleTextareaChange (e) {
|
2017-12-27 10:15:11 +00:00
|
|
|
if (!hasProp(this, 'value')) {
|
2017-12-07 04:31:12 +00:00
|
|
|
this.stateValue = e.target.value
|
|
|
|
this.$nextTick(() => {
|
|
|
|
this.resizeTextarea()
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
this.$forceUpdate()
|
2018-07-26 05:25:02 +00:00
|
|
|
}
|
|
|
|
if (!e.target.composing) {
|
2017-12-07 04:31:12 +00:00
|
|
|
this.$emit('change.value', e.target.value)
|
|
|
|
}
|
2018-07-26 05:25:02 +00:00
|
|
|
this.$emit('change', e)
|
2018-02-28 11:07:04 +00:00
|
|
|
this.$emit('input', e)
|
2017-12-07 04:31:12 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
focus () {
|
|
|
|
this.$refs.textArea.focus()
|
|
|
|
},
|
|
|
|
|
|
|
|
blur () {
|
|
|
|
this.$refs.textArea.blur()
|
|
|
|
},
|
|
|
|
|
|
|
|
},
|
|
|
|
render () {
|
2018-02-28 11:07:04 +00:00
|
|
|
const { stateValue,
|
|
|
|
getTextAreaClassName,
|
|
|
|
handleKeyDown,
|
|
|
|
handleTextareaChange,
|
|
|
|
textareaStyles,
|
|
|
|
$attrs,
|
|
|
|
$listeners,
|
|
|
|
} = this
|
2017-12-07 04:31:12 +00:00
|
|
|
const otherProps = omit(this.$props, [
|
|
|
|
'prefixCls',
|
|
|
|
'autosize',
|
|
|
|
'type',
|
|
|
|
])
|
2018-02-28 11:07:04 +00:00
|
|
|
const textareaProps = {
|
|
|
|
attrs: { ...otherProps, ...$attrs },
|
|
|
|
on: {
|
|
|
|
...$listeners,
|
|
|
|
keydown: handleKeyDown,
|
|
|
|
input: handleTextareaChange,
|
|
|
|
},
|
2017-12-07 04:31:12 +00:00
|
|
|
}
|
2018-07-26 05:25:02 +00:00
|
|
|
if ($listeners['change.value']) {
|
|
|
|
textareaProps.directives = [{ name: 'ant-input' }]
|
|
|
|
}
|
2017-12-07 04:31:12 +00:00
|
|
|
return (
|
|
|
|
<textarea
|
2018-02-28 11:07:04 +00:00
|
|
|
{...textareaProps}
|
2017-12-07 04:31:12 +00:00
|
|
|
value={stateValue}
|
|
|
|
class={getTextAreaClassName()}
|
|
|
|
style={textareaStyles}
|
|
|
|
ref='textArea'
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
},
|
|
|
|
}
|
2018-03-19 02:16:27 +00:00
|
|
|
|