@onereach/channel-transformer
Version:
Convert data model between different channels (Slack, Microsoft Teams, Generic...)
60 lines (50 loc) • 1.72 kB
text/typescript
import { IRWCAnswerComponent } from "../../../types/";
import { IGenericRange } from "../../../../generic/types";
enum DEFAULT_VALUES {
MIN = "`0`",
MAX = "`100`",
STEP = "1"
}
const setValue = (value: string, def: string): string => {
if (value && typeof value === 'string') {
const newVal = value.replace(/`/g, "");
if (newVal) return value;
}
return def;
};
export const transformSlider = (msg: IRWCAnswerComponent): IGenericRange => {
const min: string = setValue(msg.vBind?.min, DEFAULT_VALUES.MIN);
const max: string = setValue(msg.vBind?.max, DEFAULT_VALUES.MAX);
const step: string = setValue(msg.vBind?.step, DEFAULT_VALUES.STEP);
const value: [number | string, number | string] = [min, max];
const result: IGenericRange = {
type: 'range',
props: {
value,
min,
max,
step,
unit: msg.vBind?.unit,
unitPosition: msg.vBind?.unitPosition,
rwc: {
mode: msg.vBind?.mode || "single",
useCustomLabels: msg.vBind?.useCustomLabels || false
}
}
};
if (result.props.rwc.useCustomLabels) {
result.props.rwc.labelFunction = msg.vBind?.labelFunction,
result.props.rwc.options = msg.vBind?.options;
}
if (result.props.rwc.mode === "single") {
const defaultValue = setValue(msg.vBind?.defaultValue, min);
result.props.rwc.defaultValue = defaultValue;
}
if (result.props.rwc.mode === "range") {
const defaultStart = setValue(msg.vBind?.defaultValueStart, max);
const defaultEnd = setValue(msg.vBind?.defaultValueEnd, max);
result.props.rwc.defaultValueStart = defaultStart;
result.props.rwc.defaultValueEnd = defaultEnd;
}
return result;
};