add context
parent
d4a40b44d0
commit
373d073aad
|
@ -27,15 +27,23 @@ export default {
|
||||||
value: [String, Number, Boolean],
|
value: [String, Number, Boolean],
|
||||||
name: String,
|
name: String,
|
||||||
indeterminate: Boolean,
|
indeterminate: Boolean,
|
||||||
onGroupChange: Function,
|
|
||||||
},
|
},
|
||||||
model: {
|
model: {
|
||||||
prop: 'checked',
|
prop: 'checked',
|
||||||
},
|
},
|
||||||
|
inject: {
|
||||||
|
context: { default: undefined },
|
||||||
|
},
|
||||||
data () {
|
data () {
|
||||||
const { checked, defaultChecked } = this
|
const { context, checked, defaultChecked, value } = this
|
||||||
|
let stateChecked
|
||||||
|
if (context && context.checkedStatus) {
|
||||||
|
stateChecked = context.checkedStatus.has(value)
|
||||||
|
}
|
||||||
return {
|
return {
|
||||||
stateChecked: checked === undefined ? defaultChecked : checked,
|
stateChecked: stateChecked === undefined
|
||||||
|
? checked === undefined ? defaultChecked : checked
|
||||||
|
: stateChecked,
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
computed: {
|
computed: {
|
||||||
|
@ -64,26 +72,27 @@ 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 } = this
|
const { name, value, checked, context, stateChecked } = this
|
||||||
if (checked === undefined) {
|
if ((checked === undefined && !context) || (context && context.value === undefined)) {
|
||||||
this.stateChecked = targetChecked
|
this.stateChecked = targetChecked
|
||||||
}
|
}
|
||||||
const target = {
|
const target = {
|
||||||
name,
|
name,
|
||||||
value,
|
value,
|
||||||
checked: targetChecked,
|
checked: !stateChecked,
|
||||||
}
|
}
|
||||||
this.$emit('change', {
|
if (this.context) {
|
||||||
target,
|
this.context.handleChange({ target })
|
||||||
stopPropagation () {
|
} else {
|
||||||
event.stopPropagation()
|
this.$emit('change', {
|
||||||
},
|
target,
|
||||||
preventDefault () {
|
stopPropagation () {
|
||||||
event.preventDefault()
|
event.stopPropagation()
|
||||||
},
|
},
|
||||||
})
|
preventDefault () {
|
||||||
if (this.isGroup) {
|
event.preventDefault()
|
||||||
this.onGroupChange({ target })
|
},
|
||||||
|
})
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -91,6 +100,9 @@ export default {
|
||||||
checked (val) {
|
checked (val) {
|
||||||
this.stateChecked = val
|
this.stateChecked = val
|
||||||
},
|
},
|
||||||
|
'context.checkedStatus': function (checkedStatus) {
|
||||||
|
this.stateChecked = checkedStatus.has(this.value)
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
|
@ -31,6 +31,11 @@ export default {
|
||||||
model: {
|
model: {
|
||||||
prop: 'value',
|
prop: 'value',
|
||||||
},
|
},
|
||||||
|
provide () {
|
||||||
|
return {
|
||||||
|
context: this,
|
||||||
|
}
|
||||||
|
},
|
||||||
data () {
|
data () {
|
||||||
const { value, defaultValue } = this
|
const { value, defaultValue } = this
|
||||||
return {
|
return {
|
||||||
|
@ -50,9 +55,6 @@ export default {
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
created () {
|
|
||||||
this.setChildCheckbox(this.$slots.default)
|
|
||||||
},
|
|
||||||
methods: {
|
methods: {
|
||||||
handleChange (event) {
|
handleChange (event) {
|
||||||
const target = event.target
|
const target = event.target
|
||||||
|
@ -73,31 +75,12 @@ export default {
|
||||||
this.$emit('input', newVal)
|
this.$emit('input', newVal)
|
||||||
this.$emit('change', newVal)
|
this.$emit('change', newVal)
|
||||||
},
|
},
|
||||||
setChildCheckbox (children = []) {
|
|
||||||
const { options, $slots, checkedStatus } = this
|
|
||||||
if (options.length === 0 && $slots.default) {
|
|
||||||
children.forEach(({ componentOptions = {}, children: newChildren }) => {
|
|
||||||
const { Ctor, propsData } = componentOptions
|
|
||||||
if (Ctor && Ctor.options.name === 'Checkbox') {
|
|
||||||
propsData.isGroup = true
|
|
||||||
propsData.onGroupChange = this.handleChange
|
|
||||||
propsData.checked = checkedStatus.has(propsData.value)
|
|
||||||
} else {
|
|
||||||
this.setChildCheckbox(newChildren)
|
|
||||||
}
|
|
||||||
}, this)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
mounted () {
|
mounted () {
|
||||||
},
|
},
|
||||||
beforeUpdate () {
|
|
||||||
this.setChildCheckbox(this.$slots.default)
|
|
||||||
},
|
|
||||||
watch: {
|
watch: {
|
||||||
value (val) {
|
value (val) {
|
||||||
this.stateValue = val
|
this.stateValue = val
|
||||||
this.setChildCheckbox(this.$slots.default)
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
components: {
|
components: {
|
||||||
|
|
|
@ -1,18 +1,62 @@
|
||||||
<template>
|
<template>
|
||||||
<div>
|
<div>
|
||||||
<Checkbox @change="onChange">Checkbox</Checkbox>
|
<p :style="{ marginBottom: '20px' }">
|
||||||
|
<Checkbox
|
||||||
|
:checked="checked"
|
||||||
|
:disabled="disabled"
|
||||||
|
@change="onChange"
|
||||||
|
>
|
||||||
|
{{label}}
|
||||||
|
</Checkbox>
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
<AntButton
|
||||||
|
type="primary"
|
||||||
|
size="small"
|
||||||
|
@click="toggleChecked"
|
||||||
|
>
|
||||||
|
{{!checked ? 'Check' : 'Uncheck'}}
|
||||||
|
</AntButton>
|
||||||
|
<AntButton
|
||||||
|
:style="{ marginLeft: '10px' }"
|
||||||
|
type="primary"
|
||||||
|
size="small"
|
||||||
|
@click="toggleDisable"
|
||||||
|
>
|
||||||
|
{{!disabled ? 'Disable' : 'Enable'}}
|
||||||
|
</AntButton>
|
||||||
|
</p>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
<script>
|
<script>
|
||||||
import { Checkbox } from 'antd'
|
import { Checkbox, Button } from 'antd'
|
||||||
export default {
|
export default {
|
||||||
|
data () {
|
||||||
|
return {
|
||||||
|
checked: true,
|
||||||
|
disabled: false,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
computed: {
|
||||||
|
label () {
|
||||||
|
const { checked, disabled } = this
|
||||||
|
return `${checked ? 'Checked' : 'Unchecked'}-${disabled ? 'Disabled' : 'Enabled'}`
|
||||||
|
},
|
||||||
|
},
|
||||||
methods: {
|
methods: {
|
||||||
|
toggleChecked () {
|
||||||
|
this.checked = !this.checked
|
||||||
|
},
|
||||||
|
toggleDisable () {
|
||||||
|
this.disabled = !this.disabled
|
||||||
|
},
|
||||||
onChange (e) {
|
onChange (e) {
|
||||||
console.log(`checked = ${e.target.checked}`)
|
this.checked = e.target.checked
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
components: {
|
components: {
|
||||||
Checkbox,
|
Checkbox,
|
||||||
|
AntButton: Button,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
<br />
|
<br />
|
||||||
<CheckboxGroup :options="plainOptions" :defaultValue="['Apple']" @change="onChange" />
|
<CheckboxGroup :options="plainOptions" :defaultValue="['Apple']" @change="onChange" />
|
||||||
<br />
|
<br />
|
||||||
<CheckboxGroup :options="options" :defaultValue="['Pear']" @change="onChange" />
|
<CheckboxGroup :options="options" :value="['Pear']" @change="onChange" />
|
||||||
<br />
|
<br />
|
||||||
<CheckboxGroup :options="optionsWithDisabled" disabled :defaultValue="['Apple']" @change="onChange" />
|
<CheckboxGroup :options="optionsWithDisabled" disabled :defaultValue="['Apple']" @change="onChange" />
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -0,0 +1,35 @@
|
||||||
|
<template>
|
||||||
|
<div>
|
||||||
|
<h1>Basic</h1>
|
||||||
|
<Basic />
|
||||||
|
<h1>CheckAll</h1>
|
||||||
|
<CheckAll />
|
||||||
|
<h1>Controller</h1>
|
||||||
|
<Controller />
|
||||||
|
<h1>Disabled</h1>
|
||||||
|
<Disabled />
|
||||||
|
<h1>Group</h1>
|
||||||
|
<Group />
|
||||||
|
<h1>Layout</h1>
|
||||||
|
<Layout />
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
<script>
|
||||||
|
import Basic from './basic'
|
||||||
|
import CheckAll from './check-all'
|
||||||
|
import Controller from './controller'
|
||||||
|
import Disabled from './disabled'
|
||||||
|
|
||||||
|
import Group from './group'
|
||||||
|
import Layout from './layout'
|
||||||
|
export default {
|
||||||
|
components: {
|
||||||
|
Basic,
|
||||||
|
CheckAll,
|
||||||
|
Disabled,
|
||||||
|
Controller,
|
||||||
|
Group,
|
||||||
|
Layout,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
</script>
|
|
@ -1,18 +1,27 @@
|
||||||
<template>
|
<template>
|
||||||
<div>
|
<CheckboxGroup @change="onChange">
|
||||||
<Checkbox @change="onChange">Checkbox</Checkbox>
|
<Row>
|
||||||
</div>
|
<Col :span="8"><Checkbox value="A">A</Checkbox></Col>
|
||||||
|
<Col :span="8"><Checkbox value="B">B</Checkbox></Col>
|
||||||
|
<Col :span="8"><Checkbox value="C">C</Checkbox></Col>
|
||||||
|
<Col :span="8"><Checkbox value="D">D</Checkbox></Col>
|
||||||
|
<Col :span="8"><Checkbox value="E">E</Checkbox></Col>
|
||||||
|
</Row>
|
||||||
|
</CheckboxGroup>
|
||||||
</template>
|
</template>
|
||||||
<script>
|
<script>
|
||||||
import { Checkbox } from 'antd'
|
import { Checkbox, Row, Col } from 'antd'
|
||||||
export default {
|
export default {
|
||||||
methods: {
|
methods: {
|
||||||
onChange (e) {
|
onChange (checkedValues) {
|
||||||
console.log(`checked = ${e.target.checked}`)
|
console.log('checked = ', checkedValues)
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
components: {
|
components: {
|
||||||
Checkbox,
|
Checkbox,
|
||||||
|
Row,
|
||||||
|
Col,
|
||||||
|
CheckboxGroup: Checkbox.Group,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
|
@ -11,3 +11,7 @@ export { default as Grid } from './grid'
|
||||||
export { default as Rate } from './rate'
|
export { default as Rate } from './rate'
|
||||||
|
|
||||||
export { default as ToolTip } from './tooltip'
|
export { default as ToolTip } from './tooltip'
|
||||||
|
|
||||||
|
export { default as Row } from './grid/row'
|
||||||
|
|
||||||
|
export { default as Col } from './grid/col'
|
||||||
|
|
|
@ -2,3 +2,4 @@ import './button/style'
|
||||||
import './icon/style'
|
import './icon/style'
|
||||||
import './radio/style'
|
import './radio/style'
|
||||||
import './checkbox/style'
|
import './checkbox/style'
|
||||||
|
import './grid/style'
|
||||||
|
|
Loading…
Reference in New Issue