first commit

This commit is contained in:
lingxiao865
2026-02-10 08:05:03 +08:00
commit c5af079d8c
1094 changed files with 97530 additions and 0 deletions

View File

@@ -0,0 +1,121 @@
:: BASE_DOC ::
## API
### Input Props
name | type | default | description | required
-- | -- | -- | -- | --
custom-style | Object | - | CSS(Cascading Style Sheets) | N
adjust-position | Boolean | true | \- | N
align | String | left | text align type。options: left/center/right | N
allow-input-over-max | Boolean | false | allow to continue input on value length is over `maxlength` or `maxcharacter` | N
always-embed | Boolean | false | \- | N
auto-focus | Boolean | false | \- | N
borderless | Boolean | false | input without border | N
clear-trigger | String | always | show clear icon, clicked to clear input value。options: always / focus | N
clearable | Boolean / Object | false | show clear icon, clicked to clear input value | N
confirm-hold | Boolean | false | \- | N
confirm-type | String | done | options: send/search/next/go/done | N
cursor | Number | -1 | \- | N
cursor-color | String | #0052d9 | \- | N
cursor-spacing | Number | 0 | \- | N
disabled | Boolean | undefined | make input to be disabled | N
error-message | String | - | `deprecated` | N
focus | Boolean | false | \- | N
format | Function | - | input value formatter, `type=number` does not work. if you need to format number, `InputNumber` Component might be better。Typescript`InputFormatType` `type InputFormatType = (value: InputValue) => string`。[see more ts definition](https://github.com/Tencent/tdesign-miniprogram/tree/develop/packages/uniapp-components/input/type.ts) | N
hold-keyboard | Boolean | false | \- | N
label | String | - | text on the left of input | N
layout | String | horizontal | options: vertical/horizontal | N
maxcharacter | Number | - | \- | N
maxlength | Number | -1 | \- | N
password | Boolean | false | `deprecated` | N
placeholder | String | undefined | \- | N
placeholder-class | String | input-placeholder | \- | N
placeholder-style | String | - | \- | N
prefix-icon | String / Object | - | \- | N
readonly | Boolean | undefined | \- | N
safe-password-cert-path | String | - | \- | N
safe-password-custom-hash | String | - | \- | N
safe-password-length | Number | - | \- | N
safe-password-nonce | String | - | \- | N
safe-password-salt | String | - | \- | N
safe-password-time-stamp | Number | - | \- | N
selection-end | Number | -1 | \- | N
selection-start | Number | -1 | \- | N
size | String | medium | `deprecated`。options: small/medium。Typescript`'medium' \| 'small'` | N
status | String | default | options: default/success/warning/error | N
suffix | String | - | suffix content before suffixIcon | N
suffix-icon | String / Object | - | \- | N
tips | String | - | tips on the bottom of input, different `status` can make tips to be different color | N
type | String | text | options: text/number/idcard/digit/safe-password/password/nickname | N
value | String / Number | - | input value。Typescript`InputValue` `type InputValue = string \| number`。[see more ts definition](https://github.com/Tencent/tdesign-miniprogram/tree/develop/packages/uniapp-components/input/type.ts) | N
default-value | String / Number | - | input value。uncontrolled property。Typescript`InputValue` `type InputValue = string \| number`。[see more ts definition](https://github.com/Tencent/tdesign-miniprogram/tree/develop/packages/uniapp-components/input/type.ts) | N
### Input Events
name | params | description
-- | -- | --
blur | `(context: { value: InputValue })` | \-
change | `(context: { value: InputValue, cursor: number, keyCode: number })` | \-
clear | \- | \-
click | `(context: { trigger: InputTrigger })` | [see more ts definition](https://github.com/Tencent/tdesign-miniprogram/tree/develop/packages/uniapp-components/input/type.ts)。<br/>`type InputTrigger = 'suffix' \| 'suffix-icon'`<br/>
enter | `(context: { value: InputValue })` | \-
focus | `(context: { value: InputValue })` | \-
keyboardheightchange | `(context: { height: number, duration: number })` | \-
nicknamereview | `(context: { pass: boolean, timeout: boolean })` | \-
validate | `(context: { error?: 'exceed-maximum' \| 'below-minimum' })` | trigger on text length being over max length or max character
### Input Slots
name | Description
-- | --
extra | \-
label | text on the left of input
prefix-icon | \-
suffix | suffix content before suffixIcon
suffix-icon | \-
tips | \-
### Input External Classes
className | Description
-- | --
t-class | \-
t-class-clearable | \-
t-class-input | \-
t-class-label | \-
t-class-prefix-icon | \-
t-class-suffix | \-
t-class-suffix-icon | \-
t-class-tips | \-
### CSS Variables
The component provides the following CSS variables, which can be used to customize styles.
Name | Default Value | Description
-- | -- | --
--td-input-align-items | center | -
--td-input-bg-color | @bg-color-container | -
--td-input-border-color | @component-stroke | -
--td-input-border-left-space | 32rpx | -
--td-input-border-right-space | 0 | -
--td-input-default-text-color | @text-color-primary | -
--td-input-default-tips-color | @text-color-placeholder | -
--td-input-disabled-text-color | @text-color-disabled | -
--td-input-error-text-color | @error-color | -
--td-input-error-tips-color | @error-color | -
--td-input-label-max-width | 5em | -
--td-input-label-min-width | 2em | -
--td-input-label-text-color | @text-color-primary | -
--td-input-label-text-font | @font-body-large | -
--td-input-placeholder-text-color | @text-color-placeholder | -
--td-input-placeholder-text-font | @font-body-large | -
--td-input-prefix-icon-color | @text-color-primary | -
--td-input-success-text-color | @success-color | -
--td-input-success-tips-color | @success-color | -
--td-input-suffix-icon-color | @text-color-placeholder | -
--td-input-suffix-text-color | @text-color-primary | -
--td-input-vertical-padding | 32rpx | -
--td-input-warning-text-color | @warning-color | -
--td-input-warning-tips-color | @warning-color | -

View File

@@ -0,0 +1,190 @@
---
title: Input 输入框
description: 用于单行文本信息输入。
spline: form
isComponent: true
---
## 引入
可在 `main.ts` 或在需要使用的页面或组件中引入。
```js
import TInput from '@tdesign/uniapp/input/input.vue';
```
### 01 组件类型
基础输入框
{{ base }}
带字数限制输入框
{{ maxlength }}
带操作输入框
{{ suffix }}
带图标输入框
{{ prefix }}
特定类型输入框
{{ special }}
### 02 组件状态
输入框状态
{{ status }}
信息超长状态
{{ label }}
### 03 组件样式
内容位置
{{ align }}
竖排样式
{{ layout }}
非通栏样式
{{ banner }}
标签外置样式
{{ bordered }}
自定义样式文本框
{{ custom }}
## API
### Input Props
名称 | 类型 | 默认值 | 描述 | 必传
-- | -- | -- | -- | --
custom-style | Object | - | 自定义样式 | N
adjust-position | Boolean | true | 键盘弹起时,是否自动上推页面 | N
align | String | left | 文本内容位置,居左/居中/居右。可选项left/center/right | N
allow-input-over-max | Boolean | false | 超出 `maxlength``maxcharacter` 之后是否允许继续输入 | N
always-embed | Boolean | false | 强制 input 处于同层状态,默认 focus 时 input 会切到非同层状态 (仅在 iOS 下生效) | N
auto-focus | Boolean | false | (即将废弃,请直接使用 focus )自动聚焦,拉起键盘 | N
borderless | Boolean | false | 是否开启无边框模式 | N
clear-trigger | String | always | 清空图标触发方式仅在输入框有值时有效。可选项always / focus | N
clearable | Boolean / Object | false | 是否可清空,默认不启动。值为 `true` 表示使用默认清空按钮,值为 `Object` 表示透传至 `icon` | N
confirm-hold | Boolean | false | 点击键盘右下角按钮时是否保持键盘不收起 | N
confirm-type | String | done | 设置键盘右下角按钮的文字仅在type='text'时生效。<br />具体释义:<br />`send` 右下角按钮为“发送”;<br />`search` 右下角按钮为“搜索”;<br />`next` 右下角按钮为“下一个”;<br />`go` 右下角按钮为“前往”;<br />`done` 右下角按钮为“完成”。<br />[小程序官方文档](https://developers.weixin.qq.com/miniprogram/dev/component/input.html)。可选项send/search/next/go/done | N
cursor | Number | -1 | 指定 focus 时的光标位置 | N
cursor-color | String | #0052d9 | 光标颜色。iOS 下的格式为十六进制颜色值 #000000,安卓下的只支持 default 和 greenSkyline 下无限制 | N
cursor-spacing | Number | 0 | 指定光标与键盘的距离,取 input 距离底部的距离和 cursor-spacing 指定的距离的最小值作为光标与键盘的距离 | N
disabled | Boolean | undefined | 是否禁用输入框 | N
error-message | String | - | 已废弃。错误提示文本,值为空不显示(废弃属性,如果需要,请更为使用 status 和 tips | N
focus | Boolean | false | 获取焦点 | N
format | Function | - | 指定输入框展示值的格式。TS 类型:`InputFormatType` `type InputFormatType = (value: InputValue) => string`。[详细类型定义](https://github.com/Tencent/tdesign-miniprogram/tree/develop/packages/uniapp-components/input/type.ts) | N
hold-keyboard | Boolean | false | focus时点击页面的时候不收起键盘 | N
label | String | - | 左侧文本 | N
layout | String | horizontal | 标题输入框布局方式。可选项vertical/horizontal | N
maxcharacter | Number | - | 用户最多可以输入的字符个数,一个中文汉字表示两个字符长度。`maxcharacter``maxlength` 二选一使用 | N
maxlength | Number | -1 | 用户最多可以输入的文本长度,一个中文等于一个计数长度。默认为 -1不限制输入长度。`maxcharacter``maxlength` 二选一使用 | N
password | Boolean | false | 已废弃。是否是密码类型(已废弃,请更为使用 type 指定输入框类型) | N
placeholder | String | undefined | 占位符 | N
placeholder-class | String | input-placeholder | 指定 placeholder 的样式类 | N
placeholder-style | String | - | 指定 placeholder 的样式 | N
prefix-icon | String / Object | - | 组件前置图标。值为字符串表示图标名称,值为 `Object` 类型,表示透传至 `icon` | N
readonly | Boolean | undefined | 只读状态 | N
safe-password-cert-path | String | - | 安全键盘加密公钥的路径,只支持包内路径 | N
safe-password-custom-hash | String | - | 安全键盘计算 hash 的算法表达式,如 `md5(sha1('foo' + sha256(sm3(password + 'bar'))))` | N
safe-password-length | Number | - | 安全键盘输入密码长度 | N
safe-password-nonce | String | - | 安全键盘加密盐值 | N
safe-password-salt | String | - | 安全键盘计算 hash 盐值若指定custom-hash 则无效 | N
safe-password-time-stamp | Number | - | 安全键盘加密时间戳 | N
selection-end | Number | -1 | 光标结束位置,自动聚集时有效,需与 selection-start 搭配使用 | N
selection-start | Number | -1 | 光标起始位置,自动聚集时有效,需与 selection-end 搭配使用 | N
size | String | medium | 已废弃。输入框尺寸。可选项small/medium。TS 类型:`'medium' \| 'small'` | N
status | String | default | 输入框状态。可选项default/success/warning/error | N
suffix | String | - | 后置图标前的后置内容 | N
suffix-icon | String / Object | - | 后置文本内容。值为字符串则表示图标名称,值为 `Object` 类型,表示透传至 `icon` | N
tips | String | - | 输入框下方提示文本,会根据不同的 `status` 呈现不同的样式 | N
type | String | text | 输入框类型。可选项text/number/idcard/digit/safe-password/password/nickname | N
value | String / Number | - | 输入框的值。TS 类型:`InputValue` `type InputValue = string \| number`。[详细类型定义](https://github.com/Tencent/tdesign-miniprogram/tree/develop/packages/uniapp-components/input/type.ts) | N
default-value | String / Number | - | 输入框的值。非受控属性。TS 类型:`InputValue` `type InputValue = string \| number`。[详细类型定义](https://github.com/Tencent/tdesign-miniprogram/tree/develop/packages/uniapp-components/input/type.ts) | N
### Input Events
名称 | 参数 | 描述
-- | -- | --
blur | `(context: { value: InputValue })` | 失去焦点时触发
change | `(context: { value: InputValue, cursor: number, keyCode: number })` | 输入框值发生变化时触发cursor 为光标位置;
clear | \- | 清空按钮点击时触发
click | `(context: { trigger: InputTrigger })` | 点击事件。[详细类型定义](https://github.com/Tencent/tdesign-miniprogram/tree/develop/src/input/type.ts)。。[详细类型定义](https://github.com/Tencent/tdesign-miniprogram/tree/develop/packages/uniapp-components/input/type.ts)。<br/>`type InputTrigger = 'suffix' \| 'suffix-icon'`<br/>
enter | `(context: { value: InputValue })` | 回车键按下时触发
focus | `(context: { value: InputValue })` | 获得焦点时触发
keyboardheightchange | `(context: { height: number, duration: number })` | 键盘高度发生变化的时候触发此事件
nicknamereview | `(context: { pass: boolean, timeout: boolean })` | 用户昵称审核完毕后触发,仅在 type 为 "nickname" 时有效
validate | `(context: { error?: 'exceed-maximum' \| 'below-minimum' })` | 字数超出限制时触发
### Input Slots
名称 | 描述
-- | --
extra | 右侧额外内容
label | 自定义 `label` 显示内容
prefix-icon | 组件前置图标
suffix | 自定义 `suffix` 显示内容
suffix-icon | 后置文本图标
tips | 输入框下方提示内容
### Input External Classes
类名 | 描述
-- | --
t-class | 根节点样式类
t-class-clearable | 清空按钮样式类
t-class-input | 输入框样式类
t-class-label | 标签样式类
t-class-prefix-icon | 前置图标样式类
t-class-suffix | 后置样式类
t-class-suffix-icon | 后置图标样式类
t-class-tips | 提示样式类
### CSS Variables
组件提供了下列 CSS 变量,可用于自定义样式。
名称 | 默认值 | 描述
-- | -- | --
--td-input-align-items | center | -
--td-input-bg-color | @bg-color-container | -
--td-input-border-color | @component-stroke | -
--td-input-border-left-space | 32rpx | -
--td-input-border-right-space | 0 | -
--td-input-default-text-color | @text-color-primary | -
--td-input-default-tips-color | @text-color-placeholder | -
--td-input-disabled-text-color | @text-color-disabled | -
--td-input-error-text-color | @error-color | -
--td-input-error-tips-color | @error-color | -
--td-input-label-max-width | 5em | -
--td-input-label-min-width | 2em | -
--td-input-label-text-color | @text-color-primary | -
--td-input-label-text-font | @font-body-large | -
--td-input-placeholder-text-color | @text-color-placeholder | -
--td-input-placeholder-text-font | @font-body-large | -
--td-input-prefix-icon-color | @text-color-primary | -
--td-input-success-text-color | @success-color | -
--td-input-success-tips-color | @success-color | -
--td-input-suffix-icon-color | @text-color-placeholder | -
--td-input-suffix-text-color | @text-color-primary | -
--td-input-vertical-padding | 32rpx | -
--td-input-warning-text-color | @warning-color | -
--td-input-warning-tips-color | @warning-color | -

View File

@@ -0,0 +1,13 @@
export function getInputClass(classPrefix, suffix, align, disabled) {
const className = [`${classPrefix}__control`];
if (align) {
className.push(`${classPrefix}--${align}`);
}
if (disabled) {
className.push(`${classPrefix}__control--disabled`);
}
return className.join(' ');
}

View File

@@ -0,0 +1,161 @@
.t-input {
background-color: var(--td-input-bg-color, var(--td-bg-color-container, var(--td-font-white-1, #ffffff)));
display: flex;
align-items: var(--td-input-align-items, center);
flex: 1;
padding: var(--td-input-vertical-padding, 32rpx);
overflow: hidden;
}
.t-input--border {
position: relative;
}
.t-input--border::after {
content: '';
display: block;
position: absolute;
top: unset;
bottom: 0;
left: unset;
right: unset;
background-color: var(--td-input-border-color, var(--td-component-stroke, var(--td-gray-color-3, #e7e7e7)));
}
.t-input--border::after {
height: 1px;
left: 0;
right: 0;
transform: scaleY(0.5);
}
.t-input--border:after {
left: var(--td-input-border-left-space, 32rpx);
right: var(--td-input-border-right-space, 0);
}
.t-input--layout-vertical {
flex-direction: column;
align-items: start;
}
.t-input__wrap--prefix {
display: flex;
}
.t-input__icon--prefix {
font-size: 48rpx;
color: var(--td-input-prefix-icon-color, var(--td-text-color-primary, var(--td-font-gray-1, rgba(0, 0, 0, 0.9))));
}
.t-input__label:not(:empty) {
min-width: var(--td-input-label-min-width, 2em);
max-width: var(--td-input-label-max-width, 5em);
font: var(--td-input-label-text-font, var(--td-font-body-large, 32rpx / 48rpx var(--td-font-family, PingFang SC, Microsoft YaHei, Arial Regular)));
color: var(--td-input-label-text-color, var(--td-text-color-primary, var(--td-font-gray-1, rgba(0, 0, 0, 0.9))));
margin-right: var(--td-spacer-2, 32rpx);
word-wrap: break-word;
}
.t-input--layout-vertical .t-input__label:not(:empty) {
font-size: var(--td-font-size-base, 28rpx);
padding-bottom: 8rpx;
}
.t-input__icon--prefix:not(:empty) + .t-input__label:not(:empty) {
padding-left: 8rpx;
}
.t-input__label:not(:empty) + .t-input__wrap {
margin-left: var(--td-spacer-2, 32rpx);
}
.t-input__icon--prefix:not(:empty) + .t-input__label:empty {
margin-right: var(--td-spacer-2, 32rpx);
}
.t-input__wrap {
width: 100%;
display: flex;
flex-direction: column;
flex-wrap: wrap;
justify-content: center;
flex-shrink: 1;
flex: 1;
}
.t-input__wrap .t-input__content {
display: flex;
width: 100%;
align-items: center;
font: var(--td-font-body-large, 32rpx / 48rpx var(--td-font-family, PingFang SC, Microsoft YaHei, Arial Regular));
}
.t-input__wrap--clearable-icon,
.t-input__wrap--suffix-icon,
.t-input__wrap--suffix {
flex: 0 0 auto;
padding-left: var(--td-spacer-1, 24rpx);
}
.t-input__wrap--clearable-icon:empty,
.t-input__wrap--suffix-icon:empty,
.t-input__wrap--suffix:empty {
display: none;
}
.t-input__wrap--clearable-icon,
.t-input__wrap--suffix-icon {
font-size: 48rpx;
color: var(--td-input-suffix-icon-color, var(--td-text-color-placeholder, var(--td-font-gray-3, rgba(0, 0, 0, 0.4))));
}
.t-input__wrap--suffix {
font-size: var(--td-font-size-m, 32rpx);
color: var(--td-input-suffix-text-color, var(--td-text-color-primary, var(--td-font-gray-1, rgba(0, 0, 0, 0.9))));
}
.t-input__icon--prefix:empty,
.t-input__tips:empty,
.t-input__wrap--clearable-icon:empty,
.t-input__wrap--suffix-icon:empty,
.t-input__wrap--suffix:empty {
display: none;
}
.t-input__control {
display: block;
box-sizing: border-box;
width: 100%;
min-width: 0;
min-height: 48rpx;
margin: 0;
padding: 0;
color: var(--td-input-default-text-color, var(--td-text-color-primary, var(--td-font-gray-1, rgba(0, 0, 0, 0.9))));
line-height: inherit;
background-color: transparent;
border: 0;
resize: none;
font-size: inherit;
}
.t-input__control--disabled {
color: var(--td-input-disabled-text-color, var(--td-text-color-disabled, var(--td-font-gray-4, rgba(0, 0, 0, 0.26))));
cursor: not-allowed;
opacity: 1;
-webkit-text-fill-color: currentColor;
}
.t-input__control--read-only {
cursor: default;
}
.t-input--left {
text-align: left;
}
.t-input--right {
text-align: right;
}
.t-input--center {
text-align: center;
}
.t-input__placeholder {
color: var(--td-input-placeholder-text-color, var(--td-text-color-placeholder, var(--td-font-gray-3, rgba(0, 0, 0, 0.4))));
font: var(--td-input-placeholder-text-font, var(--td-font-body-large, 32rpx / 48rpx var(--td-font-family, PingFang SC, Microsoft YaHei, Arial Regular)));
}
.t-input__placeholder--disabled {
color: var(--td-input-disabled-text-color, var(--td-text-color-disabled, var(--td-font-gray-4, rgba(0, 0, 0, 0.26))));
}
.t-input__tips {
font: var(--td-font-body-small, 24rpx / 40rpx var(--td-font-family, PingFang SC, Microsoft YaHei, Arial Regular));
padding-top: 8rpx;
}
.t-input--default + .t-input__tips {
color: var(--td-input-default-tips-color, var(--td-text-color-placeholder, var(--td-font-gray-3, rgba(0, 0, 0, 0.4))));
}
.t-input--success + .t-input__tips {
color: var(--td-input-success-tips-color, var(--td-success-color, var(--td-success-color-5, #2ba471)));
}
.t-input--warning + .t-input__tips {
color: var(--td-input-warning-tips-color, var(--td-warning-color, var(--td-warning-color-5, #e37318)));
}
.t-input--error + .t-input__tips {
color: var(--td-input-error-tips-color, var(--td-error-color, var(--td-error-color-6, #d54941)));
}

View File

@@ -0,0 +1,407 @@
<template>
<view
:style="tools._style([customStyle])"
:class="tools.cls(classPrefix, [['border', !borderless], ['readonly', readonly], ['disabled', disabled]])
+ ' ' + classPrefix + '--layout-' + layout + ' ' + tClass"
aria-describedby
>
<view :class="classPrefix + '__wrap--prefix'">
<view :class="classPrefix + '__icon--prefix'">
<slot name="prefix-icon" />
<block
v-if="_prefixIcon"
name="icon"
>
<t-icon
:custom-style="_prefixIcon.style || ''"
:t-class="tClassPrefixIcon"
:prefix="_prefixIcon.prefix"
:name="_prefixIcon.name"
:size="_prefixIcon.size"
:color="_prefixIcon.color"
:aria-hidden="true"
:aria-label="_prefixIcon.ariaLabel"
:aria-role="_prefixIcon.ariaRole"
@click="_prefixIcon.click || ''"
/>
</block>
</view>
<view
:class="classPrefix + '__label ' + tClassLabel"
aria-hidden
>
<slot name="label" />
<block v-if="label">
{{ label }}
</block>
</view>
</view>
<view :class="classPrefix + '__wrap'">
<view
:class="classPrefix + '__content ' + classPrefix + '--' + status"
@click="onClick"
>
<input
:class="getInputClass(classPrefix, suffix, align, disabled) + ' ' + tClassInput"
:maxlength="allowInputOverMax ? -1 : maxlength"
:disabled="disabled || readonly"
:placeholder="placeholder"
:placeholder-style="placeholderStyle"
:placeholder-class="tools.cls(classPrefix + '__placeholder', [['disabled', disabled]]) + ' ' + placeholderClass"
:value="dataValue"
:password="type === 'password'"
:type="type === 'password' ? 'text' : type"
:focus="focus"
:confirm-type="confirmType"
:confirm-hold="confirmHold"
:cursor="cursor"
:cursor-color="cursorColor"
:cursor-spacing="cursorSpacing"
:adjust-position="adjustPosition"
:auto-focus="autoFocus"
:always-embed="alwaysEmbed"
:selection-start="selectionStart"
:selection-end="selectionEnd"
:hold-keyboard="holdKeyboard"
:safe-password-cert-path="safePasswordCertPath"
:safe-password-length="safePasswordLength"
:safe-password-time-stamp="safePasswordTimeStamp"
:safe-password-nonce="safePasswordNonce"
:safe-password-salt="safePasswordSalt"
:safe-password-custom-hash="safePasswordCustomHash"
aria-role="textbox"
:aria-label="label"
:aria-roledescription="label"
@input="onInput"
@change="onChange"
@focus="onFocus"
@blur="onBlur"
@confirm="onConfirm"
@keyboardheightchange="onKeyboardHeightChange"
@nicknamereview="onNickNameReview"
>
<view
v-if="_clearIcon && dataValue && dataValue.length && showClearIcon"
:class="classPrefix + '__wrap--clearable-icon'"
@click.stop="clearInput"
>
<t-icon
:custom-style="_clearIcon.style || ''"
:t-class="tClassClearable"
:prefix="_clearIcon.prefix"
:name="_clearIcon.name"
:size="_clearIcon.size"
:color="_clearIcon.color"
:aria-hidden="false"
:aria-label="_clearIcon.ariaLabel || '清除'"
:aria-role="_clearIcon.ariaRole || 'button'"
@click="_clearIcon.click || ''"
/>
</view>
<view
:class="classPrefix + '__wrap--suffix ' + tClassSuffix"
@click="onSuffixClick"
>
<text v-if="suffix">
{{ suffix }}
</text>
<slot name="suffix" />
</view>
<view
:class="classPrefix + '__wrap--suffix-icon'"
@click="onSuffixIconClick"
>
<slot name="suffix-icon" />
<block
v-if="_suffixIcon"
name="icon"
>
<t-icon
:custom-style="_suffixIcon.style || ''"
:t-class="tClassSuffixIcon"
:prefix="_suffixIcon.prefix"
:name="_suffixIcon.name"
:size="_suffixIcon.size"
:color="_suffixIcon.color"
:aria-hidden="true"
:aria-label="_suffixIcon.ariaLabel"
:aria-role="_suffixIcon.ariaRole || 'button'"
@click="_suffixIcon.click || ''"
/>
</block>
</view>
</view>
<view
v-if="tips && tips.length > 0"
:class="classPrefix + '__tips ' + classPrefix + '--' + align + ' ' + tClassTips"
>
{{ tips }}
</view>
<slot name="tips" />
</view>
<slot name="extra" />
</view>
</template>
<script>
import TIcon from '../icon/icon';
import { uniComponent } from '../common/src/index';
import { prefix } from '../common/config';
import props from './props';
import { getCharacterLength, calcIcon, coalesce, nextTick } from '../common/utils';
import { isDef } from '../common/validator';
import { getInputClass } from './computed.js';
import tools from '../common/utils.wxs';
import { RELATION_MAP } from '../common/relation/parent-map.js';
const name = `${prefix}-input`;
export default uniComponent({
name,
options: {
styleIsolation: 'shared',
},
inject: {
[RELATION_MAP.FormKey]: {
default: null,
},
},
externalClasses: [
`${prefix}-class`,
`${prefix}-class-prefix-icon`,
`${prefix}-class-label`,
`${prefix}-class-input`,
`${prefix}-class-clearable`,
`${prefix}-class-suffix`,
`${prefix}-class-suffix-icon`,
`${prefix}-class-tips`,
],
components: {
TIcon,
},
props: {
...props,
},
emits: [
'blur',
'change',
'clear',
'click',
'enter',
'focus',
'keyboardheightchange',
'nicknamereview',
'validate',
'update:value',
],
data() {
return {
prefix,
classPrefix: name,
classBasePrefix: prefix,
showClearIcon: true,
tools,
dataValue: coalesce(this.value, this.defaultValue),
// rawValue: '',
// innerMaxLen: -1,
};
},
computed: {
},
watch: {
prefixIcon: {
handler(v) {
this._prefixIcon = calcIcon(v);
},
immediate: true,
},
suffixIcon: {
handler(v) {
this._suffixIcon = calcIcon(v);
},
immediate: true,
},
clearable: {
handler(v) {
this._clearIcon = calcIcon(v, 'close-circle-filled');
},
immediate: true,
},
clearTrigger: 'updateClearIconVisible',
disabled: 'updateClearIconVisible',
readonly: 'updateClearIconVisible',
value: {
handler(v) {
this.dataValue = v;
nextTick().then(() => {
this.dataValue = v;
if (this[RELATION_MAP.FormKey]
&& this[RELATION_MAP.FormKey].onValueChange) {
this[RELATION_MAP.FormKey].onValueChange(v);
}
});
},
},
},
mounted() {
const { value, defaultValue } = this;
this.updateValue(coalesce(value, defaultValue, ''));
this.updateClearIconVisible();
},
methods: {
getInputClass,
updateValue(value) {
// this.rawValue = value;
this.dataValue = value;
const { allowInputOverMax, maxcharacter, maxlength } = this;
if (!allowInputOverMax && maxcharacter && maxcharacter > 0 && !Number.isNaN(maxcharacter)) {
const { length, characters } = getCharacterLength('maxcharacter', value, maxcharacter);
nextTick().then(() => {
this.dataValue = characters;
});
this.count = length;
} else if (!allowInputOverMax && maxlength && maxlength > 0 && !Number.isNaN(maxlength)) {
const { length, characters } = getCharacterLength('maxlength', value, maxlength);
nextTick().then(() => {
this.dataValue = characters;
});
this.count = length;
} else {
nextTick().then(() => {
this.dataValue = value;
});
this.dataValue = value;
this.count = isDef(value) ? String(value).length : 0;
}
// this.updateInnerMaxLen();
},
// updateInnerMaxLen() {
// this.innerMaxLen = this.getInnerMaxLen();
// },
// getInnerMaxLen() {
// const {
// allowInputOverMax,
// maxcharacter,
// maxlength,
// dataValue,
// rawValue,
// count,
// } = this;
// return getInnerMaxLen({
// allowInputOverMax,
// maxcharacter,
// maxlength,
// dataValue,
// rawValue,
// count,
// });
// },
updateClearIconVisible(value = false) {
const { clearTrigger, disabled, readonly } = this;
if (disabled || readonly) {
this.showClearIcon = false;
return;
}
this.showClearIcon = value || clearTrigger === 'always';
},
onInput(e) {
const { value, cursor, keyCode } = e.detail;
this.updateValue(value);
this.emitChange({ value: this.dataValue, cursor, keyCode });
},
onChange(e) {
if (this.type !== 'nickname') return;
const { value } = e.detail;
this.updateValue(value);
this.emitChange({ value: this.dataValue });
},
emitChange(data) {
this.$emit('change', data);
this.$emit('update:value', data.value);
},
onFocus(e) {
this.updateClearIconVisible(true);
this.$emit('focus', e.detail);
},
onBlur(e) {
this.updateClearIconVisible();
if (this[RELATION_MAP.FormKey]
&& this[RELATION_MAP.FormKey].onBlur) {
this[RELATION_MAP.FormKey].onBlur(this.dataValue);
}
// 失焦时处理 format
if (typeof this.format === 'function') {
const v = this.format(e.detail.value);
this.updateValue(v);
this.$emit('blur', { value: this.dataValue, cursor: this.count });
return;
}
this.$emit('blur', e.detail);
},
onConfirm(e) {
this.$emit('enter', e.detail);
},
onSuffixClick() {
this.$emit('click', { trigger: 'suffix' });
},
onSuffixIconClick() {
this.$emit('click', { trigger: 'suffix-icon' });
},
clearInput(e) {
this.$emit('clear', e.detail);
this.dataValue = '';
},
onKeyboardHeightChange(e) {
this.$emit('keyboardheightchange', e.detail);
},
onNickNameReview(e) {
this.$emit('nicknamereview', e.detail);
},
onClick() {
this.$emit('click', { trigger: 'input' });
},
},
});
</script>
<style scoped>
@import './input.css';
/* #ifdef H5 */
.t-input--disabled {
:deep(input) {
pointer-events: none;
}
}
.t-input--readonly {
:deep(input) {
pointer-events: none;
}
}
/* #endif */
</style>

View File

@@ -0,0 +1,247 @@
/* eslint-disable */
/**
* 该文件为脚本自动生成文件,请勿随意修改。如需修改请联系 PMC
* */
import type { TdInputProps } from './type';
export default {
/** 键盘弹起时,是否自动上推页面 */
adjustPosition: {
type: Boolean,
default: true,
},
/** 文本内容位置,居左/居中/居右 */
align: {
type: String,
default: 'left' as TdInputProps['align'],
validator(val: TdInputProps['align']): boolean {
if (!val) return true;
return ['left', 'center', 'right'].includes(val);
},
},
/** 超出 `maxlength` 或 `maxcharacter` 之后是否允许继续输入 */
allowInputOverMax: Boolean,
/** 强制 input 处于同层状态,默认 focus 时 input 会切到非同层状态 (仅在 iOS 下生效) */
alwaysEmbed: Boolean,
/** (即将废弃,请直接使用 focus )自动聚焦,拉起键盘 */
autoFocus: Boolean,
/** 是否开启无边框模式 */
borderless: Boolean,
/** 清空图标触发方式,仅在输入框有值时有效 */
clearTrigger: {
type: String,
default: 'always' as TdInputProps['clearTrigger'],
validator(val: TdInputProps['clearTrigger']): boolean {
if (!val) return true;
return ['always', 'focus'].includes(val);
},
},
/** 是否可清空,默认不启动。值为 `true` 表示使用默认清空按钮,值为 `Object` 表示透传至 `icon` */
clearable: {
type: [Boolean, Object],
default: false as TdInputProps['clearable'],
},
/** 点击键盘右下角按钮时是否保持键盘不收起 */
confirmHold: Boolean,
/** 设置键盘右下角按钮的文字仅在type='text'时生效。<br />具体释义:<br />`send` 右下角按钮为“发送”;<br />`search` 右下角按钮为“搜索”;<br />`next` 右下角按钮为“下一个”;<br />`go` 右下角按钮为“前往”;<br />`done` 右下角按钮为“完成”。<br />[小程序官方文档](https://developers.weixin.qq.com/miniprogram/dev/component/input.html) */
confirmType: {
type: String,
default: 'done' as TdInputProps['confirmType'],
validator(val: TdInputProps['confirmType']): boolean {
if (!val) return true;
return ['send', 'search', 'next', 'go', 'done'].includes(val);
},
},
/** 指定 focus 时的光标位置 */
cursor: {
type: Number,
default: -1,
},
/** 光标颜色。iOS 下的格式为十六进制颜色值 #000000安卓下的只支持 default 和 greenSkyline 下无限制 */
cursorColor: {
type: String,
default: '#0052d9',
},
/** 指定光标与键盘的距离,取 input 距离底部的距离和 cursor-spacing 指定的距离的最小值作为光标与键盘的距离 */
cursorSpacing: {
type: Number,
default: 0,
},
/** 是否禁用输入框 */
disabled: {
type: Boolean,
default: undefined,
},
/** 获取焦点 */
focus: Boolean,
/** 指定输入框展示值的格式 */
format: {
type: Function,
},
/** focus时点击页面的时候不收起键盘 */
holdKeyboard: Boolean,
/** 左侧文本 */
label: {
type: String,
},
/** 标题输入框布局方式 */
layout: {
type: String,
default: 'horizontal' as TdInputProps['layout'],
validator(val: TdInputProps['layout']): boolean {
if (!val) return true;
return ['vertical', 'horizontal'].includes(val);
},
},
/** 用户最多可以输入的字符个数,一个中文汉字表示两个字符长度。`maxcharacter` 和 `maxlength` 二选一使用 */
maxcharacter: {
type: Number,
},
/** 用户最多可以输入的文本长度,一个中文等于一个计数长度。默认为 -1不限制输入长度。`maxcharacter` 和 `maxlength` 二选一使用 */
maxlength: {
type: Number,
default: -1,
},
/** 占位符 */
placeholder: {
type: String,
default: undefined,
},
/** 指定 placeholder 的样式类 */
placeholderClass: {
type: String,
default: 'input-placeholder',
},
/** 指定 placeholder 的样式 */
placeholderStyle: {
type: String,
default: '',
},
/** 组件前置图标。值为字符串表示图标名称,值为 `Object` 类型,表示透传至 `icon` */
prefixIcon: {
type: [String, Object],
},
/** 只读状态 */
readonly: {
type: Boolean,
default: undefined,
},
/** 安全键盘加密公钥的路径,只支持包内路径 */
safePasswordCertPath: {
type: String,
default: '',
},
/** 安全键盘计算 hash 的算法表达式,如 `md5(sha1('foo' + sha256(sm3(password + 'bar'))))` */
safePasswordCustomHash: {
type: String,
default: '',
},
/** 安全键盘输入密码长度 */
safePasswordLength: {
type: Number,
},
/** 安全键盘加密盐值 */
safePasswordNonce: {
type: String,
default: '',
},
/** 安全键盘计算 hash 盐值若指定custom-hash 则无效 */
safePasswordSalt: {
type: String,
default: '',
},
/** 安全键盘加密时间戳 */
safePasswordTimeStamp: {
type: Number,
},
/** 光标结束位置,自动聚集时有效,需与 selection-start 搭配使用 */
selectionEnd: {
type: Number,
default: -1,
},
/** 光标起始位置,自动聚集时有效,需与 selection-end 搭配使用 */
selectionStart: {
type: Number,
default: -1,
},
/** 输入框状态 */
status: {
type: String,
default: 'default' as TdInputProps['status'],
validator(val: TdInputProps['status']): boolean {
if (!val) return true;
return ['default', 'success', 'warning', 'error'].includes(val);
},
},
/** 后置图标前的后置内容 */
suffix: {
type: String,
},
/** 后置文本内容。值为字符串则表示图标名称,值为 `Object` 类型,表示透传至 `icon` */
suffixIcon: {
type: [String, Object],
},
/** 输入框下方提示文本,会根据不同的 `status` 呈现不同的样式 */
tips: {
type: String,
},
/** 输入框类型 */
type: {
type: String,
default: 'text' as TdInputProps['type'],
validator(val: TdInputProps['type']): boolean {
if (!val) return true;
return ['text', 'number', 'idcard', 'digit', 'safe-password', 'password', 'nickname'].includes(val);
},
},
/** 输入框的值 */
value: {
type: [String, Number],
},
/** 失去焦点时触发 */
onBlur: {
type: Function,
default: () => ({}),
},
/** 输入框值发生变化时触发cursor 为光标位置; */
onChange: {
type: Function,
default: () => ({}),
},
/** 清空按钮点击时触发 */
onClear: {
type: Function,
default: () => ({}),
},
/** 点击事件。[详细类型定义](https://github.com/Tencent/tdesign-miniprogram/tree/develop/src/input/type.ts)。 */
onClick: {
type: Function,
default: () => ({}),
},
/** 回车键按下时触发 */
onEnter: {
type: Function,
default: () => ({}),
},
/** 获得焦点时触发 */
onFocus: {
type: Function,
default: () => ({}),
},
/** 键盘高度发生变化的时候触发此事件 */
onKeyboardheightchange: {
type: Function,
default: () => ({}),
},
/** 用户昵称审核完毕后触发,仅在 type 为 "nickname" 时有效 */
onNicknamereview: {
type: Function,
default: () => ({}),
},
/** 字数超出限制时触发 */
onValidate: {
type: Function,
default: () => ({}),
},
};

View File

@@ -0,0 +1,237 @@
/* eslint-disable */
/**
* 该文件为脚本自动生成文件,请勿随意修改。如需修改请联系 PMC
* */
export interface TdInputProps {
/**
* 键盘弹起时,是否自动上推页面
* @default true
*/
adjustPosition?: boolean;
/**
* 文本内容位置,居左/居中/居右
* @default left
*/
align?: 'left' | 'center' | 'right';
/**
* 超出 `maxlength` 或 `maxcharacter` 之后是否允许继续输入
* @default false
*/
allowInputOverMax?: boolean;
/**
* 强制 input 处于同层状态,默认 focus 时 input 会切到非同层状态 (仅在 iOS 下生效)
* @default false
*/
alwaysEmbed?: boolean;
/**
* (即将废弃,请直接使用 focus )自动聚焦,拉起键盘
* @default false
*/
autoFocus?: boolean;
/**
* 是否开启无边框模式
* @default false
*/
borderless?: boolean;
/**
* 清空图标触发方式,仅在输入框有值时有效
* @default always
*/
clearTrigger?: 'always' | 'focus';
/**
* 是否可清空,默认不启动。值为 `true` 表示使用默认清空按钮,值为 `Object` 表示透传至 `icon`
* @default false
*/
clearable?: boolean | object;
/**
* 点击键盘右下角按钮时是否保持键盘不收起
* @default false
*/
confirmHold?: boolean;
/**
* 设置键盘右下角按钮的文字仅在type='text'时生效。<br />具体释义:<br />`send` 右下角按钮为“发送”;<br />`search` 右下角按钮为“搜索”;<br />`next` 右下角按钮为“下一个”;<br />`go` 右下角按钮为“前往”;<br />`done` 右下角按钮为“完成”。<br />[小程序官方文档](https://developers.weixin.qq.com/miniprogram/dev/component/input.html)
* @default done
*/
confirmType?: 'send' | 'search' | 'next' | 'go' | 'done';
/**
* 指定 focus 时的光标位置
* @default -1
*/
cursor?: number;
/**
* 光标颜色。iOS 下的格式为十六进制颜色值 #000000安卓下的只支持 default 和 greenSkyline 下无限制
* @default #0052d9
*/
cursorColor?: string;
/**
* 指定光标与键盘的距离,取 input 距离底部的距离和 cursor-spacing 指定的距离的最小值作为光标与键盘的距离
* @default 0
*/
cursorSpacing?: number;
/**
* 是否禁用输入框
*/
disabled?: boolean;
/**
* 获取焦点
* @default false
*/
focus?: boolean;
/**
* 指定输入框展示值的格式
*/
format?: InputFormatType;
/**
* focus时点击页面的时候不收起键盘
* @default false
*/
holdKeyboard?: boolean;
/**
* 左侧文本
*/
label?: string;
/**
* 标题输入框布局方式
* @default horizontal
*/
layout?: 'vertical' | 'horizontal';
/**
* 用户最多可以输入的字符个数,一个中文汉字表示两个字符长度。`maxcharacter` 和 `maxlength` 二选一使用
*/
maxcharacter?: number;
/**
* 用户最多可以输入的文本长度,一个中文等于一个计数长度。默认为 -1不限制输入长度。`maxcharacter` 和 `maxlength` 二选一使用
* @default -1
*/
maxlength?: number;
/**
* 占位符
*/
placeholder?: string;
/**
* 指定 placeholder 的样式类
* @default input-placeholder
*/
placeholderClass?: string;
/**
* 指定 placeholder 的样式
* @default ''
*/
placeholderStyle?: string;
/**
* 组件前置图标。值为字符串表示图标名称,值为 `Object` 类型,表示透传至 `icon`
*/
prefixIcon?: string | object;
/**
* 只读状态
*/
readonly?: boolean;
/**
* 安全键盘加密公钥的路径,只支持包内路径
* @default ''
*/
safePasswordCertPath?: string;
/**
* 安全键盘计算 hash 的算法表达式,如 `md5(sha1('foo' + sha256(sm3(password + 'bar'))))`
* @default ''
*/
safePasswordCustomHash?: string;
/**
* 安全键盘输入密码长度
*/
safePasswordLength?: number;
/**
* 安全键盘加密盐值
* @default ''
*/
safePasswordNonce?: string;
/**
* 安全键盘计算 hash 盐值若指定custom-hash 则无效
* @default ''
*/
safePasswordSalt?: string;
/**
* 安全键盘加密时间戳
*/
safePasswordTimeStamp?: number;
/**
* 光标结束位置,自动聚集时有效,需与 selection-start 搭配使用
* @default -1
*/
selectionEnd?: number;
/**
* 光标起始位置,自动聚集时有效,需与 selection-end 搭配使用
* @default -1
*/
selectionStart?: number;
/**
* 输入框状态
* @default default
*/
status?: 'default' | 'success' | 'warning' | 'error';
/**
* 后置图标前的后置内容
*/
suffix?: string;
/**
* 后置文本内容。值为字符串则表示图标名称,值为 `Object` 类型,表示透传至 `icon`
*/
suffixIcon?: string | object;
/**
* 输入框下方提示文本,会根据不同的 `status` 呈现不同的样式
*/
tips?: string;
/**
* 输入框类型
* @default text
*/
type?: 'text' | 'number' | 'idcard' | 'digit' | 'safe-password' | 'password' | 'nickname';
/**
* 输入框的值
*/
value?: InputValue;
/**
* 失去焦点时触发
*/
onBlur?: (context: { value: InputValue }) => void;
/**
* 输入框值发生变化时触发cursor 为光标位置;
*/
onChange?: (context: { value: InputValue; cursor: number; keyCode: number }) => void;
/**
* 清空按钮点击时触发
*/
onClear?: () => void;
/**
* 点击事件。[详细类型定义](https://github.com/Tencent/tdesign-miniprogram/tree/develop/src/input/type.ts)。
*/
onClick?: (context: { trigger: InputTrigger }) => void;
/**
* 回车键按下时触发
*/
onEnter?: (context: { value: InputValue }) => void;
/**
* 获得焦点时触发
*/
onFocus?: (context: { value: InputValue }) => void;
/**
* 键盘高度发生变化的时候触发此事件
*/
onKeyboardheightchange?: (context: { height: number; duration: number }) => void;
/**
* 用户昵称审核完毕后触发,仅在 type 为 "nickname" 时有效
*/
onNicknamereview?: (context: { pass: boolean; timeout: boolean }) => void;
/**
* 字数超出限制时触发
*/
onValidate?: (context: { error?: 'exceed-maximum' | 'below-minimum' }) => void;
}
export type InputFormatType = (value: InputValue) => string;
export type InputValue = string | number;
export type InputTrigger = 'suffix' | 'suffix-icon';

View File

@@ -0,0 +1,38 @@
import { getCharacterLength, coalesce } from '../common/utils';
export function getInnerMaxLen({
allowInputOverMax,
maxcharacter,
maxlength,
dataValue,
rawValue,
count,
}) {
if (allowInputOverMax) {
return -1;
}
if (!maxcharacter || maxcharacter < 0) {
return maxlength;
}
if (!dataValue) {
return maxcharacter;
}
const { length: realCount } = getCharacterLength('maxcharacter', rawValue, Infinity);
if (realCount >= maxcharacter) {
return dataValue.length;
}
const { length: computedCount } = getCharacterLength(
'maxcharacter',
rawValue,
allowInputOverMax ? Infinity : maxcharacter,
);
const extra = (coalesce(count, computedCount)) - dataValue.length;
return maxcharacter - extra;
}