@registrucentras/rc-ses-react-components 1.9.0 → 1.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/assets/icons/PencilSimpleLineIcon.d.ts +1 -1
- package/dist/components/common/Badge/index.d.ts +10 -0
- package/dist/components/common/SegmentedControl/components/SegmentButton/index.d.ts +19 -0
- package/dist/components/common/SegmentedControl/index.d.ts +14 -0
- package/dist/components/common/Switch/index.d.ts +10 -0
- package/dist/components/form/inputs/SearchInput/index.d.ts +10 -0
- package/dist/library/index.d.ts +5 -1
- package/dist/library/types.d.ts +3 -1
- package/dist/registrucentras-rc-ses-react-components.cjs +10 -10
- package/dist/registrucentras-rc-ses-react-components.js +2885 -2460
- package/dist/stories/components/display/Badge.stories.d.ts +9 -0
- package/dist/stories/components/display/SegmentedControl.stories.d.ts +202 -0
- package/dist/stories/components/inputs/Switch.stories.d.ts +17 -0
- package/dist/theme/light/MuiSwitch.d.ts +3 -0
- package/package.json +1 -1
- package/src/assets/icons/PencilSimpleLineIcon.tsx +8 -2
- package/src/components/common/Badge/index.test.tsx +129 -0
- package/src/components/common/Badge/index.tsx +162 -0
- package/src/components/common/SegmentedControl/components/SegmentButton/index.test.tsx +85 -0
- package/src/components/common/SegmentedControl/components/SegmentButton/index.tsx +110 -0
- package/src/components/common/SegmentedControl/index.test.tsx +164 -0
- package/src/components/common/SegmentedControl/index.tsx +188 -0
- package/src/components/common/SelectableCardList/components/SelectableCardListItem.test.tsx +74 -1
- package/src/components/common/SelectableCardList/components/SelectableCardListItem.tsx +16 -1
- package/src/components/common/Switch/index.test.tsx +214 -0
- package/src/components/common/Switch/index.tsx +49 -0
- package/src/components/form/inputs/SearchInput/index.test.tsx +68 -2
- package/src/components/form/inputs/SearchInput/index.tsx +24 -13
- package/src/i18n/namespaces/common/en.json +4 -0
- package/src/i18n/namespaces/common/lt.json +4 -0
- package/src/i18n/namespaces/input/en.json +6 -0
- package/src/i18n/namespaces/input/lt.json +6 -0
- package/src/library/index.ts +6 -0
- package/src/library/types.ts +8 -0
- package/src/stories/components/display/Badge.stories.tsx +165 -0
- package/src/stories/components/display/SegmentedControl.stories.tsx +311 -0
- package/src/stories/components/inputs/Switch.stories.tsx +70 -0
- package/src/theme/light/MuiSwitch.tsx +77 -0
- package/src/theme/light/index.tsx +2 -0
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { Button as MuiButton } from '@mui/material'
|
|
2
|
+
import React, { useCallback, useMemo } from 'react'
|
|
3
|
+
|
|
4
|
+
import { grey, primary } from '@/theme/palette'
|
|
5
|
+
|
|
6
|
+
const SIZE_STYLES: Record<'small' | 'regular', React.CSSProperties> = {
|
|
7
|
+
small: {
|
|
8
|
+
width: 'auto',
|
|
9
|
+
maxWidth: '160px',
|
|
10
|
+
minHeight: '36px',
|
|
11
|
+
padding: '8px 12px',
|
|
12
|
+
fontSize: '14px',
|
|
13
|
+
lineHeight: '20px',
|
|
14
|
+
},
|
|
15
|
+
regular: {
|
|
16
|
+
width: 'auto',
|
|
17
|
+
maxWidth: '180px',
|
|
18
|
+
minHeight: '44px',
|
|
19
|
+
padding: '10px 16px',
|
|
20
|
+
fontSize: '15px',
|
|
21
|
+
lineHeight: '20px',
|
|
22
|
+
},
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface RcSesSegmentOption {
|
|
26
|
+
id: string
|
|
27
|
+
label: string
|
|
28
|
+
disabled?: boolean
|
|
29
|
+
panelId?: string
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface RcSesSegmentButtonProps {
|
|
33
|
+
option: RcSesSegmentOption
|
|
34
|
+
isSelected: boolean
|
|
35
|
+
isFocused: boolean
|
|
36
|
+
size: 'small' | 'regular'
|
|
37
|
+
ariaRole: 'tab' | 'radio'
|
|
38
|
+
onClick: (optionId: string) => void
|
|
39
|
+
onKeyDown: (e: React.KeyboardEvent<HTMLButtonElement>) => void
|
|
40
|
+
onFocus: () => void
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const RcSesSegmentButton = React.forwardRef<HTMLButtonElement, RcSesSegmentButtonProps>(
|
|
44
|
+
(
|
|
45
|
+
{ option, isSelected, isFocused, size, ariaRole, onClick, onKeyDown, onFocus },
|
|
46
|
+
ref,
|
|
47
|
+
) => {
|
|
48
|
+
const isDisabled = option.disabled
|
|
49
|
+
const sizeStyles = SIZE_STYLES[size]
|
|
50
|
+
|
|
51
|
+
const textColor = useMemo(() => {
|
|
52
|
+
if (isDisabled) return `${grey['500']} !important`
|
|
53
|
+
if (isSelected) return `${grey['900']} !important`
|
|
54
|
+
return `${primary['700']} !important`
|
|
55
|
+
}, [isDisabled, isSelected])
|
|
56
|
+
|
|
57
|
+
const buttonStyles = useMemo(
|
|
58
|
+
() =>
|
|
59
|
+
({
|
|
60
|
+
flex: '0 1 auto',
|
|
61
|
+
minWidth: '0',
|
|
62
|
+
height: 'auto',
|
|
63
|
+
wordBreak: 'break-word',
|
|
64
|
+
fontFamily: '"Public Sans", sans-serif !important',
|
|
65
|
+
border: `2px solid ${isFocused && !isDisabled ? primary['500'] : 'transparent'}`,
|
|
66
|
+
backgroundColor: isSelected
|
|
67
|
+
? `${primary['50']} !important`
|
|
68
|
+
: 'transparent !important',
|
|
69
|
+
color: textColor,
|
|
70
|
+
cursor: isDisabled ? 'not-allowed' : 'pointer',
|
|
71
|
+
fontWeight: 500,
|
|
72
|
+
borderRadius: '8px',
|
|
73
|
+
transition: 'all 150ms ease-in-out',
|
|
74
|
+
whiteSpace: 'normal',
|
|
75
|
+
...sizeStyles,
|
|
76
|
+
}) as React.CSSProperties,
|
|
77
|
+
[sizeStyles, isDisabled, isSelected, isFocused, textColor],
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
const handleClick = useCallback(() => {
|
|
81
|
+
onClick(option.id)
|
|
82
|
+
}, [option.id, onClick])
|
|
83
|
+
|
|
84
|
+
return (
|
|
85
|
+
<MuiButton
|
|
86
|
+
ref={ref}
|
|
87
|
+
type='button'
|
|
88
|
+
onClick={handleClick}
|
|
89
|
+
onKeyDown={onKeyDown}
|
|
90
|
+
onFocus={onFocus}
|
|
91
|
+
disabled={isDisabled}
|
|
92
|
+
role={ariaRole}
|
|
93
|
+
aria-checked={ariaRole === 'radio' ? isSelected : undefined}
|
|
94
|
+
aria-selected={ariaRole === 'tab' ? isSelected : undefined}
|
|
95
|
+
aria-controls={option.panelId}
|
|
96
|
+
tabIndex={isFocused && !isDisabled ? 0 : -1}
|
|
97
|
+
sx={{
|
|
98
|
+
...buttonStyles,
|
|
99
|
+
'&.Mui-disabled': {
|
|
100
|
+
backgroundColor: 'transparent !important',
|
|
101
|
+
},
|
|
102
|
+
}}
|
|
103
|
+
>
|
|
104
|
+
{option.label}
|
|
105
|
+
</MuiButton>
|
|
106
|
+
)
|
|
107
|
+
},
|
|
108
|
+
)
|
|
109
|
+
|
|
110
|
+
export default RcSesSegmentButton
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { fireEvent, render, screen } from '@testing-library/react'
|
|
2
|
+
import { describe, expect, test, vi } from 'vitest'
|
|
3
|
+
|
|
4
|
+
import RcSesSegmentedControl, { type RcSesSegmentOption } from './index'
|
|
5
|
+
|
|
6
|
+
describe('RcSesSegmentedControl', () => {
|
|
7
|
+
const options: RcSesSegmentOption[] = [
|
|
8
|
+
{ id: 'option-1', label: 'Option 1', disabled: false },
|
|
9
|
+
{ id: 'option-2', label: 'Option 2', disabled: false },
|
|
10
|
+
{ id: 'option-3', label: 'Option 3', disabled: false },
|
|
11
|
+
]
|
|
12
|
+
|
|
13
|
+
const defaultProps = {
|
|
14
|
+
options,
|
|
15
|
+
value: 'option-1',
|
|
16
|
+
onChange: vi.fn(),
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
test('renders all options', () => {
|
|
20
|
+
render(<RcSesSegmentedControl {...defaultProps} />)
|
|
21
|
+
|
|
22
|
+
expect(screen.getByRole('radio', { name: 'Option 1' })).toBeInTheDocument()
|
|
23
|
+
expect(screen.getByRole('radio', { name: 'Option 2' })).toBeInTheDocument()
|
|
24
|
+
expect(screen.getByRole('radio', { name: 'Option 3' })).toBeInTheDocument()
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
test('has correct container role (radiogroup by default)', () => {
|
|
28
|
+
render(<RcSesSegmentedControl {...defaultProps} />)
|
|
29
|
+
|
|
30
|
+
expect(screen.getByRole('radiogroup')).toBeInTheDocument()
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
test('marks selected option with aria-checked=true', () => {
|
|
34
|
+
render(<RcSesSegmentedControl {...defaultProps} value='option-2' />)
|
|
35
|
+
|
|
36
|
+
expect(screen.getByRole('radio', { name: 'Option 2' })).toHaveAttribute(
|
|
37
|
+
'aria-checked',
|
|
38
|
+
'true',
|
|
39
|
+
)
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
test('calls onChange when option clicked', () => {
|
|
43
|
+
const onChange = vi.fn()
|
|
44
|
+
render(<RcSesSegmentedControl {...defaultProps} onChange={onChange} />)
|
|
45
|
+
|
|
46
|
+
fireEvent.click(screen.getByRole('radio', { name: 'Option 2' }))
|
|
47
|
+
expect(onChange).toHaveBeenCalledWith('option-2')
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
test('navigates with ArrowRight key', () => {
|
|
51
|
+
const onChange = vi.fn()
|
|
52
|
+
render(<RcSesSegmentedControl {...defaultProps} onChange={onChange} />)
|
|
53
|
+
|
|
54
|
+
const option1 = screen.getByRole('radio', { name: 'Option 1' })
|
|
55
|
+
fireEvent.keyDown(option1, { key: 'ArrowRight' })
|
|
56
|
+
|
|
57
|
+
expect(onChange).toHaveBeenCalledWith('option-2')
|
|
58
|
+
expect(screen.getByRole('radio', { name: 'Option 2' })).toHaveFocus()
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
test('jumps to first/last option with Home/End keys', () => {
|
|
62
|
+
const onChange = vi.fn()
|
|
63
|
+
render(
|
|
64
|
+
<RcSesSegmentedControl {...defaultProps} onChange={onChange} value='option-2' />,
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
const option2 = screen.getByRole('radio', { name: 'Option 2' })
|
|
68
|
+
fireEvent.keyDown(option2, { key: 'Home' })
|
|
69
|
+
|
|
70
|
+
expect(onChange).toHaveBeenCalledWith('option-1')
|
|
71
|
+
expect(screen.getByRole('radio', { name: 'Option 1' })).toHaveFocus()
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
test('selects option with Enter key', () => {
|
|
75
|
+
const onChange = vi.fn()
|
|
76
|
+
render(
|
|
77
|
+
<RcSesSegmentedControl {...defaultProps} onChange={onChange} value='option-1' />,
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
const option2 = screen.getByRole('radio', { name: 'Option 2' })
|
|
81
|
+
fireEvent.focus(option2)
|
|
82
|
+
fireEvent.keyDown(option2, { key: 'Enter' })
|
|
83
|
+
|
|
84
|
+
expect(onChange).toHaveBeenCalledWith('option-2')
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
test('selects option with Space key', () => {
|
|
88
|
+
const onChange = vi.fn()
|
|
89
|
+
render(
|
|
90
|
+
<RcSesSegmentedControl {...defaultProps} onChange={onChange} value='option-1' />,
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
const option2 = screen.getByRole('radio', { name: 'Option 2' })
|
|
94
|
+
fireEvent.keyDown(option2, { key: ' ' })
|
|
95
|
+
|
|
96
|
+
expect(onChange).toHaveBeenCalledWith('option-2')
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
test('skips disabled options in navigation', () => {
|
|
100
|
+
const onChange = vi.fn()
|
|
101
|
+
const optionsWithDisabled: RcSesSegmentOption[] = [
|
|
102
|
+
{ id: 'option-1', label: 'Option 1', disabled: false },
|
|
103
|
+
{ id: 'option-2', label: 'Option 2', disabled: true },
|
|
104
|
+
{ id: 'option-3', label: 'Option 3', disabled: false },
|
|
105
|
+
]
|
|
106
|
+
|
|
107
|
+
render(
|
|
108
|
+
<RcSesSegmentedControl
|
|
109
|
+
options={optionsWithDisabled}
|
|
110
|
+
value='option-1'
|
|
111
|
+
onChange={onChange}
|
|
112
|
+
/>,
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
const option1 = screen.getByRole('radio', { name: 'Option 1' })
|
|
116
|
+
fireEvent.keyDown(option1, { key: 'ArrowRight' })
|
|
117
|
+
|
|
118
|
+
expect(onChange).toHaveBeenCalledWith('option-3')
|
|
119
|
+
expect(screen.getByRole('radio', { name: 'Option 3' })).toHaveFocus()
|
|
120
|
+
})
|
|
121
|
+
|
|
122
|
+
test('disables disabled options', () => {
|
|
123
|
+
const optionsWithDisabled: RcSesSegmentOption[] = [
|
|
124
|
+
{ id: 'option-1', label: 'Option 1', disabled: false },
|
|
125
|
+
{ id: 'option-2', label: 'Option 2', disabled: true },
|
|
126
|
+
]
|
|
127
|
+
|
|
128
|
+
render(
|
|
129
|
+
<RcSesSegmentedControl
|
|
130
|
+
options={optionsWithDisabled}
|
|
131
|
+
value='option-1'
|
|
132
|
+
onChange={vi.fn()}
|
|
133
|
+
/>,
|
|
134
|
+
)
|
|
135
|
+
|
|
136
|
+
expect(screen.getByRole('radio', { name: 'Option 2' })).toBeDisabled()
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
test('supports tab role with tab buttons', () => {
|
|
140
|
+
render(<RcSesSegmentedControl {...defaultProps} role='tablist' />)
|
|
141
|
+
|
|
142
|
+
expect(screen.getByRole('tab', { name: 'Option 1' })).toBeInTheDocument()
|
|
143
|
+
expect(screen.getByRole('tab', { name: 'Option 2' })).toBeInTheDocument()
|
|
144
|
+
expect(screen.getByRole('tab', { name: 'Option 3' })).toBeInTheDocument()
|
|
145
|
+
})
|
|
146
|
+
|
|
147
|
+
test('updates selected option when value prop changes', () => {
|
|
148
|
+
const { rerender } = render(
|
|
149
|
+
<RcSesSegmentedControl {...defaultProps} value='option-1' />,
|
|
150
|
+
)
|
|
151
|
+
|
|
152
|
+
expect(screen.getByRole('radio', { name: 'Option 1' })).toHaveAttribute(
|
|
153
|
+
'aria-checked',
|
|
154
|
+
'true',
|
|
155
|
+
)
|
|
156
|
+
|
|
157
|
+
rerender(<RcSesSegmentedControl {...defaultProps} value='option-2' />)
|
|
158
|
+
|
|
159
|
+
expect(screen.getByRole('radio', { name: 'Option 2' })).toHaveAttribute(
|
|
160
|
+
'aria-checked',
|
|
161
|
+
'true',
|
|
162
|
+
)
|
|
163
|
+
})
|
|
164
|
+
})
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import React, { useCallback, useImperativeHandle, useMemo, useRef, useState } from 'react'
|
|
2
|
+
import { useTranslation } from 'react-i18next'
|
|
3
|
+
|
|
4
|
+
import { grey } from '@/theme/palette'
|
|
5
|
+
|
|
6
|
+
import RcSesSegmentButton, { type RcSesSegmentOption } from './components/SegmentButton'
|
|
7
|
+
|
|
8
|
+
export type { RcSesSegmentOption }
|
|
9
|
+
|
|
10
|
+
export interface RcSesSegmentedControlProps {
|
|
11
|
+
options: RcSesSegmentOption[]
|
|
12
|
+
value: string
|
|
13
|
+
onChange: (value: string) => void
|
|
14
|
+
size?: 'small' | 'regular'
|
|
15
|
+
role?: 'radiogroup' | 'tablist'
|
|
16
|
+
disableBg?: boolean
|
|
17
|
+
ariaLabel?: string
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const RcSesSegmentedControl = React.forwardRef<
|
|
21
|
+
HTMLDivElement,
|
|
22
|
+
RcSesSegmentedControlProps
|
|
23
|
+
>(
|
|
24
|
+
(
|
|
25
|
+
{
|
|
26
|
+
options,
|
|
27
|
+
value,
|
|
28
|
+
onChange,
|
|
29
|
+
size = 'regular',
|
|
30
|
+
role = 'radiogroup',
|
|
31
|
+
disableBg = false,
|
|
32
|
+
ariaLabel,
|
|
33
|
+
},
|
|
34
|
+
ref,
|
|
35
|
+
) => {
|
|
36
|
+
const { t } = useTranslation('common')
|
|
37
|
+
const [focusIndex, setFocusIndex] = useState(() => {
|
|
38
|
+
const selectedIndex = options.findIndex((opt) => opt.id === value && !opt.disabled)
|
|
39
|
+
if (selectedIndex !== -1) return selectedIndex
|
|
40
|
+
return Math.max(
|
|
41
|
+
0,
|
|
42
|
+
options.findIndex((opt) => !opt.disabled),
|
|
43
|
+
)
|
|
44
|
+
})
|
|
45
|
+
const containerRef = useRef<HTMLDivElement>(null)
|
|
46
|
+
const buttonRefs = useRef<(HTMLButtonElement | null)[]>([])
|
|
47
|
+
|
|
48
|
+
useImperativeHandle(ref, () => containerRef.current as HTMLDivElement)
|
|
49
|
+
|
|
50
|
+
React.useEffect(() => {
|
|
51
|
+
const selectedIndex = options.findIndex((opt) => opt.id === value && !opt.disabled)
|
|
52
|
+
if (selectedIndex !== -1) setFocusIndex(selectedIndex)
|
|
53
|
+
}, [value, options])
|
|
54
|
+
|
|
55
|
+
const getEnabledIndices = useCallback(
|
|
56
|
+
() => options.map((opt, i) => (!opt.disabled ? i : -1)).filter((i) => i !== -1),
|
|
57
|
+
[options],
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
const focusSegment = useCallback((index: number) => {
|
|
61
|
+
setFocusIndex(index)
|
|
62
|
+
buttonRefs.current[index]?.focus()
|
|
63
|
+
}, [])
|
|
64
|
+
|
|
65
|
+
const handleKeyDown = useCallback(
|
|
66
|
+
(e: React.KeyboardEvent<HTMLButtonElement>, index: number) => {
|
|
67
|
+
const enabledIndices = getEnabledIndices()
|
|
68
|
+
const currentPos = enabledIndices.indexOf(index)
|
|
69
|
+
|
|
70
|
+
switch (e.key) {
|
|
71
|
+
case 'ArrowLeft':
|
|
72
|
+
case 'ArrowUp': {
|
|
73
|
+
e.preventDefault()
|
|
74
|
+
const prevIndex =
|
|
75
|
+
currentPos > 0
|
|
76
|
+
? enabledIndices[currentPos - 1]
|
|
77
|
+
: enabledIndices[enabledIndices.length - 1]
|
|
78
|
+
focusSegment(prevIndex)
|
|
79
|
+
onChange(options[prevIndex].id)
|
|
80
|
+
break
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
case 'ArrowRight':
|
|
84
|
+
case 'ArrowDown': {
|
|
85
|
+
e.preventDefault()
|
|
86
|
+
const nextIndex =
|
|
87
|
+
currentPos < enabledIndices.length - 1
|
|
88
|
+
? enabledIndices[currentPos + 1]
|
|
89
|
+
: enabledIndices[0]
|
|
90
|
+
focusSegment(nextIndex)
|
|
91
|
+
onChange(options[nextIndex].id)
|
|
92
|
+
break
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
case 'Home': {
|
|
96
|
+
e.preventDefault()
|
|
97
|
+
const firstIndex = enabledIndices[0]
|
|
98
|
+
focusSegment(firstIndex)
|
|
99
|
+
onChange(options[firstIndex].id)
|
|
100
|
+
break
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
case 'End': {
|
|
104
|
+
e.preventDefault()
|
|
105
|
+
const lastIndex = enabledIndices[enabledIndices.length - 1]
|
|
106
|
+
focusSegment(lastIndex)
|
|
107
|
+
onChange(options[lastIndex].id)
|
|
108
|
+
break
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
case 'Enter':
|
|
112
|
+
case ' ':
|
|
113
|
+
e.preventDefault()
|
|
114
|
+
onChange(options[index].id)
|
|
115
|
+
break
|
|
116
|
+
|
|
117
|
+
default:
|
|
118
|
+
break
|
|
119
|
+
}
|
|
120
|
+
},
|
|
121
|
+
[focusSegment, getEnabledIndices, onChange, options],
|
|
122
|
+
)
|
|
123
|
+
|
|
124
|
+
const handleSegmentClick = useCallback(
|
|
125
|
+
(optionId: string, index: number) => {
|
|
126
|
+
onChange(optionId)
|
|
127
|
+
setFocusIndex(index)
|
|
128
|
+
},
|
|
129
|
+
[onChange],
|
|
130
|
+
)
|
|
131
|
+
|
|
132
|
+
const handleFocus = useCallback((index: number) => {
|
|
133
|
+
setFocusIndex(index)
|
|
134
|
+
}, [])
|
|
135
|
+
|
|
136
|
+
const isSelected = useCallback((optionId: string) => value === optionId, [value])
|
|
137
|
+
const ariaRole = role === 'tablist' ? 'tab' : 'radio'
|
|
138
|
+
const defaultAriaLabel =
|
|
139
|
+
ariaLabel ||
|
|
140
|
+
t(
|
|
141
|
+
role === 'tablist'
|
|
142
|
+
? 'components.SegmentedControl.tabs'
|
|
143
|
+
: 'components.SegmentedControl.options',
|
|
144
|
+
)
|
|
145
|
+
const finalAriaLabel = defaultAriaLabel
|
|
146
|
+
|
|
147
|
+
const containerStyles = useMemo<React.CSSProperties>(
|
|
148
|
+
() => ({
|
|
149
|
+
display: 'flex',
|
|
150
|
+
flexWrap: 'wrap',
|
|
151
|
+
gap: '2px 8px',
|
|
152
|
+
padding: '4px',
|
|
153
|
+
backgroundColor: disableBg ? 'transparent' : grey['50'],
|
|
154
|
+
width: 'fit-content',
|
|
155
|
+
borderRadius: '8px',
|
|
156
|
+
}),
|
|
157
|
+
[disableBg],
|
|
158
|
+
)
|
|
159
|
+
|
|
160
|
+
return (
|
|
161
|
+
<div
|
|
162
|
+
ref={containerRef}
|
|
163
|
+
role={role}
|
|
164
|
+
aria-label={finalAriaLabel}
|
|
165
|
+
style={containerStyles}
|
|
166
|
+
>
|
|
167
|
+
{options.map((option, index) => (
|
|
168
|
+
<RcSesSegmentButton
|
|
169
|
+
key={option.id}
|
|
170
|
+
ref={(el) => {
|
|
171
|
+
buttonRefs.current[index] = el
|
|
172
|
+
}}
|
|
173
|
+
option={option}
|
|
174
|
+
isSelected={isSelected(option.id)}
|
|
175
|
+
isFocused={focusIndex === index && !option.disabled}
|
|
176
|
+
size={size}
|
|
177
|
+
ariaRole={ariaRole}
|
|
178
|
+
onClick={(optionId) => handleSegmentClick(optionId, index)}
|
|
179
|
+
onKeyDown={(e) => handleKeyDown(e, index)}
|
|
180
|
+
onFocus={() => handleFocus(index)}
|
|
181
|
+
/>
|
|
182
|
+
))}
|
|
183
|
+
</div>
|
|
184
|
+
)
|
|
185
|
+
},
|
|
186
|
+
)
|
|
187
|
+
|
|
188
|
+
export default RcSesSegmentedControl
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ThemeProvider } from '@mui/material/styles'
|
|
2
|
-
import { render, screen } from '@testing-library/react'
|
|
2
|
+
import { fireEvent, render, screen } from '@testing-library/react'
|
|
3
3
|
import { type ReactElement } from 'react'
|
|
4
4
|
import { describe, expect, test, vi } from 'vitest'
|
|
5
5
|
|
|
@@ -94,4 +94,77 @@ describe('SelectableCardListItem', () => {
|
|
|
94
94
|
expect(screen.getByText('List entry A')).toBeInTheDocument()
|
|
95
95
|
expect(screen.getByText('List entry B')).toBeInTheDocument()
|
|
96
96
|
})
|
|
97
|
+
|
|
98
|
+
test('calls onSelect once when Enter key is pressed on radio', () => {
|
|
99
|
+
const onSelect = vi.fn()
|
|
100
|
+
renderItem(
|
|
101
|
+
<SelectableCardListItem
|
|
102
|
+
title='Test'
|
|
103
|
+
selected={false}
|
|
104
|
+
onSelect={onSelect}
|
|
105
|
+
listItems={[]}
|
|
106
|
+
/>,
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
const radio = screen.getByRole('radio')
|
|
110
|
+
radio.focus()
|
|
111
|
+
fireEvent.keyDown(radio, { key: 'Enter', code: 'Enter' })
|
|
112
|
+
|
|
113
|
+
expect(onSelect).toHaveBeenCalledTimes(1)
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
test('calls onSelect once when radio is clicked (no double-call)', () => {
|
|
117
|
+
const onSelect = vi.fn()
|
|
118
|
+
renderItem(
|
|
119
|
+
<SelectableCardListItem
|
|
120
|
+
title='Test'
|
|
121
|
+
selected={false}
|
|
122
|
+
onSelect={onSelect}
|
|
123
|
+
listItems={[]}
|
|
124
|
+
/>,
|
|
125
|
+
)
|
|
126
|
+
|
|
127
|
+
const radio = screen.getByRole('radio')
|
|
128
|
+
fireEvent.click(radio)
|
|
129
|
+
|
|
130
|
+
expect(onSelect).toHaveBeenCalledTimes(1)
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
test('calls onSelect once when card is clicked outside radio', () => {
|
|
134
|
+
const onSelect = vi.fn()
|
|
135
|
+
renderItem(
|
|
136
|
+
<SelectableCardListItem
|
|
137
|
+
title='Test title'
|
|
138
|
+
selected={false}
|
|
139
|
+
onSelect={onSelect}
|
|
140
|
+
listItems={[]}
|
|
141
|
+
/>,
|
|
142
|
+
)
|
|
143
|
+
|
|
144
|
+
// click on the title area (outside the radio)
|
|
145
|
+
const titleElement = screen.getByRole('heading', { name: 'Test title' })
|
|
146
|
+
fireEvent.click(titleElement)
|
|
147
|
+
|
|
148
|
+
expect(onSelect).toHaveBeenCalledTimes(1)
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
test('loading card is not selectable', () => {
|
|
152
|
+
const onSelect = vi.fn()
|
|
153
|
+
const { container } = renderItem(
|
|
154
|
+
<SelectableCardListItem
|
|
155
|
+
title='Loading'
|
|
156
|
+
selected={false}
|
|
157
|
+
onSelect={onSelect}
|
|
158
|
+
isLoading
|
|
159
|
+
listItems={[]}
|
|
160
|
+
/>,
|
|
161
|
+
)
|
|
162
|
+
|
|
163
|
+
// no radio is rendered while loading, so there is no keyboard tab stop
|
|
164
|
+
expect(screen.queryByRole('radio')).toBeNull()
|
|
165
|
+
|
|
166
|
+
// mouse clicks are blocked by pointer-events on the card root
|
|
167
|
+
const card = container.querySelector('.MuiCard-root')
|
|
168
|
+
expect(card).toHaveStyle('pointer-events: none')
|
|
169
|
+
})
|
|
97
170
|
})
|
|
@@ -33,6 +33,18 @@ const SelectableCardListItem = ({
|
|
|
33
33
|
<Radio
|
|
34
34
|
aria-label={`Select ${title}`}
|
|
35
35
|
checked={selected}
|
|
36
|
+
onChange={(e) => {
|
|
37
|
+
e.stopPropagation()
|
|
38
|
+
onSelect()
|
|
39
|
+
}}
|
|
40
|
+
onClick={(e) => e.stopPropagation()}
|
|
41
|
+
onKeyDown={(e) => {
|
|
42
|
+
if (e.key === 'Enter') {
|
|
43
|
+
e.preventDefault()
|
|
44
|
+
e.stopPropagation()
|
|
45
|
+
onSelect()
|
|
46
|
+
}
|
|
47
|
+
}}
|
|
36
48
|
sx={{
|
|
37
49
|
p: 0,
|
|
38
50
|
height: '1.25rem',
|
|
@@ -78,8 +90,11 @@ const SelectableCardListItem = ({
|
|
|
78
90
|
borderRadius: '0.75rem',
|
|
79
91
|
padding: { xs: '1rem' },
|
|
80
92
|
borderColor: selected ? palette.primary.main : palette.grey[300],
|
|
81
|
-
transition: '
|
|
93
|
+
transition: 'border-color 0.2s ease',
|
|
82
94
|
gap: { xs: '0.5rem', md: '0.25rem' },
|
|
95
|
+
'&:has(input:focus-visible)': {
|
|
96
|
+
borderColor: palette.grey[900],
|
|
97
|
+
},
|
|
83
98
|
}}
|
|
84
99
|
title={titleTemplate}
|
|
85
100
|
slotProps={{
|