Skip to content

Commit

Permalink
chore: Added breakpoint index support, in order to improve the visual…
Browse files Browse the repository at this point in the history
…ise once we have more than one line (rounded the corners also in the middle of the array) (#208)
  • Loading branch information
udia76 authored Nov 19, 2024
1 parent 7bd962c commit 0ead3f8
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,16 @@ interface DayPickerProps {
onChange: (days: string) => void
options?: Option[]
isDisabled?: boolean
breakpointIndex?: number
}

const DayPicker = ({ days, onChange, options, isDisabled }: DayPickerProps) => {
const DayPicker = ({
days,
onChange,
options,
isDisabled,
breakpointIndex
}: DayPickerProps) => {
const allDays = useMemo(
() => (options || DAYS_OF_WEEK).map((day) => day.value),
[options]
Expand Down Expand Up @@ -64,6 +71,7 @@ const DayPicker = ({ days, onChange, options, isDisabled }: DayPickerProps) => {
wrapperClass='day-picker-wrapper'
isDisabled={isDisabled}
small
breakpointIndex={breakpointIndex}
/>
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,14 @@ const MonthlySelector: FC<MonthlySelectorProps> = ({
if (daysInSelectedMonths.includes(31)) {
return 'specific-month-31-days'
}
return 'specific-month-days'
return 'every-month-days'
}

const breakpointIndex = useMemo(
() => Math.ceil(daysOfMonth.length / 2),
[daysOfMonth]
)

return (
<>
<div className='monthly-selector-wrapper'>
Expand Down Expand Up @@ -202,6 +207,7 @@ const MonthlySelector: FC<MonthlySelectorProps> = ({
onChange={handleDateChange}
options={daysOfMonth}
isDisabled={isDisabled}
breakpointIndex={breakpointIndex}
/>
</div>
</div>
Expand Down
54 changes: 33 additions & 21 deletions lib/components/ToggleButton/ToggleButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ interface ToggleButtonProps {
small?: boolean
isDisabled?: boolean
wrapperClass?: string
breakpointIndex?: number
}

function ToggleButton(props: ToggleButtonProps) {
Expand All @@ -25,36 +26,47 @@ function ToggleButton(props: ToggleButtonProps) {
onChange,
small,
wrapperClass = EMPTY_STRING,
isDisabled
isDisabled,
breakpointIndex
} = props

const elementsList = useRef<HTMLDivElement>(null)
const instanceId = useRef(
`toggle-button-${Math.random().toString(36).slice(2, 11)}`
)

const selectElement = useCallback((option: HTMLElement, pad: HTMLElement) => {
if (option && pad) {
const optionRect = option.getBoundingClientRect()
const parentRect = option.parentElement?.getBoundingClientRect()
pad.style.width = `${optionRect.width}px`
pad.style.height = `${optionRect.height}px`
pad.style.transform = `translateX(${
optionRect.left - (parentRect?.left || 0)
}px) translateY(${optionRect.top - (parentRect?.top || 0)}px)`
pad.style.display = 'block'
const selectElement = useCallback(
(option: HTMLElement, pad: HTMLElement) => {
if (option && pad) {
const optionRect = option.getBoundingClientRect()
const parentRect = option.parentElement?.getBoundingClientRect()
pad.style.width = `${optionRect.width}px`
pad.style.height = `${optionRect.height}px`
pad.style.transform = `translateX(${
optionRect.left - (parentRect?.left || 0)
}px) translateY(${optionRect.top - (parentRect?.top || 0)}px)`
pad.style.display = 'block'

if (option.matches(':last-child')) {
pad.style.borderRadius = '0 5px 5px 0'
} else if (option.matches(':first-child')) {
pad.style.borderRadius = '5px 0 0 5px'
} else {
pad.style.borderRadius = '0'
}
if (
option.matches(':last-child') ||
option.getAttribute('data-value') === breakpointIndex?.toString()
) {
pad.style.borderRadius = '0 5px 5px 0'
} else if (
option.matches(':first-child') ||
option.getAttribute('data-value') ===
((breakpointIndex || 0) + 1)?.toString()
) {
pad.style.borderRadius = '5px 0 0 5px'
} else {
pad.style.borderRadius = '0'
}

option.classList.add('toggle-button-option-selected')
}
}, [])
option.classList.add('toggle-button-option-selected')
}
},
[breakpointIndex]
)

const updatePadPosition = useCallback(() => {
const instanceElement = document.getElementById(instanceId.current)
Expand Down

0 comments on commit 0ead3f8

Please sign in to comment.