mirror of
https://github.com/ant-design/ant-design.git
synced 2025-07-31 20:36:35 +08:00
* fix checkbox can't receive context update issue * add test case for checkbox handling context update
This commit is contained in:
parent
db99fe752f
commit
048a6e28a3
@ -1,9 +1,8 @@
|
|||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
import * as PropTypes from 'prop-types';
|
|
||||||
import classNames from 'classnames';
|
import classNames from 'classnames';
|
||||||
import RcCheckbox from 'rc-checkbox';
|
import RcCheckbox from 'rc-checkbox';
|
||||||
import shallowEqual from 'shallowequal';
|
import shallowEqual from 'shallowequal';
|
||||||
import CheckboxGroup, { CheckboxGroupContext } from './Group';
|
import CheckboxGroup, { CheckboxGroupContext, GroupContext } from './Group';
|
||||||
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
|
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
|
||||||
import warning from '../_util/warning';
|
import warning from '../_util/warning';
|
||||||
|
|
||||||
@ -52,9 +51,7 @@ class Checkbox extends React.Component<CheckboxProps, {}> {
|
|||||||
indeterminate: false,
|
indeterminate: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
static contextTypes = {
|
static contextType = GroupContext;
|
||||||
checkboxGroup: PropTypes.any,
|
|
||||||
};
|
|
||||||
|
|
||||||
context: any;
|
context: any;
|
||||||
|
|
||||||
|
@ -43,6 +43,10 @@ export interface CheckboxGroupContext {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const GroupContext = React.createContext<{ checkboxGroup: any }>({
|
||||||
|
checkboxGroup: undefined,
|
||||||
|
});
|
||||||
|
|
||||||
class CheckboxGroup extends React.Component<CheckboxGroupProps, CheckboxGroupState> {
|
class CheckboxGroup extends React.Component<CheckboxGroupProps, CheckboxGroupState> {
|
||||||
static defaultProps = {
|
static defaultProps = {
|
||||||
options: [],
|
options: [],
|
||||||
@ -55,10 +59,6 @@ class CheckboxGroup extends React.Component<CheckboxGroupProps, CheckboxGroupSta
|
|||||||
onChange: PropTypes.func,
|
onChange: PropTypes.func,
|
||||||
};
|
};
|
||||||
|
|
||||||
static childContextTypes = {
|
|
||||||
checkboxGroup: PropTypes.any,
|
|
||||||
};
|
|
||||||
|
|
||||||
static getDerivedStateFromProps(nextProps: CheckboxGroupProps) {
|
static getDerivedStateFromProps(nextProps: CheckboxGroupProps) {
|
||||||
if ('value' in nextProps) {
|
if ('value' in nextProps) {
|
||||||
return {
|
return {
|
||||||
@ -76,21 +76,6 @@ class CheckboxGroup extends React.Component<CheckboxGroupProps, CheckboxGroupSta
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
getChildContext() {
|
|
||||||
return {
|
|
||||||
checkboxGroup: {
|
|
||||||
toggleOption: this.toggleOption,
|
|
||||||
value: this.state.value,
|
|
||||||
disabled: this.props.disabled,
|
|
||||||
name: this.props.name,
|
|
||||||
|
|
||||||
// https://github.com/ant-design/ant-design/issues/16376
|
|
||||||
registerValue: this.registerValue,
|
|
||||||
cancelValue: this.cancelValue,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
shouldComponentUpdate(nextProps: CheckboxGroupProps, nextState: CheckboxGroupState) {
|
shouldComponentUpdate(nextProps: CheckboxGroupProps, nextState: CheckboxGroupState) {
|
||||||
return !shallowEqual(this.props, nextProps) || !shallowEqual(this.state, nextState);
|
return !shallowEqual(this.props, nextProps) || !shallowEqual(this.state, nextState);
|
||||||
}
|
}
|
||||||
@ -173,10 +158,23 @@ class CheckboxGroup extends React.Component<CheckboxGroupProps, CheckboxGroupSta
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const context = {
|
||||||
|
checkboxGroup: {
|
||||||
|
toggleOption: this.toggleOption,
|
||||||
|
value: this.state.value,
|
||||||
|
disabled: this.props.disabled,
|
||||||
|
name: this.props.name,
|
||||||
|
|
||||||
|
// https://github.com/ant-design/ant-design/issues/16376
|
||||||
|
registerValue: this.registerValue,
|
||||||
|
cancelValue: this.cancelValue,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
const classString = classNames(groupPrefixCls, className);
|
const classString = classNames(groupPrefixCls, className);
|
||||||
return (
|
return (
|
||||||
<div className={classString} style={style} {...domProps}>
|
<div className={classString} style={style} {...domProps}>
|
||||||
{children}
|
<GroupContext.Provider value={context}>{children}</GroupContext.Provider>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { mount, render } from 'enzyme';
|
import { mount, render } from 'enzyme';
|
||||||
|
import Collapse from '../../collapse';
|
||||||
import Checkbox from '../index';
|
import Checkbox from '../index';
|
||||||
import mountTest from '../../../tests/shared/mountTest';
|
import mountTest from '../../../tests/shared/mountTest';
|
||||||
import rtlTest from '../../../tests/shared/rtlTest';
|
import rtlTest from '../../../tests/shared/rtlTest';
|
||||||
@ -181,4 +182,44 @@ describe('CheckboxGroup', () => {
|
|||||||
.simulate('change');
|
.simulate('change');
|
||||||
expect(onChange).toHaveBeenCalledWith([1, 2]);
|
expect(onChange).toHaveBeenCalledWith([1, 2]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// https://github.com/ant-design/ant-design/issues/21134
|
||||||
|
it('should work when checkbox is wrapped by other components', () => {
|
||||||
|
const wrapper = mount(
|
||||||
|
<Checkbox.Group>
|
||||||
|
<Collapse bordered={false}>
|
||||||
|
<Collapse.Panel header="test panel">
|
||||||
|
<div>
|
||||||
|
<Checkbox value="1">item</Checkbox>
|
||||||
|
</div>
|
||||||
|
</Collapse.Panel>
|
||||||
|
</Collapse>
|
||||||
|
</Checkbox.Group>,
|
||||||
|
);
|
||||||
|
wrapper
|
||||||
|
.find('.ant-collapse-item')
|
||||||
|
.at(0)
|
||||||
|
.find('.ant-collapse-header')
|
||||||
|
.simulate('click');
|
||||||
|
wrapper
|
||||||
|
.find('.ant-checkbox-input')
|
||||||
|
.at(0)
|
||||||
|
.simulate('change');
|
||||||
|
expect(
|
||||||
|
wrapper
|
||||||
|
.find(Checkbox.Group)
|
||||||
|
.at(0)
|
||||||
|
.state('value'),
|
||||||
|
).toEqual(['1']);
|
||||||
|
wrapper
|
||||||
|
.find('.ant-checkbox-input')
|
||||||
|
.at(0)
|
||||||
|
.simulate('change');
|
||||||
|
expect(
|
||||||
|
wrapper
|
||||||
|
.find(Checkbox.Group)
|
||||||
|
.at(0)
|
||||||
|
.state('value'),
|
||||||
|
).toEqual([]);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user