passbolt-styleguide
Version:
Passbolt styleguide contains common styling assets used by the different sites, plugin, etc.
130 lines (120 loc) • 3.56 kB
JavaScript
/**
* Passbolt ~ Open source password manager for teams
* Copyright (c) 2020 Passbolt SA (https://www.passbolt.com)
*
* Licensed under GNU Affero General Public License version 3 of the or any later version.
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) 2020 Passbolt SA (https://www.passbolt.com)
* @license https://opensource.org/licenses/AGPL-3.0 AGPL License
* @link https://www.passbolt.com Passbolt(tm)
* @since 2.13.0
*/
import React, { Component } from "react";
import PropTypes from "prop-types";
import { Trans, withTranslation } from "react-i18next";
import SearchSVG from "../../../../../img/svg/search.svg";
import CloseSVG from "../../../../../img/svg/close.svg";
class SearchBar extends Component {
/**
* Constructor
* @param {Object} props
*/
constructor(props) {
super(props);
this.state = this.defaultState;
this.bindCallbacks();
this.createReferences();
}
/**
* Bind callbacks methods
*/
bindCallbacks() {
this.handleChangeEvent = this.handleChangeEvent.bind(this);
this.clearSearchInput = this.clearSearchInput.bind(this);
}
/**
* Create elements references
*/
createReferences() {
this.searchInputRef = React.createRef();
}
/**
* Handle search input change
* @params {ReactEvent} The react event.
*/
handleChangeEvent(event) {
const target = event.target;
const text = target.value;
if (this.props.onSearch) {
this.props.onSearch(text);
}
}
/**
* Handle clearing of search text
*/
clearSearchInput() {
this.searchInputRef.current.focus();
this.props.onSearch("");
}
get placeholderLabel() {
return this.props.placeholder || this.props.t("Search");
}
/**
* Render the component
* @return {JSX}
*/
render() {
return (
<div className="col2 search-wrapper">
<div className="search">
<div className={`input search required ${this.props.disabled ? "disabled" : ""}`}>
<label className="visuallyhidden">
<Trans>Search</Trans>
</label>
<input
ref={this.searchInputRef}
className="required"
type="search"
disabled={this.props.disabled}
onChange={this.handleChangeEvent}
placeholder={this.placeholderLabel}
value={this.props.value}
/>
<div className="search-button-wrapper">
{this.props.value ? (
<button
className="button button-transparent"
name="clear-button"
type="button"
onClick={this.clearSearchInput}
>
<CloseSVG />
<span className="visuallyhidden">
<Trans>Clear</Trans>
</span>
</button>
) : (
<div className="search-icon">
<SearchSVG name="search" />
</div>
)}
</div>
</div>
</div>
</div>
);
}
}
SearchBar.propTypes = {
disabled: PropTypes.bool,
onSearch: PropTypes.func,
placeholder: PropTypes.string,
value: PropTypes.string,
t: PropTypes.func, // The translation function
};
SearchBar.defaultProps = {
disabled: false,
};
export default withTranslation("common")(SearchBar);