Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | 527x 527x 527x 527x 143x 527x 2656x 2656x 2043x 2656x 345x 2656x 527x 527x 9x 527x 182x 527x 1x 8x 8x 8x 16x 16x 8x 16x 1x 8x 521x 521x 2x 519x | /*
* Part of Pleiar.no - a collection of tools for nurses
*
* Copyright (C) Fagforbundet 2020
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
// @flow
import * as React from 'react';
import { Row, Col, Input, Label } from 'reactstrap';
import { getValueFromLiteralOrEvent } from '../../helper.general';
import type { NEWSOptionList } from '../news';
/**
* A single entry used for {@link OptionSelector}.
* (this is explicitly not exact, so that any of our users can use a
* datastructure that they already have other items in)
*/
export type OptionSelectorKey = string;
export type OptionSelectorEntry = { //eslint-disable-line flowtype/require-exact-type
+key: OptionSelectorKey,
+name?: string,
...
};
/**
* The list of {@link OptionSelectorEntry} items that is used by @{link OptionSelector}.
*/
export type OptionSelectorList = Array<OptionSelectorEntry> | NEWSOptionList;
/**
* The required props to render any subcomponent of OptionSelector (and thus also OptionSelector)
*/
type OptionSelectorSubcomponentProps = {|
options: OptionSelectorList,
label: string | React.Node | Array<React.Node>,
placeholder?: string,
onChange: (string) => void,
selected?: string | null
|};
/**
* This component is used by {@link OptionSelector} to render a set of options
* as a select-element. This is considered an implementation detail of
* OptionSelector, and you should only use OptionSelector directly, never
* OptionSelectorRadioButtons. You can tell OptionSelector that you only want
* select-element.
*/
class OptionSelectorSelectElement extends React.Component<OptionSelectorSubcomponentProps>
{
/**
* Render the component
*/
render (): React.Node
{
const list = [];
let selected: ?string | null = this.props.selected;
let seenSelectedKey: boolean = false;
if(selected === null)
{
selected = "";
}
for(const option of this.props.options)
{
let name: string = option.key;
if(option.name)
{
name = option.name;
}
if(option.key === selected)
{
seenSelectedKey = true;
}
list.push(<option key={option.key} value={option.key}>{name}</option>);
}
let placeholder: ?string = this.props.placeholder;
if(placeholder == null || placeholder == "")
{
placeholder = '- Velg -';
}
if(selected === undefined || selected === null || selected === "" || seenSelectedKey === false)
{
list.unshift(<option value="" key="dummy-placeholder">{placeholder}</option>);
}
return <Row className="option-selector option-selector-select">
<Col md="4">
{this.props.label}
</Col>
<Col className="form-inline">
<select value={selected} onChange={(ev) => { this.props.onChange(getValueFromLiteralOrEvent(ev));}}>
{list}
</select>
</Col>
</Row>;
}
}
/**
* This component is used by {@link OptionSelector} to render a set of options
* as radio buttons. This is considered an implementation detail of
* OptionSelector, and you should only use OptionSelector directly, never
* OptionSelectorRadioButtons. You can tell OptionSelector that you only want
* radio buttons.
*/
class OptionSelectorRadioButtons extends React.Component<OptionSelectorSubcomponentProps>
{
/**
* Render the component
*/
render (): React.Node
{
const { selected } = this.props;
const list = [];
for(const option of this.props.options)
{
let name: string = option.key;
if(option.name)
{
name = option.name;
}
list.push(<Label key={option.key} className="mr-3 ml-3 ml-sm-0" check>
<Input type="radio" name={option.key} checked={selected === option.key} onChange={() =>{ this.props.onChange(option.key); }}/>{' '}
{name}
</Label>);
}
return <Row className="option-selector option-selector-radio">
<Col md="4">
{this.props.label}
</Col>
<Col className="form-inline form-check">
{list}
</Col>
</Row>;
}
}
type OptionSelectorProps = {|
mode?: "radio" | "select",
...OptionSelectorSubcomponentProps
|};
/**
* This renders a generic "option selector" element. This can be either a set
* of radio buttons, or a <select>-element.
*
* When you don't specify anything, the `OptionSelector` itself decides which
* to render. It can use multiple heuristics to decide this, including device
* type and number of elements (though it does neither at the moment).
*/
class OptionSelector extends React.Component<OptionSelectorProps>
{
/**
* Render the component
*/
render (): React.Node
{
const { mode, ...passProps } = this.props;
if(mode === 'radio')
{
return <OptionSelectorRadioButtons {...passProps} />;
}
else
{
// FIXME: Should differentiate between different OptionSelectors
return <OptionSelectorSelectElement {...passProps} />;
}
}
}
export default OptionSelector;
// Exports for tests only
export { OptionSelector, OptionSelectorSelectElement, OptionSelectorRadioButtons };
|