generated from egoist/ts-lib-starter
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from raotaohub/master
feature: drag the progress bar
- Loading branch information
Showing
6 changed files
with
181 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { expect, test } from "vitest"; | ||
import { computePercentage } from "./computePercentage"; | ||
|
||
test("Return 0 if progressBarRef.current is undefined", () => { | ||
expect( | ||
computePercentage(new MouseEvent("mouseup", {}), { current: null }) | ||
).toBe(0); | ||
}); | ||
|
||
test("Return 0 if progressBarRef.current is undefined", () => { | ||
expect( | ||
computePercentage(new MouseEvent("mousemove"), { current: null }) | ||
).toBe(0); | ||
}); | ||
|
||
test("Return 0 if progressBarRef.current is undefined", () => { | ||
expect( | ||
computePercentage(new MouseEvent("mousedown"), { | ||
current: null, | ||
}) | ||
).toBe(0); | ||
}); | ||
|
||
/* MOCK DOM */ | ||
test("Return percentage when mousedown event", () => { | ||
const container = document.createElement("div"); | ||
container.style.width = "200px"; | ||
container.style.height = "2px"; | ||
const mouseEvent = new MouseEvent("mousedown", { | ||
clientX: 50, | ||
clientY: 50, | ||
}); | ||
|
||
/* hack ! no value in the node environment , so overwrite they */ | ||
container.clientWidth = 200; | ||
container.getBoundingClientRect = () => ({ | ||
x: 10, | ||
y: 10, | ||
width: 300, | ||
height: 2, | ||
top: 10, | ||
right: 300, | ||
bottom: 10, | ||
left: 10, | ||
toJSON: function () { | ||
return ""; | ||
}, | ||
}); | ||
|
||
container.addEventListener("mousedown", function (e) { | ||
const val = computePercentage(e, { current: container }); | ||
expect(val).toBe(0.2); | ||
}); | ||
|
||
container.dispatchEvent(mouseEvent); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export function computePercentage( | ||
eventTarget: MouseEvent, | ||
progressBarRef: React.RefObject<HTMLDivElement> | ||
) { | ||
if (!progressBarRef.current) return 0; | ||
let percentage = | ||
(eventTarget.clientX - | ||
progressBarRef.current.getBoundingClientRect().left) / | ||
progressBarRef.current.clientWidth; | ||
percentage = Math.max(percentage, 0); | ||
percentage = Math.min(percentage, 1); | ||
percentage = Math.floor(percentage * 100) / 100; | ||
return percentage; | ||
} |
69db2b9
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.
Successfully deployed to the following URLs:
aplayer-react – ./
aplayer-react.vercel.app
aplayer-react-leishenghao.vercel.app
aplayer-react-git-master-leishenghao.vercel.app
aplayer-react.js.org