Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | /*
Copyright (c) 2018-2019 Uber Technologies, Inc.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
// @flow
import * as React from 'react';
import {getOverrides} from '../helpers/overrides.js';
import {Root as StyledRoot} from './styled-components.js';
import {STATE_CHANGE_TYPE} from './constants.js';
import type {
AccordionPropsT,
AccordionStateT,
StateChangeTypeT,
SharedStylePropsArgT,
} from './types.js';
export default class Accordion extends React.Component<
AccordionPropsT,
AccordionStateT,
> {
static defaultProps: $Shape<AccordionPropsT> = {
accordion: true,
disabled: false,
initialState: {
expanded: [],
},
onChange: () => {},
overrides: {},
stateReducer: (type, newState) => newState,
};
state = {
expanded: [],
...this.props.initialState,
};
onPanelChange(key: React.Key, onChange: () => {}, ...args: *) {
let activeKeys = this.state.expanded;
const {accordion} = this.props;
if (accordion) {
activeKeys = activeKeys[0] === key ? [] : [key];
} else {
activeKeys = [...activeKeys];
const index = activeKeys.indexOf(key);
const wasExpanded = index > -1;
if (wasExpanded) {
// remove active state
activeKeys.splice(index, 1);
} else {
activeKeys.push(key);
}
}
const newState = {expanded: activeKeys};
this.internalSetState(STATE_CHANGE_TYPE.expand, newState);
if (typeof onChange === 'function') onChange(...args);
}
internalSetState(type: StateChangeTypeT, changes: AccordionStateT) {
const {stateReducer, onChange} = this.props;
const newState = stateReducer(type, changes, this.state);
this.setState(newState);
typeof onChange === 'function' && onChange(newState);
}
getItems() {
const {expanded} = this.state;
const {accordion, disabled, children} = this.props;
// eslint-disable-next-line flowtype/no-weak-types
const newChildren = React.Children.map(children, (child: *, index) => {
if (!child) return;
// If there is no key provide, use the panel order as default key
const key = child.key || String(index);
let isExpanded = false;
if (accordion) {
isExpanded = expanded[0] === key;
} else {
isExpanded = expanded.indexOf(key) > -1;
}
const props = {
key,
expanded: isExpanded || child.props.expanded,
accordion,
disabled:
child.props.disabled === null ? disabled : child.props.disabled,
onChange: (...args) =>
this.onPanelChange(key, child.props.onChange, ...args),
};
return React.cloneElement(child, props);
});
return newChildren;
}
getSharedProps(): SharedStylePropsArgT {
const {disabled} = this.props;
return {
$disabled: disabled,
};
}
render() {
const sharedProps = this.getSharedProps();
const {overrides = {}} = this.props;
const {Root: RootOverride} = overrides;
const [Root, rootProps] = getOverrides(RootOverride, StyledRoot);
return (
<Root data-baseweb="accordion" {...sharedProps} {...rootProps}>
{this.getItems()}
</Root>
);
}
}
|