@registrucentras/rc-ses-react-components 2.0.0-rc.1 → 2.0.0-rc.2

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.
@@ -1,5 +1,7 @@
1
1
  import {
2
2
  Avatar,
3
+ Box,
4
+ Checkbox,
3
5
  FormControl,
4
6
  FormControlLabel,
5
7
  FormLabel,
@@ -7,15 +9,18 @@ import {
7
9
  RadioGroup,
8
10
  Stack,
9
11
  TextField,
12
+ Typography,
10
13
  } from '@mui/material'
11
14
  import type { Meta, StoryObj } from '@storybook/react-vite'
12
15
  import { useState } from 'react'
16
+ import { useForm } from 'react-hook-form'
13
17
 
14
18
  import InfoFillIcon from '@/assets/icons/InfoFillIcon'
15
19
  import NotePencilIcon from '@/assets/icons/NotePencilIcon'
16
20
  import TrashIcon from '@/assets/icons/TrashIcon'
17
21
  import UserIcon from '@/assets/icons/UserIcon'
18
22
  import AdvancedList, { type AdvancedListItemData } from '@/components/common/AdvancedList'
23
+ import RcSesSelect from '@/components/form/inputs/Select'
19
24
 
20
25
  const buildItems = (): AdvancedListItemData[] =>
21
26
  Array.from({ length: 8 }, (_, index) => ({
@@ -191,6 +196,132 @@ export const OwnerTypeExpand: Story = {
191
196
  render: () => <OwnerTypeExpandStory />,
192
197
  }
193
198
 
199
+ type OwnerBranchFormModel = {
200
+ individual: { municipality: string | null }
201
+ legalEntity: { legalForm: string | null }
202
+ }
203
+
204
+ const MUNICIPALITY_OPTIONS = [
205
+ { value: 'vilnius', label: 'Vilniaus m. sav.' },
206
+ { value: 'kaunas', label: 'Kauno m. sav.' },
207
+ ]
208
+
209
+ const LEGAL_FORM_OPTIONS = [
210
+ { value: 'uab', label: 'UAB' },
211
+ { value: 'mb', label: 'Mažoji bendrija' },
212
+ ]
213
+
214
+ const PANEL_SX = {
215
+ backgroundColor: 'grey.100',
216
+ borderRadius: '.5rem',
217
+ fontSize: '.75rem',
218
+ m: 0,
219
+ overflowX: 'auto',
220
+ p: 1.5,
221
+ }
222
+
223
+ const RadioBranchFormStory = () => {
224
+ const [ownerTypeId, setOwnerTypeId] = useState(OWNER_TYPE_OPTIONS[0].id)
225
+ const [shouldUnregister, setShouldUnregister] = useState(true)
226
+
227
+ const { control, watch } = useForm<OwnerBranchFormModel>({
228
+ defaultValues: {
229
+ individual: { municipality: null },
230
+ legalEntity: { legalForm: null },
231
+ },
232
+ })
233
+
234
+ const formState = watch()
235
+
236
+ // The non-selected branch stays in form state when shouldUnregister is off, so the
237
+ // submitted payload is built from the selected branch instead of the whole form.
238
+ const payload =
239
+ ownerTypeId === 'owner-legal'
240
+ ? { legalEntity: formState.legalEntity }
241
+ : { individual: formState.individual }
242
+
243
+ // Only the selected branch renders its fields, so switching unmounts the other one -
244
+ // that unmount is what makes shouldUnregister observable.
245
+ const renderBranchFields = (optionId: string) => {
246
+ if (optionId !== ownerTypeId) return undefined
247
+
248
+ if (optionId === 'owner-legal') {
249
+ return (
250
+ <RcSesSelect
251
+ id='owner-branch-legal-form'
252
+ control={control}
253
+ name='legalEntity.legalForm'
254
+ label='Juridinio asmens forma'
255
+ options={LEGAL_FORM_OPTIONS}
256
+ slotProps={{ controller: { shouldUnregister } }}
257
+ />
258
+ )
259
+ }
260
+
261
+ return (
262
+ <RcSesSelect
263
+ id='owner-branch-municipality'
264
+ control={control}
265
+ name='individual.municipality'
266
+ label='Savivaldybė'
267
+ options={MUNICIPALITY_OPTIONS}
268
+ slotProps={{ controller: { shouldUnregister } }}
269
+ />
270
+ )
271
+ }
272
+
273
+ const items: AdvancedListItemData[] = OWNER_TYPE_OPTIONS.map((option) => ({
274
+ id: option.id,
275
+ title: option.title,
276
+ state: ownerTypeId === option.id ? 'selected' : 'rest',
277
+ leading: {
278
+ type: 'radio',
279
+ name: 'owner-branch-form',
280
+ checked: ownerTypeId === option.id,
281
+ onChange: () => setOwnerTypeId(option.id),
282
+ },
283
+ expanded: ownerTypeId === option.id,
284
+ expandedContent: renderBranchFields(option.id),
285
+ }))
286
+
287
+ return (
288
+ <Stack sx={{ gap: 2 }}>
289
+ <FormControlLabel
290
+ control={
291
+ <Checkbox
292
+ checked={shouldUnregister}
293
+ onChange={(event) => setShouldUnregister(event.target.checked)}
294
+ />
295
+ }
296
+ label='slotProps.controller.shouldUnregister (bibliotekos numatytoji reikšmė - true)'
297
+ />
298
+
299
+ <AdvancedList items={items} />
300
+
301
+ <Stack direction={{ xs: 'column', md: 'row' }} sx={{ gap: 2 }}>
302
+ <Box sx={{ flex: 1, minWidth: 0 }}>
303
+ <Typography variant='body2'>Formos būsena</Typography>
304
+ <Box component='pre' sx={PANEL_SX}>
305
+ {JSON.stringify(formState, null, 2)}
306
+ </Box>
307
+ </Box>
308
+
309
+ <Box sx={{ flex: 1, minWidth: 0 }}>
310
+ <Typography variant='body2'>Submit payload (tik pasirinkta šaka)</Typography>
311
+ <Box component='pre' sx={PANEL_SX}>
312
+ {JSON.stringify(payload, null, 2)}
313
+ </Box>
314
+ </Box>
315
+ </Stack>
316
+ </Stack>
317
+ )
318
+ }
319
+
320
+ export const RadioBranchForm: Story = {
321
+ name: 'Preset: radio šakos su react-hook-form',
322
+ render: () => <RadioBranchFormStory />,
323
+ }
324
+
194
325
  const FigmaAnatomyStory = () => {
195
326
  const [selectedId, setSelectedId] = useState<string | null>(null)
196
327