-
-
Notifications
You must be signed in to change notification settings - Fork 829
Font Setting - Added draggable slider #10126
Changes from all commits
b4fcd91
fe3d62b
edad817
05ff6d8
554c91a
f3b8f4a
27b809d
8b016fc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -35,6 +35,12 @@ interface IProps { | |||||
} | ||||||
|
||||||
export default class Slider extends React.Component<IProps> { | ||||||
// state to keep track of the current index and percent | ||||||
state = { | ||||||
index: this.props.values.indexOf(this.props.value), | ||||||
percent: this.offset(this.props.values, this.props.value), | ||||||
}; | ||||||
|
||||||
// offset is a terrible inverse approximation. | ||||||
// if the values represents some function f(x) = y where x is the | ||||||
// index of the array and y = values[x] then offset(f, y) = x | ||||||
|
@@ -83,11 +89,60 @@ export default class Slider extends React.Component<IProps> { | |||||
|
||||||
let selection = null; | ||||||
|
||||||
const onDrag = (e: React.DragEvent) => { | ||||||
|
||||||
const target = e.target as HTMLElement; | ||||||
if (!target.parentNode) return null; | ||||||
const parent = target.parentNode as HTMLElement; | ||||||
if (!parent.parentNode) return null; | ||||||
const slider = parent.parentNode as HTMLElement; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should use the
Suggested change
Comment on lines
+94
to
+98
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe we should rely on react refs instead of traversing the DOM. |
||||||
const rect = slider.getBoundingClientRect(); | ||||||
|
||||||
const x = e.clientX - rect.left; //x position within the element. | ||||||
const width = rect.width; | ||||||
const percent = x / width; | ||||||
const value = Math.round(percent * (this.props.values.length - 1)); | ||||||
this.setState({ index: value, percent: percent * 100 }); | ||||||
|
||||||
//put the inner dot to the correct position | ||||||
const innerDot = target.parentNode as any; | ||||||
if (!innerDot) return null; | ||||||
const offset = this.offset(this.props.values, this.props.values[value]); | ||||||
innerDot.style.left = `calc(-1.195em + " + ${offset} + "%)`; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does |
||||||
|
||||||
}; | ||||||
|
||||||
const onDragEnd = (e: any) => { | ||||||
const innerDot = e.target.parentNode; | ||||||
const target = e.target as HTMLElement; | ||||||
if (!target.parentNode) return null; | ||||||
const parent = target.parentNode as HTMLElement; | ||||||
if (!parent.parentNode) return null; | ||||||
const slider = parent.parentNode as HTMLElement; | ||||||
const rect = slider.getBoundingClientRect(); | ||||||
const x = e.clientX - rect.left; //x position within the element. | ||||||
// console.log(x); | ||||||
if(x < -10 || x > rect.width + 10) return null; | ||||||
const width = rect.width; | ||||||
|
||||||
const percent = x / width; | ||||||
const value = Math.round(percent * (this.props.values.length - 1)); | ||||||
const offset = this.offset(this.props.values, this.props.values[value]); | ||||||
innerDot.style.left = `calc(-1.195em + " + ${offset} + "%)`; | ||||||
this.props.onSelectionChange(this.props.values[value]); | ||||||
}; | ||||||
|
||||||
if (!this.props.disabled) { | ||||||
const offset = this.offset(this.props.values, this.props.value); | ||||||
selection = ( | ||||||
<div className="mx_Slider_selection"> | ||||||
<div className="mx_Slider_selectionDot" style={{ left: "calc(-1.195em + " + offset + "%)" }}> | ||||||
<div | ||||||
onDrag={onDrag} | ||||||
onDragEnd={onDragEnd} | ||||||
draggable={true} | ||||||
className="mx_Slider_selectionDotInner" | ||||||
></div> | ||||||
<div className="mx_Slider_selectionText">{this.props.value}</div> | ||||||
</div> | ||||||
<hr style={{ width: offset + "%" }} /> | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a lot of logic overlap on
onDrag
andonDragEnd
could we consolidate those ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even I had that in my mind. Will make a separate function for common operations.