Skip to content

Commit 81172fc

Browse files
committed
Display choice title for selected choices in multiselect input
1 parent ae76864 commit 81172fc

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

src/components/form.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from 'react';
22
import Button from './buttons';
33
import Loader from './loaders';
44
import {TimePicker} from './widgets';
5-
import {EditorContext, getCsrfCookie, capitalize, convertType, getCoordsFromName} from '../util';
5+
import {EditorContext, getCsrfCookie, capitalize, convertType, getCoordsFromName, choicesValueTitleMap} from '../util';
66

77

88
export function Label(props) {
@@ -213,6 +213,7 @@ export class FormMultiSelectInput extends React.Component {
213213
inputRef={this.input}
214214
onClick={this.toggleOptions}
215215
value={this.props.value}
216+
options={this.props.options}
216217
onChange={this.handleChange}
217218
disabled={this.props.readOnly}
218219
placeholder={this.props.placeholder}
@@ -251,6 +252,7 @@ export class FormMultiSelectInputField extends React.Component {
251252
}
252253

253254
render() {
255+
let valueTitleMap = choicesValueTitleMap(this.props.options)
254256
return (
255257
<div
256258
className="rjf-multiselect-field-input"
@@ -262,7 +264,7 @@ export class FormMultiSelectInputField extends React.Component {
262264
this.props.value.length ?
263265
this.props.value.map((item, index) => (
264266
<span className="rjf-multiselect-field-input-item" key={item + '_' + index}>
265-
<span>{item}</span>
267+
<span>{valueTitleMap[item]}</span>
266268
{this.props.disabled || <button title="Remove" type="button" onClick={(e) => this.handleRemove(e, index)}>&times;</button>}
267269
</span>
268270
)

src/util.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,33 @@ export function getKey(obj, key, default_value) {
190190
}
191191

192192

193+
export function choicesValueTitleMap(choices) {
194+
/* Returns a mapping of {value: title} for the given choices.
195+
* E.g.:
196+
* Input: [{'title': 'One', 'value': 1}, 2]
197+
* Output: {1: 'One', 2: 2}
198+
*/
199+
200+
let map = {};
201+
202+
for (let i = 0; i < choices.length; i++) {
203+
let choice = choices[i];
204+
let value, title;
205+
if (actualType(choice) === 'object') {
206+
value = choice.value;
207+
title = choice.title;
208+
} else {
209+
value = choice;
210+
title = choice;
211+
}
212+
213+
map[value] = title;
214+
}
215+
216+
return map;
217+
}
218+
219+
193220
export function valueInChoices(schema, value) {
194221
/* Checks whether the given value is in schema choices or not.
195222
If schema doesn't have choices, returns true.

0 commit comments

Comments
 (0)