UNPKG

2.1 kBJavaScriptView Raw
1import * as React from 'react';
2export const ListAccordionGroupContext = /*#__PURE__*/React.createContext(null);
3/**
4 * List.AccordionGroup allows to control a group of List Accordions. `id` prop for List.Accordion is required in order for group to work.
5 * List.AccordionGroup can be a controlled or uncontrolled component. The example shows the uncontrolled version.
6 * At most one Accordion can be expanded at a given time.
7 *
8 * <div class="screenshots">
9 * <img class="medium" src="screenshots/list-accordion-group.png" />
10 * </div>
11 *
12 * ## Usage
13 * ```js
14 * import * as React from 'react';
15 * import { View, Text } from 'react-native';
16 * import { List } from 'react-native-paper';
17 *
18 * const MyComponent = () => (
19 * <List.AccordionGroup>
20 * <List.Accordion title="Accordion 1" id="1">
21 * <List.Item title="Item 1" />
22 * </List.Accordion>
23 * <List.Accordion title="Accordion 2" id="2">
24 * <List.Item title="Item 2" />
25 * </List.Accordion>
26 * <View>
27 * <Text>
28 * List.Accordion can be wrapped because implementation uses React.Context.
29 * </Text>
30 * <List.Accordion title="Accordion 3" id="3">
31 * <List.Item title="Item 3" />
32 * </List.Accordion>
33 * </View>
34 * </List.AccordionGroup>
35 * );
36 *
37 * export default MyComponent;
38 *```
39 */
40
41const ListAccordionGroup = _ref => {
42 let {
43 expandedId: expandedIdProp,
44 onAccordionPress,
45 children
46 } = _ref;
47 const [expandedId, setExpandedId] = React.useState(undefined);
48
49 const onAccordionPressDefault = newExpandedId => {
50 setExpandedId(currentExpandedId => currentExpandedId === newExpandedId ? undefined : newExpandedId);
51 };
52
53 return /*#__PURE__*/React.createElement(ListAccordionGroupContext.Provider, {
54 value: {
55 expandedId: expandedIdProp || expandedId,
56 // component can be controlled or uncontrolled
57 onAccordionPress: onAccordionPress || onAccordionPressDefault
58 }
59 }, children);
60};
61
62ListAccordionGroup.displayName = 'List.AccordionGroup';
63export default ListAccordionGroup;
64//# sourceMappingURL=ListAccordionGroup.js.map
\No newline at end of file