2018-05-05 09:19:28 +00:00
|
|
|
|
|
|
|
|
|
import PropTypes from '../../_util/vue-types'
|
|
|
|
|
import classNames from 'classnames'
|
2018-09-05 13:28:54 +00:00
|
|
|
|
import { getOptionProps, hasProp, initDefaultProps, getAttrs } from '../../_util/props-util'
|
2018-05-05 09:19:28 +00:00
|
|
|
|
import BaseMixin from '../../_util/BaseMixin'
|
|
|
|
|
|
|
|
|
|
export default {
|
|
|
|
|
name: 'Checkbox',
|
|
|
|
|
mixins: [BaseMixin],
|
2018-09-05 13:28:54 +00:00
|
|
|
|
inheritAttrs: false,
|
2018-05-05 09:19:28 +00:00
|
|
|
|
props: initDefaultProps({
|
|
|
|
|
prefixCls: PropTypes.string,
|
|
|
|
|
name: PropTypes.string,
|
|
|
|
|
id: PropTypes.string,
|
|
|
|
|
type: PropTypes.string,
|
|
|
|
|
defaultChecked: PropTypes.oneOfType([PropTypes.number, PropTypes.bool]),
|
|
|
|
|
checked: PropTypes.oneOfType([PropTypes.number, PropTypes.bool]),
|
|
|
|
|
disabled: PropTypes.bool,
|
|
|
|
|
// onFocus: PropTypes.func,
|
|
|
|
|
// onBlur: PropTypes.func,
|
|
|
|
|
// onChange: PropTypes.func,
|
|
|
|
|
// onClick: PropTypes.func,
|
2018-09-05 13:28:54 +00:00
|
|
|
|
tabIndex: PropTypes.string,
|
|
|
|
|
readOnly: PropTypes.bool,
|
|
|
|
|
autoFocus: PropTypes.bool,
|
2018-05-05 09:19:28 +00:00
|
|
|
|
value: PropTypes.any,
|
|
|
|
|
}, {
|
|
|
|
|
prefixCls: 'rc-checkbox',
|
|
|
|
|
type: 'checkbox',
|
|
|
|
|
defaultChecked: false,
|
|
|
|
|
}),
|
2018-09-05 13:28:54 +00:00
|
|
|
|
model: {
|
|
|
|
|
prop: 'checked',
|
|
|
|
|
event: 'change',
|
|
|
|
|
},
|
2018-05-05 09:19:28 +00:00
|
|
|
|
data () {
|
2018-09-05 13:28:54 +00:00
|
|
|
|
const checked = hasProp(this, 'checked')
|
|
|
|
|
? this.checked
|
|
|
|
|
: this.defaultChecked
|
2018-05-05 09:19:28 +00:00
|
|
|
|
return {
|
|
|
|
|
sChecked: checked,
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
watch: {
|
|
|
|
|
checked (val) {
|
|
|
|
|
this.sChecked = val
|
|
|
|
|
},
|
|
|
|
|
},
|
2018-09-05 13:28:54 +00:00
|
|
|
|
mounted () {
|
|
|
|
|
this.$nextTick(() => {
|
|
|
|
|
if (this.autoFocus) {
|
|
|
|
|
this.$refs.input && this.$refs.input.focus()
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
},
|
2018-05-05 09:19:28 +00:00
|
|
|
|
methods: {
|
|
|
|
|
focus () {
|
|
|
|
|
this.$refs.input.focus()
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
blur () {
|
|
|
|
|
this.$refs.input.blur()
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
handleChange (e) {
|
|
|
|
|
const props = getOptionProps(this)
|
|
|
|
|
if (props.disabled) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if (!('checked' in props)) {
|
|
|
|
|
this.sChecked = e.target.checked
|
|
|
|
|
}
|
2018-09-05 13:28:54 +00:00
|
|
|
|
this.$forceUpdate() // change前,维持现有状态
|
|
|
|
|
this.__emit('change', {
|
2018-05-05 09:19:28 +00:00
|
|
|
|
target: {
|
|
|
|
|
...props,
|
|
|
|
|
checked: e.target.checked,
|
|
|
|
|
},
|
|
|
|
|
stopPropagation () {
|
|
|
|
|
e.stopPropagation()
|
|
|
|
|
},
|
|
|
|
|
preventDefault () {
|
|
|
|
|
e.preventDefault()
|
|
|
|
|
},
|
2018-09-05 13:28:54 +00:00
|
|
|
|
nativeEvent: { ...e, shiftKey: this.eventShiftKey },
|
2018-05-05 09:19:28 +00:00
|
|
|
|
})
|
2018-09-05 13:28:54 +00:00
|
|
|
|
this.eventShiftKey = false
|
|
|
|
|
},
|
|
|
|
|
onClick (e) {
|
|
|
|
|
this.__emit('click', e)
|
|
|
|
|
// onChange没能获取到shiftKey,使用onClick hack
|
|
|
|
|
this.eventShiftKey = e.shiftKey
|
2018-05-05 09:19:28 +00:00
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
render () {
|
|
|
|
|
const {
|
|
|
|
|
prefixCls,
|
|
|
|
|
name,
|
|
|
|
|
id,
|
|
|
|
|
type,
|
|
|
|
|
disabled,
|
|
|
|
|
readOnly,
|
|
|
|
|
tabIndex,
|
|
|
|
|
autoFocus,
|
|
|
|
|
value,
|
|
|
|
|
...others
|
2018-09-05 13:28:54 +00:00
|
|
|
|
} = getOptionProps(this)
|
|
|
|
|
const attrs = getAttrs(this)
|
|
|
|
|
const globalProps = Object.keys({ ...others, ...attrs }).reduce((prev, key) => {
|
2018-05-05 09:19:28 +00:00
|
|
|
|
if (key.substr(0, 5) === 'aria-' || key.substr(0, 5) === 'data-' || key === 'role') {
|
|
|
|
|
prev[key] = others[key]
|
|
|
|
|
}
|
|
|
|
|
return prev
|
|
|
|
|
}, {})
|
|
|
|
|
|
2018-09-05 13:28:54 +00:00
|
|
|
|
const { sChecked } = this
|
2018-05-05 09:19:28 +00:00
|
|
|
|
const classString = classNames(prefixCls, {
|
2018-09-05 13:28:54 +00:00
|
|
|
|
[`${prefixCls}-checked`]: sChecked,
|
2018-05-05 09:19:28 +00:00
|
|
|
|
[`${prefixCls}-disabled`]: disabled,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<span class={classString}>
|
|
|
|
|
<input
|
|
|
|
|
name={name}
|
|
|
|
|
id={id}
|
|
|
|
|
type={type}
|
|
|
|
|
readOnly={readOnly}
|
|
|
|
|
disabled={disabled}
|
|
|
|
|
tabIndex={tabIndex}
|
|
|
|
|
class={`${prefixCls}-input`}
|
2018-09-05 13:28:54 +00:00
|
|
|
|
checked={!!sChecked}
|
2018-05-05 09:19:28 +00:00
|
|
|
|
autoFocus={autoFocus}
|
2018-09-05 13:28:54 +00:00
|
|
|
|
ref='input'
|
2018-05-05 09:19:28 +00:00
|
|
|
|
value={value}
|
2018-09-05 13:28:54 +00:00
|
|
|
|
{...{
|
|
|
|
|
attrs: globalProps,
|
|
|
|
|
on: {
|
|
|
|
|
...this.$listeners,
|
|
|
|
|
change: this.handleChange,
|
|
|
|
|
click: this.onClick,
|
|
|
|
|
}}
|
|
|
|
|
}
|
2018-05-05 09:19:28 +00:00
|
|
|
|
/>
|
|
|
|
|
<span class={`${prefixCls}-inner`} />
|
|
|
|
|
</span>
|
|
|
|
|
)
|
|
|
|
|
},
|
|
|
|
|
}
|