1.1.0 • Published 3 years ago

@pmwcs/textfield v1.1.0

Weekly downloads
5
License
MIT
Repository
github
Last release
3 years ago

Text Fields

Text fields allow users to input, edit, and select text.

  • Module @pmwcs/textfield
  • Import styles:
    • Using CSS Loader
      • import '@pmwcs/textfield/styles';
    • Or include stylesheets
      • '@material/textfield/dist/mdc.textfield.css'
      • '@material/floating-label/dist/mdc.floating-label.css'
      • '@material/notched-outline/dist/mdc.notched-outline.css'
      • '@material/line-ripple/dist/mdc.line-ripple.css'
      • '@material/ripple/dist/mdc.ripple.css'
      • '@pmwcs/icon/icon.css'
  • MDC Docs: https://material.io/develop/web/components/input-controls/text-field/

TextField Variants

<TextField label="standard..." />
<TextField outlined label="outlined..." />
<TextField fullwidth placeholder="fullWidth..." />
<TextField placeholder="No label" />
<>
  {/* Leading and trailing icons can be used.*/}
  <TextField icon="search" trailingIcon="close" label="icon..." />
  {/* If you need full control over the icon, you can pass the icon as options with your own props. Dont forget the TabIndex to make it clickable*/}
  <TextField
    label="trailingIcon..."
    trailingIcon={{
      icon: 'close',
      tabIndex: 0,
      onClick: () => console.log('Clear')
    }}
  />
</>

Textareas

You can make the TextField a textarea. Make sure to include outlined for proper styling You can optionally make help text always visible by passing an object as props with persistent set to true. Textareas can also have an optional character counter which will work with the maxLength property.

<TextField
  textarea
  outlined
  fullwidth
  label="textarea..."
  rows={8}
  maxLength={20}
  characterCount
  helpText={{
    persistent: true,
    validationMsg: true,
    children: 'The field is required'
  }}
/>

Validation

<TextField disabled label="Disabled..." />
<TextField required label="Required..." value="" />
<TextField
  invalid
  label="Invalid..."
  value="#@!$"
  onChange={() => {}}
/>
<TextField label="Validate Pattern" pattern="[A-Za-z]{3}" />

HTML Input Types

A preview of how material-components-web handles styling input types for your browser.

<>
  <TextField label="text" type="text" />
  <TextField label="color" type="color" style={{ width: '6rem' }} />
  <TextField label="date" type="date" />
  <TextField label="datetime-local" type="datetime-local" />
  <TextField label="month" type="month" />
  <TextField label="range" type="range" />
  <TextField label="time" type="time" />
  <TextField label="week" type="week" />
</>

TextField

A TextField component for accepting text input from a user.

Props

NameTypeDescription
align"start" \| "end"How to align the text inside the TextField. Defaults to 'start'.
characterCountundefined \| false \| trueShows the character count, must be used in conjunction with maxLength.
disabledundefined \| false \| trueMakes the Textfield disabled.
floatLabelundefined \| false \| trueThe label floats automatically based on value, but you can use this prop for manual control.
foundationRefReact.Ref<MDCTextFieldFoundation \| null>Advanced: A reference to the MDCFoundation.
fullwidthundefined \| false \| trueMakes the TextField fullwidth.
helpTextReact.ReactNode \| TextFieldHelperTextPropsAdds help text to the field
iconPMWCS.IconPropTAdd a leading icon.
inputRefReact.Ref<HTMLInputElement \| HTMLTextAreaElement \| null>A reference to the native input or textarea.
invalidundefined \| false \| trueMakes the TextField visually invalid. This is sometimes automatically applied in cases where required or pattern is used.
labelReact.ReactNodeA label for the input.
outlinedundefined \| false \| trueOutline the TextField.
requiredundefined \| false \| trueMakes the Textfield required.
rippleRipplePropTAdds a ripple effect to the component
rootPropsObjectBy default, props spread to the input. These props are for the component's root container.
textareaundefined \| false \| trueMakes a multiline TextField.
trailingIconPMWCS.IconPropTAdd a trailing icon.
typeundefined \| stringThe type of input field to render, search, number, etc
valuestring \| numberSets the value for controlled TextFields.