Skip to content

Commit a5c0e77

Browse files
committed
optimize headers
1 parent efcaaa6 commit a5c0e77

File tree

7 files changed

+90
-49
lines changed

7 files changed

+90
-49
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@
9595
"classnames": "^2.2.6",
9696
"create-react-context": "^0.2.2",
9797
"element-resize-detector": "^1.1.12",
98-
"lodash.isequal": "^4.5.0"
98+
"lodash.isequal": "^4.5.0",
99+
"memoize-one": "^5.1.1"
99100
},
100101
"peerDependencies": {
101102
"interactjs": "^1.3.4",

src/lib/headers/CustomHeader.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,6 @@ export class CustomHeader extends React.Component {
193193
data: headerData
194194
}
195195
}
196-
197196
render() {
198197
const props = this.getStateAndHelpers()
199198
const Renderer = this.props.children

src/lib/headers/DateHeader.js

Lines changed: 83 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import CustomHeader from './CustomHeader'
55
import { getNextUnit } from '../utility/calendar'
66
import { defaultHeaderFormats } from '../default-config'
77
import Interval from './Interval'
8+
import memoize from 'memoize-one'
89

910
class DateHeader extends React.Component {
1011
static propTypes = {
@@ -31,14 +32,14 @@ class DateHeader extends React.Component {
3132
return this.props.timelineUnit
3233
}
3334

34-
getRootStyle = () => {
35+
getRootStyle = memoize(style => {
3536
return {
3637
height: 30,
37-
...this.props.style
38+
...style
3839
}
39-
}
40+
})
4041

41-
getLabelFormat(interval, unit, labelWidth) {
42+
getLabelFormat = (interval, unit, labelWidth) => {
4243
const { labelFormat } = this.props
4344
if (typeof labelFormat === 'string') {
4445
const startTime = interval[0]
@@ -50,50 +51,43 @@ class DateHeader extends React.Component {
5051
}
5152
}
5253

54+
getHeaderData = memoize(
55+
(
56+
intervalRenderer,
57+
style,
58+
className,
59+
getLabelFormat,
60+
unitProp,
61+
headerData
62+
) => {
63+
return {
64+
intervalRenderer,
65+
style,
66+
className,
67+
getLabelFormat,
68+
unitProp,
69+
headerData
70+
}
71+
}
72+
)
73+
5374
render() {
5475
const unit = this.getHeaderUnit()
5576
const { headerData, height } = this.props
5677
return (
57-
<CustomHeader unit={unit} height={height} headerData={headerData}>
58-
{({
59-
headerContext: { intervals },
60-
getRootProps,
61-
getIntervalProps,
62-
showPeriod,
63-
data
64-
}) => {
65-
const unit = this.getHeaderUnit()
66-
67-
return (
68-
<div
69-
data-testid={`dateHeader`}
70-
className={this.props.className}
71-
{...getRootProps({ style: this.getRootStyle() })}
72-
>
73-
{intervals.map(interval => {
74-
const intervalText = this.getLabelFormat(
75-
[interval.startTime, interval.endTime],
76-
unit,
77-
interval.labelWidth
78-
)
79-
return (
80-
<Interval
81-
key={`label-${interval.startTime.valueOf()}`}
82-
unit={unit}
83-
interval={interval}
84-
showPeriod={showPeriod}
85-
intervalText={intervalText}
86-
primaryHeader={this.props.unit === 'primaryHeader'}
87-
getIntervalProps={getIntervalProps}
88-
intervalRenderer={this.props.intervalRenderer}
89-
headerData={data}
90-
/>
91-
)
92-
})}
93-
</div>
94-
)
95-
}}
96-
</CustomHeader>
78+
<CustomHeader
79+
unit={unit}
80+
height={height}
81+
headerData={this.getHeaderData(
82+
this.props.intervalRenderer,
83+
this.getRootStyle(this.props.style),
84+
this.props.className,
85+
this.getLabelFormat,
86+
this.props.unit,
87+
this.props.headerData
88+
)}
89+
children={Header}
90+
/>
9791
)
9892
}
9993
}
@@ -116,6 +110,7 @@ class DateHeaderWrapper extends React.Component {
116110
static defaultProps = {
117111
labelFormat: formatLabel
118112
}
113+
119114
render() {
120115
const {
121116
unit,
@@ -148,6 +143,50 @@ class DateHeaderWrapper extends React.Component {
148143
}
149144
}
150145

146+
function Header({
147+
headerContext: { intervals, unit },
148+
getRootProps,
149+
getIntervalProps,
150+
showPeriod,
151+
data: {
152+
style,
153+
intervalRenderer,
154+
className,
155+
getLabelFormat,
156+
unitProp,
157+
...restData
158+
}
159+
}) {
160+
return (
161+
<div
162+
data-testid={`dateHeader`}
163+
className={className}
164+
{...getRootProps({ style })}
165+
>
166+
{intervals.map(interval => {
167+
const intervalText = getLabelFormat(
168+
[interval.startTime, interval.endTime],
169+
unit,
170+
interval.labelWidth
171+
)
172+
return (
173+
<Interval
174+
key={`label-${interval.startTime.valueOf()}`}
175+
unit={unit}
176+
interval={interval}
177+
showPeriod={showPeriod}
178+
intervalText={intervalText}
179+
primaryHeader={unitProp === 'primaryHeader'}
180+
getIntervalProps={getIntervalProps}
181+
intervalRenderer={intervalRenderer}
182+
headerData={restData}
183+
/>
184+
)
185+
})}
186+
</div>
187+
)
188+
}
189+
151190
function formatLabel(
152191
[timeStart, timeEnd],
153192
unit,

src/lib/headers/Interval.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ class Interval extends React.PureComponent {
3636
onClick: composeEvents(this.onIntervalClick, props.onClick)
3737
}
3838
}
39-
4039
render() {
4140
const { intervalText, interval, intervalRenderer, headerData } = this.props
4241
const Renderer = intervalRenderer

src/lib/headers/SidebarHeader.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ class SidebarHeader extends React.PureComponent {
3232
data: this.props.headerData
3333
}
3434
}
35-
3635
render() {
3736
const props = this.getStateAndHelpers()
3837
const Renderer = this.props.children

src/lib/headers/TimelineHeaders.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ class TimelineHeaders extends React.Component {
5656
if (child.type === undefined) return false
5757
return child.type.secretKey === SidebarHeader.secretKey
5858
}
59-
6059
render() {
6160
let rightSidebarHeader
6261
let leftSidebarHeader

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5195,6 +5195,11 @@ mem@^4.0.0:
51955195
mimic-fn "^2.0.0"
51965196
p-is-promise "^2.0.0"
51975197

5198+
memoize-one@^5.1.1:
5199+
version "5.1.1"
5200+
resolved "https://registry.yarnpkg.com/memoize-one/-/memoize-one-5.1.1.tgz#047b6e3199b508eaec03504de71229b8eb1d75c0"
5201+
integrity sha512-HKeeBpWvqiVJD57ZUAsJNm71eHTykffzcLZVYWiVfQeI1rJtuEaS7hQiEpWfVVk18donPwJEcFKIkCmPJNOhHA==
5202+
51985203
memory-fs@^0.4.0, memory-fs@~0.4.1:
51995204
version "0.4.1"
52005205
resolved "https://registry.yarnpkg.com/memory-fs/-/memory-fs-0.4.1.tgz#3a9a20b8462523e447cfbc7e8bb80ed667bfc552"

0 commit comments

Comments
 (0)