@registrucentras/rc-ses-react-components 1.10.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.
@@ -0,0 +1,9 @@
1
+ import { Meta, StoryObj } from '@storybook/react';
2
+ import { default as RcSesBadge } from '../../../components/common/Badge';
3
+ declare const meta: Meta<typeof RcSesBadge>;
4
+ export default meta;
5
+ type Story = StoryObj<typeof RcSesBadge>;
6
+ export declare const Main: Story;
7
+ export declare const AllCombinations: Story;
8
+ export declare const LongLabel: Story;
9
+ export declare const BaselineAlignment: Story;
@@ -0,0 +1,17 @@
1
+ import { StoryObj } from '@storybook/react';
2
+ import { default as RcSesSwitch } from '../../../components/common/Switch';
3
+ declare const meta: {
4
+ title: string;
5
+ component: typeof RcSesSwitch;
6
+ parameters: {
7
+ layout: string;
8
+ };
9
+ tags: string[];
10
+ };
11
+ export default meta;
12
+ type Story = StoryObj<typeof meta>;
13
+ export declare const On: Story;
14
+ export declare const Off: Story;
15
+ export declare const OnDisabled: Story;
16
+ export declare const OffDisabled: Story;
17
+ export declare const LongLabel: Story;
@@ -0,0 +1,3 @@
1
+ import { Components } from '@mui/material';
2
+ declare const MuiSwitch: Components['MuiSwitch'];
3
+ export default MuiSwitch;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@registrucentras/rc-ses-react-components",
3
3
  "author": "VĮ REGISTRŲ CENTRAS",
4
- "version": "1.10.0",
4
+ "version": "1.11.0",
5
5
  "description": "RC SES React komponentų biblioteka",
6
6
  "type": "module",
7
7
  "private": false,
