|
1 | | -import React from 'react'; |
2 | | -import PropTypes from 'prop-types'; |
| 1 | +import React, { useCallback, useState } from 'react'; |
3 | 2 | import JSONArrow from './JSONArrow'; |
4 | | -import { CircularPropsPassedThroughItemRange } from './types'; |
| 3 | +import type { CircularCache, CommonInternalProps } from './types'; |
5 | 4 |
|
6 | | -interface Props extends CircularPropsPassedThroughItemRange { |
7 | | - data: any; |
| 5 | +interface Props extends CommonInternalProps { |
| 6 | + data: unknown; |
8 | 7 | nodeType: string; |
9 | 8 | from: number; |
10 | 9 | to: number; |
11 | 10 | renderChildNodes: (props: Props, from: number, to: number) => React.ReactNode; |
| 11 | + circularCache: CircularCache; |
| 12 | + level: number; |
12 | 13 | } |
13 | 14 |
|
14 | | -interface State { |
15 | | - expanded: boolean; |
16 | | -} |
17 | | - |
18 | | -export default class ItemRange extends React.Component<Props, State> { |
19 | | - static propTypes = { |
20 | | - styling: PropTypes.func.isRequired, |
21 | | - from: PropTypes.number.isRequired, |
22 | | - to: PropTypes.number.isRequired, |
23 | | - renderChildNodes: PropTypes.func.isRequired, |
24 | | - nodeType: PropTypes.string.isRequired, |
25 | | - }; |
26 | | - |
27 | | - constructor(props: Props) { |
28 | | - super(props); |
29 | | - this.state = { expanded: false }; |
30 | | - } |
31 | | - |
32 | | - render() { |
33 | | - const { styling, from, to, renderChildNodes, nodeType } = this.props; |
| 15 | +export default function ItemRange(props: Props) { |
| 16 | + const { styling, from, to, renderChildNodes, nodeType } = props; |
34 | 17 |
|
35 | | - return this.state.expanded ? ( |
36 | | - <div {...styling('itemRange', this.state.expanded)}> |
37 | | - {renderChildNodes(this.props, from, to)} |
38 | | - </div> |
39 | | - ) : ( |
40 | | - <div |
41 | | - {...styling('itemRange', this.state.expanded)} |
42 | | - onClick={this.handleClick} |
43 | | - > |
44 | | - <JSONArrow |
45 | | - nodeType={nodeType} |
46 | | - styling={styling} |
47 | | - expanded={false} |
48 | | - onClick={this.handleClick} |
49 | | - arrowStyle="double" |
50 | | - /> |
51 | | - {`${from} ... ${to}`} |
52 | | - </div> |
53 | | - ); |
54 | | - } |
| 18 | + const [expanded, setExpanded] = useState<boolean>(false); |
| 19 | + const handleClick = useCallback(() => { |
| 20 | + setExpanded(!expanded); |
| 21 | + }, [expanded]); |
55 | 22 |
|
56 | | - handleClick = () => { |
57 | | - this.setState({ expanded: !this.state.expanded }); |
58 | | - }; |
| 23 | + return expanded ? ( |
| 24 | + <div {...styling('itemRange', expanded)}> |
| 25 | + {renderChildNodes(props, from, to)} |
| 26 | + </div> |
| 27 | + ) : ( |
| 28 | + <div {...styling('itemRange', expanded)} onClick={handleClick}> |
| 29 | + <JSONArrow |
| 30 | + nodeType={nodeType} |
| 31 | + styling={styling} |
| 32 | + expanded={false} |
| 33 | + onClick={handleClick} |
| 34 | + arrowStyle="double" |
| 35 | + /> |
| 36 | + {`${from} ... ${to}`} |
| 37 | + </div> |
| 38 | + ); |
59 | 39 | } |
0 commit comments