@registrucentras/rc-ses-react-components 1.6.5 → 1.7.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/ArrowsClockwiseIcon.d.ts +3 -0
- package/dist/assets/icons/SearchHelpIcon.d.ts +3 -0
- package/dist/assets/icons/SearchXIcon.d.ts +3 -0
- package/dist/components/common/Breadcrumbs/index.d.ts +3 -1
- package/dist/components/common/Snackbar/SnackbarProvider.d.ts +22 -0
- package/dist/components/common/Snackbar/config.d.ts +21 -0
- package/dist/components/common/Snackbar/index.d.ts +16 -0
- package/dist/components/common/Snackbar/types.d.ts +2 -0
- package/dist/components/form/inputs/CheckboxFormControl.d.ts +2 -0
- package/dist/components/form/inputs/FormControlLabel.d.ts +10 -0
- package/dist/components/form/inputs/SearchInput/index.d.ts +27 -0
- package/dist/components/form/inputs/SimpleCheckbox.d.ts +6 -0
- package/dist/components/layout/ServiceHeader.d.ts +2 -1
- package/dist/components/utils/loading.d.ts +3 -0
- package/dist/library/icons.d.ts +5 -1
- package/dist/library/index.d.ts +11 -2
- package/dist/library/types.d.ts +3 -1
- package/dist/registrucentras-rc-ses-react-components.cjs +10 -27
- package/dist/registrucentras-rc-ses-react-components.js +3484 -2897
- package/dist/stories/components/actions/Button.stories.d.ts +5 -0
- package/dist/stories/components/{SelectableCardList.stories.d.ts → display/SelectableCardList.stories.d.ts} +1 -1
- package/dist/stories/components/feedback/Snackbar.stories.d.ts +143 -0
- package/dist/stories/components/form/CheckboxFormControl.stories.d.ts +3 -0
- package/dist/stories/components/form/SimpleCheckbox.stories.d.ts +19 -0
- package/dist/stories/components/inputs/SearchInput.stories.d.ts +11 -0
- package/dist/stories/components/layout/ServiceHeader.stories.d.ts +1 -0
- package/dist/theme/light/MuiOutlinedInput.d.ts +3 -0
- package/dist/theme/light/index.d.ts +6 -0
- package/dist/theme/palette.d.ts +9 -1
- package/package.json +1 -1
- package/src/assets/icons/ArrowsClockwiseIcon.tsx +16 -0
- package/src/assets/icons/CheckIcon.tsx +1 -1
- package/src/assets/icons/SearchHelpIcon.tsx +44 -0
- package/src/assets/icons/SearchXIcon.tsx +41 -0
- package/src/components/common/Breadcrumbs/index.tsx +4 -3
- package/src/components/common/ListWithIcons/components/ListWithIconsItem.tsx +5 -3
- package/src/components/common/ListWithIcons/index.tsx +1 -1
- package/src/components/common/SelectableCardList/components/SelectableCardListItem.tsx +3 -1
- package/src/components/common/Snackbar/SnackbarProvider.tsx +78 -0
- package/src/components/common/Snackbar/config.ts +77 -0
- package/src/components/common/Snackbar/index.test.tsx +119 -0
- package/src/components/common/Snackbar/index.tsx +181 -0
- package/src/components/common/Snackbar/types.ts +2 -0
- package/src/components/form/inputs/Checkbox.tsx +2 -2
- package/src/components/form/inputs/CheckboxFormControl.test.tsx +68 -0
- package/src/components/form/inputs/CheckboxFormControl.tsx +48 -29
- package/src/components/form/inputs/FormControlLabel.tsx +36 -0
- package/src/components/form/inputs/SearchInput/index.test.tsx +203 -0
- package/src/components/form/inputs/SearchInput/index.tsx +282 -0
- package/src/components/form/inputs/SimpleCheckbox.tsx +23 -0
- package/src/components/layout/ServiceHeader.tsx +22 -8
- package/src/components/utils/loading.tsx +9 -0
- package/src/i18n/namespaces/input/en.json +11 -0
- package/src/i18n/namespaces/input/lt.json +11 -0
- package/src/library/icons.ts +8 -0
- package/src/library/index.ts +15 -1
- package/src/library/types.ts +9 -1
- package/src/stories/Colors.mdx +127 -0
- package/src/stories/components/actions/Button.stories.tsx +130 -0
- package/src/stories/components/{SelectableCardList.stories.tsx → display/SelectableCardList.stories.tsx} +36 -19
- package/src/stories/components/feedback/Snackbar.stories.tsx +504 -0
- package/src/stories/components/form/CheckboxFormControl.stories.tsx +312 -2
- package/src/stories/components/form/SimpleCheckbox.stories.tsx +52 -0
- package/src/stories/components/icons/Icons.stories.tsx +35 -0
- package/src/stories/components/inputs/SearchInput.stories.tsx +134 -0
- package/src/stories/components/layout/ServiceHeader.stories.tsx +43 -8
- package/src/theme/light/MuiButton.tsx +34 -1
- package/src/theme/light/MuiOutlinedInput.ts +15 -0
- package/src/theme/light/index.tsx +8 -0
- package/src/theme/light/themePalette.tsx +2 -0
- package/src/theme/palette.tsx +11 -1
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { ThemeProvider } from '@mui/material/styles'
|
|
2
|
+
import { fireEvent, render, screen } from '@testing-library/react'
|
|
3
|
+
import { ReactNode } from 'react'
|
|
4
|
+
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
|
|
5
|
+
|
|
6
|
+
import theme from '@/theme/light'
|
|
7
|
+
|
|
8
|
+
import RcSesSnackbar from './index'
|
|
9
|
+
|
|
10
|
+
const DEFAULT_PROPS = { state: 'success' as const, message: 'Test', open: true }
|
|
11
|
+
|
|
12
|
+
const renderSnackbar = (props: ReactNode) =>
|
|
13
|
+
render(<ThemeProvider theme={theme}>{props}</ThemeProvider>)
|
|
14
|
+
|
|
15
|
+
const getMuiSnackbar = () => document.querySelector('.MuiSnackbar-root') as HTMLElement
|
|
16
|
+
|
|
17
|
+
describe('RcSesSnackbar', () => {
|
|
18
|
+
beforeEach(() => {
|
|
19
|
+
vi.useFakeTimers()
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
afterEach(() => {
|
|
23
|
+
vi.runOnlyPendingTimers()
|
|
24
|
+
vi.useRealTimers()
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
test('renders message', () => {
|
|
28
|
+
renderSnackbar(<RcSesSnackbar {...DEFAULT_PROPS} message='Test message' />)
|
|
29
|
+
|
|
30
|
+
expect(screen.getByText('Test message')).toBeInTheDocument()
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
test('renders standard size by default', () => {
|
|
34
|
+
const { container } = renderSnackbar(<RcSesSnackbar {...DEFAULT_PROPS} />)
|
|
35
|
+
|
|
36
|
+
expect(container.querySelector('[role="status"]')).toHaveStyle({
|
|
37
|
+
maxHeight: '120px',
|
|
38
|
+
width: '400px',
|
|
39
|
+
})
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
test('renders compact size', () => {
|
|
43
|
+
const { container } = renderSnackbar(
|
|
44
|
+
<RcSesSnackbar {...DEFAULT_PROPS} size='compact' />,
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
expect(container.querySelector('[role="status"]')).toHaveStyle({
|
|
48
|
+
maxHeight: '96px',
|
|
49
|
+
width: '320px',
|
|
50
|
+
})
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
test('closes on Escape key', () => {
|
|
54
|
+
const handleClose = vi.fn()
|
|
55
|
+
renderSnackbar(<RcSesSnackbar {...DEFAULT_PROPS} onClose={handleClose} />)
|
|
56
|
+
const snackbar = getMuiSnackbar()
|
|
57
|
+
fireEvent.keyDown(snackbar, { key: 'Escape', code: 'Escape' })
|
|
58
|
+
|
|
59
|
+
expect(handleClose).toHaveBeenCalled()
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
test('auto-hides after duration', () => {
|
|
63
|
+
const handleClose = vi.fn()
|
|
64
|
+
renderSnackbar(
|
|
65
|
+
<RcSesSnackbar {...DEFAULT_PROPS} onClose={handleClose} duration={1000} />,
|
|
66
|
+
)
|
|
67
|
+
const statusElement = screen.getByText('Test')
|
|
68
|
+
expect(statusElement).toBeInTheDocument()
|
|
69
|
+
|
|
70
|
+
vi.advanceTimersByTime(1000)
|
|
71
|
+
|
|
72
|
+
expect(handleClose).toHaveBeenCalled()
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
test('does not auto-hide when persist is true', async () => {
|
|
76
|
+
const handleClose = vi.fn()
|
|
77
|
+
renderSnackbar(
|
|
78
|
+
<RcSesSnackbar {...DEFAULT_PROPS} onClose={handleClose} persist duration={1000} />,
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
vi.advanceTimersByTime(2000)
|
|
82
|
+
|
|
83
|
+
expect(handleClose).not.toHaveBeenCalled()
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
test('renders action button in standard size with onAction', () => {
|
|
87
|
+
renderSnackbar(
|
|
88
|
+
<RcSesSnackbar
|
|
89
|
+
{...DEFAULT_PROPS}
|
|
90
|
+
size='standard'
|
|
91
|
+
onAction={() => {}}
|
|
92
|
+
actionLabel='Undo'
|
|
93
|
+
/>,
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
expect(screen.getByRole('button', { name: 'Undo' })).toBeInTheDocument()
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
test('does not render action button in compact size', () => {
|
|
100
|
+
renderSnackbar(
|
|
101
|
+
<RcSesSnackbar
|
|
102
|
+
{...DEFAULT_PROPS}
|
|
103
|
+
size='compact'
|
|
104
|
+
onAction={() => {}}
|
|
105
|
+
actionLabel='Undo'
|
|
106
|
+
/>,
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
expect(screen.queryByRole('button', { name: 'Undo' })).not.toBeInTheDocument()
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
test('has correct accessibility attributes', () => {
|
|
113
|
+
renderSnackbar(<RcSesSnackbar {...DEFAULT_PROPS} />)
|
|
114
|
+
const status = screen.getByRole('status')
|
|
115
|
+
|
|
116
|
+
expect(status).toHaveAttribute('aria-atomic', 'true')
|
|
117
|
+
expect(status).toHaveAttribute('aria-live', 'polite')
|
|
118
|
+
})
|
|
119
|
+
})
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Box,
|
|
3
|
+
Snackbar as MuiSnackbar,
|
|
4
|
+
Slide,
|
|
5
|
+
Typography,
|
|
6
|
+
useMediaQuery,
|
|
7
|
+
useTheme,
|
|
8
|
+
} from '@mui/material'
|
|
9
|
+
import { useEffect, useState } from 'react'
|
|
10
|
+
import { useTranslation } from 'react-i18next'
|
|
11
|
+
|
|
12
|
+
import CloseIcon from '@/assets/icons/CloseIcon'
|
|
13
|
+
import { grey } from '@/theme/palette'
|
|
14
|
+
|
|
15
|
+
import RcSesButton from '../Button'
|
|
16
|
+
import { SNACKBAR_DURATION, SNACKBAR_ICON_SIZE, sizeConfig, stateConfig } from './config'
|
|
17
|
+
import { type SnackbarSize, type SnackbarState } from './types'
|
|
18
|
+
|
|
19
|
+
export interface RcSesSnackbarProps {
|
|
20
|
+
state: SnackbarState
|
|
21
|
+
message: string
|
|
22
|
+
size?: SnackbarSize
|
|
23
|
+
onAction?: () => void
|
|
24
|
+
actionLabel?: string
|
|
25
|
+
onClose?: () => void
|
|
26
|
+
open?: boolean
|
|
27
|
+
dismissOnAction?: boolean
|
|
28
|
+
duration?: number
|
|
29
|
+
persist?: boolean
|
|
30
|
+
showClose?: boolean
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function RcSesSnackbar({
|
|
34
|
+
state,
|
|
35
|
+
size = 'standard',
|
|
36
|
+
message,
|
|
37
|
+
onClose,
|
|
38
|
+
onAction,
|
|
39
|
+
dismissOnAction = true,
|
|
40
|
+
actionLabel,
|
|
41
|
+
open: controlledOpen,
|
|
42
|
+
duration = SNACKBAR_DURATION,
|
|
43
|
+
persist: controlledPersist,
|
|
44
|
+
showClose = true,
|
|
45
|
+
}: RcSesSnackbarProps) {
|
|
46
|
+
const theme = useTheme()
|
|
47
|
+
const { t } = useTranslation('input')
|
|
48
|
+
const [internalOpen, setInternalOpen] = useState(controlledOpen ?? true)
|
|
49
|
+
const [isPaused, setIsPaused] = useState(false)
|
|
50
|
+
const persist = controlledPersist ?? state === 'action-needed'
|
|
51
|
+
|
|
52
|
+
useEffect(() => {
|
|
53
|
+
if (controlledOpen !== undefined) {
|
|
54
|
+
setInternalOpen(controlledOpen)
|
|
55
|
+
}
|
|
56
|
+
}, [controlledOpen])
|
|
57
|
+
|
|
58
|
+
const config = stateConfig[state]
|
|
59
|
+
const StateIcon = config.icon
|
|
60
|
+
const isStandard = size === 'standard'
|
|
61
|
+
|
|
62
|
+
const handleClose = () => {
|
|
63
|
+
setInternalOpen(false)
|
|
64
|
+
onClose?.()
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const handleAction = () => {
|
|
68
|
+
onAction?.()
|
|
69
|
+
if (dismissOnAction) handleClose()
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const prefersReducedMotion = useMediaQuery('(prefers-reduced-motion: reduce)')
|
|
73
|
+
|
|
74
|
+
return (
|
|
75
|
+
<MuiSnackbar
|
|
76
|
+
open={internalOpen}
|
|
77
|
+
autoHideDuration={persist || isPaused ? null : duration}
|
|
78
|
+
onClose={handleClose}
|
|
79
|
+
anchorOrigin={{ vertical: 'bottom', horizontal: 'center' }}
|
|
80
|
+
onMouseEnter={() => setIsPaused(true)}
|
|
81
|
+
onMouseLeave={() => setIsPaused(false)}
|
|
82
|
+
onKeyDown={(e) => e.key === 'Escape' && handleClose()}
|
|
83
|
+
TransitionComponent={Slide}
|
|
84
|
+
transitionDuration={prefersReducedMotion ? { enter: 0, exit: 0 } : undefined}
|
|
85
|
+
sx={{
|
|
86
|
+
transition: prefersReducedMotion ? 'none' : `bottom 150ms ease-in-out !important`,
|
|
87
|
+
}}
|
|
88
|
+
>
|
|
89
|
+
<Box
|
|
90
|
+
role={config.ariaLive === 'assertive' ? 'alert' : 'status'}
|
|
91
|
+
aria-live={config.ariaLive}
|
|
92
|
+
aria-atomic='true'
|
|
93
|
+
sx={{
|
|
94
|
+
display: 'flex',
|
|
95
|
+
alignItems: 'center',
|
|
96
|
+
gap: theme.spacing(1),
|
|
97
|
+
width: `${sizeConfig[size].width}px`,
|
|
98
|
+
maxHeight: `${sizeConfig[size].maxHeight}px`,
|
|
99
|
+
backgroundColor: grey['900'],
|
|
100
|
+
color: grey['50'],
|
|
101
|
+
borderRadius: '8px',
|
|
102
|
+
padding: sizeConfig[size].padding,
|
|
103
|
+
opacity: internalOpen ? 1 : 0,
|
|
104
|
+
transition: 'opacity 200ms ease-out',
|
|
105
|
+
}}
|
|
106
|
+
>
|
|
107
|
+
<Box
|
|
108
|
+
sx={{
|
|
109
|
+
flexShrink: 0,
|
|
110
|
+
display: 'flex',
|
|
111
|
+
alignItems: 'center',
|
|
112
|
+
justifyContent: 'center',
|
|
113
|
+
'& svg': {
|
|
114
|
+
width: `${SNACKBAR_ICON_SIZE.width}px !important`,
|
|
115
|
+
height: `${SNACKBAR_ICON_SIZE.height}px !important`,
|
|
116
|
+
},
|
|
117
|
+
}}
|
|
118
|
+
>
|
|
119
|
+
<StateIcon fillColor={config.color} aria-hidden />
|
|
120
|
+
</Box>
|
|
121
|
+
<Typography
|
|
122
|
+
variant='body2'
|
|
123
|
+
sx={{
|
|
124
|
+
flex: 1,
|
|
125
|
+
minWidth: 0,
|
|
126
|
+
overflow: 'hidden',
|
|
127
|
+
display: '-webkit-box',
|
|
128
|
+
WebkitBoxOrient: 'vertical',
|
|
129
|
+
WebkitLineClamp: 3,
|
|
130
|
+
textOverflow: 'ellipsis',
|
|
131
|
+
}}
|
|
132
|
+
>
|
|
133
|
+
{message}
|
|
134
|
+
</Typography>
|
|
135
|
+
<Box sx={{ display: 'flex', gap: '12px', alignItems: 'center' }}>
|
|
136
|
+
{onAction && isStandard && (
|
|
137
|
+
<RcSesButton
|
|
138
|
+
size='small'
|
|
139
|
+
variant='outlined'
|
|
140
|
+
color='light'
|
|
141
|
+
onClick={handleAction}
|
|
142
|
+
sx={{
|
|
143
|
+
textTransform: 'none',
|
|
144
|
+
fontSize: '0.875rem',
|
|
145
|
+
flexShrink: 0,
|
|
146
|
+
}}
|
|
147
|
+
>
|
|
148
|
+
{actionLabel}
|
|
149
|
+
</RcSesButton>
|
|
150
|
+
)}
|
|
151
|
+
{showClose && (
|
|
152
|
+
<RcSesButton
|
|
153
|
+
variant='outlined'
|
|
154
|
+
color='ghost'
|
|
155
|
+
onClick={handleClose}
|
|
156
|
+
sx={{
|
|
157
|
+
minWidth: 'auto',
|
|
158
|
+
padding: theme.spacing(0.625),
|
|
159
|
+
display: 'flex',
|
|
160
|
+
alignItems: 'center',
|
|
161
|
+
justifyContent: 'center',
|
|
162
|
+
width: '34px',
|
|
163
|
+
height: '34px',
|
|
164
|
+
flexShrink: 0,
|
|
165
|
+
'& svg': {
|
|
166
|
+
width: '20px',
|
|
167
|
+
height: '20px',
|
|
168
|
+
},
|
|
169
|
+
}}
|
|
170
|
+
aria-label={t('components.RcSesSnackbar.aria.close')}
|
|
171
|
+
>
|
|
172
|
+
<CloseIcon />
|
|
173
|
+
</RcSesButton>
|
|
174
|
+
)}
|
|
175
|
+
</Box>
|
|
176
|
+
</Box>
|
|
177
|
+
</MuiSnackbar>
|
|
178
|
+
)
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export default RcSesSnackbar
|
|
@@ -32,7 +32,7 @@ type Props = Pick<TControllerProps, ImmediateControllerProps> &
|
|
|
32
32
|
}
|
|
33
33
|
|
|
34
34
|
function RcSesCheckbox(props: Props) {
|
|
35
|
-
const { errors, label, slotProps } = props
|
|
35
|
+
const { errors, label, slotProps, loading } = props
|
|
36
36
|
|
|
37
37
|
// eslint-disable-next-line react/destructuring-assignment
|
|
38
38
|
const id = useMemo(() => props.id ?? uuidv4(), [props.id])
|
|
@@ -44,7 +44,7 @@ function RcSesCheckbox(props: Props) {
|
|
|
44
44
|
required={!!props?.rules?.required}
|
|
45
45
|
{...slotProps?.wrapper}
|
|
46
46
|
>
|
|
47
|
-
<RcSesCheckboxFormControl {...props} id={id} />
|
|
47
|
+
<RcSesCheckboxFormControl {...props} id={id} loading={loading} />
|
|
48
48
|
</RcSesFormControlWrapper>
|
|
49
49
|
)
|
|
50
50
|
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { ThemeProvider } from '@mui/material/styles'
|
|
2
|
+
import { render } from '@testing-library/react'
|
|
3
|
+
import { useForm } from 'react-hook-form'
|
|
4
|
+
import { describe, expect, test, vi } from 'vitest'
|
|
5
|
+
|
|
6
|
+
import theme from '@/theme/light'
|
|
7
|
+
|
|
8
|
+
import RcSesCheckboxFormControl from './CheckboxFormControl'
|
|
9
|
+
|
|
10
|
+
type WrapperProps = Partial<React.ComponentProps<typeof RcSesCheckboxFormControl>> & {
|
|
11
|
+
defaultValue?: boolean
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const TestWrapper = ({ defaultValue = false, ...props }: WrapperProps) => {
|
|
15
|
+
const { control } = useForm({ defaultValues: { checkbox: defaultValue } })
|
|
16
|
+
return (
|
|
17
|
+
<ThemeProvider theme={theme}>
|
|
18
|
+
<RcSesCheckboxFormControl
|
|
19
|
+
id='checkbox'
|
|
20
|
+
name='checkbox'
|
|
21
|
+
control={control}
|
|
22
|
+
{...props}
|
|
23
|
+
>
|
|
24
|
+
Test Checkbox
|
|
25
|
+
</RcSesCheckboxFormControl>
|
|
26
|
+
</ThemeProvider>
|
|
27
|
+
)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
describe('RcSesCheckboxFormControl - Indeterminate State', () => {
|
|
31
|
+
test('should auto-check parent when all children are checked', () => {
|
|
32
|
+
const { container } = render(<TestWrapper childValues={[true, true, true]} />)
|
|
33
|
+
const checkbox = container.querySelector('input#checkbox') as HTMLInputElement
|
|
34
|
+
expect(checkbox.checked).toBe(true)
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
test('should auto-uncheck parent when all children are unchecked', () => {
|
|
38
|
+
const { container } = render(<TestWrapper childValues={[false, false, false]} />)
|
|
39
|
+
const checkbox = container.querySelector('input#checkbox') as HTMLInputElement
|
|
40
|
+
expect(checkbox.checked).toBe(false)
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
test('should call onChildValuesChange with [true, true, true] when clicking parent in indeterminate state', () => {
|
|
44
|
+
const onChildValuesChange = vi.fn()
|
|
45
|
+
const { container } = render(
|
|
46
|
+
<TestWrapper
|
|
47
|
+
childValues={[true, false, true]}
|
|
48
|
+
onChildValuesChange={onChildValuesChange}
|
|
49
|
+
/>,
|
|
50
|
+
)
|
|
51
|
+
const checkbox = container.querySelector('input#checkbox') as HTMLInputElement
|
|
52
|
+
checkbox.click()
|
|
53
|
+
expect(onChildValuesChange).toHaveBeenCalledWith([true, true, true])
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
test('should call onChildValuesChange with [false, false, false] when clicking checked parent', () => {
|
|
57
|
+
const onChildValuesChange = vi.fn()
|
|
58
|
+
const { container } = render(
|
|
59
|
+
<TestWrapper
|
|
60
|
+
childValues={[true, true, true]}
|
|
61
|
+
onChildValuesChange={onChildValuesChange}
|
|
62
|
+
/>,
|
|
63
|
+
)
|
|
64
|
+
const checkbox = container.querySelector('input#checkbox') as HTMLInputElement
|
|
65
|
+
checkbox.click()
|
|
66
|
+
expect(onChildValuesChange).toHaveBeenCalledWith([false, false, false])
|
|
67
|
+
})
|
|
68
|
+
})
|
|
@@ -1,17 +1,16 @@
|
|
|
1
1
|
import {
|
|
2
|
-
FormControlLabel,
|
|
3
2
|
FormControlLabelProps,
|
|
4
3
|
FormLabel,
|
|
5
4
|
Checkbox as MuiCheckbox,
|
|
6
5
|
CheckboxProps as MuiCheckboxProps,
|
|
7
|
-
styled,
|
|
8
6
|
} from '@mui/material'
|
|
9
|
-
import React from 'react'
|
|
7
|
+
import React, { useEffect, useMemo } from 'react'
|
|
10
8
|
import { UseControllerProps, useController } from 'react-hook-form'
|
|
11
9
|
|
|
12
10
|
import CheckBoldIcon from '@/assets/icons/CheckBoldIcon'
|
|
13
11
|
import CheckUncheckedBoldIcon from '@/assets/icons/CheckUncheckedBoldIcon'
|
|
14
|
-
import
|
|
12
|
+
import RcSesFormControlLabel from '@/components/form/inputs/FormControlLabel'
|
|
13
|
+
import getLoadingIcon from '@/components/utils/loading'
|
|
15
14
|
import palette from '@/theme/palette'
|
|
16
15
|
|
|
17
16
|
import { RcSesFormControlWrapperProps } from '../components/FormControlWrapper'
|
|
@@ -32,6 +31,8 @@ type Props = Pick<TControllerProps, ImmediateControllerProps> &
|
|
|
32
31
|
children: React.ReactNode
|
|
33
32
|
loading?: boolean
|
|
34
33
|
variant?: 'flat' | 'outlined'
|
|
34
|
+
childValues?: boolean[]
|
|
35
|
+
onChildValuesChange?: (childValues: boolean[]) => void
|
|
35
36
|
slotProps?: {
|
|
36
37
|
field?: Partial<Omit<TFieldProps, ImmediateFieldProps>>
|
|
37
38
|
label?: Partial<FormControlLabelProps>
|
|
@@ -39,46 +40,63 @@ type Props = Pick<TControllerProps, ImmediateControllerProps> &
|
|
|
39
40
|
}
|
|
40
41
|
}
|
|
41
42
|
|
|
42
|
-
const LoadingStateIcon = styled(SpinnerGapBoldIcon)(`
|
|
43
|
-
@keyframes loadingStateIconRotation {
|
|
44
|
-
12.5% { transform: rotateZ( 45deg); }
|
|
45
|
-
25.0% { transform: rotateZ( 90deg); }
|
|
46
|
-
37.5% { transform: rotateZ(135deg); }
|
|
47
|
-
50.0% { transform: rotateZ(180deg); }
|
|
48
|
-
62.5% { transform: rotateZ(225deg); }
|
|
49
|
-
75.0% { transform: rotateZ(270deg); }
|
|
50
|
-
87.5% { transform: rotateZ(315deg); }
|
|
51
|
-
100.0% { transform: rotateZ(360deg); }
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
animation-duration: 1s;
|
|
55
|
-
animation-iteration-count: infinite;
|
|
56
|
-
animation-name: loadingStateIconRotation;
|
|
57
|
-
animation-timing-function: step-start;
|
|
58
|
-
box-shadow: none !important;
|
|
59
|
-
`)
|
|
60
|
-
|
|
61
43
|
function RcSesCheckboxFormControl(props: Props) {
|
|
62
|
-
const {
|
|
63
|
-
|
|
44
|
+
const {
|
|
45
|
+
children,
|
|
46
|
+
control,
|
|
47
|
+
disabled,
|
|
48
|
+
id,
|
|
49
|
+
loading,
|
|
50
|
+
name,
|
|
51
|
+
rules,
|
|
52
|
+
slotProps,
|
|
53
|
+
variant,
|
|
54
|
+
childValues = [],
|
|
55
|
+
onChildValuesChange,
|
|
56
|
+
} = props
|
|
64
57
|
|
|
65
58
|
const {
|
|
66
|
-
field: { value, ...controllerProps },
|
|
59
|
+
field: { value, onChange, ...controllerProps },
|
|
67
60
|
} = useController({
|
|
68
61
|
control,
|
|
69
62
|
name,
|
|
70
63
|
rules,
|
|
71
64
|
})
|
|
72
65
|
|
|
66
|
+
const checkedCount = useMemo(() => childValues.filter(Boolean).length, [childValues])
|
|
67
|
+
|
|
68
|
+
const computedIndeterminate = useMemo(
|
|
69
|
+
() => checkedCount > 0 && checkedCount < childValues.length,
|
|
70
|
+
[checkedCount, childValues.length],
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
useEffect(() => {
|
|
74
|
+
if (!childValues.length) return
|
|
75
|
+
const expectedValue = checkedCount === childValues.length
|
|
76
|
+
|
|
77
|
+
if (expectedValue !== value) onChange(expectedValue)
|
|
78
|
+
}, [checkedCount, childValues.length, value, onChange])
|
|
79
|
+
|
|
80
|
+
const handleParentChange = (event: React.ChangeEvent<HTMLInputElement>) => {
|
|
81
|
+
const newValue = event.target.checked
|
|
82
|
+
onChange(newValue)
|
|
83
|
+
|
|
84
|
+
if (onChildValuesChange && childValues.length > 0) {
|
|
85
|
+
onChildValuesChange(childValues.map(() => newValue))
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
73
89
|
return (
|
|
74
|
-
<
|
|
90
|
+
<RcSesFormControlLabel
|
|
75
91
|
control={
|
|
76
92
|
<MuiCheckbox
|
|
77
93
|
id={id}
|
|
78
94
|
checked={value === true}
|
|
79
|
-
|
|
95
|
+
indeterminate={computedIndeterminate}
|
|
96
|
+
checkedIcon={getLoadingIcon(<CheckBoldIcon />, loading)}
|
|
80
97
|
disabled={disabled}
|
|
81
|
-
icon={
|
|
98
|
+
icon={getLoadingIcon(<CheckUncheckedBoldIcon />, loading)}
|
|
99
|
+
onChange={handleParentChange}
|
|
82
100
|
{...controllerProps}
|
|
83
101
|
{...slotProps?.field}
|
|
84
102
|
disableRipple
|
|
@@ -94,6 +112,7 @@ function RcSesCheckboxFormControl(props: Props) {
|
|
|
94
112
|
)}
|
|
95
113
|
</FormLabel>
|
|
96
114
|
}
|
|
115
|
+
loading={loading}
|
|
97
116
|
{...slotProps?.label}
|
|
98
117
|
slotProps={{
|
|
99
118
|
typography: {
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { FormControlLabel, FormControlLabelProps, Skeleton } from '@mui/material'
|
|
2
|
+
import React, { ReactNode } from 'react'
|
|
3
|
+
|
|
4
|
+
export type RcSesFormControlLabelProps = Omit<FormControlLabelProps, 'label'> & {
|
|
5
|
+
loading?: boolean
|
|
6
|
+
label: ReactNode
|
|
7
|
+
skeletonWidth?: number
|
|
8
|
+
skeletonHeight?: number
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function RcSesFormControlLabel({
|
|
12
|
+
loading = false,
|
|
13
|
+
label,
|
|
14
|
+
skeletonWidth = 100,
|
|
15
|
+
skeletonHeight = 20,
|
|
16
|
+
...props
|
|
17
|
+
}: RcSesFormControlLabelProps) {
|
|
18
|
+
return (
|
|
19
|
+
<FormControlLabel
|
|
20
|
+
{...props}
|
|
21
|
+
label={
|
|
22
|
+
loading ? (
|
|
23
|
+
<Skeleton
|
|
24
|
+
width={skeletonWidth}
|
|
25
|
+
height={skeletonHeight}
|
|
26
|
+
sx={{ display: 'inline-block' }}
|
|
27
|
+
/>
|
|
28
|
+
) : (
|
|
29
|
+
label
|
|
30
|
+
)
|
|
31
|
+
}
|
|
32
|
+
/>
|
|
33
|
+
)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export default RcSesFormControlLabel
|