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

[BUG] - Slider #2980

Closed
armatrix opened this issue May 11, 2024 · 7 comments · Fixed by #3017
Closed

[BUG] - Slider #2980

armatrix opened this issue May 11, 2024 · 7 comments · Fixed by #3017
Assignees
Labels
📦 Scope : Components Related to the components 🐛 Type: Bug Something isn't working

Comments

@armatrix
Copy link

NextUI Version

v2.3.6

Describe the bug

can not get right value while slider marks clicked

Your Example Website or App

No response

Steps to Reproduce the Bug or Issue

set small steps for marks, while clicking mark value, slider value not been set right.

Expected behavior

I wanna catch the right value while clicking the 'mark', kindly check the video below

Screenshots or Videos

Screen.Recording.2024-05-11.at.21.09.29.mov

Operating System Version

macos

Browser

Chrome

Copy link

linear bot commented May 11, 2024

ENG-817 [BUG] - Slider

@wingkwong
Copy link
Member

I think I fixed a similar issue a while ago. Can you share your code (just the slider part) for me to double check?

@armatrix
Copy link
Author

armatrix commented May 12, 2024

Controller form 'react hook form'

   <Controller
          name="leverage"
          control={control}
          defaultValue={"2"}
          render={({ field: { onChange, value } }) => (
            <>
              <Slider
                label="Leverage"
                size="sm"
                step={0.01}
                maxValue={50}
                minValue={2}
                defaultValue={2}
                className="pb-6"
                marks={[
                  {
                    value: 2,
                    label: "2x",
                  },
                  {
                    value: 10,
                    label: "10x",
                  },
                  {
                    value: 20,
                    label: "20x",
                  },
                  {
                    value: 30,
                    label: "30x",
                  },
                  {
                    value: 40,
                    label: "40x",
                  },
                  {
                    value: 50,
                    label: "50x",
                  },
                ]}
                classNames={{
                  base: "max-w-md",
                  label: "text-medium",
                }}
                renderValue={({ children, ...props }) => (
                  <output {...props}>
                    <Tooltip
                      className="text-tiny text-default-500 rounded-md"
                      content="Press Enter to confirm"
                      placement="left"
                    >
                      <Input
                        className="px-1 py-0.5 w-24 text-right text-small text-default-700 font-medium bg-default-100 outline-none transition-colors rounded-small border-medium border-transparent hover:border-primary focus:border-primary"
                        type="number"
                        value={value}
                        onChange={(e) => {
                          onChange(e.target.value)
                          onChangeLeverage(e.target.value)
                        }}
                        isInvalid={!!errors.leverage}
                        onBlur={() => trigger("leverage")}
                        onKeyDown={(
                          e: React.KeyboardEvent<HTMLInputElement>
                        ) => {
                          if (e.key === "Enter" && !isNaN(Number(value))) {
                            onChange(value)
                            onChangeLeverage(value)
                          }
                        }}
                        endContent={<span>x</span>}
                      />
                    </Tooltip>
                  </output>
                )}
                value={Number(value)}
                onChange={(newValue) => {
                  setValue("leverage", newValue.toString())
                  onChangeLeverage(newValue.toString())
                }}
              />
            </>
          )}
        />

@wingkwong
Copy link
Member

@armatrix or can you verify using this canary version?

"@nextui-org/slider": "0.0.0-canary-20240504162810"

@armatrix
Copy link
Author

nope, not working, click 20x, the value is still not 20.

@armatrix or can you verify using this canary version?

"@nextui-org/slider": "0.0.0-canary-20240504162810"
image

@wingkwong wingkwong added 🐛 Type: Bug Something isn't working 📦 Scope : Components Related to the components and removed 🔎 Status: More Info Needed labels May 13, 2024
@wingkwong
Copy link
Member

ok will take a look

@wingkwong
Copy link
Member

wingkwong commented May 13, 2024

preliminary findings

  • if the step is decimal, clicking the label is not setting the corresponding value if the click is a bit off
  • the click logic is from onDownTrack from react aria side
  • the value is calculated from percent, i.e. the percent for the target click on the track
  • minimal reproducible code
const [value, setValue] = React.useState<SliderValue>(25);

return (
  <div className="flex flex-col gap-2 w-full h-full max-w-md items-start justify-center">
    <Slider
      className="pb-6"
      classNames={{
        base: "max-w-md",
        label: "text-medium",
      }}
      defaultValue={2}
      label="Leverage"
      marks={[
        {
          value: 2,
          label: "2x",
        },
        {
          value: 10,
          label: "10x",
        },
        {
          value: 20,
          label: "20x",
        },
        {
          value: 30,
          label: "30x",
        },
        {
          value: 40,
          label: "40x",
        },
        {
          value: 50,
          label: "50x",
        },
      ]}
      maxValue={50}
      minValue={2}
      size="sm"
      step={0.01}
      value={Number(value)}
      onChange={setValue}
    />
    <p className="text-default-500 text-small">Current volume: {value}</p>
  </div>
);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📦 Scope : Components Related to the components 🐛 Type: Bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants