Socket
Socket
Sign inDemoInstall

@asphalt-react/textfield

Package Overview
Dependencies
Maintainers
0
Versions
31
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@asphalt-react/textfield

Textfield


Version published
Maintainers
0
Created
Source

Textfield

npm

Textfield is an input field that enables users to type in data. It accepts data in various formats like text, numeric and password.Textfield is configurable to accept short or long form entries. You can configure its width and also attach SVG icons, words and React nodes to the input field.

The architecture of the Textfield family is flexible, so you can compose complex input fields as required.

Usage

import {
  Textfield,
  Email,
  Search
} from "@asphalt-react/textfield"

  <>
    <Textfield/>
    <Email/>
    <Search/>
  </>

Components

Textfield suite offers following components specific to the nature of the input -

  • Textfield
  • Email
  • Password
  • URL
  • Numeric
  • Search
  • Datefield
  • Timefield
  • Pinfield

Qualifiers

Qualifiers provide a hint about the expected content of the field. They can be SVG icons or words. You can attach the qualifier using qualifierStart & qualifierEnd. Qualifiers works great for use-cases like attaching currency ISO codes or symbols to the Textfield.

Addons

Along with qualifiers, Textfields also accept React nodes (a.k.a addon) to attach them beside qualifiers. You can use addons to compose components like for a phone number field with country code selector as an addon. You can add the React nodes on either ends of the field input simultaneously.

By default addons are rendered on the extreme ends of the field. Adjacent to the addons qualifiers are rendered and they are attached to the ends of the input field. The order of these entities from start to end is addOnStart, qualifierStart, input field in the middle, qualifierEnd & addOnEnd. You can swap the positions of Qualifiers and AddOns such that qualifiers appear on the extreme ends while addOns take the middle slots.

Multiline

Apply multiline prop to accept multiple rows of text. Only Textfield supports multiline. Qualifiers don't work in multiline fields.

Building blocks

You can compose complex input fields with the help of the following building blocks that are part of the Textfield family.

Unit Components

The Textfield family comes with:

  • InputWrapper
  • InputQualifier
  • InputAddOn
  • Input

Hooks

Use the useInput() hook to get all the prop getter functions and then spread them in the right children components. Pass all the props in the useInput() hook as parameter.

import { 
  useInput, 
  InputWrapper,
  InputQualifier,
  InputAddOn,
  Input
} from "@asphalt-react/textfield"

const { getInputProps, getWrapperProps, getQualifierProps } = useInput(props)

<InputWrapper {...getWrapperProps()}>
  <InputQualifier {...getQualifierProps()}>
    <IconPlaceHolder />
  </InputQualifier>

  <Input {...getInputProps()} />

  <InputAddOn>
    <Button icon>
      <Add />
    </Button>
  </InputAddOn>
</InputWrapper>

Form validation & Attributes

  1. All textfields support HTML form validations.
  2. All textfields supports data-/* attributes & DOM attributes

Accessing through React ref

To get access to the underlying DOM element, pass a React ref in the ref prop.

Accessibility

  1. All Textfields are focusable.
  2. By default Numeric render input element with inputmode="numeric".
  3. Textfields have aria-invalid attribute for components with invalid prop set to true.
  4. All Textfields accept the aria-* attributes input role.

Props

size

Controls size of the field. Accepts xs, s, m, l for extra small, small, medium & large.

typerequireddefault
enumfalse"m"

stretch

Field matches the width of its container.

typerequireddefault
boolfalsefalse

multiline

Enables the field to accept multiple lines of text.

Qualifiers do not work with multiline.

typerequireddefault
boolfalsefalse

qualifierStart

Icon or text to render as start qualifier. Accepts SVG for icons.

typerequireddefault
unionfalsenull

qualifierEnd

Icon or text to render as end qualifier. Accepts SVG for icons.

typerequireddefault
unionfalsenull

addOnStart

React node to render before the field content.

typerequireddefault
elementfalsenull

addOnEnd

React node to render after the field content.

typerequireddefault
elementfalsenull

swapSlots

Swap the slots for qualifiers and addOns.

typerequireddefault
boolfalsefalse

extent

Controls the height of a multiline field. Accepts "low", "mid" & "high".

typerequireddefault
enumfalse"low"

invalid

Adds error styles to the field if true.

typerequireddefault
boolfalsefalse

disabled

Makes the field disabled if true.

typerequireddefault
boolfalsefalse

resizeHorizontal

Make the field resizeable in horizontal direction.

This works only when multiline is true.

typerequireddefault
boolfalsetrue

resizeVertical

Make the field resizeable in vertical direction.

This works only when multiline is true.

typerequireddefault
boolfalsetrue

Email

Use Email field to accept email addresses

Props

size

Controls size of the field. Accepts xs, s, m, l for extra small, small, medium & large.

typerequireddefault
enumfalse"m"

stretch

Field matches the width of its container.

typerequireddefault
boolfalsefalse

qualifierStart

Icon or text to render as start qualifier. Accepts SVG for icons.

typerequireddefault
unionfalsenull

qualifierEnd

Icon or text to render as end qualifier. Accepts SVG for icons.

typerequireddefault
unionfalsenull

addOnStart

React node to render before the field content.

typerequireddefault
elementfalsenull

addOnEnd

React node to render after the field content.

typerequireddefault
elementfalsenull

swapSlots

Swap the slots for qualifiers and addOns.

typerequireddefault
boolfalsefalse

invalid

Adds error styles to the field if true.

typerequireddefault
boolfalsefalse

disabled

Makes the field disabled if true.

typerequireddefault
boolfalsefalse

Password

Use Password field for securely accepting secrets. It renders a toggle button to show/hide the password. You can override the toggle button using addOnEnd.

Props

size

Controls size of the field. Accepts xs, s, m, l for extra small, small, medium & large.

typerequireddefault
enumfalse"m"

stretch

Field matches the width of its container.

typerequireddefault
boolfalsefalse

qualifierStart

Icon or text to render as start qualifier. Accepts SVG for icons.

typerequireddefault
unionfalsenull

qualifierEnd

Icon or text to render as end qualifier. Accepts SVG for icons.

typerequireddefault
unionfalsenull

addOnStart

React node to render before the field content.

typerequireddefault
elementfalsenull

addOnEnd

React node to render after the field content.

This will override the default show/hide toggle button

typerequireddefault
elementfalsenull

swapSlots

Swap the slots for qualifiers and addOns.

typerequireddefault
boolfalsefalse

invalid

Adds error styles to the field if true.

typerequireddefault
boolfalsefalse

disabled

Makes the field disabled if true.

typerequireddefault
boolfalsefalse

URL

Use URL field to accept or edit a URL

Props

size

Controls size of the field. Accepts xs, s, m, l for extra small, small, medium & large.

typerequireddefault
enumfalse"m"

stretch

Field matches the width of its container.

typerequireddefault
boolfalsefalse

qualifierStart

Icon or text to render as start qualifier. Accepts SVG for icons.

typerequireddefault
unionfalsenull

qualifierEnd

Icon or text to render as end qualifier. Accepts SVG for icons.

typerequireddefault
unionfalsenull

addOnStart

React node to render before the field content.

typerequireddefault
elementfalsenull

addOnEnd

React node to render after the field content.

typerequireddefault
elementfalsenull

swapSlots

Swap the slots for qualifiers and addOns.

typerequireddefault
boolfalsefalse

invalid

Adds error styles to the field if true.

typerequireddefault
boolfalsefalse

disabled

Makes the field disabled if true.

typerequireddefault
boolfalsefalse

Numeric

Use Numeric field to accept numbers. The Numeric field renders an input element with type="text" & inputmode="numeric" to allow browsers to display an appropriate virtual keyboard. It also helps to eliminate some usability issues that comes with input with type="number".

For use cases of strictly speaking numbers like age, use Textfield component with type="number".

Props

size

Controls size of the field. Accepts xs, s, m, l for extra small, small, medium & large.

typerequireddefault
enumfalse"m"

stretch

Field matches the width of its container.

typerequireddefault
boolfalsefalse

qualifierStart

Icon or text to render as start qualifier. Accepts SVG for icons.

typerequireddefault
unionfalsenull

qualifierEnd

Icon or text to render as end qualifier. Accepts SVG for icons.

typerequireddefault
unionfalsenull

addOnStart

React node to render before the field content.

typerequireddefault
elementfalsenull

addOnEnd

React node to render after the field content.

typerequireddefault
elementfalsenull

swapSlots

Swap the slots for qualifiers and addOns.

typerequireddefault
boolfalsefalse

invalid

Adds error styles to the field if true.

typerequireddefault
boolfalsefalse

disabled

Makes the field disabled if true.

typerequireddefault
boolfalsefalse

Use Search field to accept search keywords.

Props

size

Controls size of the field. Accepts xs, s, m, l for extra small, small, medium & large.

typerequireddefault
enumfalse"m"

stretch

Field matches the width of its container.

typerequireddefault
boolfalsefalse

qualifierStart

Icon or text to render as start qualifier. Accepts SVG for icons.

typerequireddefault
unionfalsenull

qualifierEnd

Icon or text to render as end qualifier. Accepts SVG for icons.

typerequireddefault
unionfalsenull

addOnStart

React node to render before the field content.

typerequireddefault
elementfalsenull

addOnEnd

React node to render after the field content.

typerequireddefault
elementfalsenull

swapSlots

Swap the slots for qualifiers and addOns.

typerequireddefault
boolfalsefalse

invalid

Adds error styles to the field if true.

typerequireddefault
boolfalsefalse

disabled

Makes the field disabled if true.

typerequireddefault
boolfalsefalse

Datefield

Use Datefield to work with dates.

Props

size

Controls size of the field. Accepts xs, s, m, l for extra small, small, medium & large.

typerequireddefault
enumfalse"m"

stretch

Field matches the width of its container.

typerequireddefault
boolfalsefalse

qualifierStart

Icon or text to render as start qualifier. Accepts SVG for icons.

typerequireddefault
unionfalsenull

qualifierEnd

Icon or text to render as end qualifier. Accepts SVG for icons.

typerequireddefault
unionfalsenull

addOnStart

React node to render before the field content.

typerequireddefault
elementfalsenull

addOnEnd

React node to render after the field content.

typerequireddefault
elementfalsenull

swapSlots

Swap the slots for qualifiers and addOns.

typerequireddefault
boolfalsefalse

invalid

Adds error styles to the field if true.

typerequireddefault
boolfalsefalse

disabled

Makes the field disabled if true.

typerequireddefault
boolfalsefalse

Timefield

Use Timefield to select a time.

Props

size

Controls size of the field. Accepts xs, s, m, l for extra small, small, medium & large.

typerequireddefault
enumfalse"m"

stretch

Field matches the width of its container.

typerequireddefault
boolfalsefalse

qualifierStart

Icon or text to render as start qualifier. Accepts SVG for icons.

typerequireddefault
unionfalsenull

qualifierEnd

Icon or text to render as end qualifier. Accepts SVG for icons.

typerequireddefault
unionfalsenull

addOnStart

React node to render before the field content.

typerequireddefault
elementfalsenull

addOnEnd

React node to render after the field content.

typerequireddefault
elementfalsenull

swapSlots

Swap the slots for qualifiers and addOns.

typerequireddefault
boolfalsefalse

invalid

Adds error styles to the field if true.

typerequireddefault
boolfalsefalse

disabled

Makes the field disabled if true.

typerequireddefault
boolfalsefalse

Pinfield

Use Pinfield for verification purposes, it can have numbers, alphabets, or characters as input but comes with the numeric keyboard. Pinfield allows 4 characters as input by default, but you can customize it using length prop as per your use case. Pinfield applies a placeholder value by default but you can override it using placeholder prop.

Props

size

Controls size of the field. Accepts xs, s, m, l for extra small, small, medium & large.

typerequireddefault
enumfalse"m"

stretch

Field matches the width of its container.

typerequireddefault
boolfalsefalse

qualifierStart

Icon to render as start qualifier, accepts SVG.

typerequireddefault
elementfalsenull

qualifierEnd

Icon to render as end qualifier, accepts SVG.

typerequireddefault
elementfalsenull

addOnStart

React node to render before the field content.

typerequireddefault
elementfalsenull

addOnEnd

React node to render after the field content.

typerequireddefault
elementfalsenull

swapSlots

Swap the slots for qualifiers and addOns.

typerequireddefault
boolfalseN/A

invalid

Adds error styles to the field if true.

typerequireddefault
boolfalsefalse

disabled

Makes the field disabled if true.

typerequireddefault
boolfalsefalse

length

Number of characters allowed in the field.

typerequireddefault
numberfalse4

alignCenter

Aligns the input content in center.

typerequireddefault
boolfalsefalse

placeholder

Placeholder value to show in the field.

typerequireddefault
stringfalse""

InputWrapper

Use InputWrapper to wrap various building blocks.

Props

children

React node

typerequireddefault
nodefalsenull

size

Controls size of the input. Accepts xs, s, m, l for extra small, small, medium & large.

typerequireddefault
enumfalse"m"

stretch

Input matches the width of its container.

typerequireddefault
boolfalsefalse

invalid

Adds error styles to the field if true.

typerequireddefault
boolfalsefalse

shrink

Removes the spacing between its children.

typerequireddefault
boolfalsefalse

bezel

Adds padding in the container.

typerequireddefault
boolfalsetrue

disabled

Makes field non interactive.

typerequireddefault
boolfalsefalse

Input

Input renders a native input field.

Props

type

Type of input control.

typerequireddefault
stringfalse"text"

size

Controls size of the input. Accepts xs, s, m, l for extra small, small, medium & large.

typerequireddefault
enumfalse"m"

stretch

Input matches the width of its container.

typerequireddefault
boolfalsefalse

invalid

Adds error styles to the field if true.

typerequireddefault
boolfalsefalse

disabled

Makes field non interactive.

typerequireddefault
boolfalsefalse

enclosed

Add border on each sides

typerequireddefault
boolfalsetrue

bezel

Adds padding on each sides

typerequireddefault
boolfalsetrue

InputQualifier

InputQualifier provides a hint about the expected content of the field. They can be SVG icons or words.

Props

children

React node for the main content.

typerequireddefault
nodetrueN/A

size

Controls size of the field. Accepts xs, s, m, l for extra small, small, medium & large.

typerequireddefault
enumfalse"m"

InputAddOn

InputAddOn can be used to add extra React nodes along with Input.

Props

children

React node for the main content.

typerequireddefault
nodetrueN/A

useInput

React hook which returns prop getter functions. Use these functions to generate prop objects for different building blocks.

Arguments

useInput accepts the following props:

  1. size
  2. invalid
  3. disabled
  4. stretch
  5. qualifierStart
  6. qualifierEnd
  7. addOnStart
  8. addOnEnd
  9. swapSlots

Getter functions

getWrapperProps()

Use this function to create props for InputWrapper component.

const { getWrapperProps } = useInput({ size: "m", invalid: false, disabled: false, stretch: true });

<InputWrapper {...getWrapperProps()}>
  /* contents of InputWrapper */
