Fix Input className to wrapper, close #6809

This commit is contained in:
afc163 2017-07-12 21:43:06 +08:00
parent 39207efa4d
commit 81a8d71ec3
3 changed files with 24 additions and 26 deletions

View File

@ -14,7 +14,8 @@ title:
Customize Input Component Customize Input Component
````jsx ````jsx
import { AutoComplete } from 'antd'; import { AutoComplete, Input } from 'antd';
const { TextArea } = Input;
function onSelect(value) { function onSelect(value) {
console.log('onSelect', value); console.log('onSelect', value);
@ -49,7 +50,7 @@ class Complete extends React.Component {
onSearch={this.handleSearch} onSearch={this.handleSearch}
placeholder="input here" placeholder="input here"
> >
<textarea onKeyPress={this.handleKeyPress} style={{ height: 50 }} /> <TextArea onKeyPress={this.handleKeyPress} style={{ height: 50 }} />
</AutoComplete> </AutoComplete>
); );
} }

View File

@ -50,15 +50,9 @@ export default class AutoComplete extends React.Component<AutoCompleteProps, any
getInputElement = () => { getInputElement = () => {
const { children } = this.props; const { children } = this.props;
const element = children && React.isValidElement(children) && children.type !== Option ? const element = children && React.isValidElement(children) && children.type !== Option ?
React.Children.only(this.props.children) : React.Children.only(this.props.children) : <Input />;
<Input/>;
return ( return (
<InputElement <InputElement {...element.props}>{element}</InputElement>
{...element.props}
className={classNames('ant-input', element.props.className)}
>
{element}
</InputElement>
); );
} }

View File

@ -100,6 +100,15 @@ export default class Input extends Component<InputProps, any> {
this.refs.input.blur(); this.refs.input.blur();
} }
getInputClassName() {
const { prefixCls, size, disabled } = this.props;
return classNames(prefixCls, {
[`${prefixCls}-sm`]: size === 'small',
[`${prefixCls}-lg`]: size === 'large',
[`${prefixCls}-disabled`]: disabled,
});
}
renderLabeledInput(children) { renderLabeledInput(children) {
const props = this.props; const props = this.props;
// Not wrap when there is not addons // Not wrap when there is not addons
@ -169,20 +178,21 @@ export default class Input extends Component<InputProps, any> {
) : null; ) : null;
return ( return (
<span className={`${props.prefixCls}-affix-wrapper`} style={props.style}> <span
className={classNames(props.className, `${props.prefixCls}-affix-wrapper`)}
style={props.style}
>
{prefix} {prefix}
{cloneElement(children, { style: null })} {cloneElement(children, { style: null, className: this.getInputClassName() })}
{suffix} {suffix}
</span> </span>
); );
} }
renderInput() { renderInput() {
const props = { const { value, className } = this.props;
...this.props,
};
// Fix https://fb.me/react-unknown-prop // Fix https://fb.me/react-unknown-prop
const otherProps = omit(props, [ const otherProps = omit(this.props, [
'prefixCls', 'prefixCls',
'onPressEnter', 'onPressEnter',
'addonBefore', 'addonBefore',
@ -191,15 +201,8 @@ export default class Input extends Component<InputProps, any> {
'suffix', 'suffix',
]); ]);
const prefixCls = props.prefixCls; if ('value' in this.props) {
const inputClassName = classNames(prefixCls, props.className, { otherProps.value = fixControlledValue(value);
[`${prefixCls}-sm`]: props.size === 'small',
[`${prefixCls}-lg`]: props.size === 'large',
[`${prefixCls}-disabled`]: props.disabled,
});
if ('value' in props) {
otherProps.value = fixControlledValue(props.value);
// Input elements must be either controlled or uncontrolled, // Input elements must be either controlled or uncontrolled,
// specify either the value prop, or the defaultValue prop, but not both. // specify either the value prop, or the defaultValue prop, but not both.
delete otherProps.defaultValue; delete otherProps.defaultValue;
@ -207,7 +210,7 @@ export default class Input extends Component<InputProps, any> {
return this.renderLabeledIcon( return this.renderLabeledIcon(
<input <input
{...otherProps} {...otherProps}
className={inputClassName} className={classNames(this.getInputClassName(), className)}
onKeyDown={this.handleKeyDown} onKeyDown={this.handleKeyDown}
ref="input" ref="input"
/>, />,