Skip to content

Commit

Permalink
Merge pull request #753 from exadel-inc/feature/select-multiple-values
Browse files Browse the repository at this point in the history
feat(select-setting): ability to use more than one option value
  • Loading branch information
ala-n authored Jun 17, 2024
2 parents 97fab0a + fff3c73 commit 9faef4e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
10 changes: 10 additions & 0 deletions site/src/examples/list.less
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@
&.aqua {
background-color: aqua;
}

&.top {
align-content: start;
}
&.center {
align-content: center;
}
&.bottom {
align-content: end;
}
}

&-circle {
Expand Down
7 changes: 6 additions & 1 deletion site/views/examples/example/list.njk
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,12 @@ title: Example with list of cards
<uip-select-setting label="Card color:" target=".card-item" attribute="class" mode="append">
<option value="red">Red</option>
<option value="aqua">Aqua</option>
<option value="green">Green</option>
<option value="green green-clr">Green</option>
</uip-select-setting>
<uip-select-setting label="Text alignment:" target=".card-item" attribute="class" mode="append">
<option value="top">Top</option>
<option value="center">Center</option>
<option value="bottom bottom-alignment">Bottom</option>
</uip-select-setting>
<uip-bool-setting label="Wrap items" attribute="wrap-items"></uip-bool-setting>
</uip-settings>
Expand Down
14 changes: 9 additions & 5 deletions src/plugins/settings/select-setting/select-setting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,9 @@ export class UIPSelectSetting extends UIPSetting {
transformValue(value: string, attrValue: string | null): string | null {
if (!attrValue) return value || null;

const attrTokens = this.settingOptions.reduce((tokens, option) =>
TokenListUtils.remove(tokens, option), TokenListUtils.split(attrValue));
const attrTokens = this.settingOptions.reduce((tokens, option) => {
return TokenListUtils.split(option).reduce((token, optionItem) => TokenListUtils.remove(token, optionItem), tokens);
}, TokenListUtils.split(attrValue));
value && attrTokens.push(value);

return TokenListUtils.join(attrTokens);
Expand Down Expand Up @@ -125,15 +126,18 @@ export class UIPSelectSetting extends UIPSetting {
/** Updates setting's value for {@link mode} = "append" */
protected appendFrom(attrValues: (string | null)[]): void {
// array of each attribute's value intersection with select options
const valuesOptions = attrValues.map((val) => TokenListUtils.intersection(this.settingOptions, TokenListUtils.split(val)));
const valuesOptions: string[] = this.settingOptions.filter((option) => {
return TokenListUtils.split(option).length ===
TokenListUtils.intersection(TokenListUtils.split(option), TokenListUtils.split(attrValues[0])).length;
});

// make empty option active if no options intersections among attribute values
if (this.settingOptions.includes('') && valuesOptions.every((inter) => !inter.length)) {
return this.setValue('');
}

// common options among all attribute values
const commonOptions = TokenListUtils.intersection(...valuesOptions);
const commonOptions = TokenListUtils.intersection(valuesOptions);

if (this.multiple || commonOptions.length) return this.setValue(TokenListUtils.join(commonOptions));

Expand All @@ -146,7 +150,7 @@ export class UIPSelectSetting extends UIPSetting {

protected setValue(value: string): void {
this.$$off(this._onChange);
value.split(' ').forEach((opt) => this.$field.setSelected(opt, true));
this.$field.setSelected(value, true);
this.$$on(this._onChange);
}

Expand Down

0 comments on commit 9faef4e

Please sign in to comment.