From 40359a9b68a8cf4d1b5eaee07a6d4923a2183c2b Mon Sep 17 00:00:00 2001 From: Pritesh Nandgaonkar Date: Thu, 30 Jan 2020 05:56:10 -0800 Subject: [PATCH] Added a radio button when Listview is used for single select Summary: Added radio buttons when the listview enforces single selection Reviewed By: mweststrate Differential Revision: D19626710 fbshipit-source-id: 9220fad7b0825cac5ba39def4d5e85d671c61da7 --- src/chrome/ListView.tsx | 37 +++++++++++++++++++++++------- src/ui/components/Radio.tsx | 45 +++++++++++++++++++++++++++++++++++++ src/ui/index.tsx | 1 + 3 files changed, 75 insertions(+), 8 deletions(-) create mode 100644 src/ui/components/Radio.tsx diff --git a/src/chrome/ListView.tsx b/src/chrome/ListView.tsx index dbcb94da8..c229c03d6 100644 --- a/src/chrome/ListView.tsx +++ b/src/chrome/ListView.tsx @@ -15,6 +15,7 @@ import { Button, Spacer, Checkbox, + Radio, colors, View, Tooltip, @@ -91,11 +92,20 @@ type RowComponentProps = { onChange: (name: string, selected: boolean) => void; disable: boolean; toolTipMessage?: string; + type: SelectionType; }; class RowComponent extends Component { render() { - const {id, label, selected, onChange, disable, toolTipMessage} = this.props; + const { + id, + label, + selected, + onChange, + disable, + toolTipMessage, + type, + } = this.props; return ( { {label} - { - onChange(id, selected); - }} - /> + {type === 'multiple' && ( + { + onChange(id, selected); + }} + /> + )} + {type === 'single' && ( + { + onChange(id, selected); + }} + /> + )} @@ -159,7 +179,7 @@ export default class ListView extends Component { }; render() { - const {onSubmit} = this.props; + const {onSubmit, type} = this.props; return ( @@ -171,6 +191,7 @@ export default class ListView extends Component { id={id} label={label} key={id} + type={type} selected={this.state.selectedElements.has(id)} onChange={this.handleChange} disable={unselectable != null} diff --git a/src/ui/components/Radio.tsx b/src/ui/components/Radio.tsx new file mode 100644 index 000000000..67a86fe1f --- /dev/null +++ b/src/ui/components/Radio.tsx @@ -0,0 +1,45 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @format + */ + +import {PureComponent} from 'react'; +import styled from '@emotion/styled'; +import React from 'react'; + +type RadioProps = { + /** Whether the radio button is checked. */ + checked: boolean; + /** Called when a state change is triggered */ + onChange: (selected: boolean) => void; +}; + +const RadioboxContainer = styled.input({ + display: 'inline-block', + marginRight: 5, + verticalAlign: 'middle', +}); +RadioboxContainer.displayName = 'Radiobox:RadioboxContainer'; + +/** + * A radio button to toggle UI state + */ +export default class Radio extends PureComponent { + onChange = (e: React.ChangeEvent) => { + this.props.onChange(e.target.checked); + }; + + render() { + return ( + + ); + } +} diff --git a/src/ui/index.tsx b/src/ui/index.tsx index 0d69de82f..d33c80f5f 100644 --- a/src/ui/index.tsx +++ b/src/ui/index.tsx @@ -62,6 +62,7 @@ export {default as MultiLineInput} from './components/MultiLineInput'; export {default as Textarea} from './components/Textarea'; export {default as Select} from './components/Select'; export {default as Checkbox} from './components/Checkbox'; +export {default as Radio} from './components/Radio'; // code export {default as CodeBlock} from './components/CodeBlock';