PhoneNumberField - goobz22/goobs-frontend GitHub Wiki
PhoneNumberField Component
The PhoneNumberField component in goobs-frontend is a specialized input field designed for entering and formatting phone numbers. It provides a user-friendly interface with automatic formatting and various styling options.
Importing the Component
To use the PhoneNumberField component in your project, import it from the goobs-frontend package:
import { PhoneNumberField } from 'goobs-frontend';
Basic Usage
Here's a basic example of how to use the PhoneNumberField component:
import React from 'react';
import { PhoneNumberField } from 'goobs-frontend';
const PhoneNumberFieldExample: React.FC = () => {
return (
<PhoneNumberField
label="Phone Number"
onChange={() => console.log('Phone number changed')}
/>
);
};
export default PhoneNumberFieldExample;
Props
The PhoneNumberField component accepts the following props:
initialValue
: string (optional) - The initial value of the field.onChange
: () => void (optional) - Callback function triggered when the value changes.backgroundcolor
: string (optional) - Background color of the input field.outlinecolor
: string (optional) - Color of the input field outline.fontcolor
: string (optional) - Color of the text inside the input field.label
: string (optional, default: 'Phone Number') - Label for the input field.
Additionally, it supports all standard Material-UI TextField props except for onChange
.
Features
- Automatic Formatting: Formats the input as a US phone number (e.g., +1 123-456-7890).
- Input Masking: Automatically adds the country code (+1) and hyphens.
- Customizable Styling: Supports custom colors for background, outline, and font.
- Accessibility: Includes proper labeling and ARIA attributes for screen readers.
Styling
You can customize the appearance of the PhoneNumberField component using the provided props. Here's an example with custom styling:
import React from 'react';
import { PhoneNumberField } from 'goobs-frontend';
const StyledPhoneNumberFieldExample: React.FC = () => {
return (
<PhoneNumberField
label="Custom Styled Phone Number"
backgroundcolor="#f0f0f0"
outlinecolor="#3f51b5"
fontcolor="#333"
onChange={() => console.log('Phone number changed')}
/>
);
};
export default StyledPhoneNumberFieldExample;
Advanced Usage
With Form Validation
You can use the PhoneNumberField component within a form with custom validation:
import React, { useState } from 'react';
import { PhoneNumberField } from 'goobs-frontend';
const ValidatedPhoneNumberFieldExample: React.FC = () => {
const [phoneNumber, setPhoneNumber] = useState('');
const [error, setError] = useState<string | null>(null);
const handleChange = () => {
const newPhoneNumber = document.querySelector('input').value;
setPhoneNumber(newPhoneNumber);
// Simple validation: check if the phone number is complete
if (newPhoneNumber.replace(/\D/g, '').length !== 11) {
setError('Please enter a complete phone number');
} else {
setError(null);
}
};
return (
<PhoneNumberField
label="Enter your phone number"
initialValue={phoneNumber}
onChange={handleChange}
error={!!error}
helperText={error}
/>
);
};
export default ValidatedPhoneNumberFieldExample;
With Custom Formatting
While the component uses a default US phone number format, you can implement custom formatting for international numbers:
import React, { useState } from 'react';
import { PhoneNumberField } from 'goobs-frontend';
const CustomFormattingPhoneNumberFieldExample: React.FC = () => {
const [phoneNumber, setPhoneNumber] = useState('');
const formatInternationalNumber = (input: string) => {
// This is a simple example and may need to be adjusted for different country formats
const numbers = input.replace(/\D/g, '');
let formatted = '';
if (numbers.length > 0) {
formatted += '+' + numbers.substring(0, 2);
if (numbers.length > 2) {
formatted += ' ' + numbers.substring(2, 5);
if (numbers.length > 5) {
formatted += ' ' + numbers.substring(5, 8);
if (numbers.length > 8) {
formatted += ' ' + numbers.substring(8);
}
}
}
}
return formatted;
};
const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const formatted = formatInternationalNumber(event.target.value);
setPhoneNumber(formatted);
};
return (
<PhoneNumberField
label="International Phone Number"
value={phoneNumber}
onChange={handleChange}
/>
);
};
export default CustomFormattingPhoneNumberFieldExample;
Accessibility
The PhoneNumberField component is designed with accessibility in mind:
- It uses proper labeling to describe the input field's purpose.
- The component is keyboard accessible.
- It supports screen readers for announcing the current value and changes.
To further enhance accessibility:
- Provide clear instructions for the expected phone number format.
- Ensure error messages are descriptive and helpful.
- Consider adding ARIA live regions for dynamic feedback on input validity.
Best Practices
- Clear Labels: Use descriptive labels to indicate the purpose of the phone number field.
- Format Expectations: Clearly communicate the expected phone number format to users.
- Error Handling: Provide clear error messages for invalid inputs.
- International Considerations: If your application is used internationally, consider supporting multiple phone number formats.
- Validation: Implement both client-side and server-side validation for phone numbers.
Performance Considerations
The PhoneNumberField component is generally lightweight, but keep in mind:
- The formatting function runs on every input change. For very large forms with multiple phone number fields, consider debouncing the formatting function.
- If implementing custom international formatting, ensure your formatting function is optimized for performance.
Troubleshooting
- Formatting Issues: If the phone number isn't formatting correctly, check that you haven't overridden the default formatting behavior unintentionally.
- Styling Problems: Verify that your custom styles aren't conflicting with the component's built-in styles.
- Validation Errors: Ensure that your validation logic accounts for the formatted nature of the phone number input.
By leveraging these features and following the best practices, you can effectively use the PhoneNumberField component in your goobs-frontend projects to create user-friendly phone number input interfaces with proper formatting and validation.