Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(slider): calculate the correct value on mark click #3017

Merged
merged 4 commits into from
May 19, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/olive-kids-hide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nextui-org/slider": patch
---

calculate the correct value on mark click (#2980)
209 changes: 146 additions & 63 deletions packages/components/slider/__tests__/slider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -213,78 +213,161 @@ describe("Slider", () => {

expect(setValues).toStrictEqual([[15, 25]]);
});
});

it("should supports hideThumb", async function () {
const {container} = render(<Slider hideThumb defaultValue={20} label="The Label" />);
it("should supports hideThumb", async function () {
const {container} = render(<Slider hideThumb defaultValue={20} label="The Label" />);

const track = container.querySelector("[data-slot='track']");
const track = container.querySelector("[data-slot='track']");

expect(track).toHaveAttribute("data-thumb-hidden", "true");
});
expect(track).toHaveAttribute("data-thumb-hidden", "true");
});

it("should supports marks", async function () {
const {container} = render(
<Slider
hideThumb
defaultValue={20}
label="The Label"
marks={[
{
value: 0.2,
label: "20%",
},
{
value: 0.5,
label: "50%",
},
{
value: 0.8,
label: "80%",
},
]}
maxValue={1}
minValue={0}
step={0.1}
/>,
);
it("should supports marks", async function () {
const {container} = render(
<Slider
hideThumb
defaultValue={20}
label="The Label"
marks={[
{
value: 0.2,
label: "20%",
},
{
value: 0.5,
label: "50%",
},
{
value: 0.8,
label: "80%",
},
]}
maxValue={1}
minValue={0}
step={0.1}
/>,
);

const marks = container.querySelectorAll("[data-slot='mark']");
const marks = container.querySelectorAll("[data-slot='mark']");

expect(marks).toHaveLength(3);
});
expect(marks).toHaveLength(3);
});

it("should supports marks with hideThumb", async function () {
const {container} = render(
<Slider
hideThumb
defaultValue={20}
label="The Label"
marks={[
{
value: 0.2,
label: "20%",
},
{
value: 0.5,
label: "50%",
},
{
value: 0.8,
label: "80%",
},
]}
maxValue={1}
minValue={0}
step={0.1}
/>,
);
it("should supports marks with hideThumb", async function () {
const {container} = render(
<Slider
hideThumb
defaultValue={20}
label="The Label"
marks={[
{
value: 0.2,
label: "20%",
},
{
value: 0.5,
label: "50%",
},
{
value: 0.8,
label: "80%",
},
]}
maxValue={1}
minValue={0}
step={0.1}
/>,
);

const track = container.querySelector("[data-slot='track']");

expect(track).toHaveAttribute("data-thumb-hidden", "true");

const track = container.querySelector("[data-slot='track']");
const marks = container.querySelectorAll("[data-slot='mark']");

expect(marks).toHaveLength(3);
});

it("should move thumb after clicking mark (single thumb)", async function () {
const {getByRole, container} = render(
<Slider
hideThumb
defaultValue={0.2}
label="The Label"
marks={[
{
value: 0.2,
label: "20%",
},
{
value: 0.5,
label: "50%",
},
{
value: 0.8,
label: "80%",
},
]}
maxValue={1}
minValue={0}
step={0.1}
/>,
);

expect(track).toHaveAttribute("data-thumb-hidden", "true");
const marks = container.querySelectorAll("[data-slot='mark']");

const marks = container.querySelectorAll("[data-slot='mark']");
expect(marks).toHaveLength(3);

expect(marks).toHaveLength(3);
await act(async () => {
await userEvent.click(marks[1]);
});

const slider = getByRole("slider");

expect(slider).toHaveProperty("value", "0.5");
expect(slider).toHaveAttribute("aria-valuetext", "0.5");
});

it("should move thumb after clicking mark (left and right thumbs)", async function () {
const {getAllByRole, container} = render(
<Slider
hideThumb
defaultValue={[0.2, 0.8]}
label="The Label"
marks={[
{
value: 0.2,
label: "20%",
},
{
value: 0.5,
label: "50%",
},
{
value: 0.8,
label: "80%",
},
]}
maxValue={1}
minValue={0}
step={0.1}
/>,
);

const marks = container.querySelectorAll("[data-slot='mark']");

expect(marks).toHaveLength(3);

await act(async () => {
await userEvent.click(marks[1]);
});

const [leftSlider, rightSlider] = getAllByRole("slider");

expect(leftSlider).toHaveProperty("value", "0.5");
expect(leftSlider).toHaveAttribute("aria-valuetext", "0.5");

expect(rightSlider).toHaveProperty("value", "0.8");
expect(rightSlider).toHaveAttribute("aria-valuetext", "0.8");
});
});
24 changes: 24 additions & 0 deletions packages/components/slider/src/use-slider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,30 @@ export function useSlider(originalProps: UseSliderProps) {
style: {
[isVertical ? "bottom" : direction === "rtl" ? "right" : "left"]: `${percent * 100}%`,
},
// avoid `onDownTrack` is being called since when you click the mark,
// `onDownTrack` will calculate the percent based on the position you click
// the calculated value will be set instead of the actual value defined in `marks`
onMouseDown: (e: React.MouseEvent) => e.stopPropagation(),
onPointerDown: (e: React.PointerEvent) => e.stopPropagation(),
onClick: (e: any) => {
e.stopPropagation();
if (state.values.length === 1) {
state.setThumbPercent(0, percent);
} else {
const leftThumbPos = state.values[0];
const rightThumbPos = state.values[1];

if (mark.value < leftThumbPos) {
state.setThumbPercent(0, percent);
} else if (mark.value > rightThumbPos) {
state.setThumbPercent(1, percent);
} else if (Math.abs(mark.value - leftThumbPos) < Math.abs(mark.value - rightThumbPos)) {
state.setThumbPercent(0, percent);
} else {
state.setThumbPercent(1, percent);
}
}
},
};
};

Expand Down
Loading