From 040fe12d758b2b9ab009d385743dec4c69635584 Mon Sep 17 00:00:00 2001 From: jos Date: Wed, 13 Dec 2017 17:03:26 +0100 Subject: [PATCH] Fix clearing search result --- src/components/TreeMode.js | 21 +++++---------------- src/components/menu/Search.js | 16 +++++++--------- src/eson.js | 6 +++--- 3 files changed, 15 insertions(+), 28 deletions(-) diff --git a/src/components/TreeMode.js b/src/components/TreeMode.js index 911daf7..d0adb9e 100644 --- a/src/components/TreeMode.js +++ b/src/components/TreeMode.js @@ -198,23 +198,12 @@ export default class TreeMode extends Component { // data = addErrors(data, this.getErrors()) // } - // enrich the data with search results - // TODO: reimplement search and selection - const searchResults = [] - // const searchResults = this.state.search.text ? search(data, this.state.search.text) : null - // if (searchResults) { - // data = applySearchResults(data, searchResults, this.state.search.active) - // } - // if (this.state.selection) { - // data = applySelection(data, this.state.selection) - // } - return h('div', { className: `jsoneditor jsoneditor-mode-${props.mode}`, onKeyDown: this.handleKeyDown, 'data-jsoneditor': 'true' }, [ - this.renderMenu(searchResults), + this.renderMenu(), h('div', { key: 'contents', @@ -244,7 +233,7 @@ export default class TreeMode extends Component { ]) } - renderMenu (searchResults: [] | null) { + renderMenu () { let items = [ h('button', { key: 'expand-all', @@ -301,7 +290,7 @@ export default class TreeMode extends Component { h('div', {key: 'search', className: 'jsoneditor-menu-panel-right'}, h(Search, { text: this.state.search.text, - searchResults, + resultCount: this.state.search.matches ? this.state.search.matches.length : 0, onChange: this.handleSearch, onNext: this.handleNext, onPrevious: this.handlePrevious, @@ -592,7 +581,6 @@ export default class TreeMode extends Component { handleSearch = (text) => { const { eson, matches, active } = search(this.state.eson, text) - if (matches.length > 0) { this.setState({ search: { text, active, matches }, @@ -604,7 +592,8 @@ export default class TreeMode extends Component { } else { this.setState({ - search: { text, active, matches } + search: { text, active, matches }, + eson }) } } diff --git a/src/components/menu/Search.js b/src/components/menu/Search.js index 6102384..ce56be3 100644 --- a/src/components/menu/Search.js +++ b/src/components/menu/Search.js @@ -22,7 +22,7 @@ export default class Search extends Component { render () { return h('div', {className: 'jsoneditor-search'}, [ - this.renderResultsCount(this.props.searchResults), + this.renderResultsCount(this.props.resultCount), h('form', { key: 'box', className: 'jsoneditor-search-box', @@ -54,21 +54,19 @@ export default class Search extends Component { ]) } - renderResultsCount (searchResults : []) { - if (!searchResults) { + renderResultsCount (resultCount) { + if (resultCount === 0) { return null } - const count = searchResults.length - - if (count === 0) { + if (resultCount === 0) { return h('div', {key: 'count', className: 'jsoneditor-results'}, '(no results)') } - if (count > 0) { - const suffix = count === 1 ? ' result' : ' results' + if (resultCount > 0) { + const suffix = resultCount === 1 ? ' result' : ' results' - return h('div', {key: 'count', className: 'jsoneditor-results'}, count + suffix) + return h('div', {key: 'count', className: 'jsoneditor-results'}, resultCount + suffix) } return null diff --git a/src/eson.js b/src/eson.js index 6cf61b3..dd67621 100644 --- a/src/eson.js +++ b/src/eson.js @@ -352,7 +352,7 @@ export function addErrors (eson: ESON, errors) { /** * Search some text in all properties and values * @param {ESON} eson - * @param {String} text + * @param {String} text Search text * @return {{eson: ESON, matches: ESONPointer[], active: ESONPointer}} Returns search result: * An updated eson object containing the search results, * and an array with the paths of all matches @@ -367,7 +367,7 @@ export function search (eson, text) { // check property name const prop = last(path) - if (typeof prop === 'string' && containsCaseInsensitive(prop, text)) { + if (typeof prop === 'string' && text !== '' && containsCaseInsensitive(prop, text)) { const searchState = isEmpty(matches) ? 'active' : 'normal' matches.push({path, area: 'property'}) updatedValue = setIn(updatedValue, ['_meta', 'searchProperty'], searchState) @@ -377,7 +377,7 @@ export function search (eson, text) { } // check value - if (value._meta.type === 'value' && containsCaseInsensitive(value._meta.value, text)) { + if (value._meta.type === 'value' && text !== '' && containsCaseInsensitive(value._meta.value, text)) { const searchState = isEmpty(matches) ? 'active' : 'normal' matches.push({path, area: 'value'}) updatedValue = setIn(updatedValue, ['_meta', 'searchValue'], searchState)