@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,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,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
|
+
};
|
|
@@ -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;
|
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,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,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
|
+
})
|