@@ -0,0 +1,129 @@
1
+ import { ThemeProvider } from '@mui/material/styles'
2
+ import { fireEvent, render, screen } from '@testing-library/react'
3
+ import { ReactElement } from 'react'
4
+ import { beforeEach, describe, expect, it, vi } from 'vitest'
5
+
6
+ import i18n from '@/i18n/i18n'
7
+ import theme from '@/theme/light'
8
+
9
+ import RcSesBadge from './index'
10
+
11
+ const getCloseAriaLabel = (label: string) =>
12
+ i18n.t('components.RcSesBadge.aria.close', { label, ns: 'input' })
13
+
14
+ const renderBadge = (ui: ReactElement) =>
15
+ render(<ThemeProvider theme={theme}>{ui}</ThemeProvider>)
16
+
17
+ describe('RcSesBadge', () => {
18
+ beforeEach(async () => {
19
+ await i18n.changeLanguage('lt')
20
+ })
21
+
22
+ it('renders the label', () => {
23
+ renderBadge(<RcSesBadge label='Dabartinė' variant='success' size='regular' />)
24
+
25
+ expect(screen.getByText('Dabartinė')).toBeInTheDocument()
26
+ })
27
+
28
+ it('sets the label as the title attribute for truncation tooltips', () => {
29
+ renderBadge(
30
+ <RcSesBadge label='Labai ilgas pavadinimas' variant='info' size='regular' />,
31
+ )
32
+
33
+ expect(screen.getByText('Labai ilgas pavadinimas')).toHaveAttribute(
34
+ 'title',
35
+ 'Labai ilgas pavadinimas',
36
+ )
37
+ })
38
+
39
+ it.each([
40
+ ['small', '1.625rem'],
41
+ ['regular', '2.125rem'],
42
+ ] as const)('renders the %s size', (size, expectedHeight) => {
43
+ const { container } = renderBadge(
44
+ <RcSesBadge label='Label' variant='neutral' size={size} />,
45
+ )
46
+
47
+ expect(container.firstChild).toHaveStyle({ height: expectedHeight })
48
+ })
49
+
50
+ it('shows the status icon by default', () => {
51
+ const { container } = renderBadge(
52
+ <RcSesBadge label='Label' variant='neutral' size='regular' />,
53
+ )
54
+
55
+ expect(container.querySelector('svg')).toBeInTheDocument()
56
+ })
57
+
58
+ it('hides the status icon when showIcon is false', () => {
59
+ const { container } = renderBadge(
60
+ <RcSesBadge label='Label' variant='neutral' size='regular' showIcon={false} />,
61
+ )
62
+
63
+ expect(container.querySelector('svg')).not.toBeInTheDocument()
64
+ })
65
+
66
+ it('does not render a close button by default', () => {
67
+ renderBadge(<RcSesBadge label='Label' variant='neutral' size='regular' />)
68
+
69
+ expect(screen.queryByRole('button')).not.toBeInTheDocument()
70
+ })
71
+
72
+ it('renders a close button with a translated aria-label when showClose is true', () => {
73
+ renderBadge(
74
+ <RcSesBadge
75
+ label='Dabartinė'
76
+ variant='neutral'
77
+ size='regular'
78
+ showClose
79
+ onClose={() => {}}
80
+ />,
81
+ )
82
+
83
+ expect(
84
+ screen.getByRole('button', { name: getCloseAriaLabel('Dabartinė') }),
85
+ ).toBeInTheDocument()
86
+ })
87
+
88
+ it('calls onClose when the close button is clicked', () => {
89
+ const handleClose = vi.fn()
90
+ renderBadge(
91
+ <RcSesBadge
92
+ label='Dabartinė'
93
+ variant='neutral'
94
+ size='regular'
95
+ showClose
96
+ onClose={handleClose}
97
+ />,
98
+ )
99
+
100
+ fireEvent.click(screen.getByRole('button', { name: getCloseAriaLabel('Dabartinė') }))
101
+
102
+ expect(handleClose).toHaveBeenCalledTimes(1)
103
+ })
104
+
105
+ it('renders in English when the language is changed', async () => {
106
+ await i18n.changeLanguage('en')
107
+ renderBadge(
108
+ <RcSesBadge
109
+ label='Current'
110
+ variant='neutral'
111
+ size='regular'
112
+ showClose
113
+ onClose={() => {}}
114
+ />,
115
+ )
116
+
117
+ expect(
118
+ screen.getByRole('button', { name: getCloseAriaLabel('Current') }),
119
+ ).toBeInTheDocument()
120
+ })
121
+
122
+ it('does not throw when the close button is clicked without an onClose handler', () => {
123
+ renderBadge(<RcSesBadge label='Label' variant='neutral' size='regular' showClose />)
124
+
125
+ expect(() =>
126
+ fireEvent.click(screen.getByRole('button', { name: getCloseAriaLabel('Label') })),
127
+ ).not.toThrow()
128
+ })
129
+ })
@@ -0,0 +1,162 @@
1
+ import { Box } from '@mui/material'
2
+ import { useTranslation } from 'react-i18next'
3
+
4
+ import CircleFilledIcon from '@/assets/icons/CircleFilledIcon'
5
+ import CloseIcon from '@/assets/icons/CloseIcon'
6
+ import { common, error, grey, primary, secondary, warning } from '@/theme/palette'
7
+
8
+ export type RcSesBadgeProps = {
9
+ label: string
10
+ variant: 'neutral' | 'success' | 'warning' | 'error' | 'info' | 'brand'
11
+ size: 'small' | 'regular'
12
+ showIcon?: boolean
13
+ showClose?: boolean
14
+ onClose?: () => void
15
+ }
16
+
17
+ type VariantStyle = {
18
+ border: string
19
+ background: string
20
+ iconColor: string
21
+ color: string
22
+ }
23
+
24
+ const variantStyles: Record<RcSesBadgeProps['variant'], VariantStyle> = {
25
+ neutral: {
26
+ border: grey['300'],
27
+ background: common.white,
28
+ iconColor: grey['900'],
29
+ color: grey['900'],
30
+ },
31
+ success: {
32
+ border: secondary['500'],
33
+ background: secondary['100'],
34
+ iconColor: secondary['600'],
35
+ color: secondary['800'],
36
+ },
37
+ warning: {
38
+ border: warning['400'],
39
+ background: warning['100'],
40
+ iconColor: warning['500'],
41
+ color: warning['800'],
42
+ },
43
+ error: {
44
+ border: error['500'],
45
+ background: error['100'],
46
+ iconColor: error['600'],
47
+ color: error['700'],
48
+ },
49
+ info: {
50
+ border: primary['400'],
51
+ background: primary['100'],
52
+ iconColor: primary['500'],
53
+ color: primary['800'],
54
+ },
55
+ brand: {
56
+ border: primary['500'],
57
+ background: primary['200'],
58
+ iconColor: primary['950'],
59
+ color: primary['950'],
60
+ },
61
+ }
62
+
63
+ type SizeStyle = {
64
+ height: string
65
+ padding: string
66
+ fontSize: string
67
+ lineHeight: string
68
+ }
69
+
70
+ const sizeStyles: Record<RcSesBadgeProps['size'], SizeStyle> = {
71
+ small: {
72
+ height: '1.625rem',
73
+ padding: '0.25rem 0.5rem',
74
+ fontSize: '0.8125rem',
75
+ lineHeight: '1.125rem',
76
+ },
77
+ regular: {
78
+ height: '2.125rem',
79
+ padding: '0.375rem 0.75rem',
80
+ fontSize: '0.9375rem',
81
+ lineHeight: '1.375rem',
82
+ },
83
+ }
84
+
85
+ function RcSesBadge(props: RcSesBadgeProps) {
86
+ const { label, variant, size, showIcon = true, showClose = false, onClose } = props
87
+ const { t } = useTranslation('input', { keyPrefix: 'components.RcSesBadge' })
88
+
89
+ const variantStyle = variantStyles[variant]
90
+ const sizeStyle = sizeStyles[size]
91
+
92
+ const baseStyle = {
93
+ display: 'inline-flex',
94
+ alignItems: 'center',
95
+ justifyContent: 'center',
96
+ verticalAlign: 'baseline',
97
+ gap: '0.25rem',
98
+ maxWidth: '100%',
99
+ borderRadius: '9999px',
100
+ border: `1px solid ${variantStyle.border}`,
101
+ background: variantStyle.background,
102
+ color: variantStyle.color,
103
+ ...sizeStyle,
104
+ }
105
+
106
+ return (
107
+ <Box sx={baseStyle}>
108
+ {showIcon && (
109
+ <CircleFilledIcon
110
+ size={14}
111
+ fillColor={variantStyle.iconColor}
112
+ aria-hidden
113
+ focusable={false}
114
+ style={{ flexShrink: 0 }}
115
+ />
116
+ )}
117
+ <Box
118
+ component='span'
119
+ title={label}
120
+ sx={{
121
+ overflow: 'hidden',
122
+ textOverflow: 'ellipsis',
123
+ whiteSpace: 'nowrap',
124
+ minWidth: 0,
125
+ }}
126
+ >
127
+ {label}
128
+ </Box>
129
+ {showClose && (
130
+ <Box
131
+ component='button'
132
+ type='button'
133
+ onClick={onClose}
134
+ aria-label={t('aria.close', { label })}
135
+ sx={{
136
+ display: 'inline-flex',
137
+ alignItems: 'center',
138
+ justifyContent: 'center',
139
+ flexShrink: 0,
140
+ minWidth: 16,
141
+ minHeight: 16,
142
+ padding: 0,
143
+ border: 'none',
144
+ background: 'none',
145
+ cursor: onClose ? 'pointer' : 'default',
146
+ opacity: 0.6,
147
+ '&:hover': { opacity: 1 },
148
+ }}
149
+ >
150
+ <CloseIcon
151
+ size={14}
152
+ fillColor={variantStyle.iconColor}
153
+ aria-hidden
154
+ focusable={false}
155
+ />
156
+ </Box>
157
+ )}
158
+ </Box>
159
+ )
160
+ }
161
+
162
+ export default RcSesBadge
@@ -0,0 +1,214 @@
1
+ import { fireEvent, render, screen } from '@testing-library/react'
2
+ import { describe, expect, it, vi } from 'vitest'
3
+
4
+ import RcSesSwitch from './index'
5
+
6
+ describe('RcSesSwitch Component', () => {
7
+ describe('Switch semantics', () => {
8
+ it('should expose role="switch" instead of the native checkbox role', () => {
9
+ render(<RcSesSwitch label='Notifications' checked={false} onChange={vi.fn()} />)
10
+
11
+ expect(screen.getByRole('switch')).toBeInTheDocument()
12
+ expect(screen.queryByRole('checkbox')).not.toBeInTheDocument()
13
+ })
14
+
15
+ it('should reflect the checked state via the native checked attribute', () => {
16
+ render(<RcSesSwitch label='Notifications' checked onChange={vi.fn()} />)
17
+
18
+ expect(screen.getByRole('switch')).toBeChecked()
19
+ })
20
+
21
+ it('should update checked state when the checked prop changes', () => {
22
+ const { rerender } = render(
23
+ <RcSesSwitch label='Notifications' checked={false} onChange={vi.fn()} />,
24
+ )
25
+
26
+ const input = screen.getByRole('switch')
27
+ expect(input).not.toBeChecked()
28
+
29
+ rerender(<RcSesSwitch label='Notifications' checked onChange={vi.fn()} />)
30
+
31
+ expect(input).toBeChecked()
32
+ })
33
+ })
34
+
35
+ describe('Accessible name', () => {
36
+ it('should associate the visible label with the switch via FormControlLabel', () => {
37
+ render(<RcSesSwitch label='Notifications' checked={false} onChange={vi.fn()} />)
38
+
39
+ expect(screen.getByRole('switch', { name: 'Notifications' })).toBeInTheDocument()
40
+ })
41
+
42
+ it('should use ariaLabel as the accessible name when no visible label is given', () => {
43
+ render(<RcSesSwitch ariaLabel='Notifications' checked={false} onChange={vi.fn()} />)
44
+
45
+ const input = screen.getByRole('switch', { name: 'Notifications' })
46
+ expect(input).toBeInTheDocument()
47
+ expect(screen.queryByText('Notifications')).not.toBeInTheDocument()
48
+ })
49
+
50
+ it('should keep the visible label as the accessible name even when ariaLabel is also passed', () => {
51
+ render(
52
+ <RcSesSwitch
53
+ label='Notifications'
54
+ ariaLabel='Enable notifications'
55
+ checked={false}
56
+ onChange={vi.fn()}
57
+ />,
58
+ )
59
+
60
+ expect(screen.getByRole('switch', { name: 'Notifications' })).toBeInTheDocument()
61
+ expect(
62
+ screen.queryByRole('switch', { name: 'Enable notifications' }),
63
+ ).not.toBeInTheDocument()
64
+ })
65
+
66
+ it('should not set aria-label at all when a visible label is present', () => {
67
+ render(
68
+ <RcSesSwitch
69
+ label='Notifications'
70
+ ariaLabel=''
71
+ checked={false}
72
+ onChange={vi.fn()}
73
+ />,
74
+ )
75
+
76
+ const input = screen.getByRole('switch')
77
+ expect(input).not.toHaveAttribute('aria-label')
78
+ })
79
+ })
80
+
81
+ describe('Checked state', () => {
82
+ it('should reflect checked=false', () => {
83
+ render(<RcSesSwitch label='Notifications' checked={false} onChange={vi.fn()} />)
84
+
85
+ expect(screen.getByRole('switch')).not.toBeChecked()
86
+ })
87
+
88
+ it('should reflect checked=true', () => {
89
+ render(<RcSesSwitch label='Notifications' checked onChange={vi.fn()} />)
90
+
91
+ expect(screen.getByRole('switch')).toBeChecked()
92
+ })
93
+
94
+ it('should not change visually on click unless the caller updates the checked prop', () => {
95
+ const handleChange = vi.fn()
96
+ render(
97
+ <RcSesSwitch label='Notifications' checked={false} onChange={handleChange} />,
98
+ )
99
+
100
+ const input = screen.getByRole('switch')
101
+ fireEvent.click(input)
102
+
103
+ expect(handleChange).toHaveBeenCalledTimes(1)
104
+ expect(input).not.toBeChecked()
105
+ })
106
+
107
+ it('should support uncontrolled usage via defaultChecked', () => {
108
+ render(<RcSesSwitch label='Notifications' defaultChecked onChange={vi.fn()} />)
109
+
110
+ const input = screen.getByRole('switch')
111
+ expect(input).toBeChecked()
112
+
113
+ fireEvent.click(input)
114
+
115
+ expect(input).not.toBeChecked()
116
+ })
117
+ })
118
+
119
+ describe('onChange', () => {
120
+ it('should call onChange with the new checked value when toggled on', () => {
121
+ const handleChange = vi.fn()
122
+ render(
123
+ <RcSesSwitch label='Notifications' checked={false} onChange={handleChange} />,
124
+ )
125
+
126
+ fireEvent.click(screen.getByRole('switch'))
127
+
128
+ expect(handleChange).toHaveBeenCalledTimes(1)
129
+ expect(handleChange.mock.calls[0][1]).toBe(true)
130
+ })
131
+
132
+ it('should call onChange with false when toggled off', () => {
133
+ const handleChange = vi.fn()
134
+ render(<RcSesSwitch label='Notifications' checked onChange={handleChange} />)
135
+
136
+ fireEvent.click(screen.getByRole('switch'))
137
+
138
+ expect(handleChange.mock.calls[0][1]).toBe(false)
139
+ })
140
+
141
+ it('should toggle when the visible label is clicked', () => {
142
+ const handleChange = vi.fn()
143
+ render(
144
+ <RcSesSwitch label='Notifications' checked={false} onChange={handleChange} />,
145
+ )
146
+
147
+ fireEvent.click(screen.getByText('Notifications'))
148
+
149
+ expect(handleChange).toHaveBeenCalledTimes(1)
150
+ expect(handleChange.mock.calls[0][1]).toBe(true)
151
+ })
152
+ })
153
+
154
+ describe('Keyboard interaction', () => {
155
+ it('should toggle when Enter is pressed', () => {
156
+ const handleChange = vi.fn()
157
+ render(
158
+ <RcSesSwitch label='Notifications' checked={false} onChange={handleChange} />,
159
+ )
160
+
161
+ fireEvent.keyDown(screen.getByRole('switch'), { key: 'Enter' })
162
+
163
+ expect(handleChange).toHaveBeenCalledTimes(1)
164
+ expect(handleChange.mock.calls[0][1]).toBe(true)
165
+ })
166
+
167
+ it('should not toggle on Enter when disabled', () => {
168
+ const handleChange = vi.fn()
169
+ render(
170
+ <RcSesSwitch
171
+ label='Notifications'
172
+ disabled
173
+ checked={false}
174
+ onChange={handleChange}
175
+ />,
176
+ )
177
+
178
+ fireEvent.keyDown(screen.getByRole('switch'), { key: 'Enter' })
179
+
180
+ expect(handleChange).not.toHaveBeenCalled()
181
+ })
182
+ })
183
+
184
+ describe('Disabled state', () => {
185
+ it('should disable the switch when disabled=true', () => {
186
+ render(
187
+ <RcSesSwitch label='Notifications' disabled checked={false} onChange={vi.fn()} />,
188
+ )
189
+
190
+ expect(screen.getByRole('switch')).toBeDisabled()
191
+ })
192
+
193
+ it('should mark the FormControlLabel as disabled', () => {
194
+ const { container } = render(
195
+ <RcSesSwitch label='Notifications' disabled checked={false} onChange={vi.fn()} />,
196
+ )
197
+
198
+ expect(container.querySelector('.MuiFormControlLabel-root')).toHaveClass(
199
+ 'Mui-disabled',
200
+ )
201
+ })
202
+ })
203
+
204
+ describe('Without a visible label', () => {
205
+ it('should render the bare switch without a wrapping FormControlLabel', () => {
206
+ const { container } = render(
207
+ <RcSesSwitch ariaLabel='Notifications' checked={false} onChange={vi.fn()} />,
208
+ )
209
+
210
+ expect(container.querySelector('.MuiFormControlLabel-root')).not.toBeInTheDocument()
211
+ expect(container.querySelector('.MuiSwitch-root')).toBeInTheDocument()
212
+ })
213
+ })
214
+ })
@@ -0,0 +1,49 @@
1
+ import { FormControlLabel, Switch, type SwitchProps } from '@mui/material'
2
+
3
+ import palette from '@/theme/palette'
4
+
5
+ export type RcSesSwitchProps = SwitchProps &
6
+ ({ label: string; ariaLabel?: string } | { label?: undefined; ariaLabel: string })
7
+
8
+ const formControlLabelStyles = {
9
+ marginLeft: 0,
10
+ alignItems: 'center',
11
+ '& .MuiFormControlLabel-label': {
12
+ paddingLeft: '0.625rem',
13
+ },
14
+ '&.Mui-disabled .MuiFormControlLabel-label': {
15
+ color: palette.grey['500'],
16
+ },
17
+ }
18
+
19
+ function RcSesSwitch(props: RcSesSwitchProps) {
20
+ const { label, ariaLabel, disabled, ...switchProps } = props
21
+
22
+ const handleKeyDown = (event: React.KeyboardEvent<HTMLInputElement>) => {
23
+ if (event.key === 'Enter' && !disabled) {
24
+ event.preventDefault()
25
+ event.currentTarget.click()
26
+ }
27
+ }
28
+
29
+ const inputProps = {
30
+ ...(label === undefined && { 'aria-label': ariaLabel }),
31
+ role: 'switch',
32
+ onKeyDown: handleKeyDown,
33
+ }
34
+
35
+ const switchEl = <Switch disabled={disabled} inputProps={inputProps} {...switchProps} />
36
+
37
+ return label ? (
38
+ <FormControlLabel
39
+ control={switchEl}
40
+ label={label}
41
+ disabled={disabled}
42
+ sx={formControlLabelStyles}
43
+ />
44
+ ) : (
45
+ switchEl
46
+ )
47
+ }
48
+
49
+ export default RcSesSwitch
@@ -4,6 +4,12 @@
4
4
  "close": "Close"
