Skip to content

Commit

Permalink
refactor(input): update input style (vueComponent#2876)
Browse files Browse the repository at this point in the history
* refactor(input): update input style

* refactor(form): update form style about input component

* refactor(input): update clearable icon style
Modify the clearable icon style to prevent the width of the input box from changing due to the insertion of the icon
  • Loading branch information
John60676 authored Sep 29, 2020
1 parent 51fb7cd commit ca2c77e
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 60 deletions.
12 changes: 0 additions & 12 deletions components/form/style/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -136,23 +136,11 @@

form {
.has-feedback {
.@{ant-prefix}-input {
padding-right: @input-padding-horizontal-base + @input-affix-width;
}

// https://github.com/ant-design/ant-design/issues/19884
.@{ant-prefix}-input-affix-wrapper {
.@{ant-prefix}-input-suffix {
padding-right: 18px;
}
.@{ant-prefix}-input {
padding-right: @input-padding-horizontal-base + @input-affix-width * 2;
}
&.@{ant-prefix}-input-affix-wrapper-input-with-clear-btn {
.@{ant-prefix}-input {
padding-right: @input-padding-horizontal-base + @input-affix-width * 3;
}
}
}

// Fix overlapping between feedback icon and <Select>'s arrow.
Expand Down
26 changes: 16 additions & 10 deletions components/input/ClearableLabeledInput.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,30 @@ const ClearableLabeledInput = {
addonBefore: PropTypes.any,
addonAfter: PropTypes.any,
readonly: PropTypes.bool,
isFocused: PropTypes.bool,
style: PropTypes.object,
},
methods: {
renderClearIcon(prefixCls) {
const { allowClear, value, disabled, readonly, inputType, handleReset } = this.$props;
if (
!allowClear ||
disabled ||
readonly ||
value === undefined ||
value === null ||
value === ''
) {
if (!allowClear) {
return null;
}
const showClearIcon =
!disabled && !readonly && value !== undefined && value !== null && value !== '';
const className =
inputType === ClearableInputType[0]
? `${prefixCls}-textarea-clear-icon`
: `${prefixCls}-clear-icon`;
return <CloseCircleFilled onClick={handleReset} class={className} role="button" />;
return (
<CloseCircleFilled
onClick={handleReset}
class={classNames(className, {
[`${className}-hidden`]: !showClearIcon,
})}
role="button"
/>
);
},

renderSuffix(prefixCls) {
Expand Down Expand Up @@ -81,12 +86,13 @@ const ClearableLabeledInput = {
) : null;

const affixWrapperCls = classNames(this.$attrs?.class, `${prefixCls}-affix-wrapper`, {
[`${prefixCls}-affix-wrapper-focused`]: props.isFocused,
[`${prefixCls}-affix-wrapper-disabled`]: props.disabled,
[`${prefixCls}-affix-wrapper-sm`]: props.size === 'small',
[`${prefixCls}-affix-wrapper-lg`]: props.size === 'large',
[`${prefixCls}-affix-wrapper-input-with-clear-btn`]:
props.suffix && props.allowClear && this.$props.value,
});

return (
<span class={affixWrapperCls} style={props.style}>
{prefix}
Expand Down
26 changes: 24 additions & 2 deletions components/input/Input.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export default {
const value = typeof props.value === 'undefined' ? props.defaultValue : props.value;
return {
stateValue: typeof value === 'undefined' ? '' : value,
isFocused: false,
};
},
watch: {
Expand All @@ -87,6 +88,16 @@ export default {
}
},
methods: {
handleInputFocus(e) {
this.isFocused = true;
this.onFocus && this.onFocus(e);
},

handleInputBlur(e) {
this.isFocused = false;
this.onBlur && this.onBlur(e);
},

focus() {
this.input.focus();
},
Expand Down Expand Up @@ -147,7 +158,15 @@ export default {
'inputPrefixCls',
'loading',
]);
const { handleKeyDown, handleChange, size, disabled, $attrs } = this;
const {
handleKeyDown,
handleChange,
handleInputFocus,
handleInputBlur,
size,
disabled,
$attrs,
} = this;

const inputProps = {
...otherProps,
Expand All @@ -160,6 +179,8 @@ export default {
key: 'ant-input',
onInput: handleChange,
onChange: handleChange,
onFocus: handleInputFocus,
onBlur: handleInputBlur,
};
if (!inputProps.autofocus) {
delete inputProps.autofocus;
Expand Down Expand Up @@ -205,7 +226,7 @@ export default {
// return <TextArea {...textareaProps} ref="input" />;
// }
const { prefixCls: customizePrefixCls } = this.$props;
const { stateValue } = this.$data;
const { stateValue, isFocused } = this.$data;
const getPrefixCls = this.configProvider.getPrefixCls;
const prefixCls = getPrefixCls('input', customizePrefixCls);
const addonAfter = getComponent(this, 'addonAfter');
Expand All @@ -224,6 +245,7 @@ export default {
addonBefore,
suffix,
prefix,
isFocused,
};
return <ClearableLabeledInput {...props} ref={this.saveClearableInput} />;
},
Expand Down
8 changes: 8 additions & 0 deletions components/input/style/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@
vertical-align: 0;
}

.@{ant-prefix}-input-clear-icon-hidden {
visibility: hidden;
}

.@{ant-prefix}-input-textarea-clear-icon-hidden {
visibility: hidden;
}

.@{ant-prefix}-input-textarea-clear-icon {
.clear-icon;
position: absolute;
Expand Down
73 changes: 38 additions & 35 deletions components/input/style/mixin.less
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
@import '../../style/themes/index';
@import '../../style/mixins/index';

@input-affix-margin: 4px;
@input-affix-width: 19px;
@input-affix-with-clear-btn-width: 38px;

// size mixins for input
.input-lg() {
height: @input-height-lg;
padding: @input-padding-vertical-lg @input-padding-horizontal-lg;
font-size: @font-size-lg;
}

.input-sm() {
height: @input-height-sm;
padding: @input-padding-vertical-sm @input-padding-horizontal-sm;
}

Expand Down Expand Up @@ -47,7 +46,6 @@
position: relative;
display: inline-block;
width: 100%;
height: @input-height-base;
padding: @input-padding-vertical-base @input-padding-horizontal-base;
color: @input-color;
font-size: @font-size-base;
Expand Down Expand Up @@ -264,12 +262,6 @@
height: @input-height-sm;
}

.@{inputClass}-affix-wrapper {
display: table-cell;
float: left;
width: 100%;
}

&&-compact {
display: block;
.clearfix;
Expand Down Expand Up @@ -297,6 +289,10 @@
border-radius: 0;
}

& > .@{inputClass}-affix-wrapper {
display: inline-flex;
}

& > *:not(:last-child) {
margin-right: -@border-width-base;
border-right-width: @border-width-base;
Expand Down Expand Up @@ -364,17 +360,44 @@

.input-affix-wrapper(@inputClass) {
position: relative;
display: inline-block;
display: inline-flex;
border: @border-width-base @border-style-base @input-border-color;
border-radius: @border-radius-base;
padding: @input-padding-vertical-base @input-padding-horizontal-base;
width: 100%;
text-align: start;

&:hover .@{inputClass}:not(.@{inputClass}-disabled) {
&:hover {
.hover();
}

&-disabled {
.disabled();
}

&-focused {
.active();
}

// Size
&-lg {
.input-lg();
}

&-sm {
.input-sm();
}

.@{inputClass} {
position: relative;
text-align: inherit;
border: none;
padding: 0;
&:focus {
border: none;
outline: none;
box-shadow: none;
}
}

// Should not break align of icon & text
Expand All @@ -385,14 +408,10 @@
// https://codesandbox.io/embed/nifty-benz-gb7ml
.@{inputClass}-prefix,
.@{inputClass}-suffix {
position: absolute;
top: 50%;
z-index: 2;
display: flex;
align-items: center;
color: @input-color;
line-height: 0;
transform: translateY(-50%);
white-space: nowrap;

:not(.anticon) {
line-height: @line-height-base;
Expand All @@ -407,27 +426,11 @@
}

.@{inputClass}-prefix {
left: @input-padding-horizontal-base + 1px;
margin-right: @input-affix-margin;
}

.@{inputClass}-suffix {
right: @input-padding-horizontal-base + 1px;
}

.@{inputClass}:not(:first-child) {
padding-left: @input-padding-horizontal-base + @input-affix-width;
}

.@{inputClass}:not(:last-child) {
padding-right: @input-padding-horizontal-base + @input-affix-width;
}

&.@{inputClass}-affix-wrapper-input-with-clear-btn .@{inputClass}:not(:last-child) {
padding-right: @input-padding-horizontal-base + @input-affix-with-clear-btn-width;
}

&.@{inputClass}-affix-wrapper-textarea-with-clear-btn .@{inputClass} {
padding-right: 22px;
margin-left: @input-affix-margin;
}
}

Expand All @@ -438,7 +441,7 @@
// https://codesandbox.io/s/wizardly-sun-u10br
cursor: pointer;
transition: color 0.3s;

margin: 0 @input-affix-margin;
&:hover {
color: @text-color-secondary;
}
Expand Down
2 changes: 1 addition & 1 deletion components/style/themes/default.less
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@
@input-padding-horizontal-lg: @input-padding-horizontal;
@input-padding-vertical-base: 4px;
@input-padding-vertical-sm: 1px;
@input-padding-vertical-lg: 6px;
@input-padding-vertical-lg: 6.5px;
@input-placeholder-color: hsv(0, 0, 75%);
@input-color: @text-color;
@input-border-color: @border-color-base;
Expand Down

0 comments on commit ca2c77e

Please sign in to comment.