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

feat: Slider #88

Merged
merged 3 commits into from
Jan 25, 2024
Merged
Changes from 2 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
54 changes: 54 additions & 0 deletions src/components/slider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import React, { forwardRef } from 'react'

import { cn } from '@/utils/cn'

export interface SliderProps extends React.InputHTMLAttributes<HTMLInputElement> {
errorMessage?: string
disabled?: boolean
icon?: React.ReactNode
min?: number
max?: number
value?: number
defaultValue?: number
}

export const Slider = forwardRef<HTMLInputElement, SliderProps>(function Slider(
{ errorMessage, defaultValue, value, disabled, ...props },
dianafulga marked this conversation as resolved.
Show resolved Hide resolved
ref,
) {
const [innerValue, setInnerValue] = React.useState<number>(value || defaultValue || 0)

return (
<div className="w-100">
<input
ref={ref}
type="range"
className={cn(
`[&::-webkit-slider-thumb]:appearance-none
[&::-webkit-slider-thumb]:w-5
[&::-webkit-slider-thumb]:h-5
[&::-webkit-slider-thumb]:bg-switch-base
[&::-webkit-slider-thumb]:rounded-full
[&::-moz-range-thumb]:appearance-none
[&::-moz-range-thumb]:w-5
[&::-moz-range-thumb]:h-5
[&::-moz-range-thumb]:bg-switch-base
[&::-moz-range-thumb]:rounded-full
w-full h-1 bg-disabled-strong rounded-lg appearance-none cursor-pointer dark:bg-disabled-strong`,
innerValue === 0 &&
'[&::-webkit-slider-thumb]:bg-disabled-strong [&::-moz-range-thumb]:bg-disabled-strong',
)}
dianafulga marked this conversation as resolved.
Show resolved Hide resolved
disabled={disabled ?? false}
aria-disabled={disabled ?? false}
aria-invalid={!!errorMessage}
aria-describedby={errorMessage}
defaultValue={defaultValue}
value={innerValue}
onChange={e => setInnerValue(Number(e.target.value))}
{...props}
/>

{errorMessage && <p className="text-error text-sm px-2">{errorMessage}</p>}
</div>
)
})