-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathSegment.tsx
More file actions
80 lines (72 loc) · 3.12 KB
/
Segment.tsx
File metadata and controls
80 lines (72 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { ReactElement, useMemo } from 'react'
import { Tooltip } from '@Common/Tooltip'
import { Icon } from '@Shared/Components'
import { ComponentSizeType } from '@Shared/constants'
import { getUniqueId } from '@Shared/Helpers'
import { ConditionalWrap } from '../Helper'
import { COMPONENT_SIZE_TO_ICON_CLASS_MAP, COMPONENT_SIZE_TO_SEGMENT_CLASS_MAP } from './constants'
import { SegmentProps, SegmentType } from './types'
const wrapWithTooltip = (tooltipProps: SegmentType['tooltipProps']) => (children: ReactElement) => (
<Tooltip content={tooltipProps.content} placement="bottom" {...tooltipProps} alwaysShowTippyOnHover>
{children}
</Tooltip>
)
const Segment = ({
segment,
isSelected,
name,
selectedSegmentRef,
onChange,
fullWidth,
size,
disabled,
}: SegmentProps) => {
const inputId = useMemo(getUniqueId, [])
const { value, icon, isError, label, tooltipProps, ariaLabel } = segment
const handleChange = () => {
onChange(segment)
}
return (
<ConditionalWrap key={value} condition={!!tooltipProps?.content} wrap={wrapWithTooltip(tooltipProps)}>
<div
className={`dc__position-rel dc__text-center dc__no-shrink ${fullWidth ? 'flex-grow-1' : ''}`}
ref={selectedSegmentRef}
>
<input
type="radio"
value={value}
id={inputId}
name={name}
onChange={handleChange}
checked={isSelected}
className="dc__opacity-0 m-0-imp dc__top-0 dc__left-0 dc__position-abs dc__bottom-0 dc__right-0 w-100 pointer h-100 dc__visibility-hidden"
disabled={disabled}
/>
<label
htmlFor={inputId}
className={`pointer m-0 flex ${!fullWidth ? 'left' : ''} dc__gap-4 br-4 segmented-control__segment segmented-control__segment--${size} ${isSelected ? 'fw-6 segmented-control__segment--selected' : 'fw-4'} ${segment.isError ? 'cr-5' : 'cn-9'} ${disabled ? 'cursor-not-allowed' : ''} ${COMPONENT_SIZE_TO_SEGMENT_CLASS_MAP[size]}`}
aria-label={ariaLabel}
>
{(isError || icon) && (
<span className={`flex ${COMPONENT_SIZE_TO_ICON_CLASS_MAP[size]}`}>
<Icon
{...(isError
? {
name: 'ic-error',
color: null,
}
: {
name: icon,
color: isSelected ? 'N900' : 'N700',
})}
size={size === ComponentSizeType.xs ? 14 : 16}
/>
</span>
)}
{label && <span>{label}</span>}
</label>
</div>
</ConditionalWrap>
)
}
export default Segment