</InputWrapper>

getInputProps()

Use this function to create props for Input component.

const { getInputProps } = useInput({ size: "m", invalid: false, disabled: false, stretch: true });

<InputWrapper>
  <Input
    {...getInputProps()}
  />
  /* rest of the contents */
</InputWrapper>

getQualifierProps()

Use this function to create props for InputQualifier component.

const { getQualifierProps } = useInput({ size: "m" });

<InputWrapper>
  <InputQualifier
    {...getQualifierProps()}
  />
  /* rest of the contents */
</InputWrapper>

getStartSlots()

Use this function to handle qualifiers and addOns attached to the start of the field. This function comes in handy when you create a new input field using the unit components like InputWrapper and Input as it handles the order of the elements attached. getStartSlots() accepts two arguments:

  • qualifierProps: a props object to pass on to the qualifier
  • addOnProps: a props object to pass on to the addOn
const { getStartSlots } = useInput({
    qualifierStart: <IconPlaceholder />,
    addOnStart: <Button>action</Button>,
    swapSlots: false,
  });

  <InputWrapper>
    <>{getStartSlots({ qualifierProps: { ariaLabel: "qualifier" } })}</>
    <Input />
  </InputWrapper>

getEndSlots()

Use this function to handle qualifiers and addOns attached to the end of the field. This function comes in handy when you create a new input field using the unit components like InputWrapper and Input as it handles the order of the elements attached. getEndSlots() accepts two arguments:

  • qualifierProps: a props object to pass on to the qualifier
  • addOnProps: a props object to pass on to the addOn
const { getEndSlots } = useInput({
    qualifierStart: <IconPlaceholder />,
    addOnStart: <Button>action</Button>,
    swapSlots: true,
  });

  <InputWrapper>
    <Input />
    <>{getEndSlots({ qualifierProps: { ariaLabel: "qualifier" } })}</>
  </InputWrapper>

The above code snipped will render the button (addOn) followed by the icon (qualifier) as swapSlots is true.

Keywords

FAQs

Package last updated on 10 Jul 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc