@registrucentras/rc-ses-react-components 1.9.0 → 1.10.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/SegmentedControl/components/SegmentButton/index.d.ts +19 -0
- package/dist/components/common/SegmentedControl/index.d.ts +14 -0
- package/dist/components/form/inputs/SearchInput/index.d.ts +10 -0
- package/dist/library/index.d.ts +2 -0
- 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 +2240 -2042
- package/dist/stories/components/display/SegmentedControl.stories.d.ts +202 -0
- package/package.json +1 -1
- package/src/assets/icons/PencilSimpleLineIcon.tsx +8 -2
- 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/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/library/index.ts +2 -0
- package/src/library/types.ts +8 -0
- package/src/stories/components/display/SegmentedControl.stories.tsx +311 -0
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
import { ForwardRefExoticComponent, RefAttributes } from 'react';
|
|
2
|
+
import { RcSesSegmentedControlProps } from '../../../components/common/SegmentedControl';
|
|
3
|
+
declare const meta: {
|
|
4
|
+
title: string;
|
|
5
|
+
component: ForwardRefExoticComponent< RcSesSegmentedControlProps & RefAttributes<HTMLDivElement>>;
|
|
6
|
+
parameters: {
|
|
7
|
+
layout: string;
|
|
8
|
+
};
|
|
9
|
+
tags: string[];
|
|
10
|
+
argTypes: {
|
|
11
|
+
options: {
|
|
12
|
+
control: false;
|
|
13
|
+
description: string;
|
|
14
|
+
};
|
|
15
|
+
value: {
|
|
16
|
+
control: "text";
|
|
17
|
+
description: string;
|
|
18
|
+
};
|
|
19
|
+
onChange: {
|
|
20
|
+
control: false;
|
|
21
|
+
description: string;
|
|
22
|
+
};
|
|
23
|
+
size: {
|
|
24
|
+
control: "radio";
|
|
25
|
+
options: string[];
|
|
26
|
+
description: string;
|
|
27
|
+
};
|
|
28
|
+
role: {
|
|
29
|
+
control: "radio";
|
|
30
|
+
options: string[];
|
|
31
|
+
description: string;
|
|
32
|
+
};
|
|
33
|
+
disableBg: {
|
|
34
|
+
control: "boolean";
|
|
35
|
+
description: string;
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
};
|
|
39
|
+
export default meta;
|
|
40
|
+
export declare const Default: {
|
|
41
|
+
readonly args: {
|
|
42
|
+
readonly options: readonly [{
|
|
43
|
+
readonly id: "1";
|
|
44
|
+
readonly label: "Fizinis asmuo";
|
|
45
|
+
}, {
|
|
46
|
+
readonly id: "2";
|
|
47
|
+
readonly label: "Juridinis asmuo";
|
|
48
|
+
}, {
|
|
49
|
+
readonly id: "3";
|
|
50
|
+
readonly label: "Advokatas";
|
|
51
|
+
}];
|
|
52
|
+
readonly value: "1";
|
|
53
|
+
readonly onChange: () => void;
|
|
54
|
+
readonly size: "regular";
|
|
55
|
+
readonly role: "radiogroup";
|
|
56
|
+
readonly disableBg: false;
|
|
57
|
+
};
|
|
58
|
+
readonly render: ({ options, value: initialValue, onChange, size, role, disableBg }: any) => import("react/jsx-runtime").JSX.Element;
|
|
59
|
+
};
|
|
60
|
+
export declare const RegularTwoOptions: {
|
|
61
|
+
readonly args: {
|
|
62
|
+
readonly options: readonly [{
|
|
63
|
+
readonly id: "1";
|
|
64
|
+
readonly label: "Fizinis asmuo";
|
|
65
|
+
}, {
|
|
66
|
+
readonly id: "2";
|
|
67
|
+
readonly label: "Juridinis asmuo";
|
|
68
|
+
}];
|
|
69
|
+
readonly value: "1";
|
|
70
|
+
readonly size: "regular";
|
|
71
|
+
};
|
|
72
|
+
readonly render: (args: any) => import("react/jsx-runtime").JSX.Element;
|
|
73
|
+
readonly parameters: {
|
|
74
|
+
readonly docs: {
|
|
75
|
+
readonly source: {
|
|
76
|
+
readonly code: "const [value, setValue] = useState('1');\n\n<RcSesSegmentedControl\n options={[\n { id: '1', label: 'Fizinis asmuo' },\n { id: '2', label: 'Juridinis asmuo' },\n ]}\n value={value}\n onChange={(newValue) => setValue(newValue)}\n size=\"regular\"\n/>";
|
|
77
|
+
};
|
|
78
|
+
};
|
|
79
|
+
};
|
|
80
|
+
};
|
|
81
|
+
export declare const RegularFourOptions: {
|
|
82
|
+
args: {
|
|
83
|
+
options: {
|
|
84
|
+
id: string;
|
|
85
|
+
label: string;
|
|
86
|
+
}[];
|
|
87
|
+
value: string;
|
|
88
|
+
size: string;
|
|
89
|
+
};
|
|
90
|
+
render: (args: any) => import("react/jsx-runtime").JSX.Element;
|
|
91
|
+
parameters: {
|
|
92
|
+
docs: {
|
|
93
|
+
source: {
|
|
94
|
+
code: string;
|
|
95
|
+
};
|
|
96
|
+
};
|
|
97
|
+
};
|
|
98
|
+
};
|
|
99
|
+
export declare const RegularWithDisabled: {
|
|
100
|
+
args: {
|
|
101
|
+
options: ({
|
|
102
|
+
id: string;
|
|
103
|
+
label: string;
|
|
104
|
+
disabled?: undefined;
|
|
105
|
+
} | {
|
|
106
|
+
id: string;
|
|
107
|
+
label: string;
|
|
108
|
+
disabled: boolean;
|
|
109
|
+
})[];
|
|
110
|
+
value: string;
|
|
111
|
+
size: string;
|
|
112
|
+
};
|
|
113
|
+
render: (args: any) => import("react/jsx-runtime").JSX.Element;
|
|
114
|
+
parameters: {
|
|
115
|
+
docs: {
|
|
116
|
+
source: {
|
|
117
|
+
code: string;
|
|
118
|
+
};
|
|
119
|
+
};
|
|
120
|
+
};
|
|
121
|
+
};
|
|
122
|
+
export declare const SmallTwoOptions: {
|
|
123
|
+
readonly args: {
|
|
124
|
+
readonly options: readonly [{
|
|
125
|
+
readonly id: "1";
|
|
126
|
+
readonly label: "Fizinis asmuo";
|
|
127
|
+
}, {
|
|
128
|
+
readonly id: "2";
|
|
129
|
+
readonly label: "Juridinis asmuo";
|
|
130
|
+
}];
|
|
131
|
+
readonly value: "1";
|
|
132
|
+
readonly size: "small";
|
|
133
|
+
};
|
|
134
|
+
readonly render: (args: any) => import("react/jsx-runtime").JSX.Element;
|
|
135
|
+
readonly parameters: {
|
|
136
|
+
readonly docs: {
|
|
137
|
+
readonly source: {
|
|
138
|
+
readonly code: "const [value, setValue] = useState('1');\n\n<RcSesSegmentedControl\n options={[\n { id: '1', label: 'Fizinis asmuo' },\n { id: '2', label: 'Juridinis asmuo' },\n ]}\n value={value}\n onChange={(newValue) => setValue(newValue)}\n size=\"small\"\n/>";
|
|
139
|
+
};
|
|
140
|
+
};
|
|
141
|
+
};
|
|
142
|
+
};
|
|
143
|
+
export declare const SmallFourOptions: {
|
|
144
|
+
args: {
|
|
145
|
+
options: {
|
|
146
|
+
id: string;
|
|
147
|
+
label: string;
|
|
148
|
+
}[];
|
|
149
|
+
value: string;
|
|
150
|
+
size: string;
|
|
151
|
+
};
|
|
152
|
+
render: (args: any) => import("react/jsx-runtime").JSX.Element;
|
|
153
|
+
parameters: {
|
|
154
|
+
docs: {
|
|
155
|
+
source: {
|
|
156
|
+
code: string;
|
|
157
|
+
};
|
|
158
|
+
};
|
|
159
|
+
};
|
|
160
|
+
};
|
|
161
|
+
export declare const SmallWithDisabled: {
|
|
162
|
+
args: {
|
|
163
|
+
options: ({
|
|
164
|
+
id: string;
|
|
165
|
+
label: string;
|
|
166
|
+
disabled?: undefined;
|
|
167
|
+
} | {
|
|
168
|
+
id: string;
|
|
169
|
+
label: string;
|
|
170
|
+
disabled: boolean;
|
|
171
|
+
})[];
|
|
172
|
+
value: string;
|
|
173
|
+
size: string;
|
|
174
|
+
};
|
|
175
|
+
render: (args: any) => import("react/jsx-runtime").JSX.Element;
|
|
176
|
+
parameters: {
|
|
177
|
+
docs: {
|
|
178
|
+
source: {
|
|
179
|
+
code: string;
|
|
180
|
+
};
|
|
181
|
+
};
|
|
182
|
+
};
|
|
183
|
+
};
|
|
184
|
+
export declare const RegularNoBackground: {
|
|
185
|
+
args: {
|
|
186
|
+
options: {
|
|
187
|
+
id: string;
|
|
188
|
+
label: string;
|
|
189
|
+
}[];
|
|
190
|
+
value: string;
|
|
191
|
+
size: string;
|
|
192
|
+
disableBg: boolean;
|
|
193
|
+
};
|
|
194
|
+
render: (args: any) => import("react/jsx-runtime").JSX.Element;
|
|
195
|
+
parameters: {
|
|
196
|
+
docs: {
|
|
197
|
+
source: {
|
|
198
|
+
code: string;
|
|
199
|
+
};
|
|
200
|
+
};
|
|
201
|
+
};
|
|
202
|
+
};
|
package/package.json
CHANGED
|
@@ -2,13 +2,19 @@ import { PencilSimpleLineIcon as PencilSimpleLine } from '@phosphor-icons/react'
|
|
|
2
2
|
|
|
3
3
|
import IconProps from '../IconProps'
|
|
4
4
|
|
|
5
|
-
function PencilSimpleLineIcon({
|
|
5
|
+
function PencilSimpleLineIcon({
|
|
6
|
+
className,
|
|
7
|
+
fillColor,
|
|
8
|
+
size = 24,
|
|
9
|
+
weight = 'regular',
|
|
10
|
+
...rest
|
|
11
|
+
}: IconProps) {
|
|
6
12
|
return (
|
|
7
13
|
<PencilSimpleLine
|
|
8
14
|
{...rest}
|
|
9
15
|
className={className}
|
|
10
16
|
size={size}
|
|
11
|
-
weight=
|
|
17
|
+
weight={weight}
|
|
12
18
|
color={fillColor}
|
|
13
19
|
/>
|
|
14
20
|
)
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { fireEvent, render, screen } from '@testing-library/react'
|
|
2
|
+
import { describe, expect, test, vi } from 'vitest'
|
|
3
|
+
|
|
4
|
+
import RcSesSegmentButton, { type RcSesSegmentOption } from './index'
|
|
5
|
+
|
|
6
|
+
describe('RcSesSegmentButton', () => {
|
|
7
|
+
const defaultOption: RcSesSegmentOption = {
|
|
8
|
+
id: 'option-1',
|
|
9
|
+
label: 'Option 1',
|
|
10
|
+
disabled: false,
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const defaultProps = {
|
|
14
|
+
option: defaultOption,
|
|
15
|
+
isSelected: false,
|
|
16
|
+
isFocused: false,
|
|
17
|
+
size: 'regular' as const,
|
|
18
|
+
ariaRole: 'radio' as const,
|
|
19
|
+
onClick: vi.fn(),
|
|
20
|
+
onKeyDown: vi.fn(),
|
|
21
|
+
onFocus: vi.fn(),
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
test('renders button with option label', () => {
|
|
25
|
+
render(<RcSesSegmentButton {...defaultProps} />)
|
|
26
|
+
expect(screen.getByRole('radio', { name: 'Option 1' })).toBeInTheDocument()
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
test('calls onClick handler when clicked', () => {
|
|
30
|
+
const onClick = vi.fn()
|
|
31
|
+
|
|
32
|
+
render(<RcSesSegmentButton {...defaultProps} onClick={onClick} />)
|
|
33
|
+
|
|
34
|
+
fireEvent.click(screen.getByRole('radio'))
|
|
35
|
+
expect(onClick).toHaveBeenCalledWith('option-1')
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
test('calls onFocus handler when focused', () => {
|
|
39
|
+
const onFocus = vi.fn()
|
|
40
|
+
|
|
41
|
+
render(<RcSesSegmentButton {...defaultProps} onFocus={onFocus} />)
|
|
42
|
+
|
|
43
|
+
fireEvent.focus(screen.getByRole('radio'))
|
|
44
|
+
expect(onFocus).toHaveBeenCalled()
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
test('calls onKeyDown handler when key pressed', () => {
|
|
48
|
+
const onKeyDown = vi.fn()
|
|
49
|
+
|
|
50
|
+
render(<RcSesSegmentButton {...defaultProps} onKeyDown={onKeyDown} />)
|
|
51
|
+
|
|
52
|
+
const button = screen.getByRole('radio')
|
|
53
|
+
fireEvent.keyDown(button, { key: 'ArrowRight', code: 'ArrowRight' })
|
|
54
|
+
expect(onKeyDown).toHaveBeenCalled()
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
test('disables button when option.disabled is true', () => {
|
|
58
|
+
render(
|
|
59
|
+
<RcSesSegmentButton
|
|
60
|
+
{...defaultProps}
|
|
61
|
+
option={{ ...defaultOption, disabled: true }}
|
|
62
|
+
/>,
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
expect(screen.getByRole('radio')).toBeDisabled()
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
test('sets aria-checked when selected', () => {
|
|
69
|
+
render(<RcSesSegmentButton {...defaultProps} isSelected />)
|
|
70
|
+
|
|
71
|
+
expect(screen.getByRole('radio')).toHaveAttribute('aria-checked', 'true')
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
test('sets tabIndex to 0 when focused', () => {
|
|
75
|
+
render(<RcSesSegmentButton {...defaultProps} isFocused />)
|
|
76
|
+
|
|
77
|
+
expect(screen.getByRole('radio')).toHaveAttribute('tabIndex', '0')
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
test('supports tab role', () => {
|
|
81
|
+
render(<RcSesSegmentButton {...defaultProps} ariaRole='tab' />)
|
|
82
|
+
|
|
83
|
+
expect(screen.getByRole('tab')).toBeInTheDocument()
|
|
84
|
+
})
|
|
85
|
+
})
|
|
@@ -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
|
+
})
|