Skip to content

Commit cff7185

Browse files
author
Vojtech Miksu
authored
Merge pull request #178 from wolfd/master
Don't create markRefs unless renderMarks is provided
2 parents 34b6dcf + 8eca42e commit cff7185

1 file changed

Lines changed: 20 additions & 12 deletions

File tree

src/Range.tsx

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ class Range extends React.Component<IProps> {
3434
};
3535
trackRef = React.createRef<HTMLElement>();
3636
thumbRefs: React.RefObject<HTMLElement>[] = [];
37-
markRefs: React.RefObject<HTMLElement>[] = [];
38-
numOfMarks: number;
37+
markRefs?: React.RefObject<HTMLElement>[];
38+
numOfMarks?: number;
3939
resizeObserver: any;
4040
schdOnMouseMove: (e: MouseEvent) => void;
4141
schdOnTouchMove: (e: TouchEvent) => void;
@@ -54,14 +54,11 @@ class Range extends React.Component<IProps> {
5454
throw new Error('"step" property should be a positive number');
5555
}
5656

57-
this.numOfMarks = (props.max - props.min) / this.props.step;
5857
this.schdOnMouseMove = schd(this.onMouseMove);
5958
this.schdOnTouchMove = schd(this.onTouchMove);
6059
this.schdOnEnd = schd(this.onEnd);
6160
this.thumbRefs = props.values.map(() => React.createRef<HTMLElement>());
62-
for (let i = 0; i < this.numOfMarks + 1; i++) {
63-
this.markRefs[i] = React.createRef<HTMLElement>();
64-
}
61+
this.updateMarkRefs(props);
6562
}
6663

6764
componentDidMount() {
@@ -103,11 +100,7 @@ class Range extends React.Component<IProps> {
103100
prevProps.min !== min ||
104101
prevProps.step !== step
105102
) {
106-
this.markRefs = [];
107-
this.numOfMarks = (max - min) / step;
108-
for (let i = 0; i < this.numOfMarks + 1; i++) {
109-
this.markRefs[i] = React.createRef<HTMLElement>();
110-
}
103+
this.updateMarkRefs(this.props);
111104
}
112105
translateThumbs(this.getThumbs(), this.getOffsets(), rtl);
113106
// ensure offsets are calculated when the refs for the marks have been created
@@ -557,10 +550,25 @@ class Range extends React.Component<IProps> {
557550
}
558551
};
559552

553+
updateMarkRefs = (props: IProps) => {
554+
if (!props.renderMark) { // don't create mark refs unless we are rendering marks
555+
this.numOfMarks = undefined;
556+
this.markRefs = undefined;
557+
return;
558+
}
559+
this.numOfMarks = (props.max - props.min) / this.props.step;
560+
this.markRefs = [];
561+
for (let i = 0; i < this.numOfMarks + 1; i++) {
562+
this.markRefs[i] = React.createRef<HTMLElement>();
563+
}
564+
}
565+
560566
calculateMarkOffsets = () => {
561567
if (
562568
!this.props.renderMark ||
563569
!this.trackRef ||
570+
!this.numOfMarks ||
571+
!this.markRefs ||
564572
this.trackRef.current === null
565573
)
566574
return;
@@ -657,7 +665,7 @@ class Range extends React.Component<IProps> {
657665
marginLeft: `${offset[1]}px`
658666
},
659667
key: `mark${index}`,
660-
ref: this.markRefs[index]
668+
ref: this.markRefs![index]
661669
},
662670
index
663671
})

0 commit comments

Comments
 (0)