InputField Component
The <InputField> component is a versatile React component that provides various styling and functional options for text input fields. It supports icons, validation states, and different input styles.
Basic Usage
All of the React.CSSProperties are valid props with the benefit of passing just the number without unit (px are used by default). The library extends the CSS properties and adds all of them with Hover suffix that will take effect only when the component is hovered by the device pointer.
- Basic Input
- With Hover
import { InputField } from "react-better-html";
function App() {
return (
<InputField
label="Username"
placeholder="Enter your username"
/>
);
}
import { InputField } from "react-better-html";
function App() {
return (
<InputField
label="Username"
placeholder="Enter your username"
borderColorHover="#ff0000"
/>
);
}
When hovering the input field the border color will change to red.
Common Props
All standard <input> attributes are valid props for this component.
import { InputField } from "react-better-html";
function App() {
return (
<InputField
label="Username"
placeholder="Enter your username"
required
disabled
maxLength={50}
onChange={(event) => {
console.log("Input value:", event.target.value);
}}
/>
);
}
Value change
The library provides a new onChange event called onChangeValue that is of type (value: string) => void.
import { InputField } from "react-better-html";
function App() {
return (
<InputField
label="Username"
placeholder="Enter your username"
onChangeValue={(value) => {
console.log("Input value:", value);
}}
/>
);
}
Keep in mind that onChange and onChangeValue will both fire if passed.
Icons
The component supports both left and right icons:
leftIcon- Icon name to display on the left side of the inputrightIcon- Icon name to display on the right side of the inputonClickRightIcon- Callback function when the right icon is clicked
import { InputField } from "react-better-html";
function App() {
return (
<InputField
label="Username"
placeholder="Username..."
leftIcon="user"
rightIcon="XMark"
onClickRightIcon={() => {
console.log("Right icon clicked");
}}
/>
);
}
Prefix and Suffix
The prefix and suffix props render an element attached to the left or the right side of the input. Their background can be changed with prefixBackgroundColor and suffixBackgroundColor.
- Prefix
- Suffix
import { InputField } from "react-better-html";
function App() {
return (
<InputField
label="Website"
placeholder="example.com"
prefix="https://"
/>
);
}
import { InputField } from "react-better-html";
function App() {
return (
<InputField
label="Price"
type="number"
placeholder="0.00"
suffix="EUR"
/>
);
}
For elements rendered inside the input field holder (e.g. a custom dropdown) use insideInputFieldBeforeComponent and insideInputFieldAfterComponent. The holder element can be accessed with holderRef.
Validation States
The component supports various validation states:
errorText- Text to display when there's an errorinfoText- Text to display as additional informationinfoTextColor- The color of the info textrequired- Whether the field is required or not
import { InputField } from "react-better-html";
function App() {
return (
<InputField
label="Username"
placeholder="Enter your username"
required
errorText="Please enter a valid username"
/>
);
}
Label
The label can be customized with the following props:
labelColor,labelFontSize,labelFontFamily,labelLetterSpacing,labelTextTransform- the style of the labellabelGap- the space between the label and the input
import { InputField } from "react-better-html";
function App() {
return (
<InputField
label="Username"
labelFontSize={12}
labelTextTransform="uppercase"
labelLetterSpacing={1}
/>
);
}
With Debounce
The prop withDebounce is required to turn on that feature. When true, the onChangeValue event will be called with a delay (debounce). You can change the delay by passing the debounceDelay prop. The default value for it is 0.5s and it works for the most part.
import { InputField } from "react-better-html";
function App() {
return (
<InputField
label="Address"
placeholder="Enter your address"
withDebounce
onChangeValue={(value) => {
console.log("Searched address:", value); // Will fire ones after the user stops typing
}}
/>
);
}
Subcomponents
A number of components in the library have a subcomponent feature witch is like a preset of the same component that is frequently used.
InputField.multiline
This component renders a <textarea> component instead of <input>.
import { InputField } from "react-better-html";
function App() {
return (
<InputField.multiline label="Description" placeholder="Enter your description" />
);
}
InputField.email
This component renders a ready for email input field with placeholder and auto-fills.
import { InputField } from "react-better-html";
function App() {
return (
<InputField.email
onChangeValue={(value) => {
console.log("email:", value);
}}
/>
);
}
InputField.password
This component renders a ready for password input field with placeholder and auto-fills. It includes an eye button for unhiding the text.
import { InputField } from "react-better-html";
function App() {
return (
<InputField.password
onChangeValue={(value) => {
console.log("password:", value);
}}
/>
);
}
InputField.search
This component renders a ready for search input field with placeholder and auto-fills. It includes an icon to indicate the search functionality.
import { InputField } from "react-better-html";
function App() {
return (
<InputField.search
onChangeValue={(value) => {
console.log("Search value:", value);
}}
/>
);
}
InputField.phoneNumber
This component renders an input field with dropdown to pick the country code. Local country code is preselected by default.
import { InputField } from "react-better-html";
function App() {
return (
<InputField.phoneNumber
onChangeValue={(value) => {
console.log("Ready phone number:", value);
}}
/>
);
}
InputField.date
This component renders an input field with calendar to pick a date. The value is in YYYY-MM-DD format.
import { InputField } from "react-better-html";
function App() {
return (
<InputField.date
minDate={new Date()}
dropdownPosition="right"
onChangeValue={(value) => {
console.log("Ready date:", value);
}}
/>
);
}
minDate/maxDate- the range of dates that can be pickeddropdownPosition- align the calendar to theleft(default) or to therightside of the field
On iOS Safari the native date picker is used.
InputField.time
This component renders an input field with time picker to pick a time. The value is in HH:mm format.
import { InputField } from "react-better-html";
function App() {
return (
<InputField.time
minTime="09:00"
maxTime="18:00"
minutesToRender={[0, 15, 30, 45]}
onChangeValue={(value) => {
console.log("Ready time:", value);
}}
/>
);
}
minTime/maxTime- the range of time that can be picked (defaults to00:00-23:59)hoursToRender/minutesToRender- the hours and minutes rendered in the pickerdropdownPosition- align the picker to theleft(default) or to therightside of the field
InputField.dateTime
This component renders an input field with calendar and time picker to pick a date and time.
import { InputField } from "react-better-html";
function App() {
return (
<InputField.dateTime
minutesToRender={[0, 30]}
defaultTimeAfterDatePick="09:00"
onChangeValue={(value) => {
console.log("Ready date and time:", value);
}}
/>
);
}
minDate/maxDate- the range of dates that can be pickedhoursToRender/minutesToRender- the hours and minutes rendered in the pickerdefaultTimeAfterDatePick- the time that is set when only a date is picked (defaults to00:00)defaultDateAfterTimePick- the date that is set when only a time is picked (defaults to today)dropdownPosition- align the picker to theleft(default) or to therightside of the field
InputField.color
This component renders an input field with color picker to pick a color.
import { InputField } from "react-better-html";
function App() {
return (
<InputField.color
onChangeValue={(value) => {
console.log("Ready color:", value);
}}
/>
);
}