@anypoint-web-components/anypoint-input

中文文档
0.2.30发布于 2023/06/17Apache-2.0ESM + CJS近 30 天下载 3408

Accessible text input styled for Anypoint platform

原始使用文档
文档目录12 节
  1. Styling options
  2. Usage
  3. Installation
  4. In an HTML file
  5. Prefixes and suffixes
  6. Validation
  7. Assistive text
  8. Form-associated custom elements
  9. Accessibility
  10. Development
  11. Running the demo locally
  12. Running the tests

Deprecated

This component has been moved to anypoint-web-components/awc.


This component is based on Material Design text field and adjusted for Anypoint platform components.

Anypoint web components are set of components that allows to build Anypoint enabled UI in open source projects.

Text field allows the user to enter a value into the UI. It can appear in a form or a dialog.

Published on NPM

tests

Styling options

The element has three built-in themes:

  • Material Design - Filled
  • Material Design - Outlined
  • Anypoint Design - Compatibility

By default the input renders filled input.

Filled text input

Outlined style is rendered when outlined property is set.

Outlined text input

Anypoint ready styles are rendered when compatibility property is set.

Anypoint text input

OSS application should not use Anypoint based styling as it's protected by MuleSoft copyrights. This property is reserved for OSS applications embedded in the Anypoint platform.

Usage

Installation

npm install --save @anypoint-web-components/anypoint-input

In an HTML file

<html>
  <head>
    <script type="module">
      import '@anypoint-web-components/anypoint-input/anypoint-input.js';
    </script>
  </head>
  <body>
    <anypoint-input value="My awesome API">
      <label slot="label">API title</label>
    </anypoint-input>
  </body>
</html>

Prefixes and suffixes

The element allows to render a widget before or after the text input but inside the input's border.

When it make sense a prefix or suffix can be used to suggest right input. Fox example in cash amount field input a prefix could be $ sign which suggest the value is interpreted in US dollars.

<anypoint-input name="ex1">
  <label slot="label">Amount to transfer</label>
  <span slot="prefix" aria-label="Value in US dollars">$</span>
</anypoint-input>

Similarly suffix can provide additional information about the format of input. For the same cash amount input suffix could render .00 to suggest that the input is an integer.

<anypoint-input name="ex2">
  <label slot="label">Amount to transfer</label>
  <span slot="suffix" aria-label="Use integers">.00</span>
</anypoint-input>

Suffixes can also be active widget. It can be an icon button that toggles visibility of the password. Just remember that adding interactive suffixes is not a common design pattern and your suffix has to have clear meaning to the user.

<anypoint-input type="password" name="ex3">
  <label slot="label">Password</label>
  <anypoint-button slot="suffix" aria-label="Activate the button to show the password" onclick="this.parentNode.type='text'">Show</anypoint-button>
</anypoint-input>
<anypoint-input type="email" name="ex4">
  <label slot="label">Email</label>
  <div slot="suffix">@mulesoft.com</div>
</anypoint-input>

Validation

The element supports all input's properties that helps with validation, like required, min, max, pattern, and so on. It also supports autoValidate property that validates the input when value changes.

<anypoint-input required autovalidate pattern="[a-z]*" invalidMessage="Only letters allowed">
  <label slot="label">Enter text</label>
</anypoint-input>

When invalid it renders error message defined as invalidmessage property.

The element also has preventInvalidInput and allowedPattern properties that, when both set, cancels any input that does not matches allowedPattern. Remember to use this properties carefully as this is very invasive behavior.

<anypoint-input allowedPattern="[a-z]*" preventInvalidInput>
  <label slot="label">Enter text</label>
</anypoint-input>

Assistive text

Assistive text allows the user to better understand what kind of input is required. It can be an info message or invalid message when invalid input has been detected.

Info message

Info message provides the user with additional description for the field. It should be used when the label can be confusing or to ensure the user about the reason of collecting the input.

<anypoint-input infoMessage="Used to confirm your order." type="email" name="ex5">
  <label slot="label">Email</label>
</anypoint-input>

Filled text input

Do not try to put too detailed information. The user should be able to scan the message in a fraction of a second. Treat it as an additional text for the label.

Invalid message

Error message should help the user recover from the error state. Use clear message with simple instructions of how to fix the problem, for example Only letters are allowed.

<anypoint-input invalidmessage="Only letters are allowed" type="text" name="ex6" invalid>
  <label slot="label">Username</label>
</anypoint-input>

Filled text input

Form-associated custom elements

The form-associated custom elements allows to associate a custom element with a <form> element. Original custom elements spec does not allow this. The form-associated custom elements may not be supported in some browsers so custom form elements may be required to be used (for example iron-form).

The element supports this API in browser that has this API implemented. This means that the element behaves like a regular <input> element when inserted into the form element.

Accessibility

The element is a11y ready. To comply with Web Content Accessibility Guidelines (WCAG) add name attribute to the input and add the label as a child.

When using prefixes or suffixes describe the meaning to screen reader by using aria-label property. Note, by setting aria-label, the screen reader will skip reading text value of the element.

<anypoint-input name="amount-usd">
  <label slot="label">Amount to transfer</label>
  <span slot="prefix" aria-label="Value in US dollars">$</span>
</anypoint-input>

Development

git clone https://github.com/anypoint-web-components/anypoint-input
cd anypoint-input
npm install

Running the demo locally

npm start

Running the tests

npm test

版本与发布记录

@anypoint-web-components/anypoint-input 共发布 38 个版本,首次发布于 2019/08/09;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.2.30latest2023/06/173 年前
0.2.292022/08/104 年前
0.2.282021/11/204 年前
0.2.272021/08/185 年前
0.2.262021/07/035 年前
0.2.252021/05/315 年前

关于 @anypoint-web-components/anypoint-input 的常见问题

@anypoint-web-components/anypoint-input 是什么?

Accessible text input styled for Anypoint platform

如何安装 @anypoint-web-components/anypoint-input?

在项目目录运行 npm install @anypoint-web-components/anypoint-input,或使用 pnpm add / yarn add / bun add。

@anypoint-web-components/anypoint-input 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/anypoint-web-components__anypoint-input 是否存在。

@anypoint-web-components/anypoint-input 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@anypoint-web-components/anypoint-input 可以商用吗?

@anypoint-web-components/anypoint-input 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@anypoint-web-components/anypoint-input 的下载量有多少?

根据 npm 官方下载统计,@anypoint-web-components/anypoint-input 在 2026/10/06 – 2026/11/04 这 30 天内被下载 3,408 次,日均约 114 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@anypoint-web-components/anypoint-input 还在维护吗?

最新版本 0.2.30 发布于 2023/06/17(3 年前);至今共发布 38 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/08/09。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。