From 1a04e6093df767c640702c0d8962181971cb56e0 Mon Sep 17 00:00:00 2001 From: MapleLeaf <19603573+itsMapleLeaf@users.noreply.github.com> Date: Mon, 10 Jan 2022 22:15:53 -0600 Subject: [PATCH] clearer select option resolution --- .../library/core/components/select.tsx | 30 ++++++++++++------- packages/reacord/library/internal/message.ts | 9 ++++-- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/packages/reacord/library/core/components/select.tsx b/packages/reacord/library/core/components/select.tsx index 3519091..c8fb79d 100644 --- a/packages/reacord/library/core/components/select.tsx +++ b/packages/reacord/library/core/components/select.tsx @@ -4,7 +4,11 @@ import React from "react" import { isInstanceOf } from "../../../helpers/is-instance-of" import { ReacordElement } from "../../internal/element.js" import type { ComponentInteraction } from "../../internal/interaction" -import type { ActionRow, MessageOptions } from "../../internal/message" +import type { + ActionRow, + ActionRowItem, + MessageOptions, +} from "../../internal/message" import { Node } from "../../internal/node.js" import type { ComponentEvent } from "../component-event" import { OptionNode } from "./option-node" @@ -108,19 +112,25 @@ class SelectNode extends Node { ...props } = this.props - actionRow.push({ + const item: ActionRowItem = { ...props, type: "select", customId: this.customId, options, - // I'm not counting on people using value and values at the same time, - // but maybe we should resolve this differently anyhow? e.g. one should override the other - // or just warn if there are both - // or... try some other alternative design entirely - values: [...(values || []), ...(value ? [value] : [])], - minValues: multiple ? minValues : undefined, - maxValues: multiple ? Math.max(minValues, maxValues) : undefined, - }) + values: [], + } + + if (multiple) { + item.minValues = minValues + item.maxValues = maxValues + if (values) item.values = values + } + + if (!multiple && value != undefined) { + item.values = [value] + } + + actionRow.push(item) } override handleComponentInteraction( diff --git a/packages/reacord/library/internal/message.ts b/packages/reacord/library/internal/message.ts index a670a12..246f8ac 100644 --- a/packages/reacord/library/internal/message.ts +++ b/packages/reacord/library/internal/message.ts @@ -9,9 +9,12 @@ export type MessageOptions = { actionRows: ActionRow[] } -export type ActionRow = Array< - MessageButtonOptions | MessageLinkOptions | MessageSelectOptions -> +export type ActionRow = ActionRowItem[] + +export type ActionRowItem = + | MessageButtonOptions + | MessageLinkOptions + | MessageSelectOptions export type MessageButtonOptions = { type: "button"