Skip to content

Commit 1d4eee7

Browse files
committed
Add property that hides the array keys
Change: Closes mac-s-g#180 This change introduces the `displayArrayKey` property to the `ReactJsonView` component defaulting to `true`. This property will allow users to hide the `array-key` in the component output. Verification: - Run unit test - Added unit test - Toggle the `displayArrayKey` and visually verified the key is not displayed when expected
1 parent 4d008ac commit 1d4eee7

File tree

5 files changed

+27
-10
lines changed

5 files changed

+27
-10
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ Name|Type|Default|Description
6060
`onSelect`|`(select)=>{}`|`false`|When a function is passed in, clicking a value triggers the `onSelect` method to be called.
6161
`sortKeys`|`boolean`|`false`|set to true to sort object keys
6262
`validationMessage`|`string`|"Validation Error"|Custom message for validation failures to `onEdit`, `onAdd`, or `onDelete` callbacks
63+
`displayArrayKey`|`boolean`|`true`|When set to `true`, the index of the elements prefix values
6364

6465
### Features
6566
* `onEdit`, `onAdd` and `onDelete` props allow users to edit the `src` variable

src/js/components/ObjectName.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@ import Theme from './../themes/getStyle';
33

44
export default function getObjectName(props) {
55
const {
6-
parent_type, namespace, theme, jsvRoot, name
6+
parent_type, namespace, theme, jsvRoot, name, displayArrayKey
77
} = props;
88

99
const display_name = props.name ? props.name : '';
1010

1111
if (jsvRoot && (name === false || name === null)) {
1212
return (<span />);
1313
} else if (parent_type == 'array') {
14-
return (
14+
return displayArrayKey ? (
1515
<span {...Theme(theme, 'array-key')} key={namespace}>
1616
<span class="array-key">{display_name}</span>
1717
<span {...Theme(theme, 'colon')}>:</span>
1818
</span>
19-
);
19+
) : (<span />);
2020
} else {
2121
return (
2222
<span {...Theme(theme, 'object-name')} key={namespace}>

src/js/components/VariableEditor.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ class VariableEditor extends React.PureComponent {
5454
onEdit,
5555
onDelete,
5656
onSelect,
57-
rjvId
57+
rjvId,
58+
displayArrayKey
5859
} = this.props;
5960
const { editMode } = this.state;
60-
6161
return (
6262
<div
6363
{...Theme(theme, 'objectKeyVal', {
@@ -66,14 +66,14 @@ class VariableEditor extends React.PureComponent {
6666
class="variable-row"
6767
key={variable.name}
6868
>
69-
{type == 'array' ? (
70-
<span
69+
{(type == 'array') ? ((displayArrayKey ?
70+
(<span
7171
{...Theme(theme, 'array-key')}
72-
key={variable.name + '_' + namespace}
73-
>
72+
key={variable.name + '_' + namespace}>
7473
{variable.name}
7574
<div {...Theme(theme, 'colon')}>:</div>
76-
</span>
75+
</span>)
76+
: null)
7777
) : (
7878
<span>
7979
<span

src/js/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class ReactJsonView extends React.PureComponent {
5959
style: {},
6060
validationMessage: 'Validation Error',
6161
defaultValue: null,
62+
displayArrayKey: true,
6263
}
6364

6465
// will trigger whenever setState() is called, or parent passes in new props.

test/tests/js/components/ObjectName-test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ describe("<ObjectName />", function() {
2525
parent_type="array"
2626
theme="rjv-default"
2727
jsvRoot={false}
28+
displayArrayKey={true}
2829
/>
2930
)
3031
expect(wrapper.find(".array-key")).to.have.length(1)
@@ -41,4 +42,18 @@ describe("<ObjectName />", function() {
4142
)
4243
expect(wrapper.find("span").children()).to.have.length(0)
4344
})
45+
46+
it("ObjectName array hides key", function() {
47+
const wrapper = render(
48+
<ObjectName
49+
namespace={"test"}
50+
name="test"
51+
parent_type="array"
52+
theme="rjv-default"
53+
jsvRoot={false}
54+
displayArrayKey={false}
55+
/>
56+
)
57+
expect(wrapper.find(".array-key")).to.have.length(0)
58+
})
4459
})

0 commit comments

Comments
 (0)