Skip to content

Commit ab37031

Browse files
committed
Add lang property
Resolves #108.
1 parent f79b234 commit ab37031

File tree

7 files changed

+42
-5
lines changed

7 files changed

+42
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* [#79]: Add the ability to expand or collapse all nodes in the tree
88
* [#102]: Add `icons` property to allow specification of icon components
99
* [#103]: Add `title` node property and `showNodeTitle` tree property
10+
* [#108]: Add `lang` property for language customization
1011

1112
### Other
1213

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ Note that you can override as many or as little icons as you like.
122122
| `expandOnClick` | bool | If true, nodes will be expanded by clicking on labels. Requires a non-empty `onClick` function. | `false` |
123123
| `icons` | object | An object containing the mappings for the various icons and their components. See **Changing the Default Icons**. | { ... } |
124124
| `expanded` | array | An array of expanded node values. | `[]` |
125+
| `lang` | object | An object containing the language mappings for the various text elements. | { ... } |
125126
| `name` | string | Optional name for the hidden `<input>` element. | `undefined` |
126127
| `nameAsArray` | bool | If true, the hidden `<input>` will encode its values as an array rather than a joined string. | `false` |
127128
| `nativeCheckboxes` | bool | If true, native browser checkboxes will be used instead of pseudo-checkbox icons. | `false` |

src/js/CheckboxTree.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import nanoid from 'nanoid';
77
import Button from './Button';
88
import TreeNode from './TreeNode';
99
import iconsShape from './iconsShape';
10+
import languageShape from './languageShape';
1011
import listShape from './listShape';
1112
import nodeShape from './nodeShape';
1213

@@ -20,6 +21,7 @@ class CheckboxTree extends React.Component {
2021
expandOnClick: PropTypes.bool,
2122
expanded: listShape,
2223
icons: iconsShape,
24+
lang: languageShape,
2325
name: PropTypes.string,
2426
nameAsArray: PropTypes.bool,
2527
nativeCheckboxes: PropTypes.bool,
@@ -52,6 +54,11 @@ class CheckboxTree extends React.Component {
5254
parentOpen: <span className="rct-icon rct-icon-parent-open" />,
5355
leaf: <span className="rct-icon rct-icon-leaf" />,
5456
},
57+
lang: {
58+
collapseAll: 'Collapse all',
59+
expandAll: 'Expand all',
60+
toggle: 'Toggle',
61+
},
5562
name: undefined,
5663
nameAsArray: false,
5764
nativeCheckboxes: false,
@@ -264,6 +271,7 @@ class CheckboxTree extends React.Component {
264271
expandDisabled,
265272
expandOnClick,
266273
icons,
274+
lang,
267275
noCascade,
268276
onlyLeafCheckboxes,
269277
optimisticToggle,
@@ -293,6 +301,7 @@ class CheckboxTree extends React.Component {
293301
icon={node.icon}
294302
icons={{ ...defaultIcons, ...icons }}
295303
label={node.label}
304+
lang={lang}
296305
optimisticToggle={optimisticToggle}
297306
rawChildren={node.children}
298307
showCheckbox={showCheckbox}
@@ -325,7 +334,7 @@ class CheckboxTree extends React.Component {
325334
}
326335

327336
renderExpandAll() {
328-
const { icons: { expandAll, collapseAll }, showExpandAll } = this.props;
337+
const { icons: { expandAll, collapseAll }, lang, showExpandAll } = this.props;
329338

330339
if (!showExpandAll) {
331340
return null;
@@ -335,14 +344,14 @@ class CheckboxTree extends React.Component {
335344
<div className="rct-options">
336345
<Button
337346
className="rct-option rct-option-expand-all"
338-
title="Expand all"
347+
title={lang.expandAll}
339348
onClick={this.onExpandAll}
340349
>
341350
{expandAll}
342351
</Button>
343352
<Button
344353
className="rct-option rct-option-collapse-all"
345-
title="Collapse all"
354+
title={lang.collapseAll}
346355
onClick={this.onCollapseAll}
347356
>
348357
{collapseAll}

src/js/TreeNode.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import React from 'react';
55
import Button from './Button';
66
import NativeCheckbox from './NativeCheckbox';
77
import iconsShape from './iconsShape';
8+
import languageShape from './languageShape';
89
import nodeShape from './nodeShape';
910

1011
class TreeNode extends React.Component {
@@ -15,6 +16,7 @@ class TreeNode extends React.Component {
1516
expanded: PropTypes.bool.isRequired,
1617
icons: iconsShape.isRequired,
1718
label: PropTypes.node.isRequired,
19+
lang: languageShape.isRequired,
1820
optimisticToggle: PropTypes.bool.isRequired,
1921
showNodeIcon: PropTypes.bool.isRequired,
2022
treeId: PropTypes.string.isRequired,
@@ -110,7 +112,7 @@ class TreeNode extends React.Component {
110112
}
111113

112114
renderCollapseButton() {
113-
const { expandDisabled } = this.props;
115+
const { expandDisabled, lang } = this.props;
114116

115117
if (!this.hasChildren()) {
116118
return (
@@ -124,7 +126,7 @@ class TreeNode extends React.Component {
124126
<Button
125127
className="rct-collapse rct-collapse-btn"
126128
disabled={expandDisabled}
127-
title="Toggle"
129+
title={lang.toggle}
128130
onClick={this.onExpand}
129131
>
130132
{this.renderCollapseIcon()}

src/js/languageShape.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import PropTypes from 'prop-types';
2+
3+
export default {
4+
collapseAll: PropTypes.string.isRequired,
5+
expandAll: PropTypes.string.isRequired,
6+
toggle: PropTypes.string.isRequired,
7+
};

test/CheckboxTree.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,20 @@ describe('<CheckboxTree />', () => {
8686
});
8787
});
8888

89+
describe('lang', () => {
90+
it('should override default language values', () => {
91+
const wrapper = shallow(
92+
<CheckboxTree
93+
lang={{ expandAll: 'Expand it', collapseAll: 'Collapse it' }}
94+
nodes={[]}
95+
showExpandAll
96+
/>,
97+
);
98+
99+
assert.equal('Expand it', wrapper.find('.rct-option-expand-all').prop('title'));
100+
});
101+
});
102+
89103
describe('nativeCheckboxes', () => {
90104
it('should add the class rct-native-display to the root', () => {
91105
const wrapper = shallow(

test/TreeNode.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ const baseProps = {
99
disabled: false,
1010
expandDisabled: false,
1111
expanded: false,
12+
lang: {
13+
toggle: 'Toggle',
14+
},
1215
icons: {
1316
check: <span className="rct-icon rct-icon-check" />,
1417
uncheck: <span className="rct-icon rct-icon-uncheck" />,

0 commit comments

Comments
 (0)