@greenharness/greenrack-ui 7.2.1 → 7.3.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@greenharness/greenrack-ui",
3
- "version": "7.2.1",
3
+ "version": "7.3.0",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -93,7 +93,7 @@
93
93
  "@tanstack/react-virtual": "^3.14.13",
94
94
  "@types/qrcode": "^1.5.6",
95
95
  "qrcode": "^1.5.4",
96
- "@greenharness/greenrack-tokens": "^7.2.1"
96
+ "@greenharness/greenrack-tokens": "^7.3.0"
97
97
  },
98
98
  "devDependencies": {
99
99
  "@babel/preset-typescript": "^7.28.0",
@@ -2,7 +2,7 @@ import type {ComponentMeta} from '../component-meta'
2
2
 
3
3
  export const meta: ComponentMeta = {
4
4
  description:
5
- 'Compound form field with label, description, and error display. Inside Form, auto-binds to react-hook-form via name prop. Works standalone with manual invalid prop.',
5
+ 'Compound form field with label, description, and error display. Inside Form, auto-binds to react-hook-form via name prop. Works standalone with manual invalid prop. A native validation message from an Input or Textarea (required, type, pattern) is shown by Field.Error; explicit Error children and form errors take precedence.',
6
6
  whenToUse: [
7
7
  'Any form input that needs a label and/or error message',
8
8
  'Standalone labeled input (without Form) with manual error display',
@@ -3,6 +3,7 @@ import {expect} from 'storybook/test'
3
3
  import {css, html} from 'react-strict-dom'
4
4
  import {Field} from './Field'
5
5
  import {Input} from '../Input/Input'
6
+ import {Button} from '../Button/Button'
6
7
  import {ToggleGroup} from '../ToggleGroup/ToggleGroup'
7
8
  import {Toggle} from '../Toggle/Toggle'
8
9
  import {CheckboxGroup} from '../CheckboxGroup/CheckboxGroup'
@@ -253,3 +254,29 @@ export const FieldsetLegendNamesTheGroup: Story = {
253
254
  await expect(canvas.getByRole('group', {name: 'Notifications'})).toBeInTheDocument()
254
255
  },
255
256
  }
257
+
258
+ export const NativeValidation: Story = {
259
+ render: () => (
260
+ <html.form>
261
+ <Field.Root>
262
+ <Field.Label>Website</Field.Label>
263
+ <Input type="url" required placeholder="https://example.com" />
264
+ <Field.Error />
265
+ </Field.Root>
266
+ <Button type="submit">Submit</Button>
267
+ </html.form>
268
+ ),
269
+ play: async ({canvas, userEvent}) => {
270
+ const input = canvas.getByPlaceholderText('https://example.com') as HTMLInputElement
271
+ await userEvent.click(canvas.getByRole('button', {name: 'Submit'}))
272
+ const alert = await canvas.findByRole('alert')
273
+ await expect(alert).toBeInTheDocument()
274
+ await expect(alert.textContent).not.toBe('')
275
+ await expect(alert.textContent).toBe(input.validationMessage)
276
+ await expect(input).toHaveAttribute('aria-invalid', 'true')
277
+ await expect(input.getAttribute('aria-describedby')).toContain(alert.id)
278
+
279
+ await userEvent.type(input, 'https://example.com')
280
+ await expect(canvas.queryByRole('alert')).not.toBeInTheDocument()
281
+ },
282
+ }
@@ -1,9 +1,9 @@
1
- import {type ReactNode, Children, isValidElement, useId, useMemo} from 'react'
1
+ import {type ReactNode, Children, isValidElement, useId, useMemo, useState} from 'react'
2
2
  import {html} from 'react-strict-dom'
3
3
  import {useFormContext as useRHFFormContext, useController} from 'react-hook-form'
4
4
  import {FieldContext, useFieldContext} from './FieldContext'
5
5
  import {useFormContext} from '../Form/FormContext'
6
- import type {LabelPosition, NecessityIndicator} from '../Form/FormContext'
6
+ import type {LabelPosition} from '../Form/FormContext'
7
7
  import {styles} from './styles.css'
8
8
 
9
9
  // --- Root ---
@@ -64,7 +64,8 @@ function ControlledRoot({
64
64
  const id = useId()
65
65
  const {control} = useRHFFormContext()
66
66
  const {field, fieldState} = useController({control, name})
67
- const invalid = invalidProp || !!fieldState.error
67
+ const [nativeError, setNativeError] = useState<string | undefined>()
68
+ const invalid = invalidProp || !!fieldState.error || nativeError !== undefined
68
69
 
69
70
  const effectiveDisabled = disabled ?? formCtx.disabled
70
71
  const effectiveLabelPosition = labelPosition ?? formCtx.labelPosition
@@ -90,6 +91,8 @@ function ControlledRoot({
90
91
  name: field.name,
91
92
  },
92
93
  errorMessage: fieldState.error?.message,
94
+ nativeError,
95
+ setNativeError,
93
96
  }),
94
97
  [
95
98
  id,
@@ -105,6 +108,7 @@ function ControlledRoot({
105
108
  field.ref,
106
109
  field.name,
107
110
  fieldState.error,
111
+ nativeError,
108
112
  ],
109
113
  )
110
114
 
@@ -118,7 +122,7 @@ function ControlledRoot({
118
122
  // Current behavior, no RHF dependency
119
123
  function StaticRoot({
120
124
  group,
121
- invalid = false,
125
+ invalid: invalidProp = false,
122
126
  required,
123
127
  disabled,
124
128
  labelPosition = 'top',
@@ -132,6 +136,8 @@ function StaticRoot({
132
136
  children: ReactNode
133
137
  }) {
134
138
  const id = useId()
139
+ const [nativeError, setNativeError] = useState<string | undefined>()
140
+ const invalid = invalidProp || nativeError !== undefined
135
141
  const ctx = useMemo(
136
142
  () => ({
137
143
  controlId: `${id}-control`,
@@ -143,8 +149,10 @@ function StaticRoot({
143
149
  required,
144
150
  disabled,
145
151
  labelPosition,
152
+ nativeError,
153
+ setNativeError,
146
154
  }),
147
- [id, group, invalid, required, disabled, labelPosition],
155
+ [id, group, invalid, required, disabled, labelPosition, nativeError],
148
156
  )
149
157
 
150
158
  return (
@@ -253,7 +261,7 @@ interface ErrorProps {
253
261
 
254
262
  function Error({children}: ErrorProps) {
255
263
  const ctx = useFieldContext()
256
- const content = children ?? ctx?.errorMessage
264
+ const content = children ?? ctx?.errorMessage ?? ctx?.nativeError
257
265
  if (!ctx?.invalid && !content) return null
258
266
 
259
267
  return (
@@ -24,6 +24,9 @@ interface FieldContextValue {
24
24
  name: string
25
25
  }
26
26
  errorMessage?: string
27
+ /** The control's own `validationMessage`, reported by the DOM `invalid` event. */
28
+ nativeError?: string
29
+ setNativeError?: (message: string | undefined) => void
27
30
  }
28
31
 
29
32
  export type {FieldContextValue}
@@ -34,6 +37,33 @@ export function useFieldContext() {
34
37
  return useContext(FieldContext)
35
38
  }
36
39
 
40
+ interface NativeValidityTarget {
41
+ validationMessage: string
42
+ validity: {valid: boolean}
43
+ }
44
+
45
+ /** Wires a text control to its Field's native validation message: the DOM
46
+ * `invalid` event sets it, and a later change clears or refreshes it. The
47
+ * browser's own bubble is left alone. Outside a Field both handlers are
48
+ * undefined. Structural types: the native build (no DOM lib) types this too. */
49
+ export function useNativeValidation() {
50
+ const ctx = useFieldContext()
51
+ const setNativeError = ctx?.setNativeError
52
+ if (!setNativeError) return {onInvalid: undefined, afterChange: undefined}
53
+ const hasNativeError = ctx.nativeError !== undefined
54
+ return {
55
+ onInvalid: (event: {currentTarget: object}) =>
56
+ setNativeError((event.currentTarget as NativeValidityTarget).validationMessage),
57
+ afterChange: hasNativeError
58
+ ? (event: {currentTarget: object}) => {
59
+ // Native DOM typings lack the validity members on the element type.
60
+ const target = event.currentTarget as NativeValidityTarget
61
+ setNativeError(target.validity.valid ? undefined : target.validationMessage)
62
+ }
63
+ : undefined,
64
+ }
65
+ }
66
+
37
67
  interface GroupLabelling {
38
68
  'aria-label'?: string
39
69
  'aria-labelledby'?: string
@@ -2,7 +2,7 @@ import type {ComponentMeta} from '../component-meta'
2
2
 
3
3
  export const meta: ComponentMeta = {
4
4
  description:
5
- 'Text input with automatic Field/Form integration. Variant auto-switches to error when inside an invalid Field. variant="ghost" reads as text until hovered (hairline border) or focused (accent border and ring): only for inline edit where a visible Field.Label names it; it shows the error state when the Field is invalid.',
5
+ 'Text input with automatic Field/Form integration. Variant auto-switches to error when inside an invalid Field. variant="ghost" reads as text until hovered (hairline border) or focused (accent border and ring): only for inline edit where a visible Field.Label names it; it shows the error state when the Field is invalid. Inside a Field, the native browser validation message (required, type=url, pattern) is shown by Field.Error after a failed submit.',
6
6
  whenToUse: [
7
7
  'Single-line text entry (text, email, password, url, tel, number, search)',
8
8
  'Inline edit of a labelled value in place (a ticket title) — variant="ghost"',
@@ -1,5 +1,5 @@
1
1
  import {html} from 'react-strict-dom'
2
- import {useFieldContext} from '../Field/FieldContext'
2
+ import {useFieldContext, useNativeValidation} from '../Field/FieldContext'
3
3
  import {useInputGroupContext} from '../InputGroup/InputGroupContext'
4
4
  import {styles} from './styles.css'
5
5
 
@@ -105,6 +105,7 @@ export function Input({
105
105
  ref,
106
106
  }: InputProps) {
107
107
  const ctx = useFieldContext()
108
+ const {onInvalid, afterChange} = useNativeValidation()
108
109
  const groupCtx = useInputGroupContext()
109
110
 
110
111
  const fieldCtx = ctx?.field
@@ -136,6 +137,7 @@ export function Input({
136
137
  if (inputMode !== undefined) extraProps.inputMode = inputMode
137
138
  if (list !== undefined) extraProps.list = list
138
139
  if (ariaLabel !== undefined) extraProps['aria-label'] = ariaLabel
140
+ if (onInvalid) extraProps.onInvalid = onInvalid
139
141
 
140
142
  return (
141
143
  <html.input
@@ -153,7 +155,14 @@ export function Input({
153
155
  disabled={effectiveDisabled}
154
156
  aria-describedby={describedBy || undefined}
155
157
  aria-invalid={invalid || undefined}
156
- onChange={effectiveOnChange as StrictInputProps['onChange']}
158
+ onChange={
159
+ (afterChange
160
+ ? (e: React.ChangeEvent<HTMLInputElement>) => {
161
+ ;(effectiveOnChange as ((e: unknown) => void) | undefined)?.(e)
162
+ afterChange(e)
163
+ }
164
+ : effectiveOnChange) as StrictInputProps['onChange']
165
+ }
157
166
  onBlur={effectiveOnBlur as StrictInputProps['onBlur']}
158
167
  onFocus={onFocus as StrictInputProps['onFocus']}
159
168
  onKeyDown={onKeyDown as StrictInputProps['onKeyDown']}
@@ -1,7 +1,7 @@
1
1
  import {useCallback, useRef} from 'react'
2
2
  import {html} from 'react-strict-dom'
3
3
  import {isNative} from '../../platform'
4
- import {useFieldContext} from '../Field/FieldContext'
4
+ import {useFieldContext, useNativeValidation} from '../Field/FieldContext'
5
5
  import type {ListboxAnchorProps} from '../Listbox/Listbox'
6
6
  import {useAutoGrowFallback} from './useAutoGrowFallback'
7
7
  import {useFileDrop} from './useFileDrop'
@@ -84,6 +84,7 @@ export function Textarea({
84
84
  ref,
85
85
  }: TextareaProps) {
86
86
  const ctx = useFieldContext()
87
+ const {onInvalid, afterChange} = useNativeValidation()
87
88
 
88
89
  const fieldCtx = ctx?.field
89
90
  const effectiveName = name ?? fieldCtx?.name
@@ -96,6 +97,7 @@ export function Textarea({
96
97
 
97
98
  const extraProps: Record<string, unknown> = {}
98
99
  if (autoFocus) extraProps.autoFocus = true
100
+ if (onInvalid) extraProps.onInvalid = onInvalid
99
101
  if (onSelectionChange) {
100
102
  // Structural event types: the native build (no DOM lib) types this file too.
101
103
  if (isNative) {
@@ -146,7 +148,14 @@ export function Textarea({
146
148
  ctx ? `${ctx.descriptionId} ${ctx.invalid ? ctx.errorId : ''}`.trim() : undefined
147
149
  }
148
150
  aria-invalid={ctx?.invalid || variant === 'error' || undefined}
149
- onChange={effectiveOnChange as StrictTextareaProps['onChange']}
151
+ onChange={
152
+ (afterChange
153
+ ? (e: React.ChangeEvent<HTMLTextAreaElement>) => {
154
+ ;(effectiveOnChange as ((e: unknown) => void) | undefined)?.(e)
155
+ afterChange(e)
156
+ }
157
+ : effectiveOnChange) as StrictTextareaProps['onChange']
158
+ }
150
159
  onBlur={effectiveOnBlur as StrictTextareaProps['onBlur']}
151
160
  onFocus={onFocus as StrictTextareaProps['onFocus']}
152
161
  onKeyDown={onKeyDown as StrictTextareaProps['onKeyDown']}