Skip to content

Commit bafa3ab

Browse files
committed
Extract global actions into into a component
1 parent 05e73db commit bafa3ab

File tree

5 files changed

+55
-37
lines changed

5 files changed

+55
-37
lines changed

CHANGELOG.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,19 @@
44

55
### Breaking Changes
66

7+
#### Accessibility
8+
9+
* Hide the pseudo-checkbox from the accessibility tree
10+
* Change the clickable label from `role="link"` to `role="button"`
11+
712
#### Language
813

914
* Replaced `toggle` key in `lang` property with `collapseNode` and `expandNode`
1015

1116
#### Styling
1217

1318
* The `iconsClass` property now defaults to `"fa5"` for Font Awesome 5/6 instead of Font Awesome 4
14-
15-
#### Accessibility
16-
17-
* Hide the pseudo-checkbox from the accessibility tree
18-
* Change the clickable label from `role="link"` to `role="button"`
19+
* The `rct-options` and `rct-option` CSS classes are now `rct-actions` and `rct-action`
1920

2021
### New Features
2122

src/js/CheckboxTree.jsx

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { nanoid } from 'nanoid';
55
import PropTypes from 'prop-types';
66
import React from 'react';
77

8-
import Button from './components/Button';
8+
import GlobalActions from './components/GlobalActions';
99
import TreeNode from './components/TreeNode';
1010
import defaultLang from './lang/default';
1111
import iconsShape from './shapes/iconsShape';
@@ -288,35 +288,14 @@ class CheckboxTree extends React.Component {
288288
);
289289
}
290290

291-
renderExpandAll() {
292-
const {
293-
icons: { expandAll, collapseAll },
294-
lang,
295-
showExpandAll,
296-
} = this.props;
291+
renderGlobalOptions() {
292+
const { showExpandAll } = this.props;
297293

298294
if (!showExpandAll) {
299295
return null;
300296
}
301297

302-
return (
303-
<div className="rct-options">
304-
<Button
305-
className="rct-option rct-option-expand-all"
306-
title={lang.expandAll}
307-
onClick={this.onExpandAll}
308-
>
309-
{expandAll}
310-
</Button>
311-
<Button
312-
className="rct-option rct-option-collapse-all"
313-
title={lang.collapseAll}
314-
onClick={this.onCollapseAll}
315-
>
316-
{collapseAll}
317-
</Button>
318-
</div>
319-
);
298+
return <GlobalActions onCollapseAll={this.onCollapseAll} onExpandAll={this.onExpandAll} />;
320299
}
321300

322301
renderHiddenInput() {
@@ -377,7 +356,7 @@ class CheckboxTree extends React.Component {
377356
<div className={className} id={id}>
378357
<LanguageContext.Provider value={lang}>
379358
<IconContext.Provider value={mergedIcons}>
380-
{this.renderExpandAll()}
359+
{this.renderGlobalOptions()}
381360
{this.renderHiddenInput()}
382361
{treeNodes}
383362
</IconContext.Provider>

src/js/components/GlobalActions.jsx

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import PropTypes from 'prop-types';
2+
import React, { useContext } from 'react';
3+
4+
import { IconContext, LanguageContext } from '../contexts';
5+
import Button from './Button';
6+
7+
const propTypes = {
8+
onCollapseAll: PropTypes.func.isRequired,
9+
onExpandAll: PropTypes.func.isRequired,
10+
};
11+
12+
function GlobalActions({ onExpandAll, onCollapseAll }) {
13+
const { expandAll, collapseAll } = useContext(IconContext);
14+
const { expandAll: expandLang, collapseAll: collapseLang } = useContext(LanguageContext);
15+
16+
return (
17+
<div className="rct-actions">
18+
<Button
19+
className="rct-action rct-action-expand-all"
20+
title={expandLang}
21+
onClick={onExpandAll}
22+
>
23+
{expandAll}
24+
</Button>
25+
<Button
26+
className="rct-action rct-action-collapse-all"
27+
title={collapseLang}
28+
onClick={onCollapseAll}
29+
>
30+
{collapseAll}
31+
</Button>
32+
</div>
33+
);
34+
}
35+
36+
GlobalActions.propTypes = propTypes;
37+
38+
export default GlobalActions;

src/less/react-checkbox-tree.less

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,13 @@
8888
align-items: center;
8989
}
9090

91-
.rct-options {
91+
.rct-actions {
9292
flex: 0 0 auto;
9393
margin-left: .5rem;
9494
text-align: right;
9595
}
9696

97-
.rct-option {
97+
.rct-action {
9898
opacity: .75;
9999
border: 0;
100100
background: none;
@@ -106,7 +106,7 @@
106106
opacity: 1;
107107
}
108108

109-
+ .rct-option {
109+
+ & {
110110
margin-left: 2px;
111111
}
112112
}

src/scss/react-checkbox-tree.scss

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,13 @@ $rct-clickable-focus: rgba($rct-icon-color, .2) !default;
9494
align-items: center;
9595
}
9696

97-
.rct-options {
97+
.rct-actions {
9898
flex: 0 0 auto;
9999
margin-left: .5rem;
100100
text-align: right;
101101
}
102102

103-
.rct-option {
103+
.rct-action {
104104
opacity: .75;
105105
border: 0;
106106
background: none;
@@ -112,7 +112,7 @@ $rct-clickable-focus: rgba($rct-icon-color, .2) !default;
112112
opacity: 1;
113113
}
114114

115-
+ .rct-option {
115+
+ & {
116116
margin-left: 2px;
117117
}
118118
}

0 commit comments

Comments
 (0)