Start with being able to set focus (WIP)

This commit is contained in:
jos 2016-12-02 11:38:54 +01:00
parent 37f2f77124
commit 8824cd10f8
3 changed files with 43 additions and 2 deletions

View File

@ -151,6 +151,7 @@ export default class JSONNode extends Component {
const rootName = JSONNode.getRootName(data, options) const rootName = JSONNode.getRootName(data, options)
return h('div', { return h('div', {
ref: 'property',
class: 'jsoneditor-property jsoneditor-readonly', class: 'jsoneditor-property jsoneditor-readonly',
spellCheck: 'false', spellCheck: 'false',
onBlur: this.handleChangeProperty onBlur: this.handleChangeProperty
@ -194,6 +195,7 @@ export default class JSONNode extends Component {
const editable = !options.isValueEditable || options.isValueEditable(this.getPath()) const editable = !options.isValueEditable || options.isValueEditable(this.getPath())
if (editable) { if (editable) {
return h('div', { return h('div', {
ref: 'value',
class: JSONNode.getValueClass(type, itsAnUrl, isEmpty, searchValue), class: JSONNode.getValueClass(type, itsAnUrl, isEmpty, searchValue),
contentEditable: 'true', contentEditable: 'true',
spellCheck: 'false', spellCheck: 'false',
@ -340,7 +342,7 @@ export default class JSONNode extends Component {
}) })
} }
shouldComponentUpdate(nextProps, nextState) { shouldComponentUpdate (nextProps, nextState) {
let prop let prop
for (prop in nextProps) { for (prop in nextProps) {
@ -358,6 +360,19 @@ export default class JSONNode extends Component {
return false return false
} }
componentDidUpdate (prevProps, prevState) {
// TODO: focus to input field
// if (this.props.data.focusProperty && !prevProps.data.focusProperty) {
// console.log('focus property', this.getPath())
// this.refs.property.focus()
// }
//
// if (this.props.data.focusValue && !prevProps.data.focusValue) {
// console.log('focus value', this.getPath())
// this.refs.value.focus()
// }
}
static getRootName (data, options) { static getRootName (data, options) {
return typeof options.name === 'string' return typeof options.name === 'string'
? options.name ? options.name
@ -488,6 +503,7 @@ export default class JSONNode extends Component {
* Get the path of this JSONNode * Get the path of this JSONNode
* @return {Path} * @return {Path}
*/ */
// TODO: get rid of getPath, it's not nice having a reference to the parent in the child
getPath () { getPath () {
const path = this.props.parent const path = this.props.parent
? this.props.parent.getPath() ? this.props.parent.getPath()

View File

@ -6,7 +6,7 @@ import { parseJSON } from '../utils/jsonUtils'
import { enrichSchemaError } from '../utils/schemaUtils' import { enrichSchemaError } from '../utils/schemaUtils'
import { import {
jsonToData, dataToJson, toDataPath, patchData, pathExists, jsonToData, dataToJson, toDataPath, patchData, pathExists,
expand, addErrors, search, addSearchResults expand, addErrors, search, addSearchResults, addFocus
} from '../jsonData' } from '../jsonData'
import { import {
duplicate, insert, append, remove, duplicate, insert, append, remove,
@ -77,7 +77,11 @@ export default class TreeMode extends Component {
const searchResults = this.state.search.text ? search(data, this.state.search.text) : null const searchResults = this.state.search.text ? search(data, this.state.search.text) : null
if (searchResults) { if (searchResults) {
data = addSearchResults(data, searchResults) data = addSearchResults(data, searchResults)
data = addFocus(data, searchResults[0]) // TODO: change to using focus from state
} }
console.log('render', data)
// TODO: pass number of search results to search box in top menu // TODO: pass number of search results to search box in top menu
return h('div', { return h('div', {

View File

@ -544,6 +544,7 @@ export function search (data, text) {
* *
* @param {JSONData} data * @param {JSONData} data
* @param {SearchResult[]} searchResults * @param {SearchResult[]} searchResults
* @return {JSONData} Returns an updated copy of data
*/ */
export function addSearchResults (data, searchResults) { export function addSearchResults (data, searchResults) {
let updatedData = data let updatedData = data
@ -554,6 +555,7 @@ export function addSearchResults (data, searchResults) {
const dataPath = toDataPath(data, searchResult.dataPath).concat('searchValue') const dataPath = toDataPath(data, searchResult.dataPath).concat('searchValue')
updatedData = setIn(updatedData, dataPath, true) updatedData = setIn(updatedData, dataPath, true)
} }
if (searchResult.property) { if (searchResult.property) {
const dataPath = toDataPath(data, searchResult.dataPath).concat('searchProperty') const dataPath = toDataPath(data, searchResult.dataPath).concat('searchProperty')
updatedData = setIn(updatedData, dataPath, true) updatedData = setIn(updatedData, dataPath, true)
@ -564,6 +566,25 @@ export function addSearchResults (data, searchResults) {
return updatedData return updatedData
} }
/**
* Merge a object describing where the focus is to the data
*
* @param {JSONData} data
* @param {SearchResult} focusOn
* @return {JSONData} Returns an updated copy of data
*/
export function addFocus (data, focusOn) {
if (focusOn.value) {
const dataPath = toDataPath(data, focusOn.dataPath).concat('focusValue')
return setIn(data, dataPath, true)
}
if (focusOn.property) {
const dataPath = toDataPath(data, focusOn.dataPath).concat('focusProperty')
return setIn(data, dataPath, true)
}
}
/** /**
* Do a case insensitive search for a search text in a text * Do a case insensitive search for a search text in a text
* @param {String} text * @param {String} text