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

Import

index.tsx
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.

MyComponent.tsx
1<Input placeholder="Enter your name" />

Variants

Inputs support multiple visual variant options: flat, bordered (default), faded, and underlined.

MyComponent.tsx
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.

MyComponent.tsx
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.

MyComponent.tsx
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.

MyComponent.tsx
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.

MyComponent.tsx
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.

MyComponent.tsx
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.

MyComponent.tsx
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.

MyComponent.tsx
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.

MyComponent.tsx
1<Input placeholder="You can't type here" label="Disabled Input" disabled />

Props Overview

PROPTYPEDEFAULTDESCRIPTION
variantInputVariant'bordered'Visual style.
colorInputColor'default'Thematic color on focus/invalid.
sizeInputSize'md'Size of the input.
radiusInputRadius'md'Border radius.
labelReactNode-Label content for the input.
descriptionReactNode-Helper text below the input.
errorMessageReactNode-Error message to display when isInvalid.
isInvalidbooleanfalseMarks input as invalid.
validate(value) => string | null-Validation function run on blur.
startContentReactNode-Content at the start of the input.
endContentReactNode-Content at the end of the input.
stylesInputSlotsStyles-Custom styles for slots (base, label, inputWrapper, etc.).
styleStyleProp<ViewStyle>-Style for the outermost container (styles.base equivalent).
placeholderTextColorColorValue-Custom color for placeholder text.
disabledbooleanfalseDisable the input.
inputRefReact.Ref<TextInput>-Ref for the underlying TextInput.
valuestring-Controlled value of the input.
defaultValuestring-Initial value for uncontrolled input.
...TextInputPropsvarious-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 to styles.base.
  • styles.base: Styles for the outermost container.
  • styles.label: Styles for the label Text component.
  • styles.inputWrapper: Styles for the View that wraps the actual TextInput and any start/end content.
  • styles.input: Styles for the TextInput element itself.
  • styles.description: Styles for the description Text component.
  • styles.errorMessage: Styles for the error message Text component.
  • styles.startContentWrapper: Styles for the View wrapping the startContent.
  • styles.endContentWrapper: Styles for the View wrapping the endContent.
MyComponent.tsx
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 an accessibilityLabel is provided on the TextInput itself (passed via props).
  • When isInvalid is true and an errorMessage is provided, this information is available to assistive technologies.
  • Standard TextInput accessibility props like accessible, accessibilityLabel, accessibilityHint, etc., can be passed directly.