@registrucentras/rc-ses-react-components 1.10.0 → 1.12.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/README.md +5 -1
- package/dist/components/common/Badge/index.d.ts +10 -0
- package/dist/components/common/SelectableCardList/index.d.ts +1 -1
- package/dist/components/common/Switch/index.d.ts +10 -0
- package/dist/components/common/Tooltip/index.d.ts +20 -0
- package/dist/library/index.d.ts +5 -1
- package/dist/library/types.d.ts +2 -1
- package/dist/registrucentras-rc-ses-react-components.cjs +10 -10
- package/dist/registrucentras-rc-ses-react-components.js +3120 -2757
- package/dist/stories/components/display/Badge.stories.d.ts +9 -0
- package/dist/stories/components/display/SelectableCardList.stories.d.ts +8 -1
- package/dist/stories/components/display/Tooltip.stories.d.ts +48 -0
- package/dist/stories/components/inputs/Switch.stories.d.ts +17 -0
- package/dist/theme/light/MuiSwitch.d.ts +3 -0
- package/dist/theme/light/MuiTooltip.d.ts +3 -0
- package/package.json +1 -1
- package/src/components/common/Badge/index.test.tsx +129 -0
- package/src/components/common/Badge/index.tsx +162 -0
- package/src/components/common/ListWithIcons/components/ListWithIconsItem.tsx +7 -5
- package/src/components/common/SelectableCardList/index.test.tsx +56 -2
- package/src/components/common/SelectableCardList/index.tsx +23 -10
- package/src/components/common/Switch/index.test.tsx +214 -0
- package/src/components/common/Switch/index.tsx +49 -0
- package/src/components/common/Tooltip/index.test.tsx +87 -0
- package/src/components/common/Tooltip/index.tsx +167 -0
- package/src/components/form/inputs/Select/index.test.tsx +102 -0
- package/src/components/form/inputs/Select/index.tsx +107 -53
- package/src/i18n/namespaces/common/en.json +3 -0
- package/src/i18n/namespaces/common/lt.json +3 -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 +2 -0
- package/src/stories/components/display/Badge.stories.tsx +165 -0
- package/src/stories/components/display/SelectableCardList.stories.tsx +16 -0
- package/src/stories/components/display/Tooltip.stories.tsx +67 -0
- package/src/stories/components/inputs/Select.stories.tsx +38 -2
- package/src/stories/components/inputs/Switch.stories.tsx +70 -0
- package/src/theme/light/MuiAutocomplete.ts +3 -1
- package/src/theme/light/MuiInputBase.ts +4 -0
- package/src/theme/light/MuiSwitch.tsx +77 -0
- package/src/theme/light/MuiTooltip.ts +40 -0
- package/src/theme/light/index.tsx +4 -0
|
@@ -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
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { fireEvent, render, screen, waitFor } from '@testing-library/react'
|
|
2
|
+
import { createRef } from 'react'
|
|
3
|
+
import { describe, expect, test, vi } from 'vitest'
|
|
4
|
+
|
|
5
|
+
import RcSesTooltip from './index'
|
|
6
|
+
|
|
7
|
+
describe('RcSesTooltip', () => {
|
|
8
|
+
test('renders default trigger button', () => {
|
|
9
|
+
render(<RcSesTooltip title='Help text' />)
|
|
10
|
+
|
|
11
|
+
expect(
|
|
12
|
+
screen.getByRole('button', { name: 'components.Tooltip.trigger' }),
|
|
13
|
+
).toBeInTheDocument()
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
test('opens tooltip on trigger click', async () => {
|
|
17
|
+
render(<RcSesTooltip title='Text' />)
|
|
18
|
+
|
|
19
|
+
fireEvent.click(screen.getByRole('button'))
|
|
20
|
+
|
|
21
|
+
expect(await screen.findByRole('tooltip')).toHaveTextContent('Text')
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
test('closes on pointerdown outside', async () => {
|
|
25
|
+
render(<RcSesTooltip title='Text' />)
|
|
26
|
+
|
|
27
|
+
fireEvent.click(screen.getByRole('button'))
|
|
28
|
+
await screen.findByRole('tooltip')
|
|
29
|
+
|
|
30
|
+
fireEvent.pointerDown(document.body)
|
|
31
|
+
|
|
32
|
+
await waitFor(() => {
|
|
33
|
+
expect(screen.queryByRole('tooltip')).not.toBeInTheDocument()
|
|
34
|
+
})
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
test('keeps only one tooltip open at a time', async () => {
|
|
38
|
+
render(
|
|
39
|
+
<>
|
|
40
|
+
<RcSesTooltip title='A' />
|
|
41
|
+
<RcSesTooltip title='B' />
|
|
42
|
+
</>,
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
const [first, second] = screen.getAllByRole('button')
|
|
46
|
+
|
|
47
|
+
fireEvent.click(first)
|
|
48
|
+
await screen.findByRole('tooltip')
|
|
49
|
+
|
|
50
|
+
fireEvent.click(second)
|
|
51
|
+
|
|
52
|
+
await waitFor(() => {
|
|
53
|
+
const tooltips = screen.getAllByRole('tooltip')
|
|
54
|
+
expect(tooltips).toHaveLength(1)
|
|
55
|
+
expect(tooltips[0]).toHaveTextContent('B')
|
|
56
|
+
})
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
test('passes trigger slotProps to the default button', () => {
|
|
60
|
+
render(
|
|
61
|
+
<RcSesTooltip title='Text' slotProps={{ trigger: { 'aria-label': 'Custom' } }} />,
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
expect(screen.getByRole('button', { name: 'Custom' })).toBeInTheDocument()
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
test('custom children trigger opens tooltip, chains onClick and forwards ref', async () => {
|
|
68
|
+
const onClick = vi.fn()
|
|
69
|
+
const ref = createRef<HTMLButtonElement>()
|
|
70
|
+
|
|
71
|
+
render(
|
|
72
|
+
<RcSesTooltip title='Text'>
|
|
73
|
+
<button type='button' ref={ref} onClick={onClick}>
|
|
74
|
+
Custom trigger
|
|
75
|
+
</button>
|
|
76
|
+
</RcSesTooltip>,
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
const trigger = screen.getByRole('button', { name: 'Custom trigger' })
|
|
80
|
+
expect(ref.current).toBe(trigger)
|
|
81
|
+
|
|
82
|
+
fireEvent.click(trigger)
|
|
83
|
+
|
|
84
|
+
expect(onClick).toHaveBeenCalledTimes(1)
|
|
85
|
+
expect(await screen.findByRole('tooltip')).toHaveTextContent('Text')
|
|
86
|
+
})
|
|
87
|
+
})
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import type { IconButtonProps, TooltipProps } from '@mui/material'
|
|
2
|
+
import { Fade, IconButton, Tooltip } from '@mui/material'
|
|
3
|
+
import { useForkRef } from '@mui/material/utils'
|
|
4
|
+
import React from 'react'
|
|
5
|
+
import { useTranslation } from 'react-i18next'
|
|
6
|
+
|
|
7
|
+
import QuestionIcon from '@/assets/icons/QuestionIcon'
|
|
8
|
+
|
|
9
|
+
export type RcSesTooltipProps = {
|
|
10
|
+
title: string
|
|
11
|
+
placement?: TooltipProps['placement']
|
|
12
|
+
} & (
|
|
13
|
+
| {
|
|
14
|
+
/* overrides for default trigger button */
|
|
15
|
+
slotProps?: { trigger?: Partial<IconButtonProps> }
|
|
16
|
+
children?: never
|
|
17
|
+
}
|
|
18
|
+
| {
|
|
19
|
+
slotProps?: never
|
|
20
|
+
/* custom trigger replaces default button, but should forward ref and define a11y name */
|
|
21
|
+
children: React.ReactElement
|
|
22
|
+
}
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
// only one tooltip should be visible, no need for context for that
|
|
26
|
+
let closeCurrent: (() => void) | null = null
|
|
27
|
+
|
|
28
|
+
const popperProps: TooltipProps['PopperProps'] = {
|
|
29
|
+
popperOptions: {
|
|
30
|
+
modifiers: [
|
|
31
|
+
{ name: 'arrow', options: { padding: 14 } },
|
|
32
|
+
{
|
|
33
|
+
name: 'offset',
|
|
34
|
+
options: {
|
|
35
|
+
offset: ({ placement }: { placement: string }) => {
|
|
36
|
+
if (placement.endsWith('-end')) {
|
|
37
|
+
return [11, -4]
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (placement.endsWith('-start')) {
|
|
41
|
+
return [-11, -4]
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return [0, -4]
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
],
|
|
49
|
+
},
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Controlled MUI Tooltip, ensures only one tooltip is visible and adds tap-to-dismiss to mobile
|
|
54
|
+
* (ClickAwayListener doesn't work with bubble itself)
|
|
55
|
+
*/
|
|
56
|
+
function RcSesTooltip({
|
|
57
|
+
title,
|
|
58
|
+
placement = 'top-start',
|
|
59
|
+
slotProps,
|
|
60
|
+
children,
|
|
61
|
+
}: RcSesTooltipProps) {
|
|
62
|
+
const { t } = useTranslation('common')
|
|
63
|
+
|
|
64
|
+
const [open, setOpen] = React.useState(false)
|
|
65
|
+
|
|
66
|
+
const triggerRef = React.useRef<HTMLElement>(null)
|
|
67
|
+
|
|
68
|
+
// if children is used, child ref merge is needed
|
|
69
|
+
const childRef = (
|
|
70
|
+
children as (React.ReactElement & { ref?: React.Ref<HTMLElement> }) | undefined
|
|
71
|
+
)?.ref
|
|
72
|
+
|
|
73
|
+
const handleTriggerRef = useForkRef(triggerRef, childRef ?? null)
|
|
74
|
+
|
|
75
|
+
const hide = React.useCallback(() => setOpen(false), [])
|
|
76
|
+
|
|
77
|
+
const show = React.useCallback(() => {
|
|
78
|
+
if (closeCurrent !== hide) {
|
|
79
|
+
closeCurrent?.()
|
|
80
|
+
closeCurrent = hide
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
setOpen(true)
|
|
84
|
+
}, [hide])
|
|
85
|
+
|
|
86
|
+
// on mobile: tap anywhere to dismiss
|
|
87
|
+
React.useEffect(() => {
|
|
88
|
+
if (!open) {
|
|
89
|
+
return undefined
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const onPointerDown = ({ target }: PointerEvent) => {
|
|
93
|
+
const element = target instanceof Element ? target : null
|
|
94
|
+
|
|
95
|
+
if (
|
|
96
|
+
triggerRef.current?.contains(element) ||
|
|
97
|
+
element?.closest('.MuiTooltip-popper')
|
|
98
|
+
) {
|
|
99
|
+
return
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
hide()
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
document.addEventListener('pointerdown', onPointerDown)
|
|
106
|
+
|
|
107
|
+
return () => document.removeEventListener('pointerdown', onPointerDown)
|
|
108
|
+
}, [open, hide])
|
|
109
|
+
|
|
110
|
+
React.useEffect(
|
|
111
|
+
() => () => {
|
|
112
|
+
if (closeCurrent === hide) {
|
|
113
|
+
closeCurrent = null
|
|
114
|
+
}
|
|
115
|
+
},
|
|
116
|
+
[hide],
|
|
117
|
+
)
|
|
118
|
+
|
|
119
|
+
const { sx: triggerSx, ...triggerProps } = slotProps?.trigger ?? {}
|
|
120
|
+
|
|
121
|
+
const trigger = children ?? (
|
|
122
|
+
<IconButton
|
|
123
|
+
size='small'
|
|
124
|
+
aria-label={t('components.Tooltip.trigger')}
|
|
125
|
+
{...triggerProps}
|
|
126
|
+
sx={[
|
|
127
|
+
{
|
|
128
|
+
p: 0.25,
|
|
129
|
+
ml: 0.5,
|
|
130
|
+
verticalAlign: 'text-bottom',
|
|
131
|
+
color: 'grey.600',
|
|
132
|
+
'&:hover, &.Mui-focusVisible': { color: 'primary.main' },
|
|
133
|
+
},
|
|
134
|
+
...(Array.isArray(triggerSx) ? triggerSx : [triggerSx]),
|
|
135
|
+
]}
|
|
136
|
+
>
|
|
137
|
+
<QuestionIcon size={16} aria-hidden />
|
|
138
|
+
</IconButton>
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
return (
|
|
142
|
+
<Tooltip
|
|
143
|
+
title={title}
|
|
144
|
+
open={open}
|
|
145
|
+
describeChild
|
|
146
|
+
arrow
|
|
147
|
+
placement={placement}
|
|
148
|
+
leaveDelay={100}
|
|
149
|
+
disableTouchListener
|
|
150
|
+
TransitionComponent={Fade}
|
|
151
|
+
PopperProps={popperProps}
|
|
152
|
+
onOpen={show}
|
|
153
|
+
onClose={hide}
|
|
154
|
+
>
|
|
155
|
+
{/* same principle as in MUI Tooltip */}
|
|
156
|
+
{React.cloneElement(trigger, {
|
|
157
|
+
ref: handleTriggerRef,
|
|
158
|
+
onClick: (event: React.MouseEvent) => {
|
|
159
|
+
trigger.props.onClick?.(event)
|
|
160
|
+
show()
|
|
161
|
+
},
|
|
162
|
+
})}
|
|
163
|
+
</Tooltip>
|
|
164
|
+
)
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export default RcSesTooltip
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { ThemeProvider } from '@mui/material/styles'
|
|
2
|
+
import { fireEvent, render, screen, waitFor } from '@testing-library/react'
|
|
3
|
+
import { useForm } from 'react-hook-form'
|
|
4
|
+
|
|
5
|
+
import theme from '@/theme/light'
|
|
6
|
+
|
|
7
|
+
import RcSesSelect from './index'
|
|
8
|
+
|
|
9
|
+
type TestWrapperProps = {
|
|
10
|
+
defaultValue?: string | null
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const options = [
|
|
14
|
+
{
|
|
15
|
+
value: 'long-option',
|
|
16
|
+
label: 'Juridiniu asmenu registro isplestinis pavadinimas su ilgu tekstu',
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
value: 'short-option',
|
|
20
|
+
label: 'Trumpas pasirinkimas',
|
|
21
|
+
},
|
|
22
|
+
]
|
|
23
|
+
|
|
24
|
+
const TestWrapper = ({ defaultValue = null }: TestWrapperProps) => {
|
|
25
|
+
const { control } = useForm({
|
|
26
|
+
defaultValues: {
|
|
27
|
+
selection: defaultValue,
|
|
28
|
+
},
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
return (
|
|
32
|
+
<ThemeProvider theme={theme}>
|
|
33
|
+
<RcSesSelect
|
|
34
|
+
id='selection'
|
|
35
|
+
name='selection'
|
|
36
|
+
control={control}
|
|
37
|
+
label='Pasirinkti'
|
|
38
|
+
options={options}
|
|
39
|
+
/>
|
|
40
|
+
</ThemeProvider>
|
|
41
|
+
)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
describe('RcSesSelect', () => {
|
|
45
|
+
test('single select rodo pilna pasirinktos reiksmes label', () => {
|
|
46
|
+
render(<TestWrapper defaultValue='long-option' />)
|
|
47
|
+
|
|
48
|
+
expect(
|
|
49
|
+
screen.getByText(
|
|
50
|
+
'Juridiniu asmenu registro isplestinis pavadinimas su ilgu tekstu',
|
|
51
|
+
),
|
|
52
|
+
).toBeInTheDocument()
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
test('single select lieka pasirenkamas klaviatura kai niekas nepasirinkta', async () => {
|
|
56
|
+
render(<TestWrapper />)
|
|
57
|
+
|
|
58
|
+
const input = screen.getByRole('combobox')
|
|
59
|
+
expect(input).not.toHaveAttribute('readonly')
|
|
60
|
+
|
|
61
|
+
fireEvent.mouseDown(input)
|
|
62
|
+
fireEvent.keyDown(input, { key: 'ArrowDown' })
|
|
63
|
+
fireEvent.keyDown(input, { key: 'Enter' })
|
|
64
|
+
|
|
65
|
+
await waitFor(() => {
|
|
66
|
+
expect(
|
|
67
|
+
screen.getByText(
|
|
68
|
+
'Juridiniu asmenu registro isplestinis pavadinimas su ilgu tekstu',
|
|
69
|
+
),
|
|
70
|
+
).toBeInTheDocument()
|
|
71
|
+
})
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
test('single select siulo visas parinktis kai reiksme jau pasirinkta', async () => {
|
|
75
|
+
render(<TestWrapper defaultValue='long-option' />)
|
|
76
|
+
|
|
77
|
+
fireEvent.mouseDown(screen.getByRole('combobox'))
|
|
78
|
+
|
|
79
|
+
await waitFor(() => {
|
|
80
|
+
expect(screen.getAllByRole('option')).toHaveLength(options.length)
|
|
81
|
+
})
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
test('single select leidzia pakeisti jau pasirinkta reiksme', async () => {
|
|
85
|
+
render(<TestWrapper defaultValue='long-option' />)
|
|
86
|
+
|
|
87
|
+
fireEvent.mouseDown(screen.getByRole('combobox'))
|
|
88
|
+
fireEvent.click(await screen.findByRole('option', { name: 'Trumpas pasirinkimas' }))
|
|
89
|
+
|
|
90
|
+
await waitFor(() => {
|
|
91
|
+
expect(
|
|
92
|
+
screen.getByText('Trumpas pasirinkimas', { selector: 'div' }),
|
|
93
|
+
).toBeInTheDocument()
|
|
94
|
+
})
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
test('single select neleidzia rasti kai reiksme jau pasirinkta', () => {
|
|
98
|
+
render(<TestWrapper defaultValue='long-option' />)
|
|
99
|
+
|
|
100
|
+
expect(screen.getByRole('combobox')).toHaveAttribute('readonly')
|
|
101
|
+
})
|
|
102
|
+
})
|