Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 修复 collapse 组件同时设置 activeKey 和 defaultActiveKey 属性属性导致的 bug #698

Merged
merged 6 commits into from
Jun 30, 2021
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions packages/zarm/src/collapse/Collapse.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ interface CollapseStates {

const getActiveKey = (props: CollapseProps) => {
const { multiple, activeKey, defaultActiveKey } = props;

let value;

if (typeof activeKey !== 'undefined') {
value = activeKey;
}
if (typeof defaultActiveKey !== 'undefined') {

if (!('activeKey' in props) && typeof defaultActiveKey !== 'undefined') {
value = defaultActiveKey;
}

Expand All @@ -50,19 +52,16 @@ export default class Collapse extends Component<CollapseProps, CollapseStates> {

static getDerivedStateFromProps(nextProps: CollapseProps, state: CollapseStates) {
const newState: CollapseStates = {};
if ('activeKey' in nextProps && nextProps.activeKey !== state.prevActiveKey) {

if ('activeKey' in nextProps && nextProps.activeKey !== state.activeKey) {
newState.activeKey = getActiveKey(nextProps);
newState.prevActiveKey = nextProps.activeKey;
}
if ('animated' in nextProps) {
newState.animated = nextProps.animated;
}
if ('multiple' in nextProps) {
newState.multiple = nextProps.multiple;

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

删除 animated 后,动画效果丢失了

if ('activeKey' in newState) {
return newState;
}
return 'activeKey' in newState || 'animated' in newState || 'multiple' in newState
? newState
: null;

return null;
}

onItemChange = (onItemChange, key) => {
Expand Down Expand Up @@ -102,7 +101,8 @@ export default class Collapse extends Component<CollapseProps, CollapseStates> {
};

renderItems = () => {
const { activeKey, multiple, animated } = this.state;
const { activeKey } = this.state;
const { multiple, animated } = this.props;
return Children.map(this.props.children, (ele: ReactElement<CollapseItemProps>) => {
const { disabled, onChange } = ele.props;
const { key } = ele;
Expand Down