-
-
Notifications
You must be signed in to change notification settings - Fork 33
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
TextField component #511
base: main
Are you sure you want to change the base?
TextField component #511
Conversation
WalkthroughA new Changes
Possibly related PRs
Suggested reviewers
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #511 +/- ##
=======================================
Coverage 92.30% 92.30%
=======================================
Files 12 12
Lines 65 65
Branches 15 15
=======================================
Hits 60 60
Misses 5 5 ☔ View full report in Codecov by Sentry. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 11
🧹 Outside diff range and nitpick comments (8)
src/components/ui/TextField/fragments/TextFieldRoot.tsx (3)
7-10
: Add JSDoc documentation for better developer experience.Consider adding JSDoc comments to document the props interface, especially explaining the difference between
className
andcustomRootClass
.+/** + * Props for the TextFieldRoot component + * @property {string} [className] - Additional CSS classes to be applied + * @property {string} [customRootClass] - Custom class to override the default root class + */ type TextFieldRootProps = React.HTMLAttributes<HTMLDivElement> & { className?: string; customRootClass?: string; };
17-17
: Use a more specific display name.Consider using a more specific display name to better identify this sub-component in React DevTools.
-TextFieldRoot.displayName = COMPONENT_NAME; +TextFieldRoot.displayName = `${COMPONENT_NAME}Root`;
1-19
: Consider enhancing component architecture.As part of a compound component pattern, consider these improvements:
- Add React Context for sharing state between TextField components
- Implement proper accessibility roles and ARIA attributes
- Add runtime prop-types validation for non-TypeScript consumers
Example Context implementation:
import { createContext, useContext } from 'react'; type TextFieldContextType = { id?: string; disabled?: boolean; // Add other shared state }; export const TextFieldContext = createContext<TextFieldContextType | undefined>(undefined); export function useTextField() { const context = useContext(TextFieldContext); if (!context) { throw new Error('TextField components must be used within a TextField'); } return context; }src/components/ui/TextField/TextField.tsx (2)
7-10
: Add JSDoc documentation and consider type constraints.The type definition would benefit from:
- JSDoc documentation explaining the purpose of each prop
- More specific type constraints for className and customRootClass
+/** + * Props for the TextField component + * @property {string} [className] - Additional CSS class for styling the input + * @property {string} [customRootClass] - Custom class for the root wrapper element + */ type TextFieldProps = React.InputHTMLAttributes<HTMLInputElement> & { className?: string; customRootClass?: string; };
12-26
: Consider implementing compound component pattern properly.The current implementation with hardcoded slots limits flexibility. Consider implementing a proper compound component pattern.
Example usage pattern to support:
<TextField> <TextField.Slot> <SearchIcon /> </TextField.Slot> <TextField.Input placeholder="Search..." /> <TextField.Slot> <ClearButton /> </TextField.Slot> </TextField>Would you like assistance in implementing this pattern?
src/components/ui/TextField/stories/TextField.stories.js (3)
13-17
: Consider documenting supported props in the story.While spreading props provides flexibility, explicitly documenting the supported props would improve the story's documentation value.
Add prop documentation using Storybook's argTypes:
export default { title: 'Components/TextField', component: TextField, - render: (args) => <Template {...args} /> + render: (args) => <Template {...args} />, + argTypes: { + className: { description: 'Additional CSS classes' }, + // Add other supported props here + } };
19-20
: Remove unnecessary comment.The link to Storybook documentation doesn't add value here as it's standard boilerplate.
-// More on writing stories with args: https://storybook.js.org/docs/react/writing-stories/args
46-47
: Remove empty args object.The empty args object doesn't provide any value and can be removed.
export const WithForm = WithFormTemplate.bind({}); -WithForm.args = { -};
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (7)
src/components/ui/TextField/TextField.tsx
(1 hunks)src/components/ui/TextField/fragments/TextFieldInput.tsx
(1 hunks)src/components/ui/TextField/fragments/TextFieldRoot.tsx
(1 hunks)src/components/ui/TextField/fragments/TextFieldSlot.tsx
(1 hunks)src/components/ui/TextField/stories/TextField.stories.js
(1 hunks)styles/themes/components/text-field.scss
(1 hunks)styles/themes/default.scss
(1 hunks)
✅ Files skipped from review due to trivial changes (1)
- styles/themes/components/text-field.scss
🔇 Additional comments (6)
src/components/ui/TextField/fragments/TextFieldSlot.tsx (1)
7-7
: LGTM!
The default export is appropriate for this single-component file.
src/components/ui/TextField/fragments/TextFieldRoot.tsx (1)
3-3
: Verify the ~
path alias configuration.
The ~
path alias needs to be properly configured in your build tools (webpack/vite/tsconfig) to ensure consistent resolution across different environments.
src/components/ui/TextField/stories/TextField.stories.js (2)
1-6
: LGTM! Import statements are well-organized.
The imports follow a logical grouping pattern and all imported components are utilized in the stories.
7-11
: LGTM! Storybook configuration follows best practices.
The story configuration properly sets up the component metadata and uses a template for consistent rendering.
styles/themes/default.scss (2)
17-17
: LGTM! Import placement follows existing patterns.
The text-field styles import is correctly positioned alphabetically among other component imports.
Line range hint 28-33
: Consider uncommenting and updating focus styles for accessibility.
The commented-out focus styles could be important for the TextField component's accessibility. Consider updating and re-enabling these styles to ensure proper keyboard navigation and focus indicators.
Let's check if there are any other focus-related styles that might compensate for this:
Consider implementing consistent focus styles that meet WCAG 2.1 Success Criterion 2.4.7 (Focus Visible). The styles should:
- Be sufficiently visible (high contrast)
- Indicate the focused element clearly
- Be consistent across all interactive elements
const TextFieldSlot = ({ children }: React.HTMLAttributes<HTMLDivElement>) => { | ||
return <div className="rad-ui-text-field-slot">{children}</div>; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Improve component typing and props handling.
The component could benefit from more precise typing and proper props handling:
- The component should explicitly type children and spread other HTML attributes
- Consider adding explicit return type annotation
Consider this improved implementation:
-const TextFieldSlot = ({ children }: React.HTMLAttributes<HTMLDivElement>) => {
- return <div className="rad-ui-text-field-slot">{children}</div>;
+type TextFieldSlotProps = {
+ children: React.ReactNode;
+} & Omit<React.HTMLAttributes<HTMLDivElement>, 'children'>;
+
+const TextFieldSlot: React.FC<TextFieldSlotProps> = ({ children, className, ...props }) => {
+ return (
+ <div
+ className={['rad-ui-text-field-slot', className].filter(Boolean).join(' ')}
+ {...props}
+ >
+ {children}
+ </div>
+ );
};
This change:
- Explicitly types the children prop
- Properly handles className merging
- Spreads remaining HTML attributes
- Adds explicit return type annotation
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
const TextFieldSlot = ({ children }: React.HTMLAttributes<HTMLDivElement>) => { | |
return <div className="rad-ui-text-field-slot">{children}</div>; | |
}; | |
type TextFieldSlotProps = { | |
children: React.ReactNode; | |
} & Omit<React.HTMLAttributes<HTMLDivElement>, 'children'>; | |
const TextFieldSlot: React.FC<TextFieldSlotProps> = ({ children, className, ...props }) => { | |
return ( | |
<div | |
className={['rad-ui-text-field-slot', className].filter(Boolean).join(' ')} | |
{...props} | |
> | |
{children} | |
</div> | |
); | |
}; |
import React from 'react'; | ||
|
||
const TextFieldInput = ({ className = '', ...props }: React.InputHTMLAttributes<HTMLInputElement>) => { | ||
return <input className={`${className}`} {...props} />; | ||
}; | ||
|
||
export default TextFieldInput; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Export component type for better developer experience.
Consider exporting the component's type definition to improve TypeScript integration.
import React from 'react';
+export type TextFieldInputProps = React.InputHTMLAttributes<HTMLInputElement>;
+
-const TextFieldInput = ({ className = '', ...props }: React.InputHTMLAttributes<HTMLInputElement>) => {
+const TextFieldInput = ({ className = '', ...props }: TextFieldInputProps) => {
return <input className={`${className}`} {...props} />;
};
export default TextFieldInput;
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
import React from 'react'; | |
const TextFieldInput = ({ className = '', ...props }: React.InputHTMLAttributes<HTMLInputElement>) => { | |
return <input className={`${className}`} {...props} />; | |
}; | |
export default TextFieldInput; | |
import React from 'react'; | |
export type TextFieldInputProps = React.InputHTMLAttributes<HTMLInputElement>; | |
const TextFieldInput = ({ className = '', ...props }: TextFieldInputProps) => { | |
return <input className={`${className}`} {...props} />; | |
}; | |
export default TextFieldInput; |
const TextFieldInput = ({ className = '', ...props }: React.InputHTMLAttributes<HTMLInputElement>) => { | ||
return <input className={`${className}`} {...props} />; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Consider integrating with the project's styling system.
The className handling could be improved by integrating with the project's styling system mentioned in the summary (rad-ui-text-field).
-const TextFieldInput = ({ className = '', ...props }: React.InputHTMLAttributes<HTMLInputElement>) => {
- return <input className={`${className}`} {...props} />;
+const TextFieldInput = ({ className = '', ...props }: React.InputHTMLAttributes<HTMLInputElement>) => {
+ return <input className={`rad-ui-text-field__input ${className}`} {...props} />;
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
const TextFieldInput = ({ className = '', ...props }: React.InputHTMLAttributes<HTMLInputElement>) => { | |
return <input className={`${className}`} {...props} />; | |
}; | |
const TextFieldInput = ({ className = '', ...props }: React.InputHTMLAttributes<HTMLInputElement>) => { | |
return <input className={`rad-ui-text-field__input ${className}`} {...props} />; | |
}; |
🛠️ Refactor suggestion
Enhance accessibility with ARIA attributes.
Consider adding appropriate ARIA attributes and role to improve accessibility.
const TextFieldInput = ({ className = '', ...props }: React.InputHTMLAttributes<HTMLInputElement>) => {
- return <input className={`${className}`} {...props} />;
+ return (
+ <input
+ className={`${className}`}
+ role="textbox"
+ aria-label={props['aria-label'] || 'text input'}
+ {...props}
+ />
+ );
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
const TextFieldInput = ({ className = '', ...props }: React.InputHTMLAttributes<HTMLInputElement>) => { | |
return <input className={`${className}`} {...props} />; | |
}; | |
const TextFieldInput = ({ className = '', ...props }: React.InputHTMLAttributes<HTMLInputElement>) => { | |
return ( | |
<input | |
className={`${className}`} | |
role="textbox" | |
aria-label={props['aria-label'] || 'text input'} | |
{...props} | |
/> | |
); | |
}; |
🛠️ Refactor suggestion
Consider handling controlled component behavior.
Since this is a form input component, it might be helpful to provide some built-in handling for controlled component patterns.
-const TextFieldInput = ({ className = '', ...props }: React.InputHTMLAttributes<HTMLInputElement>) => {
- return <input className={`${className}`} {...props} />;
+const TextFieldInput = ({
+ className = '',
+ value,
+ defaultValue,
+ onChange,
+ ...props
+}: TextFieldInputProps) => {
+ const isControlled = value !== undefined;
+ return (
+ <input
+ className={`${className}`}
+ value={isControlled ? value : undefined}
+ defaultValue={!isControlled ? defaultValue : undefined}
+ onChange={(e) => {
+ onChange?.(e);
+ }}
+ {...props}
+ />
+ );
Committable suggestion skipped: line range outside the PR's diff.
const TextFieldRoot = ({ className = '', customRootClass = '', ...props }: TextFieldRootProps) => { | ||
const rootClass = customClassSwitcher(customRootClass, COMPONENT_NAME); | ||
return <div className={`${rootClass} ${className}`} {...props} />; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Consider using a class name utility library.
The current class name concatenation could lead to extra spaces and doesn't handle falsy values. Consider using a utility like clsx
or classnames
for more robust class name handling.
+import clsx from 'clsx';
+
const TextFieldRoot = ({ className = '', customRootClass = '', ...props }: TextFieldRootProps) => {
const rootClass = customClassSwitcher(customRootClass, COMPONENT_NAME);
- return <div className={`${rootClass} ${className}`} {...props} />;
+ return <div className={clsx(rootClass, className)} {...props} />;
};
Committable suggestion skipped: line range outside the PR's diff.
import React from 'react'; | ||
|
||
import TextFieldRoot from './fragments/TextFieldRoot'; | ||
import TextFieldInput from './fragments/TextFieldInput'; | ||
import TextFieldSlot from './fragments/TextFieldSlot'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Consider importing types from fragment components.
For better type safety and maintainability, consider importing and re-exporting the types from your fragment components. This would make the API surface more explicit and help with documentation.
import React from 'react';
import TextFieldRoot from './fragments/TextFieldRoot';
import TextFieldInput from './fragments/TextFieldInput';
import TextFieldSlot from './fragments/TextFieldSlot';
+import type { TextFieldRootProps } from './fragments/TextFieldRoot';
+import type { TextFieldInputProps } from './fragments/TextFieldInput';
+import type { TextFieldSlotProps } from './fragments/TextFieldSlot';
+
+export type { TextFieldRootProps, TextFieldInputProps, TextFieldSlotProps };
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
import React from 'react'; | |
import TextFieldRoot from './fragments/TextFieldRoot'; | |
import TextFieldInput from './fragments/TextFieldInput'; | |
import TextFieldSlot from './fragments/TextFieldSlot'; | |
import React from 'react'; | |
import TextFieldRoot from './fragments/TextFieldRoot'; | |
import TextFieldInput from './fragments/TextFieldInput'; | |
import TextFieldSlot from './fragments/TextFieldSlot'; | |
import type { TextFieldRootProps } from './fragments/TextFieldRoot'; | |
import type { TextFieldInputProps } from './fragments/TextFieldInput'; | |
import type { TextFieldSlotProps } from './fragments/TextFieldSlot'; | |
export type { TextFieldRootProps, TextFieldInputProps, TextFieldSlotProps }; |
const TextField = ({ className = '', customRootClass = '', value, onChange, ...props }: TextFieldProps) => { | ||
return ( | ||
<TextFieldRoot customRootClass={customRootClass} className={className}> | ||
<TextFieldSlot> | ||
slot 1 | ||
</TextFieldSlot> | ||
{/* */} | ||
<TextFieldInput type="text" value={value} onChange={onChange} {...props} /> | ||
{/* */} | ||
<TextFieldSlot> | ||
slot 2 | ||
</TextFieldSlot> | ||
</TextFieldRoot> | ||
); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Component implementation needs significant improvements.
Several issues need to be addressed:
- Hardcoded slot content ("slot 1", "slot 2")
- Missing accessibility attributes
- Empty comments in JSX
- No error handling
Here's a suggested improvement:
-const TextField = ({ className = '', customRootClass = '', value, onChange, ...props }: TextFieldProps) => {
+const TextField = ({
+ className = '',
+ customRootClass = '',
+ value,
+ onChange,
+ 'aria-label': ariaLabel,
+ ...props
+}: TextFieldProps) => {
return (
- <TextFieldRoot customRootClass={customRootClass} className={className}>
- <TextFieldSlot>
- slot 1
- </TextFieldSlot>
- {/* */}
- <TextFieldInput type="text" value={value} onChange={onChange} {...props} />
- {/* */}
- <TextFieldSlot>
- slot 2
- </TextFieldSlot>
+ <TextFieldRoot customRootClass={customRootClass} className={className} role="group">
+ {props.children || (
+ <>
+ <TextFieldSlot />
+ <TextFieldInput
+ type="text"
+ value={value}
+ onChange={onChange}
+ aria-label={ariaLabel}
+ {...props}
+ />
+ <TextFieldSlot />
+ </>
+ )}
</TextFieldRoot>
);
};
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
const TextField = ({ className = '', customRootClass = '', value, onChange, ...props }: TextFieldProps) => { | |
return ( | |
<TextFieldRoot customRootClass={customRootClass} className={className}> | |
<TextFieldSlot> | |
slot 1 | |
</TextFieldSlot> | |
{/* */} | |
<TextFieldInput type="text" value={value} onChange={onChange} {...props} /> | |
{/* */} | |
<TextFieldSlot> | |
slot 2 | |
</TextFieldSlot> | |
</TextFieldRoot> | |
); | |
}; | |
const TextField = ({ | |
className = '', | |
customRootClass = '', | |
value, | |
onChange, | |
'aria-label': ariaLabel, | |
...props | |
}: TextFieldProps) => { | |
return ( | |
<TextFieldRoot customRootClass={customRootClass} className={className} role="group"> | |
{props.children || ( | |
<> | |
<TextFieldSlot /> | |
<TextFieldInput | |
type="text" | |
value={value} | |
onChange={onChange} | |
aria-label={ariaLabel} | |
{...props} | |
/> | |
<TextFieldSlot /> | |
</> | |
)} | |
</TextFieldRoot> | |
); | |
}; |
💡 Codebase verification
Add basic input validation and error handling to TextField component
The TextField component currently lacks input validation and error handling. Based on the codebase analysis:
- The component extends React.InputHTMLAttributes but doesn't handle common input validation scenarios
- No error state or error message display mechanism exists
- No validation patterns or utilities are present in the codebase
Suggested improvements:
- Add
error
anderrorMessage
props to TextFieldProps interface - Add visual error state handling in TextFieldRoot and TextFieldInput
- Implement basic input validation based on HTML5 input attributes (required, pattern, minLength, etc.)
🔗 Analysis chain
Add error handling and validation.
The component should handle common error cases and validate props appropriately.
Let's check if there are any existing error handling patterns in the codebase:
Let me check the error handling pattern found and look for any prop validation patterns in the codebase.
Let me check the specific TextField interface and any existing validation patterns in similar input components.
Let me try one final search to understand the component's interface and validation patterns.
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Look for error handling patterns in other components
rg -g '*.tsx' -g '*.ts' 'try.*catch|throw.*Error|Error\(' src/components/
Length of output: 156
Script:
#!/bin/bash
# Check the specific error handling in ProgressIndicator
rg -A 5 'throw new Error' src/components/ui/Progress/shards/ProgressIndicator.tsx
# Look for prop validation patterns, particularly with PropTypes or TypeScript
ast-grep --pattern 'interface $_Props {
$$$
}'
# Check for any validation utilities or helper functions
rg -g '*.ts' -g '*.tsx' 'validate|isValid|check' src/
Length of output: 1684
Script:
#!/bin/bash
# Check TextField interface
rg -A 10 'interface TextFieldProps' src/
# Look for validation patterns in input-like components
rg -g '*.tsx' -A 10 'input.*value.*onChange' src/components/
# Check for any existing input validation examples
ast-grep --pattern 'const $_ = ({ value, onChange, $$ }) => {
$$$
}'
Length of output: 178
Script:
#!/bin/bash
# Search for the TextField type definitions
fd "TextField.tsx" --exec cat {}
# Look for any input validation patterns in the entire codebase
rg -g '*.tsx' -g '*.ts' -B 2 -A 2 'onChange.*=.*\(.*\).*=>' src/
# Check for any prop validation utilities
fd -e ts -e tsx --exec grep -l "validate\|isValid" {} src/utils/
Length of output: 7164
const handleSubmit = (e) => { | ||
e.preventDefault(); | ||
console.log(value); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Replace console.log with Storybook action.
Using console.log for form submission isn't ideal in Storybook stories.
+import { action } from '@storybook/addon-actions';
+
const handleSubmit = (e) => {
e.preventDefault();
- console.log(value);
+ action('form-submitted')(value);
};
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
const handleSubmit = (e) => { | |
e.preventDefault(); | |
console.log(value); | |
}; | |
import { action } from '@storybook/addon-actions'; | |
const handleSubmit = (e) => { | |
e.preventDefault(); | |
action('form-submitted')(value); | |
}; |
export const All = { | ||
args: { | ||
className: '' | ||
} | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Enhance the basic story to showcase component capabilities.
The current story with an empty className doesn't effectively demonstrate the TextField's features.
Consider expanding the story to show different states and use cases:
export const All = {
args: {
- className: ''
+ placeholder: 'Enter text here',
+ defaultValue: 'Sample text',
+ disabled: false,
+ // Add other relevant props to showcase functionality
}
};
Committable suggestion skipped: line range outside the PR's diff.
WIP
Summary by CodeRabbit
TextField
component for text input with additional slots for content.TextFieldInput
,TextFieldRoot
, andTextFieldSlot
components for enhanced structure and styling..rad-ui-text-field
for styling the text field component.TextField
component, showcasing its functionality in various scenarios.