/** * Horizontal switch component */ import * as React from "react"; import "./RadioSwitchComponent.less"; import { Icon } from "office-ui-fabric-react/lib/Icon"; import { NormalizedEventKey } from "../../../Common/Constants"; export interface Choice { key: string; onSelect: () => void; label: string; } export interface RadioSwitchComponentProps { choices: Choice[]; selectedKey: string; onSelectionKeyChange?: (newValue: string) => void; } export class RadioSwitchComponent extends React.Component { public render(): JSX.Element { return (
{this.props.choices.map((choice: Choice) => ( this.onSelect(choice)} onKeyPress={event => this.onKeyPress(event, choice)} > {choice.label} ))}
); } private onSelect(choice: Choice): void { this.props.onSelectionKeyChange && this.props.onSelectionKeyChange(choice.key); choice.onSelect(); } private onKeyPress(event: React.KeyboardEvent, choice: Choice): void { if (event.key === NormalizedEventKey.Enter || event.key === NormalizedEventKey.Space) { this.onSelect(choice); } } }