5
5
  },
6
6
 
7
+ "RcSesBadge": {
8
+ "aria": {
9
+ "close": "Remove {{label}}"
10
+ }
11
+ },
12
+
7
13
  "RcSesButtonWithPopover": {
8
14
  "aria": {
9
15
  "label": "Additional information"
@@ -4,6 +4,12 @@
4
4
  "close": "Uždaryti"
5
5
  },
6
6
 
7
+ "RcSesBadge": {
8
+ "aria": {
9
+ "close": "Pašalinti {{label}}"
10
+ }
11
+ },
12
+
7
13
  "RcSesButtonWithPopover": {
8
14
  "aria": {
9
15
  "label": "Daugiau informacijos"
@@ -6,6 +6,7 @@ import RcSesIconProps from '@/assets/IconProps'
6
6
  import RcSesAccordion from '@/components/common/Accordion'
7
7
  import useAccordionController from '@/components/common/Accordion/hooks/useAccordionController'
8
8
  import RcSesAlert from '@/components/common/Alert'
9
+ import RcSesBadge from '@/components/common/Badge'
9
10
  import RcSesBreadcrumbs from '@/components/common/Breadcrumbs'
10
11
  import RcSesButton from '@/components/common/Button'
11
12
  import RcSesButtonWithPopover from '@/components/common/Button/ButtonWithPopover'
@@ -20,6 +21,7 @@ import {
20
21
  RcSesSnackbarProvider,
21
22
  useSnackbar,
22
23
  } from '@/components/common/Snackbar/SnackbarProvider'
24
+ import RcSesSwitch from '@/components/common/Switch'
23
25
  import RcSesTab from '@/components/common/Tabs/Tab'
24
26
  import RcSesTabPanel from '@/components/common/Tabs/TabPanel'
25
27
  import RcSesTabs from '@/components/common/Tabs/Tabs'
@@ -67,6 +69,7 @@ export { RcSesPalette, RcSesTheme }
67
69
  export { RcSesAccordion, useAccordionController }
68
70
  export {
69
71
  RcSesAlert,
72
+ RcSesBadge,
70
73
  RcSesBreadcrumbs,
71
74
  RcSesButton,
72
75
  RcSesButtonWithPopover,
@@ -80,6 +83,7 @@ export {
80
83
  RcSesSnackbar,
81
84
  RcSesSnackbarProvider,
82
85
  RcSesModal,
86
+ RcSesSwitch,
83
87
  }
84
88
  export { RcSesSegmentedControl }
85
89
  export { RcSesCheckbox, RcSesCheckboxFormControl, RcSesSimpleCheckbox }