add hasProp

pull/9/head
tangjinzhou 2017-12-27 18:15:11 +08:00
parent 7d52bd4852
commit 68f1bc65fc
11 changed files with 50 additions and 33 deletions

View File

@ -0,0 +1,5 @@
export default (instance, prop) => {
const $options = instance.$options || {}
const propsData = $options.propsData || {}
return prop in propsData
}

View File

@ -13,6 +13,7 @@
</label> </label>
</template> </template>
<script> <script>
import hasProp from '../_util/hasProp'
export default { export default {
name: 'Checkbox', name: 'Checkbox',
props: { props: {
@ -42,7 +43,7 @@ export default {
} }
return { return {
stateChecked: stateChecked === undefined stateChecked: stateChecked === undefined
? checked === undefined ? defaultChecked : checked ? !hasProp(this, 'checked') ? defaultChecked : checked
: stateChecked, : stateChecked,
} }
}, },

View File

@ -8,6 +8,7 @@
</template> </template>
<script> <script>
import Checkbox from './Checkbox.vue' import Checkbox from './Checkbox.vue'
import hasProp from '../_util/hasProp'
export default { export default {
name: 'CheckboxGroup', name: 'CheckboxGroup',
props: { props: {
@ -60,7 +61,7 @@ export default {
handleChange (event) { handleChange (event) {
const target = event.target const target = event.target
const { value: targetValue, checked } = target const { value: targetValue, checked } = target
const { stateValue, value } = this const { stateValue } = this
let newVal = [] let newVal = []
if (checked) { if (checked) {
newVal = [...stateValue, targetValue] newVal = [...stateValue, targetValue]
@ -70,7 +71,7 @@ export default {
index >= 0 && newVal.splice(index, 1) index >= 0 && newVal.splice(index, 1)
} }
newVal = [...new Set(newVal)] newVal = [...new Set(newVal)]
if (value === undefined) { if (!hasProp(this, 'value')) {
this.stateValue = newVal this.stateValue = newVal
} }
this.$emit('input', newVal) this.$emit('input', newVal)

View File

@ -2,6 +2,7 @@
import TextArea from './TextArea' import TextArea from './TextArea'
import omit from 'omit.js' import omit from 'omit.js'
import inputProps from './inputProps' import inputProps from './inputProps'
import hasProp from '../_util/hasProp'
function fixControlledValue (value) { function fixControlledValue (value) {
if (typeof value === 'undefined' || value === null) { if (typeof value === 'undefined' || value === null) {
@ -22,7 +23,7 @@ export default {
data () { data () {
const { value, defaultValue } = this.$props const { value, defaultValue } = this.$props
return { return {
stateValue: fixControlledValue(value === undefined ? defaultValue : value), stateValue: fixControlledValue(!hasProp(this, 'value') ? defaultValue : value),
} }
}, },
computed: { computed: {
@ -40,8 +41,7 @@ export default {
this.$emit('keydown', e) this.$emit('keydown', e)
}, },
handleChange (e) { handleChange (e) {
const { value } = this.$props if (!hasProp(this, 'value')) {
if (value === undefined) {
this.stateValue = e.target.value this.stateValue = e.target.value
} else { } else {
this.$forceUpdate() this.$forceUpdate()

View File

@ -1,9 +1,8 @@
<script> <script>
import omit from 'omit.js' import omit from 'omit.js'
import inputProps from './inputProps' import inputProps from './inputProps'
import calculateNodeHeight from './calculateNodeHeight' import calculateNodeHeight from './calculateNodeHeight'
import hasProp from '../_util/hasProp'
function onNextFrame (cb) { function onNextFrame (cb) {
if (window.requestAnimationFrame) { if (window.requestAnimationFrame) {
@ -39,7 +38,7 @@ export default {
data () { data () {
const { value, defaultValue } = this.$props const { value, defaultValue } = this.$props
return { return {
stateValue: fixControlledValue(value === undefined ? defaultValue : value), stateValue: fixControlledValue(!hasProp(this, 'value') ? defaultValue : value),
nextFrameActionId: undefined, nextFrameActionId: undefined,
textareaStyles: {}, textareaStyles: {},
} }
@ -85,8 +84,7 @@ export default {
}, },
handleTextareaChange (e) { handleTextareaChange (e) {
const { value } = this.$props if (!hasProp(this, 'value')) {
if (value === undefined) {
this.stateValue = e.target.value this.stateValue = e.target.value
this.$nextTick(() => { this.$nextTick(() => {
this.resizeTextarea() this.resizeTextarea()

View File

@ -1,11 +1,10 @@
<script> <script>
import PropTypes from 'vue-types' import PropTypes from '../../_util/vue-types'
import MenuMixin from './MenuMixin' import MenuMixin from './MenuMixin'
import StateMixin from '../../_util/StateMixin' import StateMixin from '../../_util/StateMixin'
const Menu = { const Menu = {
name: 'Menu', name: 'Menu',
props: { props: {
defaultSelectedKeys: PropTypes.arrayOf(PropTypes.string).def([]), defaultSelectedKeys: PropTypes.arrayOf(PropTypes.string).def([]),
selectedKeys: PropTypes.arrayOf(PropTypes.string), selectedKeys: PropTypes.arrayOf(PropTypes.string),
@ -38,16 +37,20 @@ const Menu = {
isRootMenu: true, isRootMenu: true,
} }
}, },
watch: {
componentWillReceiveProps (nextProps) { '$props': {
const props = {} handler: function (nextProps) {
if ('selectedKeys' in nextProps) { const props = {}
props.selectedKeys = nextProps.selectedKeys || [] if (nextProps.selectedKeys === undefined) {
} props.selectedKeys = nextProps.selectedKeys || []
if ('openKeys' in nextProps) { }
props.openKeys = nextProps.openKeys || [] if (nextProps.openKeys === undefined) {
} props.openKeys = nextProps.openKeys || []
this.setState(props) }
this.setState(props)
},
deep: true,
},
}, },
methods: { methods: {
onDestroy (key) { onDestroy (key) {

View File

@ -13,6 +13,7 @@
</label> </label>
</template> </template>
<script> <script>
import hasProp from '../_util/hasProp'
export default { export default {
name: 'Radio', name: 'Radio',
props: { props: {
@ -41,7 +42,7 @@ export default {
} }
return { return {
stateChecked: stateChecked === undefined stateChecked: stateChecked === undefined
? checked === undefined ? defaultChecked : checked ? !hasProp(this, 'checked') ? defaultChecked : checked
: stateChecked, : stateChecked,
} }
}, },
@ -70,8 +71,8 @@ export default {
handleChange (event) { handleChange (event) {
const targetChecked = event.target.checked const targetChecked = event.target.checked
this.$emit('input', targetChecked) this.$emit('input', targetChecked)
const { name, value, checked, radioGroupContext, stateChecked } = this const { name, value, radioGroupContext, stateChecked } = this
if ((checked === undefined && !radioGroupContext) || (radioGroupContext && radioGroupContext.value === undefined)) { if ((!hasProp(this, 'checked') && !radioGroupContext) || (radioGroupContext && radioGroupContext.value === undefined)) {
this.stateChecked = targetChecked this.stateChecked = targetChecked
} }
const target = { const target = {

View File

@ -3,6 +3,7 @@ import Star from './Star.vue'
import Icon from '../icon' import Icon from '../icon'
import { getOffsetLeft } from './util' import { getOffsetLeft } from './util'
import { cloneVNodes } from '../_util/vnode' import { cloneVNodes } from '../_util/vnode'
import hasProp from '../_util/hasProp'
export default { export default {
name: 'Rate', name: 'Rate',
@ -35,7 +36,7 @@ export default {
}, },
data () { data () {
const { value, defaultValue } = this const { value, defaultValue } = this
const reValue = value === undefined ? defaultValue : value const reValue = !hasProp(this, 'value') ? defaultValue : value
return { return {
hoverValue: undefined, hoverValue: undefined,
stateValue: reValue, stateValue: reValue,
@ -59,7 +60,10 @@ export default {
methods: { methods: {
onClick (event, index) { onClick (event, index) {
const value = this.getStarValue(index, event.pageX) const value = this.getStarValue(index, event.pageX)
this.stateValue = value if (!hasProp(this, 'value')) {
this.stateValue = value
}
this.onMouseLeave() this.onMouseLeave()
this.$emit('input', value) this.$emit('input', value)
this.$emit('change', value) this.$emit('change', value)

View File

@ -3,6 +3,7 @@ import Icon from '../icon'
import KeyCode from './KeyCode' import KeyCode from './KeyCode'
import TabContent from './TabContent' import TabContent from './TabContent'
import ScrollableInkTabBar from './ScrollableInkTabBar' import ScrollableInkTabBar from './ScrollableInkTabBar'
import hasProp from '../_util/hasProp'
function getDefaultActiveKey (t) { function getDefaultActiveKey (t) {
let activeKey let activeKey
t.$slots.default && t.$slots.default.forEach(({ componentOptions = {}, key: tabKey }) => { t.$slots.default && t.$slots.default.forEach(({ componentOptions = {}, key: tabKey }) => {
@ -108,7 +109,7 @@ export default {
setActiveKey (activeKey) { setActiveKey (activeKey) {
if (this.stateActiveKey !== activeKey) { if (this.stateActiveKey !== activeKey) {
if (!this.activeKey) { if (!hasProp(this, 'activeKey')) {
this.stateActiveKey = activeKey this.stateActiveKey = activeKey
} }
this.onChange(activeKey) this.onChange(activeKey)

View File

@ -1,6 +1,7 @@
<script> <script>
import Tabs from './Tabs' import Tabs from './Tabs'
import isFlexSupported from '../_util/isFlexSupported' import isFlexSupported from '../_util/isFlexSupported'
import hasProp from '../_util/hasProp'
export default { export default {
props: { props: {
prefixCls: { type: String, default: 'ant-tabs' }, prefixCls: { type: String, default: 'ant-tabs' },
@ -135,7 +136,6 @@ export default {
tabBarProps: tabBarProps, tabBarProps: tabBarProps,
tabContentProps: tabContentProps, tabContentProps: tabContentProps,
destroyInactiveTabPane, destroyInactiveTabPane,
activeKey,
defaultActiveKey, defaultActiveKey,
type, type,
onTabClick: this.onTabClick, onTabClick: this.onTabClick,
@ -146,6 +146,9 @@ export default {
}, },
}, },
} }
if (hasProp(this, 'activeKey')) {
tabsProps.props.activeKey = activeKey
}
return ( return (
<Tabs <Tabs
class={cls} class={cls}

View File

@ -1,6 +1,7 @@
<script> <script>
import PropTypes from '../_util/vue-types' import PropTypes from '../_util/vue-types'
import contains from '../_util/Dom/contains' import contains from '../_util/Dom/contains'
import hasProp from '../_util/hasProp'
import addEventListener from '../_util/Dom/addEventListener' import addEventListener from '../_util/Dom/addEventListener'
import warning from '../_util/warning' import warning from '../_util/warning'
import Popup from './Popup' import Popup from './Popup'
@ -22,7 +23,6 @@ const ALL_HANDLERS = ['click', 'mousedown', 'touchStart', 'mouseenter',
export default { export default {
name: 'Trigger', name: 'Trigger',
props: { props: {
children: PropTypes.any,
action: PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string)]).def([]), action: PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string)]).def([]),
showAction: PropTypes.any.def([]), showAction: PropTypes.any.def([]),
hideAction: PropTypes.any.def([]), hideAction: PropTypes.any.def([]),
@ -66,7 +66,7 @@ export default {
data () { data () {
const props = this.$props const props = this.$props
let popupVisible let popupVisible
if (props.popupVisible === undefined) { if (hasProp(this, 'popupVisible')) {
popupVisible = !!props.popupVisible popupVisible = !!props.popupVisible
} else { } else {
popupVisible = !!props.defaultPopupVisible popupVisible = !!props.defaultPopupVisible
@ -352,7 +352,7 @@ export default {
setPopupVisible (sPopupVisible) { setPopupVisible (sPopupVisible) {
this.clearDelayTimer() this.clearDelayTimer()
if (this.$data.sPopupVisible !== sPopupVisible) { if (this.$data.sPopupVisible !== sPopupVisible) {
if (this.$props.popupVisible === undefined) { if (!hasProp(this, 'popupVisible')) {
this.setState({ this.setState({
sPopupVisible, sPopupVisible,
}) })