-
Notifications
You must be signed in to change notification settings - Fork 6
fix(Field): id generation #824
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@cube-dev/ui-kit": patch | ||
| --- | ||
|
|
||
| Generate id even for input components that are not connected to a form. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@cube-dev/ui-kit": patch | ||
| --- | ||
|
|
||
| Prevent form prop from leaking to the DOM. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -142,6 +142,7 @@ function Checkbox( | |
| labelPosition, | ||
| inputStyles, | ||
| isHidden, | ||
| form, | ||
| ...otherProps | ||
| } = props; | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| # Form System Essential Knowledge | ||
|
|
||
| ## ID Generation and Field Connection | ||
|
|
||
| ### useFieldProps Behavior | ||
|
|
||
| `useFieldProps` handles ID generation and label-input connection automatically. It has **two distinct modes**: | ||
|
|
||
| #### 1. Form-Connected Fields (with `name` prop) | ||
| - Calls `useField` for full form integration | ||
| - Generates unique IDs using the `createId` mechanism based on field name | ||
| - Manages form state, validation, onChange/onBlur handlers | ||
| - Example: `<TextInput name="email" />` inside a `<Form>` | ||
|
|
||
| #### 2. Standalone Fields (no `name` prop) | ||
| - **Does NOT call `useField`** to avoid interference with controlled component behavior | ||
| - Uses React's `useId()` to generate IDs directly | ||
| - Preserves original props including `value` and `onChange` handlers | ||
| - Example: `<RadioGroup value={state} onChange={setState} />` without `name` | ||
|
|
||
| ### Critical Rule: Don't Call useField for Standalone Fields | ||
|
|
||
| **Why**: Calling `useField` for standalone fields creates state management and side effects that interfere with controlled component behavior. This breaks components like RadioGroup in controlled mode. | ||
|
|
||
| ### Label-Input Connection | ||
|
|
||
| `useFieldProps` automatically sets `labelProps.for` to match the field's ID when an ID exists. This ensures proper accessibility and label clicking behavior. | ||
|
|
||
| **Components should NOT manually set**: | ||
| - `labelProps.for` | ||
| - Generate their own IDs using `useId()` | ||
|
|
||
| This is handled centrally by `useFieldProps`. | ||
|
|
||
| ## Field Component Pattern | ||
|
|
||
| ```tsx | ||
| function MyField(props) { | ||
| props = useProviderProps(props); | ||
| props = useFormProps(props); | ||
| props = useFieldProps(props, { defaultValidationTrigger: 'onBlur' }); | ||
|
|
||
| // Now props.id is set (if needed) and props.labelProps.for matches it | ||
| // Just use props.id directly, don't generate your own | ||
|
|
||
| return <input id={props.id} {...otherProps} />; | ||
| } | ||
| ``` | ||
|
|
||
| ## ID Generation Examples | ||
|
|
||
| - Form field: `username` → `username`, `username_1`, `username_2` (incremental) | ||
| - Standalone: `undefined` → `react-aria-123` (React's useId format) | ||
| - Explicit ID: Always preserved as-is | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| import { useDebugValue, useRef } from 'react'; | ||
| import { useDebugValue, useId, useRef } from 'react'; | ||
|
|
||
| import { useChainedCallback, useEvent } from '../../../../_internal/index'; | ||
| import { mergeProps } from '../../../../utils/react/index'; | ||
|
|
@@ -54,14 +54,31 @@ export function useFieldProps< | |
| ); | ||
| } | ||
|
|
||
| if ( | ||
| isInsideLegacyField || | ||
| isDisabledRef.current === true || | ||
| props.name == null | ||
| ) { | ||
| if (isInsideLegacyField || isDisabledRef.current === true) { | ||
| return props; | ||
| } | ||
|
|
||
| // For standalone fields (no name), just generate an ID without calling useField | ||
| const hasName = props.name != null; | ||
| const generatedId = useId(); | ||
|
|
||
| if (!hasName) { | ||
| // Standalone field - just add generated ID if not provided | ||
| if (!props.id) { | ||
| const result = { ...props, id: generatedId }; | ||
|
|
||
| if (result.id && !result.labelProps) { | ||
| result.labelProps = { for: result.id }; | ||
| } else if (result.id && result.labelProps && !result.labelProps.for) { | ||
| result.labelProps = { ...result.labelProps, for: result.id }; | ||
| } | ||
|
|
||
| return result as Props; | ||
| } | ||
| return props; | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| // Form-connected field - use full useField logic | ||
| const field = useField<T, Props>(props, { | ||
| defaultValidationTrigger: params.defaultValidationTrigger, | ||
| }); | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.