Input
The Input component allows users to enter and edit text. It supports various styles, sizes, validation, and custom content.
Installation
$ cbui-cli install @crossbuildui/input
cbui-cli
globally and authenticated your account. This will ensure you can access all the necessary features.Import
1import { Input } from '@/crossbuildui/input';
2// import Icon from 'react-native-vector-icons/MaterialCommunityIcons'; // Or your preferred icon library for start/end content
Input Component
Basic Usage
A simple input field with a placeholder.
1<Input placeholder="Enter your name" />
Variants
Inputs support multiple visual variant
options: flat
, bordered
(default), faded
, and underlined
.
1<View style={{gap: 16}}>
2 <Input variant="flat" placeholder="Flat input" />
3 <Input variant="bordered" placeholder="Bordered input (default)" />
4 <Input variant="faded" placeholder="Faded input" />
5 <Input variant="underlined" placeholder="Underlined input" />
6</View>
Sizes
Adjust input size with sm
, md
(default), or lg
.
1<View style={{gap: 16}}>
2 <Input size="sm" placeholder="Small input" label="Small" />
3 <Input size="md" placeholder="Medium input (default)" label="Medium" />
4 <Input size="lg" placeholder="Large input" label="Large" />
5</View>
Radius
Control the border radius with none
, sm
, md
(default), lg
, or full
.
1<View style={{gap: 16}}>
2 <Input radius="none" placeholder="No radius" />
3 <Input radius="sm" placeholder="Small radius" />
4 <Input radius="md" placeholder="Medium radius (default)" />
5 <Input radius="lg" placeholder="Large radius" />
6 <Input radius="full" placeholder="Full radius" startContent={<Icon name="magnify" size={18} />} />
7</View>
Colors
The color
prop influences the input's appearance on focus or when invalid. Supported colors include primary
, secondary
, success
, warning
, danger
, and default
.
1<View style={{gap: 16}}>
2 <Input label="Primary Focus" color="primary" placeholder="Focus to see color" />
3 <Input label="Secondary Focus" color="secondary" placeholder="Focus to see color" />
4 <Input label="Success Focus" color="success" placeholder="Focus to see color" />
5 <Input label="Warning Focus" color="warning" placeholder="Focus to see color" />
6 <Input label="Danger (Invalid)" color="danger" placeholder="Invalid state" isInvalid errorMessage="This field is invalid" />
7 <Input label="Default Focus" color="default" placeholder="Focus to see color" />
8</View>
Label and Description
Provide context to your input fields using the label
and description
props.
1<View style={{gap: 16}}>
2 <Input label="Email Address" placeholder="you@example.com" />
3 <Input
4 label="Password"
5 placeholder="Enter your password"
6 description="Must be at least 8 characters long."
7 secureTextEntry
8 />
9</View>
Error Message & Validation
Display validation errors using isInvalid
and errorMessage
. Use the validate
prop for built-in validation logic on blur.
1// Controlled example for validation feedback
2function ValidatedInput() {
3 const [value, setValue] = React.useState("");
4 const [error, setError] = React.useState("");
5
6 const validateEmail = (email) => {
7 if (!email) return "Email is required.";
8 if (!/\S+@\S+\.\S+/.test(email)) return "Invalid email address.";
9 return null;
10 };
11
12 const handleChange = (text) => {
13 setValue(text);
14 const validationError = validateEmail(text);
15 setError(validationError);
16 };
17
18 return (
19 <Input
20 label="Validated Email"
21 placeholder="Enter email"
22 value={value}
23 onChangeText={handleChange}
24 isInvalid={!!error}
25 errorMessage={error}
26 // Or use the validate prop for onBlur validation:
27 // validate={validateEmail}
28 />
29 );
30}
31
32// <ValidatedInput />
33
Start and End Content
Enhance inputs with icons or text elements using startContent
and endContent
.
1<View style={{gap: 16}}>
2 <Input
3 placeholder="Search..."
4 startContent={<Icon name="magnify" size={20} color="gray" />}
5 />
6 <Input
7 placeholder="Amount"
8 endContent={<Text style={{color: 'gray'}}>USD</Text>}
9 keyboardType="numeric"
10 />
11 <Input
12 placeholder="Website"
13 startContent={<Text style={{color: 'gray'}}>https://</Text>}
14 endContent={<Icon name="link-variant" size={20} color="gray" />}
15 />
16</View>
Controlled and Uncontrolled
Manage input state externally using value
and onChangeText
for a controlled component, or use defaultValue
for an uncontrolled component.
1// Controlled Input
2function ControlledInputExample() {
3 const [text, setText] = React.useState('');
4 return (
5 <Input
6 value={text}
7 onChangeText={setText}
8 placeholder="Type something (controlled)"
9 label="Controlled"
10 />
11 );
12}
13
14// Uncontrolled Input
15<Input
16 defaultValue="Initial text"
17 placeholder="Type something (uncontrolled)"
18 label="Uncontrolled"
19/>
20
Disabled State
Disable the input using the disabled
prop.
1<Input placeholder="You can't type here" label="Disabled Input" disabled />
Props Overview
PROP | TYPE | DEFAULT | DESCRIPTION |
---|---|---|---|
variant | InputVariant | 'bordered' | Visual style. |
color | InputColor | 'default' | Thematic color on focus/invalid. |
size | InputSize | 'md' | Size of the input. |
radius | InputRadius | 'md' | Border radius. |
label | ReactNode | - | Label content for the input. |
description | ReactNode | - | Helper text below the input. |
errorMessage | ReactNode | - | Error message to display when isInvalid . |
isInvalid | boolean | false | Marks input as invalid. |
validate | (value) => string | null | - | Validation function run on blur. |
startContent | ReactNode | - | Content at the start of the input. |
endContent | ReactNode | - | Content at the end of the input. |
styles | InputSlotsStyles | - | Custom styles for slots (base, label, inputWrapper, etc.). |
style | StyleProp<ViewStyle> | - | Style for the outermost container (styles.base equivalent). |
placeholderTextColor | ColorValue | - | Custom color for placeholder text. |
disabled | boolean | false | Disable the input. |
inputRef | React.Ref<TextInput> | - | Ref for the underlying TextInput. |
value | string | - | Controlled value of the input. |
defaultValue | string | - | Initial value for uncontrolled input. |
...TextInputProps | various | - | Other props from React Native's TextInput . |
Styling
Customize the Input appearance using the style
prop for the main wrapper or the styles
prop for more granular control over internal slots:
style
: Applied to the outermost container (View
) of the entire input component. Equivalent tostyles.base
.styles.base
: Styles for the outermost container.styles.label
: Styles for the labelText
component.styles.inputWrapper
: Styles for theView
that wraps the actualTextInput
and any start/end content.styles.input
: Styles for theTextInput
element itself.styles.description
: Styles for the descriptionText
component.styles.errorMessage
: Styles for the error messageText
component.styles.startContentWrapper
: Styles for theView
wrapping thestartContent
.styles.endContentWrapper
: Styles for theView
wrapping theendContent
.
1<Input
2 label="Custom Styled Input"
3 placeholder="Enter details..."
4 description="This input has custom slot styling."
5 style={{ marginBottom: 20 }} // Styles the outermost 'base' container
6 styles={{
7 base: { backgroundColor: '#f0f8ff' },
8 label: { color: 'navy', fontWeight: 'bold', fontStyle: 'italic' },
9 inputWrapper: { borderColor: 'teal', borderWidth: 2, backgroundColor: 'white' },
10 input: { color: 'purple', fontSize: 16 },
11 description: { color: 'green', marginTop: 8 },
12 errorMessage: { color: 'orange', fontWeight: 'bold' }
13 }}
14 startContent={<Icon name="pencil" size={20} color="teal" />}
15/>
Accessibility
The Input component aims to be accessible:
- The
label
prop is crucial for accessibility. It will be associated with the input field. If a visible label is not desired, ensure anaccessibilityLabel
is provided on theTextInput
itself (passed via props). - When
isInvalid
is true and anerrorMessage
is provided, this information is available to assistive technologies. - Standard
TextInput
accessibility props likeaccessible
,accessibilityLabel
,accessibilityHint
, etc., can be passed directly.