Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add keyboard navigation #383

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion dev-server/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ ReactDom.render(
return false;
}}
defaultValue=""
autoFocus
/>

<br />
Expand Down Expand Up @@ -183,7 +184,7 @@ function getExampleJson1() {
integer: 42,
empty_array: [],
empty_object: {},
array: [1, 2, 3, 'test'],
array: [{ a: 1, b: 2 }, 2, 3, 'test'],
float: -2.757,
undefined_var: undefined,
parent: {
Expand Down
6 changes: 6 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@ export interface ReactJsonViewProps {
* Default: null
*/
defaultValue?: TypeDefaultValue | TypeDefaultValue[] | null;
/**
* Wether or not to automatically focus on the root key
*
* Default: false
*/
autoFocus?: boolean;
}

export interface OnCopyProps {
Expand Down
27,558 changes: 27,535 additions & 23 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion src/js/components/DataTypes/Object.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,11 @@ class RjvObject extends React.PureComponent {
>
<IconComponent {...{ theme, iconStyle }} />
</div>
<ObjectName {...this.props} />
<ObjectName
{...this.props}
onToggleCollapsed={this.toggleCollapsed}
isExpanded={expanded}
/>
<span {...Theme(theme, 'brace')}>
{object_type === 'array' ? '[' : '{'}
</span>
Expand Down
33 changes: 30 additions & 3 deletions src/js/components/ObjectName.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react';
import { handleObjectKeyKeyDown } from '../helpers/util';
import Theme from './../themes/getStyle';

export default function getObjectName(props) {
Expand All @@ -9,7 +10,9 @@ export default function getObjectName(props) {
theme,
jsvRoot,
name,
displayArrayKey
displayArrayKey,
onToggleCollapsed,
isExpanded
} = props;

const display_name = props.name ? props.name : '';
Expand All @@ -19,7 +22,20 @@ export default function getObjectName(props) {
} else if (parent_type == 'array') {
return displayArrayKey ? (
<span {...Theme(theme, 'array-key')} key={namespace}>
<span class="array-key">{display_name}</span>
<span
class="array-key"
tabIndex={0}
onKeyDown={e =>
handleObjectKeyKeyDown(
'object-name',
e,
isExpanded,
onToggleCollapsed
)
}
>
{display_name}
</span>
<span {...Theme(theme, 'colon')}>:</span>
</span>
) : (
Expand All @@ -28,7 +44,18 @@ export default function getObjectName(props) {
} else {
return (
<span {...Theme(theme, 'object-name')} key={namespace}>
<span class="object-key">
<span
class="object-key"
tabIndex={0}
onKeyDown={e =>
handleObjectKeyKeyDown(
'object-name',
e,
isExpanded,
onToggleCollapsed
)
}
>
{quotesOnKeys && (
<span style={{ verticalAlign: 'top' }}>"</span>
)}
Expand Down
17 changes: 14 additions & 3 deletions src/js/components/VariableEditor.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import AutosizeTextarea from 'react-textarea-autosize';

import { toType } from './../helpers/util';
import { handleObjectKeyKeyDown, toType } from './../helpers/util';
import dispatcher from './../helpers/dispatcher';
import parseInput from './../helpers/parseInput';
import stringifyVariable from './../helpers/stringifyVariable';
Expand Down Expand Up @@ -78,15 +78,26 @@ class VariableEditor extends React.PureComponent {
{...Theme(theme, 'array-key')}
key={variable.name + '_' + namespace}
>
{variable.name}
<span
tabIndex={0}
onKeyDown={e =>
handleObjectKeyKeyDown('array-variable', e)
}
>
{variable.name}
</span>
<div {...Theme(theme, 'colon')}>:</div>
</span>
) : null
) : (
<span>
<span
{...Theme(theme, 'object-name')}
class="object-key"
class="object-key variable-name"
tabIndex={0}
onKeyDown={e =>
handleObjectKeyKeyDown('variable', e)
}
key={variable.name + '_' + namespace}
>
{!!quotesOnKeys && (
Expand Down
83 changes: 83 additions & 0 deletions src/js/helpers/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,86 @@ export function isTheme(theme) {
}
return false;
}

export function handleObjectKeyKeyDown(type, e, isExpanded, onToggleCollapsed) {
const wrapper = document.querySelector('.react-json-view');
const target = e.target;

function focusOn(which) {
const elements = Array.from(wrapper.querySelectorAll('[tabindex="0"]'));
const index = elements.findIndex(curr => curr === target);
if (which === 'next' && index < elements.length - 1) {
elements[index + 1].focus();
}
if (which === 'previous' && index > 0) {
elements[index - 1].focus();
}
}

if (e.key === 'ArrowDown') {
e.preventDefault();
e.stopPropagation();

focusOn('next');
}

if (e.key === 'ArrowUp') {
e.preventDefault();
e.stopPropagation();

focusOn('previous');
}

if (e.key === 'ArrowRight') {
e.preventDefault();
e.stopPropagation();

if (
target.classList.contains('object-key') ||
target.classList.contains('array-key')
) {
if (!isExpanded) {
if (onToggleCollapsed) {
onToggleCollapsed();
}
} else {
focusOn('next');
}
}
}

if (e.key === 'ArrowLeft') {
e.preventDefault();
e.stopPropagation();

if (isExpanded && onToggleCollapsed) {
onToggleCollapsed();
} else {
if (type === 'array-variable') {
target
.closest('.object-key-val')
.querySelector('.object-key')
.focus();
}

if (type === 'variable') {
target
.closest('.object-key-val')
.querySelector('.object-key, .array-key')
.focus();
}

if (type === 'object-name') {
const closestObject = target
.closest('.object-key-val')
.parentElement.closest('.object-key-val');

if (closestObject) {
closestObject
.querySelector('.object-key, .array-key')
.focus();
}
}
}
}
}
26 changes: 26 additions & 0 deletions src/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import Theme from './themes/getStyle';

//forward src through to JsonObject component
class ReactJsonView extends React.PureComponent {
wrapperRef = React.createRef();

constructor(props) {
super(props);
this.state = {
Expand Down Expand Up @@ -94,6 +96,15 @@ class ReactJsonView extends React.PureComponent {
addKeyRequest: false,
editKeyRequest: false
});

if (this.props.autoFocus) {
const rootKey = this.wrapperRef.current.querySelector(
'.object-key'
);
if (rootKey) {
rootKey.focus();
}
}
}

componentDidUpdate(prevProps, prevState) {
Expand All @@ -111,6 +122,20 @@ class ReactJsonView extends React.PureComponent {
if (prevProps.src !== this.state.src) {
ObjectAttributes.set(this.rjvId, 'global', 'src', this.state.src);
}

if (
prevProps.src !== this.state.src ||
prevProps.autoFocus !== this.props.autoFocus
) {
if (this.props.autoFocus) {
const rootKey = this.wrapperRef.current.querySelector(
'.object-key'
);
if (rootKey) {
rootKey.focus();
}
}
}
}

componentWillUnmount() {
Expand Down Expand Up @@ -173,6 +198,7 @@ class ReactJsonView extends React.PureComponent {
return (
<div
class="react-json-view"
ref={this.wrapperRef}
style={{ ...Theme(theme, 'app-container').style, ...style }}
>
<ValidationFailure
Expand Down