Skip to content

Commit a8b089c

Browse files
committed
choices text box refactoring: try to reuse choiceToken code a bit more
1 parent 9848ec6 commit a8b089c

File tree

3 files changed

+25
-35
lines changed

3 files changed

+25
-35
lines changed

app/client/widgets/ChoiceListEditor.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import {CellValue} from "app/common/DocActions";
1515
import {CompiledPredicateFormula} from 'app/common/PredicateFormula';
1616
import {EmptyRecordView} from 'app/common/RecordView';
1717
import {decodeObject, encodeObject} from 'app/plugin/objtypes';
18-
import {ChoiceOptions, getRenderFillColor, getRenderTextColor} from 'app/client/widgets/ChoiceTextBox';
19-
import {choiceToken, cssChoiceACItem, cssChoiceToken} from 'app/client/widgets/ChoiceToken';
18+
import {ChoiceOptions} from 'app/client/widgets/ChoiceTextBox';
19+
import {choiceToken, choiceTokenDomArgs, cssChoiceACItem} from 'app/client/widgets/ChoiceToken';
2020
import {icon} from 'app/client/ui2018/icons';
2121
import {dom, styled} from 'grainjs';
2222

@@ -95,17 +95,14 @@ export class ChoiceListEditor extends NewBaseEditor {
9595

9696
this._tokenField = TokenField.ctor<ChoiceItem>().create(this, {
9797
initialValue: startTokens,
98-
renderToken: item => [
98+
renderToken: item => choiceTokenDomArgs(
9999
item.isBlank ? '[Blank]' : item.label,
100-
dom.style('background-color', getRenderFillColor(this._choiceOptionsByName[item.label])),
101-
dom.style('color', getRenderTextColor(this._choiceOptionsByName[item.label])),
102-
dom.cls('font-bold', this._choiceOptionsByName[item.label]?.fontBold ?? false),
103-
dom.cls('font-underline', this._choiceOptionsByName[item.label]?.fontUnderline ?? false),
104-
dom.cls('font-italic', this._choiceOptionsByName[item.label]?.fontItalic ?? false),
105-
dom.cls('font-strikethrough', this._choiceOptionsByName[item.label]?.fontStrikethrough ?? false),
106-
cssChoiceToken.cls('-invalid', item.isInvalid),
107-
cssChoiceToken.cls('-blank', item.isBlank),
108-
],
100+
{
101+
...(this._choiceOptionsByName[item.label] || {}),
102+
invalid: item.isInvalid,
103+
blank: item.isBlank,
104+
},
105+
),
109106
createToken: label => new ChoiceItem(label, !this._choicesSet.has(label), label.trim() === ''),
110107
acOptions,
111108
openAutocompleteOnFocus: true,

app/client/widgets/ChoiceTextBox.ts

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,7 @@ import {cssLabel, cssRow} from 'app/client/ui/RightPanelStyles';
1414
import {testId, theme} from 'app/client/ui2018/cssVars';
1515
import {icon} from 'app/client/ui2018/icons';
1616
import {ChoiceListEntry} from 'app/client/widgets/ChoiceListEntry';
17-
import {
18-
choiceToken,
19-
DEFAULT_BACKGROUND_COLOR,
20-
DEFAULT_COLOR,
21-
getReadableColorsCombo,
22-
} from 'app/client/widgets/ChoiceToken';
17+
import {choiceToken} from 'app/client/widgets/ChoiceToken';
2318
import {NTextBox} from 'app/client/widgets/NTextBox';
2419
import {Computed, dom, styled} from 'grainjs';
2520

@@ -29,20 +24,6 @@ export type ChoiceOptionsByName = Map<string, IChoiceOptions | undefined>;
2924

3025
const t = makeT('ChoiceTextBox');
3126

32-
export function getRenderFillColor(choiceOptions?: IChoiceOptions) {
33-
if (!choiceOptions) {
34-
return DEFAULT_BACKGROUND_COLOR;
35-
}
36-
return getReadableColorsCombo(choiceOptions).bg;
37-
}
38-
39-
export function getRenderTextColor(choiceOptions?: IChoiceOptions) {
40-
if (!choiceOptions) {
41-
return DEFAULT_COLOR;
42-
}
43-
return getReadableColorsCombo(choiceOptions).fg;
44-
}
45-
4627
/**
4728
* ChoiceTextBox - A textbox for choice values.
4829
*/

app/client/widgets/ChoiceToken.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,22 @@ export function choiceToken(
3434
options: IChoiceTokenOptions,
3535
...args: DomElementArg[]
3636
): DomContents {
37+
return cssChoiceToken(choiceTokenDomArgs(label, options, ...args));
38+
}
39+
40+
/**
41+
* Exposes the choiceToken dom args outside of cssChoiceToken to allow
42+
* easy usage of them with TokenField#renderToken, that has its own wrapper dom el.
43+
*/
44+
export function choiceTokenDomArgs(
45+
label: DomElementArg,
46+
options: IChoiceTokenOptions,
47+
...args: DomElementArg[]
48+
): DomElementArg {
3749
const {fillColor, textColor, fontBold, fontItalic, fontUnderline,
38-
fontStrikethrough, invalid, blank} = options;
50+
fontStrikethrough, invalid, blank} = options;
3951
const {bg, fg} = getReadableColorsCombo({fillColor, textColor});
40-
return cssChoiceToken(
52+
return [
4153
label,
4254
dom.style('background-color', bg),
4355
dom.style('color', fg),
@@ -48,7 +60,7 @@ export function choiceToken(
4860
invalid ? cssChoiceToken.cls('-invalid') : null,
4961
blank ? cssChoiceToken.cls('-blank') : null,
5062
...args
51-
);
63+
];
5264
}
5365

5466
export const cssChoiceToken = styled('div', `

0 commit comments

Comments
 (0)