diff options
author | Grégoire Aubert <gregoire.aubert@sonarsource.com> | 2018-02-01 09:11:06 +0100 |
---|---|---|
committer | Grégoire Aubert <gregoire.aubert@sonarsource.com> | 2018-02-01 09:11:06 +0100 |
commit | 39f671dce022e19460606d9639f3727493a1faf2 (patch) | |
tree | cfcef1955b8393d709398ec8375e8bd19e8f0b4f /server/sonar-web/src/main/js/components | |
parent | db4dafc9914328cc00115b96e9317baf7ec84284 (diff) | |
parent | 1f271fd894cdc74682b801cbc202309b120a3738 (diff) | |
download | sonarqube-39f671dce022e19460606d9639f3727493a1faf2.tar.gz sonarqube-39f671dce022e19460606d9639f3727493a1faf2.zip |
Merge remote-tracking branch 'origin/branch-7.0'
Diffstat (limited to 'server/sonar-web/src/main/js/components')
-rw-r--r-- | server/sonar-web/src/main/js/components/controls/SearchSelect.tsx | 42 | ||||
-rw-r--r-- | server/sonar-web/src/main/js/components/controls/__tests__/SearchSelect-test.tsx (renamed from server/sonar-web/src/main/js/components/controls/__tests__/SearchSelect-test.js) | 2 | ||||
-rw-r--r-- | server/sonar-web/src/main/js/components/controls/__tests__/__snapshots__/SearchSelect-test.tsx.snap (renamed from server/sonar-web/src/main/js/components/controls/__tests__/__snapshots__/SearchSelect-test.js.snap) | 0 | ||||
-rw-r--r-- | server/sonar-web/src/main/js/components/facet/__tests__/__snapshots__/FacetFooter-test.tsx.snap | 1 |
4 files changed, 24 insertions, 21 deletions
diff --git a/server/sonar-web/src/main/js/components/controls/SearchSelect.tsx b/server/sonar-web/src/main/js/components/controls/SearchSelect.tsx index 88b6d07ed5b..07f9b2dac2a 100644 --- a/server/sonar-web/src/main/js/components/controls/SearchSelect.tsx +++ b/server/sonar-web/src/main/js/components/controls/SearchSelect.tsx @@ -22,10 +22,14 @@ import { debounce } from 'lodash'; import Select from '../../components/controls/Select'; import { translate, translateWithParameters } from '../../helpers/l10n'; -type Option = { label: string; value: string }; +interface Option { + label: string; + value: string; +} interface Props { autofocus?: boolean; + defaultOptions?: Option[]; minimumQueryLength?: number; onSearch: (query: string) => Promise<Option[]>; onSelect: (value: string) => void; @@ -43,15 +47,10 @@ interface State { export default class SearchSelect extends React.PureComponent<Props, State> { mounted: boolean; - static defaultProps = { - autofocus: true, - resetOnBlur: true - }; - constructor(props: Props) { super(props); - this.state = { loading: false, options: [], query: '' }; - this.search = debounce(this.search, 250); + this.state = { loading: false, options: props.defaultOptions || [], query: '' }; + this.handleSearch = debounce(this.handleSearch, 250); } componentDidMount() { @@ -62,11 +61,18 @@ export default class SearchSelect extends React.PureComponent<Props, State> { this.mounted = false; } + get autofocus() { + return this.props.autofocus !== undefined ? this.props.autofocus : true; + } + get minimumQueryLength() { - return this.props.minimumQueryLength || 2; + return this.props.minimumQueryLength !== undefined ? this.props.minimumQueryLength : 2; + } + get resetOnBlur() { + return this.props.resetOnBlur !== undefined ? this.props.resetOnBlur : true; } - search = (query: string) => { + handleSearch = (query: string) => this.props.onSearch(query).then( options => { if (this.mounted) { @@ -79,20 +85,18 @@ export default class SearchSelect extends React.PureComponent<Props, State> { } } ); - }; - handleChange = (option: Option) => { - this.props.onSelect(option.value); - }; + handleChange = (option: Option) => this.props.onSelect(option.value); handleInputChange = (query: string) => { // `onInputChange` is called with an empty string after a user selects a value // in this case we shouldn't reset `options`, because it also resets select value :( if (query.length >= this.minimumQueryLength) { this.setState({ loading: true, query }); - this.search(query); - } else if (query.length > 0) { - this.setState({ options: [], query }); + this.handleSearch(query); + } else { + const options = (query.length === 0 && this.props.defaultOptions) || []; + this.setState({ options, query }); } }; @@ -102,7 +106,7 @@ export default class SearchSelect extends React.PureComponent<Props, State> { render() { return ( <Select - autofocus={this.props.autofocus} + autofocus={this.autofocus} className="input-super-large" clearable={false} filterOption={this.handleFilterOption} @@ -112,7 +116,7 @@ export default class SearchSelect extends React.PureComponent<Props, State> { ? translateWithParameters('select2.tooShort', this.minimumQueryLength) : translate('select2.noMatches') } - onBlurResetsInput={this.props.resetOnBlur} + onBlurResetsInput={this.resetOnBlur} onChange={this.handleChange} onInputChange={this.handleInputChange} optionRenderer={this.props.renderOption} diff --git a/server/sonar-web/src/main/js/components/controls/__tests__/SearchSelect-test.js b/server/sonar-web/src/main/js/components/controls/__tests__/SearchSelect-test.tsx index 0f8017bf766..34d964549b4 100644 --- a/server/sonar-web/src/main/js/components/controls/__tests__/SearchSelect-test.js +++ b/server/sonar-web/src/main/js/components/controls/__tests__/SearchSelect-test.tsx @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -import React from 'react'; +import * as React from 'react'; import { shallow } from 'enzyme'; import SearchSelect from '../SearchSelect'; diff --git a/server/sonar-web/src/main/js/components/controls/__tests__/__snapshots__/SearchSelect-test.js.snap b/server/sonar-web/src/main/js/components/controls/__tests__/__snapshots__/SearchSelect-test.tsx.snap index 0ecd070f31d..0ecd070f31d 100644 --- a/server/sonar-web/src/main/js/components/controls/__tests__/__snapshots__/SearchSelect-test.js.snap +++ b/server/sonar-web/src/main/js/components/controls/__tests__/__snapshots__/SearchSelect-test.tsx.snap diff --git a/server/sonar-web/src/main/js/components/facet/__tests__/__snapshots__/FacetFooter-test.tsx.snap b/server/sonar-web/src/main/js/components/facet/__tests__/__snapshots__/FacetFooter-test.tsx.snap index 3fab99c21a5..9043a2cc22c 100644 --- a/server/sonar-web/src/main/js/components/facet/__tests__/__snapshots__/FacetFooter-test.tsx.snap +++ b/server/sonar-web/src/main/js/components/facet/__tests__/__snapshots__/FacetFooter-test.tsx.snap @@ -8,7 +8,6 @@ exports[`should render 1`] = ` autofocus={false} onSearch={[MockFunction]} onSelect={[MockFunction]} - resetOnBlur={true} /> </div> `; |