-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathApp.start.js
More file actions
83 lines (75 loc) · 2.21 KB
/
Copy pathApp.start.js
File metadata and controls
83 lines (75 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
Follow the WAI ARIA Radio Group example at:
https://www.w3.org/TR/wai-aria-practices-1.1/examples/radio/radio-1/radio-1.html
- Turn the span into a button to get keyboard and focus events
- Use tabIndex to allow only the active button to be tabbable
- Use left/right arrows to select the next/previous radio button
- Tip: you can figure out the next value with React.Children.forEach(fn),
or React.Children.toArray(children).reduce(fn)
- Move the focus in cDU to the newly selected item
- Tip: do it in RadioButton not RadioGroup
- Tip: you'll need a ref
- Add the aria attributes
- radiogroup
- radio
- aria-checked
- aria-label on the icons
*/
import React, { Component } from "react";
import FaPlay from "react-icons/lib/fa/play";
import FaPause from "react-icons/lib/fa/pause";
import FaForward from "react-icons/lib/fa/forward";
import FaBackward from "react-icons/lib/fa/backward";
class RadioGroup extends Component {
state = {
value: this.props.defaultValue
};
render() {
const children = React.Children.map(this.props.children, child => {
return React.cloneElement(child, {
isActive: child.props.value === this.state.value,
onSelect: () => this.setState({ value: child.props.value })
});
});
return (
<fieldset className="radio-group">
<legend>{this.props.legend}</legend>
{children}
</fieldset>
);
}
}
class RadioButton extends Component {
render() {
const { isActive, onSelect } = this.props;
const className = "radio-button " + (isActive ? "active" : "");
return (
<span className={className} onClick={onSelect}>
{this.props.children}
</span>
);
}
}
class App extends Component {
render() {
return (
<div>
<RadioGroup defaultValue="pause" legend="Radio Group">
<RadioButton value="back">
<FaBackward />
</RadioButton>
<RadioButton value="play">
<FaPlay />
</RadioButton>
<RadioButton value="pause">
<FaPause />
</RadioButton>
<RadioButton value="forward">
<FaForward />
</RadioButton>
</RadioGroup>
</div>
);
}
}
export default App;