@open-mercato/ui 0.10.1-develop.8129.1.149839452c → 0.10.1-develop.8143.1.bf633ea932

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.
@@ -1396,13 +1396,15 @@ function CrudFormImpl<TValues extends Record<string, unknown>>({
1396
1396
  )
1397
1397
 
1398
1398
  const navigateWithGuard = React.useCallback(
1399
- async (target: string) => {
1400
- if (!target) return
1399
+ async (target: string): Promise<boolean> => {
1400
+ if (!target) return false
1401
1401
  const allowed = await canNavigateTo(target)
1402
1402
  if (allowed) {
1403
1403
  allowNextNavigation()
1404
1404
  router.push(target)
1405
+ return true
1405
1406
  }
1407
+ return false
1406
1408
  },
1407
1409
  [allowNextNavigation, canNavigateTo, router],
1408
1410
  )
@@ -3119,6 +3121,7 @@ function CrudFormImpl<TValues extends Record<string, unknown>>({
3119
3121
  if (formReadOnly) return
3120
3122
  if (submittingRef.current) return
3121
3123
  submittingRef.current = true
3124
+ let redirectNavigationStarted = false
3122
3125
  try {
3123
3126
  setFormError(null)
3124
3127
  setErrors({})
@@ -3376,7 +3379,7 @@ function CrudFormImpl<TValues extends Record<string, unknown>>({
3376
3379
  logger.error('Error in onSubmitSuccess', { err })
3377
3380
  getTelemetryRuntime()?.reportError(err, { module: 'ui', code: 'ui.form_submit_success_failed' })
3378
3381
  }
3379
- if (successRedirect) await navigateWithGuard(successRedirect)
3382
+ if (successRedirect) redirectNavigationStarted = await navigateWithGuard(successRedirect)
3380
3383
  } catch (err: unknown) {
3381
3384
  clearSubmitNavigationBypass()
3382
3385
  try {
@@ -3448,10 +3451,14 @@ function CrudFormImpl<TValues extends Record<string, unknown>>({
3448
3451
  }
3449
3452
  setFormError(displayMessage)
3450
3453
  } finally {
3451
- setPending(false)
3454
+ // A successful submit that kicks off the success-redirect keeps the form
3455
+ // "pending"/locked until it actually unmounts, so a second submit fired
3456
+ // during router navigation (which resolves well before unmount) doesn't
3457
+ // re-run onSubmit and create a duplicate record.
3458
+ if (!redirectNavigationStarted) setPending(false)
3452
3459
  }
3453
3460
  } finally {
3454
- submittingRef.current = false
3461
+ if (!redirectNavigationStarted) submittingRef.current = false
3455
3462
  }
3456
3463
  }
3457
3464
 
@@ -3607,6 +3614,7 @@ function CrudFormImpl<TValues extends Record<string, unknown>>({
3607
3614
  entityIdForField={primaryEntityId ?? undefined}
3608
3615
  recordId={explicitResourceId ?? recordId}
3609
3616
  markRequired={widgetRequiredFieldIds.has(f.id)}
3617
+ formId={formId}
3610
3618
  />
3611
3619
  )
3612
3620
  })}
@@ -4182,6 +4190,7 @@ function CrudFormImpl<TValues extends Record<string, unknown>>({
4182
4190
  entityIdForField={primaryEntityId ?? undefined}
4183
4191
  recordId={explicitResourceId ?? recordId}
4184
4192
  markRequired={widgetRequiredFieldIds.has(f.id)}
4193
+ formId={formId}
4185
4194
  />
4186
4195
  )
4187
4196
  })}
@@ -4273,6 +4282,7 @@ function RelationSelect({
4273
4282
  }
4274
4283
  // Local-buffer text input to avoid focus loss when parent re-renders
4275
4284
  function TextInput({
4285
+ id,
4276
4286
  value,
4277
4287
  onChange,
4278
4288
  placeholder,
@@ -4283,6 +4293,7 @@ function TextInput({
4283
4293
  suggestions,
4284
4294
  inputType = 'text',
4285
4295
  }: {
4296
+ id?: string
4286
4297
  value: string
4287
4298
  onChange: (v: string) => void
4288
4299
  placeholder?: string
@@ -4339,6 +4350,7 @@ function TextInput({
4339
4350
  if (inputType === 'password') {
4340
4351
  return (
4341
4352
  <PasswordInput
4353
+ id={id}
4342
4354
  placeholder={placeholder}
4343
4355
  value={local}
4344
4356
  onChange={handleChange}
@@ -4356,6 +4368,7 @@ function TextInput({
4356
4368
  return (
4357
4369
  <>
4358
4370
  <Input
4371
+ id={id}
4359
4372
  type={inputType}
4360
4373
  placeholder={placeholder}
4361
4374
  value={local}
@@ -4383,6 +4396,7 @@ function TextInput({
4383
4396
 
4384
4397
  // Local-buffer number input to avoid focus loss when parent re-renders
4385
4398
  function NumberInput({
4399
+ id,
4386
4400
  value,
4387
4401
  onChange,
4388
4402
  placeholder,
@@ -4391,6 +4405,7 @@ function NumberInput({
4391
4405
  disabled,
4392
4406
  readOnly,
4393
4407
  }: {
4408
+ id?: string
4394
4409
  value: number | string | null | undefined
4395
4410
  onChange: (v: number | undefined) => void
4396
4411
  placeholder?: string
@@ -4471,6 +4486,7 @@ function NumberInput({
4471
4486
  // against the BROWSER locale, so it discards the separator the app locale displays
4472
4487
  // before React ever sees it (issue #5552). inputMode keeps the mobile numeric keypad.
4473
4488
  <Input
4489
+ id={id}
4474
4490
  type="text"
4475
4491
  inputMode="decimal"
4476
4492
  // A text input is eligible for autofill and spellcheck; a number field is not.
@@ -4492,6 +4508,7 @@ function NumberInput({
4492
4508
 
4493
4509
  // Local-buffer textarea to avoid form-wide re-renders while typing
4494
4510
  function TextAreaInput({
4511
+ id,
4495
4512
  value,
4496
4513
  onChange,
4497
4514
  placeholder,
@@ -4502,6 +4519,7 @@ function TextAreaInput({
4502
4519
  disabled,
4503
4520
  readOnly,
4504
4521
  }: {
4522
+ id?: string
4505
4523
  value: string
4506
4524
  onChange: (v: string) => void
4507
4525
  placeholder?: string
@@ -4538,6 +4556,7 @@ function TextAreaInput({
4538
4556
 
4539
4557
  return (
4540
4558
  <Textarea
4559
+ id={id}
4541
4560
  placeholder={placeholder}
4542
4561
  value={local}
4543
4562
  onChange={handleChange}
@@ -4667,6 +4686,7 @@ type FieldControlProps = {
4667
4686
  entityIdForField?: string
4668
4687
  recordId?: string
4669
4688
  markRequired?: boolean
4689
+ formId: string
4670
4690
  }
4671
4691
 
4672
4692
  function supportsWrapperBlurValidation(field: CrudField): boolean {
@@ -4781,6 +4801,7 @@ const FieldControl = React.memo(function FieldControlImpl({
4781
4801
  entityIdForField,
4782
4802
  recordId,
4783
4803
  markRequired,
4804
+ formId,
4784
4805
  }: FieldControlProps) {
4785
4806
  const t = useT()
4786
4807
  const fieldSetValue = React.useCallback(
@@ -4826,6 +4847,20 @@ const FieldControl = React.memo(function FieldControlImpl({
4826
4847
  ? options.find((option) => option.value === singleSelectValue)?.label
4827
4848
  : undefined
4828
4849
 
4850
+ // Fields rendering a single focusable control get a real htmlFor/id pair;
4851
+ // fields rendering a composite widget (date/time pickers, rich text, tags,
4852
+ // relation search, multi-value selects) fall back to aria-labelledby on a
4853
+ // group wrapper, per #5360's accessible-name requirement.
4854
+ const controlId = `${formId}-field-${field.id}`
4855
+ const labelId = `${controlId}-label`
4856
+ const hasSingleControl =
4857
+ field.type === 'text' ||
4858
+ field.type === 'password' ||
4859
+ field.type === 'number' ||
4860
+ field.type === 'textarea' ||
4861
+ field.type === 'combobox' ||
4862
+ (field.type === 'select' && !builtin?.multiple)
4863
+
4829
4864
  return (
4830
4865
  <div
4831
4866
  className={rootClassName}
@@ -4839,13 +4874,18 @@ const FieldControl = React.memo(function FieldControlImpl({
4839
4874
  : undefined}
4840
4875
  >
4841
4876
  {field.type !== 'checkbox' && field.label.trim().length > 0 ? (
4842
- <label className="block text-sm font-medium">
4877
+ <label
4878
+ id={labelId}
4879
+ htmlFor={hasSingleControl ? controlId : undefined}
4880
+ className="block text-sm font-medium"
4881
+ >
4843
4882
  {field.label}
4844
4883
  {field.required || markRequired ? <span className="text-status-error-text"> *</span> : null}
4845
4884
  </label>
4846
4885
  ) : null}
4847
4886
  {field.type === 'text' && (
4848
4887
  <TextInput
4888
+ id={controlId}
4849
4889
  value={value == null ? '' : String(value)}
4850
4890
  placeholder={placeholder}
4851
4891
  onChange={(next) => fieldSetValue(next)}
@@ -4858,6 +4898,7 @@ const FieldControl = React.memo(function FieldControlImpl({
4858
4898
  )}
4859
4899
  {field.type === 'password' && (
4860
4900
  <TextInput
4901
+ id={controlId}
4861
4902
  value={value == null ? '' : String(value)}
4862
4903
  placeholder={placeholder}
4863
4904
  onChange={(next) => fieldSetValue(next)}
@@ -4870,6 +4911,7 @@ const FieldControl = React.memo(function FieldControlImpl({
4870
4911
  )}
4871
4912
  {field.type === 'number' && (
4872
4913
  <NumberInput
4914
+ id={controlId}
4873
4915
  value={typeof value === 'number' || typeof value === 'string' ? value : null}
4874
4916
  placeholder={placeholder}
4875
4917
  onChange={fieldSetValue}
@@ -4880,73 +4922,84 @@ const FieldControl = React.memo(function FieldControlImpl({
4880
4922
  />
4881
4923
  )}
4882
4924
  {field.type === 'date' && (
4883
- <DatePicker
4884
- value={typeof value === 'string' && value ? parseISO(value) : value instanceof Date ? value : null}
4885
- onChange={(date) => setValue(field.id, date ? format(date, 'yyyy-MM-dd') : undefined)}
4886
- disabled={disabled}
4887
- readOnly={readOnly}
4888
- placeholder={placeholder}
4889
- minDate={builtin?.minDate}
4890
- maxDate={builtin?.maxDate}
4891
- displayFormat={builtin?.displayFormat}
4892
- closeOnSelect={builtin?.closeOnSelect}
4893
- locale={builtin?.locale}
4894
- />
4925
+ <div role="group" aria-labelledby={labelId}>
4926
+ <DatePicker
4927
+ value={typeof value === 'string' && value ? parseISO(value) : value instanceof Date ? value : null}
4928
+ onChange={(date) => setValue(field.id, date ? format(date, 'yyyy-MM-dd') : undefined)}
4929
+ disabled={disabled}
4930
+ readOnly={readOnly}
4931
+ placeholder={placeholder}
4932
+ minDate={builtin?.minDate}
4933
+ maxDate={builtin?.maxDate}
4934
+ displayFormat={builtin?.displayFormat}
4935
+ closeOnSelect={builtin?.closeOnSelect}
4936
+ locale={builtin?.locale}
4937
+ />
4938
+ </div>
4895
4939
  )}
4896
4940
  {field.type === 'datetime-local' && (
4897
- <DateTimePicker
4898
- value={typeof value === 'string' && value ? new Date(value) : value instanceof Date ? value : null}
4899
- onChange={(date) => setValue(field.id, date ? format(date, "yyyy-MM-dd'T'HH:mm") : undefined)}
4900
- disabled={disabled}
4901
- readOnly={readOnly}
4902
- placeholder={placeholder}
4903
- minuteStep={builtin?.minuteStep}
4904
- minDate={builtin?.minDate}
4905
- maxDate={builtin?.maxDate}
4906
- displayFormat={builtin?.displayFormat}
4907
- locale={builtin?.locale}
4908
- />
4941
+ <div role="group" aria-labelledby={labelId}>
4942
+ <DateTimePicker
4943
+ value={typeof value === 'string' && value ? new Date(value) : value instanceof Date ? value : null}
4944
+ onChange={(date) => setValue(field.id, date ? format(date, "yyyy-MM-dd'T'HH:mm") : undefined)}
4945
+ disabled={disabled}
4946
+ readOnly={readOnly}
4947
+ placeholder={placeholder}
4948
+ minuteStep={builtin?.minuteStep}
4949
+ minDate={builtin?.minDate}
4950
+ maxDate={builtin?.maxDate}
4951
+ displayFormat={builtin?.displayFormat}
4952
+ locale={builtin?.locale}
4953
+ />
4954
+ </div>
4909
4955
  )}
4910
4956
  {field.type === 'datepicker' && (
4911
- <DatePicker
4912
- value={typeof value === 'string' && value ? parseISO(value) : value instanceof Date ? value : null}
4913
- onChange={(date) => setValue(field.id, date ? format(date, 'yyyy-MM-dd') : undefined)}
4914
- disabled={disabled}
4915
- readOnly={readOnly}
4916
- placeholder={placeholder}
4917
- minDate={builtin?.minDate}
4918
- maxDate={builtin?.maxDate}
4919
- displayFormat={builtin?.displayFormat}
4920
- closeOnSelect={builtin?.closeOnSelect}
4921
- locale={builtin?.locale}
4922
- />
4957
+ <div role="group" aria-labelledby={labelId}>
4958
+ <DatePicker
4959
+ value={typeof value === 'string' && value ? parseISO(value) : value instanceof Date ? value : null}
4960
+ onChange={(date) => setValue(field.id, date ? format(date, 'yyyy-MM-dd') : undefined)}
4961
+ disabled={disabled}
4962
+ readOnly={readOnly}
4963
+ placeholder={placeholder}
4964
+ minDate={builtin?.minDate}
4965
+ maxDate={builtin?.maxDate}
4966
+ displayFormat={builtin?.displayFormat}
4967
+ closeOnSelect={builtin?.closeOnSelect}
4968
+ locale={builtin?.locale}
4969
+ />
4970
+ </div>
4923
4971
  )}
4924
4972
  {field.type === 'datetime' && (
4925
- <DateTimePicker
4926
- value={typeof value === 'string' && value ? new Date(value) : value instanceof Date ? value : null}
4927
- onChange={(date) => setValue(field.id, date ? date.toISOString() : undefined)}
4928
- disabled={disabled}
4929
- readOnly={readOnly}
4930
- placeholder={placeholder}
4931
- minuteStep={builtin?.minuteStep}
4932
- minDate={builtin?.minDate}
4933
- maxDate={builtin?.maxDate}
4934
- displayFormat={builtin?.displayFormat}
4935
- locale={builtin?.locale}
4936
- />
4973
+ <div role="group" aria-labelledby={labelId}>
4974
+ <DateTimePicker
4975
+ value={typeof value === 'string' && value ? new Date(value) : value instanceof Date ? value : null}
4976
+ onChange={(date) => setValue(field.id, date ? date.toISOString() : undefined)}
4977
+ disabled={disabled}
4978
+ readOnly={readOnly}
4979
+ placeholder={placeholder}
4980
+ minuteStep={builtin?.minuteStep}
4981
+ minDate={builtin?.minDate}
4982
+ maxDate={builtin?.maxDate}
4983
+ displayFormat={builtin?.displayFormat}
4984
+ locale={builtin?.locale}
4985
+ />
4986
+ </div>
4937
4987
  )}
4938
4988
  {field.type === 'time' && (
4939
- <TimePicker
4940
- value={typeof value === 'string' ? value : null}
4941
- onChange={(time) => setValue(field.id, time ?? undefined)}
4942
- disabled={disabled}
4943
- readOnly={readOnly}
4944
- placeholder={placeholder}
4945
- minuteStep={builtin?.minuteStep}
4946
- />
4989
+ <div role="group" aria-labelledby={labelId}>
4990
+ <TimePicker
4991
+ value={typeof value === 'string' ? value : null}
4992
+ onChange={(time) => setValue(field.id, time ?? undefined)}
4993
+ disabled={disabled}
4994
+ readOnly={readOnly}
4995
+ placeholder={placeholder}
4996
+ minuteStep={builtin?.minuteStep}
4997
+ />
4998
+ </div>
4947
4999
  )}
4948
5000
  {field.type === 'textarea' && (
4949
5001
  <TextAreaInput
5002
+ id={controlId}
4950
5003
  value={value == null ? '' : String(value)}
4951
5004
  placeholder={placeholder}
4952
5005
  onChange={(next) => fieldSetValue(next)}
@@ -4959,28 +5012,37 @@ const FieldControl = React.memo(function FieldControlImpl({
4959
5012
  />
4960
5013
  )}
4961
5014
  {field.type === 'richtext' && builtin?.editor === 'simple' && (
4962
- <SimpleMarkdownEditor value={String(value ?? '')} onChange={fieldSetValue} />
5015
+ <div role="group" aria-labelledby={labelId}>
5016
+ <SimpleMarkdownEditor value={String(value ?? '')} onChange={fieldSetValue} />
5017
+ </div>
4963
5018
  )}
4964
5019
  {field.type === 'richtext' && builtin?.editor === 'html' && (
4965
- <HtmlRichEditorField value={String(value ?? '')} onChange={fieldSetValue} />
5020
+ <div role="group" aria-labelledby={labelId}>
5021
+ <HtmlRichEditorField value={String(value ?? '')} onChange={fieldSetValue} />
5022
+ </div>
4966
5023
  )}
4967
5024
  {field.type === 'richtext' && (!builtin?.editor || (builtin.editor !== 'simple' && builtin.editor !== 'html')) && (
4968
- <MarkdownEditor value={String(value ?? '')} onChange={fieldSetValue} />
5025
+ <div role="group" aria-labelledby={labelId}>
5026
+ <MarkdownEditor value={String(value ?? '')} onChange={fieldSetValue} />
5027
+ </div>
4969
5028
  )}
4970
5029
  {field.type === 'tags' && (
4971
- <TagsInput
4972
- value={Array.isArray(value) ? value.filter((item): item is string => typeof item === 'string') : []}
4973
- onChange={(next) => fieldSetValue(next)}
4974
- placeholder={placeholder}
4975
- autoFocus={autoFocusField}
4976
- suppressInitialSuggestionsOnFocus={autoFocusField}
4977
- suggestions={options.map((opt) => ({ value: opt.value, label: opt.label }))}
4978
- loadSuggestions={loadFieldSuggestions}
4979
- disabled={disabled}
4980
- />
5030
+ <div role="group" aria-labelledby={labelId}>
5031
+ <TagsInput
5032
+ value={Array.isArray(value) ? value.filter((item): item is string => typeof item === 'string') : []}
5033
+ onChange={(next) => fieldSetValue(next)}
5034
+ placeholder={placeholder}
5035
+ autoFocus={autoFocusField}
5036
+ suppressInitialSuggestionsOnFocus={autoFocusField}
5037
+ suggestions={options.map((opt) => ({ value: opt.value, label: opt.label }))}
5038
+ loadSuggestions={loadFieldSuggestions}
5039
+ disabled={disabled}
5040
+ />
5041
+ </div>
4981
5042
  )}
4982
5043
  {field.type === 'combobox' && (
4983
5044
  <ComboboxInput
5045
+ id={controlId}
4984
5046
  value={typeof value === 'string' ? value : String(value ?? '')}
4985
5047
  onChange={(next) => fieldSetValue(next)}
4986
5048
  placeholder={placeholder}
@@ -5003,8 +5065,9 @@ const FieldControl = React.memo(function FieldControlImpl({
5003
5065
  />
5004
5066
  )}
5005
5067
  {field.type === 'checkbox' && (
5006
- <label className="inline-flex items-center gap-2 cursor-pointer">
5068
+ <label htmlFor={controlId} className="inline-flex items-center gap-2 cursor-pointer">
5007
5069
  <Checkbox
5070
+ id={controlId}
5008
5071
  checked={value === true}
5009
5072
  onCheckedChange={(next) => setValue(field.id, next === true)}
5010
5073
  data-crud-focus-target=""
@@ -5035,7 +5098,7 @@ const FieldControl = React.memo(function FieldControlImpl({
5035
5098
  }}
5036
5099
  disabled={disabled || readOnly}
5037
5100
  >
5038
- <SelectTrigger data-crud-focus-target="">
5101
+ <SelectTrigger id={controlId} data-crud-focus-target="">
5039
5102
  <SelectValue placeholder={t('ui.forms.select.emptyOption', '—')}>
5040
5103
  {singleSelectLabel}
5041
5104
  </SelectValue>
@@ -5057,16 +5120,18 @@ const FieldControl = React.memo(function FieldControlImpl({
5057
5120
  </Select>
5058
5121
  )}
5059
5122
  {field.type === 'select' && builtin?.multiple && builtin.listbox === true && (
5060
- <ListboxMultiSelect
5061
- options={options}
5062
- placeholder={placeholder}
5063
- value={Array.isArray(value) ? value.filter((item): item is string => typeof item === 'string') : []}
5064
- onChange={(vals) => setValue(field.id, vals)}
5065
- autoFocus={autoFocusField}
5066
- />
5123
+ <div role="group" aria-labelledby={labelId}>
5124
+ <ListboxMultiSelect
5125
+ options={options}
5126
+ placeholder={placeholder}
5127
+ value={Array.isArray(value) ? value.filter((item): item is string => typeof item === 'string') : []}
5128
+ onChange={(vals) => setValue(field.id, vals)}
5129
+ autoFocus={autoFocusField}
5130
+ />
5131
+ </div>
5067
5132
  )}
5068
5133
  {field.type === 'select' && builtin?.multiple && builtin.listbox !== true && (
5069
- <div className="flex flex-wrap gap-3">
5134
+ <div className="flex flex-wrap gap-3" role="group" aria-labelledby={labelId}>
5070
5135
  {options.map((opt) => {
5071
5136
  const arr = Array.isArray(value)
5072
5137
  ? value.filter((item): item is string => typeof item === 'string')
@@ -5094,22 +5159,24 @@ const FieldControl = React.memo(function FieldControlImpl({
5094
5159
  </div>
5095
5160
  )}
5096
5161
  {field.type === 'relation' && (
5097
- <RelationSelect
5098
- options={options}
5099
- placeholder={placeholder}
5100
- value={
5101
- Array.isArray(value)
5102
- ? String(value[0] ?? '')
5103
- : value == null
5104
- ? ''
5105
- : String(value)
5106
- }
5107
- onChange={(selected) => setValue(field.id, selected)}
5108
- autoFocus={autoFocusField}
5109
- />
5162
+ <div role="group" aria-labelledby={labelId}>
5163
+ <RelationSelect
5164
+ options={options}
5165
+ placeholder={placeholder}
5166
+ value={
5167
+ Array.isArray(value)
5168
+ ? String(value[0] ?? '')
5169
+ : value == null
5170
+ ? ''
5171
+ : String(value)
5172
+ }
5173
+ onChange={(selected) => setValue(field.id, selected)}
5174
+ autoFocus={autoFocusField}
5175
+ />
5176
+ </div>
5110
5177
  )}
5111
5178
  {field.type === 'custom' && (
5112
- <>
5179
+ <div role="group" aria-labelledby={labelId}>
5113
5180
  {field.component({
5114
5181
  id: field.id,
5115
5182
  value,
@@ -5122,7 +5189,7 @@ const FieldControl = React.memo(function FieldControlImpl({
5122
5189
  autoFocus,
5123
5190
  disabled,
5124
5191
  })}
5125
- </>
5192
+ </div>
5126
5193
  )}
5127
5194
  {field.description ? (
5128
5195
  <div className="flex items-start gap-2 text-xs text-muted-foreground">
@@ -5154,6 +5221,7 @@ const FieldControl = React.memo(function FieldControlImpl({
5154
5221
  prev.wrapperClassName === next.wrapperClassName &&
5155
5222
  prev.entityIdForField === next.entityIdForField &&
5156
5223
  prev.recordId === next.recordId &&
5224
+ prev.formId === next.formId &&
5157
5225
  (prev.field.type !== 'custom' ||
5158
5226
  (prev.values === next.values &&
5159
5227
  prev.field.component === (next.field as CrudCustomField).component &&
@@ -0,0 +1,108 @@
1
+ /** @jest-environment jsdom */
2
+ jest.setTimeout(15000)
3
+
4
+ jest.mock('next/navigation', () => ({
5
+ useRouter: () => ({ push: () => {} }),
6
+ usePathname: () => '/',
7
+ useSearchParams: () => new URLSearchParams(),
8
+ }))
9
+ jest.mock('remark-gfm', () => ({ __esModule: true, default: {} }))
10
+ jest.mock('../injection/InjectionSpot', () => ({
11
+ __esModule: true,
12
+ InjectionSpot: () => null,
13
+ useInjectionWidgets: () => ({ widgets: [], loading: false, error: null }),
14
+ useInjectionSpotEvents: () => ({ triggerEvent: async () => ({ ok: true }) }),
15
+ }))
16
+ jest.mock('../injection/useInjectionDataWidgets', () => ({
17
+ __esModule: true,
18
+ useInjectionDataWidgets: () => ({ widgets: [], isLoading: false, error: null }),
19
+ }))
20
+
21
+ import * as React from 'react'
22
+ import { fireEvent } from '@testing-library/react'
23
+ import { renderWithProviders } from '@open-mercato/shared/lib/testing/renderWithProviders'
24
+ import { CrudForm, type CrudField } from '../CrudForm'
25
+
26
+ const dict = {
27
+ 'ui.forms.actions.save': 'Save',
28
+ 'ui.forms.select.emptyOption': '—',
29
+ }
30
+
31
+ describe('CrudForm accessible field labels (#5360)', () => {
32
+ it('associates a text field label with its input via htmlFor/id', () => {
33
+ const fields: CrudField[] = [{ id: 'displayName', label: 'Display name', type: 'text' }]
34
+ const { getByLabelText } = renderWithProviders(
35
+ <CrudForm title="Form" fields={fields} initialValues={{ displayName: '' }} onSubmit={() => {}} />,
36
+ { dict },
37
+ )
38
+
39
+ const input = getByLabelText('Display name') as HTMLInputElement
40
+ expect(input.tagName).toBe('INPUT')
41
+ expect(input.id).toBeTruthy()
42
+
43
+ const label = document.querySelector(`label[for="${input.id}"]`) as HTMLLabelElement
44
+ expect(label).not.toBeNull()
45
+ fireEvent.click(label)
46
+ expect(document.activeElement).toBe(input)
47
+ })
48
+
49
+ it('associates a select field label with its trigger via htmlFor/id', () => {
50
+ const fields: CrudField[] = [
51
+ {
52
+ id: 'status',
53
+ label: 'Status',
54
+ type: 'select',
55
+ options: [{ value: 'open', label: 'Open' }, { value: 'closed', label: 'Closed' }],
56
+ },
57
+ ]
58
+ const { getByLabelText } = renderWithProviders(
59
+ <CrudForm title="Form" fields={fields} initialValues={{ status: 'open' }} onSubmit={() => {}} />,
60
+ { dict },
61
+ )
62
+
63
+ const trigger = getByLabelText('Status')
64
+ expect(trigger).not.toBeNull()
65
+ })
66
+
67
+ it('associates a checkbox field label with its control via htmlFor/id, and clicking the label toggles it', () => {
68
+ const onSubmit = jest.fn()
69
+ const fields: CrudField[] = [{ id: 'active', label: 'Active', type: 'checkbox' }]
70
+ const { getByLabelText } = renderWithProviders(
71
+ <CrudForm title="Form" fields={fields} initialValues={{ active: false }} onSubmit={onSubmit} />,
72
+ { dict },
73
+ )
74
+
75
+ const checkbox = getByLabelText('Active') as HTMLElement
76
+ expect(checkbox).not.toBeNull()
77
+ expect(checkbox).toHaveAttribute('aria-checked', 'false')
78
+
79
+ const label = document.querySelector(`label[for="${checkbox.id}"]`) as HTMLLabelElement
80
+ expect(label).not.toBeNull()
81
+ fireEvent.click(label)
82
+ expect(checkbox).toHaveAttribute('aria-checked', 'true')
83
+ })
84
+
85
+ it('gives a custom field group an accessible name derived from its label (aria-labelledby fallback)', () => {
86
+ const fields: CrudField[] = [
87
+ {
88
+ id: 'notes',
89
+ label: 'Notes',
90
+ type: 'custom',
91
+ component: ({ value, setValue }) => (
92
+ <textarea
93
+ value={typeof value === 'string' ? value : ''}
94
+ onChange={(event) => setValue(event.target.value)}
95
+ />
96
+ ),
97
+ },
98
+ ]
99
+ const { getByRole } = renderWithProviders(
100
+ <CrudForm title="Form" fields={fields} initialValues={{ notes: '' }} onSubmit={() => {}} />,
101
+ { dict },
102
+ )
103
+
104
+ const group = getByRole('group', { name: 'Notes' })
105
+ expect(group).not.toBeNull()
106
+ expect(group.querySelector('textarea')).not.toBeNull()
107
+ })
108
+ })