office-ui-fabric-react
Version:
Reusable React components for building experiences for Office 365.
162 lines • 6.67 kB
JavaScript
import * as tslib_1 from "tslib";
import * as React from 'react';
import { BaseComponent, autobind, getId, customizable, classNamesFunction } from '../../Utilities';
import { IconButton } from '../../Button';
import { Icon } from '../../Icon';
var getClassNames = classNamesFunction();
var SearchBoxBase = /** @class */ (function (_super) {
tslib_1.__extends(SearchBoxBase, _super);
function SearchBoxBase(props) {
var _this = _super.call(this, props) || this;
_this._warnDeprecations({
'labelText': 'placeholder'
});
_this._latestValue = props.value || '';
_this.state = {
value: _this._latestValue,
hasFocus: false,
id: getId('SearchBox')
};
return _this;
}
SearchBoxBase.prototype.componentWillReceiveProps = function (newProps) {
if (newProps.value !== undefined) {
this._latestValue = newProps.value;
this.setState({
value: newProps.value
});
}
};
SearchBoxBase.prototype.render = function () {
var _a = this.props, ariaLabel = _a.ariaLabel, placeholder = _a.placeholder, className = _a.className, disabled = _a.disabled, underlined = _a.underlined, getStyles = _a.getStyles, labelText = _a.labelText, theme = _a.theme, clearButtonProps = _a.clearButtonProps;
var _b = this.state, value = _b.value, hasFocus = _b.hasFocus, id = _b.id;
var placeholderValue = labelText === undefined ? placeholder : labelText;
var classNames = getClassNames(getStyles, {
theme: theme,
className: className,
underlined: underlined,
hasFocus: hasFocus,
disabled: disabled,
hasInput: value.length > 0
});
return (React.createElement("div", { ref: this._resolveRef('_rootElement'), className: classNames.root, onFocusCapture: this._onFocusCapture },
React.createElement("div", { className: classNames.iconContainer },
React.createElement(Icon, { className: classNames.icon, iconName: 'Search' })),
React.createElement("input", { id: id, className: classNames.field, placeholder: placeholderValue, onChange: this._onInputChange, onInput: this._onInputChange, onKeyDown: this._onKeyDown, value: value, disabled: this.props.disabled, "aria-label": ariaLabel ? ariaLabel : placeholder, ref: this._resolveRef('_inputElement') }),
value.length > 0 &&
React.createElement("div", { className: classNames.clearButton },
React.createElement(IconButton, tslib_1.__assign({ styles: { root: { height: 'auto' }, icon: { fontSize: '12px' } }, iconProps: { iconName: 'Clear' } }, clearButtonProps, { onClick: this._onClearClick })))));
};
/**
* Sets focus to the search box input field
*/
SearchBoxBase.prototype.focus = function () {
if (this._inputElement) {
this._inputElement.focus();
}
};
SearchBoxBase.prototype._onClear = function (ev) {
this.props.onClear && this.props.onClear(ev);
if (!ev.defaultPrevented) {
this._latestValue = '';
this.setState({
value: ''
});
this._callOnChange('');
ev.stopPropagation();
ev.preventDefault();
this._inputElement.focus();
}
};
SearchBoxBase.prototype._onFocusCapture = function (ev) {
this.setState({
hasFocus: true
});
this._events.on(this._rootElement, 'blur', this._onBlur, true);
if (this.props.onFocus) {
this.props.onFocus(ev);
}
};
SearchBoxBase.prototype._onClearClick = function (ev) {
var clearButtonProps = this.props.clearButtonProps;
if (clearButtonProps && clearButtonProps.onClick) {
clearButtonProps.onClick(ev);
}
if (!ev.defaultPrevented) {
this._onClear(ev);
}
};
SearchBoxBase.prototype._onKeyDown = function (ev) {
switch (ev.which) {
case 27 /* escape */:
this.props.onEscape && this.props.onEscape(ev);
if (!ev.defaultPrevented) {
this._onClear(ev);
}
break;
case 13 /* enter */:
if (this.props.onSearch && this.state.value.length > 0) {
this.props.onSearch(this.state.value);
}
break;
default:
this.props.onKeyDown && this.props.onKeyDown(ev);
if (!ev.defaultPrevented) {
return;
}
}
// We only get here if the keypress has been handled,
// or preventDefault was called in case of default keyDown handler
ev.preventDefault();
ev.stopPropagation();
};
SearchBoxBase.prototype._onBlur = function (ev) {
this._events.off(this._rootElement, 'blur');
this.setState({
hasFocus: false
});
if (this.props.onBlur) {
this.props.onBlur(ev);
}
};
SearchBoxBase.prototype._onInputChange = function (ev) {
var value = ev.target.value;
if (value === this._latestValue) {
return;
}
this._latestValue = value;
this.setState({ value: value });
this._callOnChange(value);
};
SearchBoxBase.prototype._callOnChange = function (newValue) {
var _a = this.props, onChange = _a.onChange, onChanged = _a.onChanged;
// Call @deprecated method.
if (onChanged) {
onChanged(newValue);
}
if (onChange) {
onChange(newValue);
}
};
tslib_1.__decorate([
autobind
], SearchBoxBase.prototype, "_onFocusCapture", null);
tslib_1.__decorate([
autobind
], SearchBoxBase.prototype, "_onClearClick", null);
tslib_1.__decorate([
autobind
], SearchBoxBase.prototype, "_onKeyDown", null);
tslib_1.__decorate([
autobind
], SearchBoxBase.prototype, "_onBlur", null);
tslib_1.__decorate([
autobind
], SearchBoxBase.prototype, "_onInputChange", null);
SearchBoxBase = tslib_1.__decorate([
customizable('SearchBox', ['theme'])
], SearchBoxBase);
return SearchBoxBase;
}(BaseComponent));
export { SearchBoxBase };
//# sourceMappingURL=SearchBox.base.js.map