Skip to content

Commit

Permalink
feat: 增加input组件密码可见切换/增加清除输入按钮 (#119)
Browse files Browse the repository at this point in the history
* fix: fix: 修改交互问题

* fix: fix: 修改交互问题

* fix: fix:修改样式
  • Loading branch information
nightchanges authored Sep 11, 2024
1 parent 5355b5d commit a7a39fb
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 16 deletions.
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions src/input/_example/clearable.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import 'tdesign-web-components/input';
import 'tdesign-web-components/space';
import 'tdesign-icons-web-components/esm/components/lock-on';

import { Component } from 'omi';

export default class InputBase extends Component {
value1 = 'Welcome to TDesign';

render() {
return (
<t-space direction="vertical">
<t-input value={this.value1} placeholder="请输入" clearable />
</t-space>
);
}
}
20 changes: 20 additions & 0 deletions src/input/_example/password.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import 'tdesign-web-components/input';
import 'tdesign-web-components/space';
import 'tdesign-icons-web-components/esm/components/lock-on';

import { Component } from 'omi';

export default class InputBase extends Component {
value1 = '';

value2 = 'Welcome to TDesign';

render() {
return (
<t-space direction="vertical">
<t-input value={this.value1} placeholder="请输入密码" type="password" prefixIcon={<t-icon-lock-on />} />
<t-input value={this.value2} placeholder="请输入密码" type="password" prefixIcon={<t-icon-lock-on />} />
</t-space>
);
}
}
69 changes: 62 additions & 7 deletions src/input/input.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import 'tdesign-icons-web-components/esm/components/close-circle-filled';
import 'tdesign-icons-web-components/esm/components/browse';
import 'tdesign-icons-web-components/esm/components/browse-off';

import { Component, createRef, OmiProps, tag } from 'omi';
import { cloneElement, Component, createRef, OmiProps, tag, VNode } from 'omi';

import classname, { getClassPrefix } from '../_util/classname';
import { convertToLightDomNode } from '../_util/lightDom';
import parseTNode from '../_util/parseTNode';
import { StyledProps, TElement, TNode } from '../common';
import { InputValue, TdInputProps } from './type';
Expand Down Expand Up @@ -90,7 +93,7 @@ export default class Input extends Component<InputProps> {

status: TdInputProps['status'] = 'default';

renderType = '';
renderType = 'text';

isFocused = false;

Expand All @@ -100,6 +103,14 @@ export default class Input extends Component<InputProps> {

eventProps;

private handlePasswordVisible = (e: MouseEvent) => {
e.stopImmediatePropagation();
if (this.props.disabled) return;
const toggleType = this.renderType === 'password' ? 'text' : 'password';
this.renderType = toggleType;
this.update();
};

private handleChange = (e) => {
e.stopImmediatePropagation();
const { maxlength, maxcharacter, allowInputOverMax, status, onValidate, onChange } = this.props;
Expand Down Expand Up @@ -158,13 +169,15 @@ export default class Input extends Component<InputProps> {
};

private handleMouseEnter = (e: MouseEvent) => {
e.stopImmediatePropagation();
const { onMouseenter } = this.props;
this.isHover = true;
this.update();
onMouseenter?.({ e });
this.update();
};

private handleMouseLeave = (e: MouseEvent) => {
e.stopImmediatePropagation();
const { onMouseleave } = this.props;
this.isHover = false;
this.update();
Expand Down Expand Up @@ -310,20 +323,62 @@ export default class Input extends Component<InputProps> {
});

const isShowClearIcon = ((clearable && this.value && !disabled) || showClearIconOnEmpty) && this.isHover;
const prefixIconContent = renderIcon('t', 'prefix', parseTNode(prefixIcon));
let suffixIconNew = suffixIcon;

const prefixIconContent = prefixIcon
? renderIcon(
't',
'prefix',
cloneElement(parseTNode(convertToLightDomNode(prefixIcon)) as VNode, {
className: `${classPrefix}-input__prefix`,
style: { marginRight: '0px' },
}),
)
: renderIcon('t', 'prefix', parseTNode(convertToLightDomNode(prefixIcon)));
let suffixIconNew = suffixIcon;
const cleanMargin = { marginLeft: '0px' };
if (isShowClearIcon) {
suffixIconNew = (
<t-icon-close-circle-filled
name={'close-circle-filled'}
class={classname(`${classPrefix}-input__suffix-clear`)}
className={classname(
`${classPrefix}-input__suffix-clear`,
`${classPrefix}-input__suffix`,
`${classPrefix}-input__suffix-icon`,
)}
style={cleanMargin}
onClick={this.handleClear}
/>
) as any;
}
if (props.type === 'password' && typeof suffixIcon === 'undefined') {
if (this.renderType === 'password') {
suffixIconNew = (
<t-icon-browse-off
onClick={this.handlePasswordVisible}
className={classname(
`${classPrefix}-input__suffix-clear`,
`${classPrefix}-input__suffix`,
`${classPrefix}-input__suffix-icon`,
)}
style={cleanMargin}
/>
) as any;
} else if (this.renderType === 'text') {
suffixIconNew = (
<t-icon-browse
onClick={this.handlePasswordVisible}
className={classname(
`${classPrefix}-input__suffix-clear`,
`${classPrefix}-input__suffix`,
`${classPrefix}-input__suffix-icon`,
)}
style={cleanMargin}
/>
) as any;
}
}

const suffixIconContent = renderIcon('t', 'suffix', parseTNode(suffixIconNew));
const suffixIconContent = renderIcon('t', 'suffix', parseTNode(convertToLightDomNode(suffixIconNew)));
const labelContent = isFunction(label) ? label() : label;
const suffixContent = isFunction(suffix) ? suffix() : suffix;

Expand Down
4 changes: 2 additions & 2 deletions src/skeleton/skeleton.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import isNumber from 'lodash/isNumber';
import { classNames, Component, createRef,tag } from 'omi';
import { classNames, Component, createRef, tag } from 'omi';

import { pxCompat } from '../_common/js/utils/helper';
import { getClassPrefix } from '../_util/classname';
import parseTNode from '../_util/parseTNode';
import { StyledProps, Styles, TNode } from '../common';
import { SkeletonRowCol, SkeletonRowColObj,TdSkeletonProps } from './type';
import { SkeletonRowCol, SkeletonRowColObj, TdSkeletonProps } from './type';

export type SkeletonProps = TdSkeletonProps & StyledProps & { children: TNode };
type SkeletonInjection = (props: SkeletonProps) => SkeletonProps;
Expand Down

0 comments on commit a7a39fb

Please sign in to comment.