@greenharness/greenrack-ui 7.2.0 → 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/dist/components/Checkbox/Checkbox.stories.d.ts.map +1 -1
- package/dist/components/Checkbox/styles.css.d.ts +1 -0
- package/dist/components/Checkbox/styles.css.d.ts.map +1 -1
- package/dist/components/Field/Field.d.ts.map +1 -1
- package/dist/components/Field/Field.stories.d.ts +1 -0
- package/dist/components/Field/Field.stories.d.ts.map +1 -1
- package/dist/components/Field/FieldContext.d.ts +18 -0
- package/dist/components/Field/FieldContext.d.ts.map +1 -1
- package/dist/components/Input/Input.d.ts.map +1 -1
- package/dist/components/RadioGroup/RadioGroup.stories.d.ts.map +1 -1
- package/dist/components/RadioGroup/styles.css.d.ts +1 -0
- package/dist/components/RadioGroup/styles.css.d.ts.map +1 -1
- package/dist/components/Switch/Switch.stories.d.ts.map +1 -1
- package/dist/components/Switch/styles.css.d.ts +1 -0
- package/dist/components/Switch/styles.css.d.ts.map +1 -1
- package/dist/components/Textarea/Textarea.d.ts.map +1 -1
- package/dist/index.js +3403 -3360
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
- package/src/components/Checkbox/Checkbox.stories.tsx +2 -0
- package/src/components/Checkbox/styles.css.ts +4 -0
- package/src/components/Field/Field.meta.ts +1 -1
- package/src/components/Field/Field.stories.tsx +27 -0
- package/src/components/Field/Field.tsx +14 -6
- package/src/components/Field/FieldContext.ts +30 -0
- package/src/components/Input/Input.meta.ts +1 -1
- package/src/components/Input/Input.tsx +11 -2
- package/src/components/RadioGroup/RadioGroup.stories.tsx +4 -0
- package/src/components/RadioGroup/styles.css.ts +4 -0
- package/src/components/Switch/Switch.stories.tsx +2 -0
- package/src/components/Switch/styles.css.ts +4 -0
- package/src/components/Textarea/Textarea.tsx +11 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@greenharness/greenrack-ui",
|
|
3
|
-
"version": "7.
|
|
3
|
+
"version": "7.3.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -91,14 +91,14 @@
|
|
|
91
91
|
},
|
|
92
92
|
"dependencies": {
|
|
93
93
|
"@tanstack/react-virtual": "^3.14.13",
|
|
94
|
+
"@types/qrcode": "^1.5.6",
|
|
94
95
|
"qrcode": "^1.5.4",
|
|
95
|
-
"@greenharness/greenrack-tokens": "^7.
|
|
96
|
+
"@greenharness/greenrack-tokens": "^7.3.0"
|
|
96
97
|
},
|
|
97
98
|
"devDependencies": {
|
|
98
99
|
"@babel/preset-typescript": "^7.28.0",
|
|
99
100
|
"@hookform/resolvers": "^5.0.0",
|
|
100
101
|
"@phosphor-icons/core": "2.1.1",
|
|
101
|
-
"@types/qrcode": "^1.5.6",
|
|
102
102
|
"@types/react": "^19.0.0",
|
|
103
103
|
"@types/react-dom": "^19.0.0",
|
|
104
104
|
"@vitejs/plugin-react": "^4.0.0",
|
|
@@ -26,6 +26,8 @@ export const Default: Story = {
|
|
|
26
26
|
const checkbox = canvas.getByRole('checkbox')
|
|
27
27
|
await expect(checkbox).toBeInTheDocument()
|
|
28
28
|
await expect(checkbox).toBeEnabled()
|
|
29
|
+
// The hidden input is placed against its own label, never a page ancestor.
|
|
30
|
+
await expect(checkbox.offsetParent).toBe(checkbox.closest('label'))
|
|
29
31
|
|
|
30
32
|
await userEvent.click(checkbox)
|
|
31
33
|
await expect(args.onChange).toHaveBeenCalledTimes(1)
|
|
@@ -8,6 +8,10 @@ import {borders} from '@greenharness/greenrack-tokens/tokens/borders.css'
|
|
|
8
8
|
|
|
9
9
|
export const styles = css.create({
|
|
10
10
|
root: {
|
|
11
|
+
// The containing block of the visually hidden input (position: absolute):
|
|
12
|
+
// without it the input escapes to the nearest positioned ancestor, outside
|
|
13
|
+
// any scroller between, and lengthens the page.
|
|
14
|
+
position: 'relative',
|
|
11
15
|
display: 'inline-flex',
|
|
12
16
|
alignItems: 'center',
|
|
13
17
|
gap: spacing.sm,
|
|
@@ -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
|
|
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
|
|
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={
|
|
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']}
|
|
@@ -28,6 +28,10 @@ export const Default: Story = {
|
|
|
28
28
|
await expect(radios.length).toBe(3)
|
|
29
29
|
await expect(radios[0]).toBeChecked()
|
|
30
30
|
await expect(radios[1]).not.toBeChecked()
|
|
31
|
+
// Each hidden input is placed against its own label, never a page ancestor.
|
|
32
|
+
for (const radio of radios) {
|
|
33
|
+
await expect(radio.offsetParent).toBe(radio.closest('label'))
|
|
34
|
+
}
|
|
31
35
|
},
|
|
32
36
|
}
|
|
33
37
|
|
|
@@ -16,6 +16,10 @@ export const styles = css.create({
|
|
|
16
16
|
flexDirection: 'row',
|
|
17
17
|
},
|
|
18
18
|
item: {
|
|
19
|
+
// The containing block of the visually hidden input (position: absolute):
|
|
20
|
+
// without it the input escapes to the nearest positioned ancestor, outside
|
|
21
|
+
// any scroller between, and lengthens the page.
|
|
22
|
+
position: 'relative',
|
|
19
23
|
display: 'inline-flex',
|
|
20
24
|
alignItems: 'center',
|
|
21
25
|
gap: spacing.sm,
|
|
@@ -26,6 +26,8 @@ export const Default: Story = {
|
|
|
26
26
|
const switchEl = canvas.getByRole('switch')
|
|
27
27
|
await expect(switchEl).toBeInTheDocument()
|
|
28
28
|
await expect(switchEl).not.toBeChecked()
|
|
29
|
+
// The hidden input is placed against its own label, never a page ancestor.
|
|
30
|
+
await expect(switchEl.offsetParent).toBe(switchEl.closest('label'))
|
|
29
31
|
|
|
30
32
|
await userEvent.click(switchEl)
|
|
31
33
|
await expect(args.onCheckedChange).toHaveBeenCalledWith(true)
|
|
@@ -8,6 +8,10 @@ import {borders} from '@greenharness/greenrack-tokens/tokens/borders.css'
|
|
|
8
8
|
|
|
9
9
|
export const styles = css.create({
|
|
10
10
|
root: {
|
|
11
|
+
// The containing block of the visually hidden input (position: absolute):
|
|
12
|
+
// without it the input escapes to the nearest positioned ancestor, outside
|
|
13
|
+
// any scroller between, and lengthens the page.
|
|
14
|
+
position: 'relative',
|
|
11
15
|
display: 'inline-flex',
|
|
12
16
|
alignItems: 'center',
|
|
13
17
|
gap: spacing.sm,
|
|
@@ -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={
|
|
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']}
|