All files / js/widgets types.ts

0% Statements 0/6
0% Branches 0/4
0% Functions 0/1
0% Lines 0/6

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64                                                                                                                               
import { ComponentType } from "react";
import { Translatable } from "../i18n";
 
export interface BaseWidgetType {
  widget: string;
  name: string;
  size: "normal" | "small" | "large";
  width?: string;
}
 
export interface WidgetType extends BaseWidgetType {
  heading_i18n?: Translatable;
  checkbox_label_i18n?: Translatable;
  addon_label_i18n?: Translatable;
}
 
export interface MultiWidgetType extends BaseWidgetType {
  widget: "checkboxes" | "select";
  choices?: [string, Translatable][];
}
 
export interface Field {
  name: string;
  type: WidgetType;
  default: string | null;
  description_i18n: Translatable;
  label_i18n: Translatable;
  hide_label: boolean;
  alts_enabled: boolean | null;
}
 
export type WidgetProps<V = string, W = WidgetType> = {
  value?: V;
  type: W;
  placeholder?: V;
  onChange: (value: V, uiChange?: boolean) => void;
  disabled?: boolean;
};
 
interface SerializableWidget {
  deserializeValue: (value: string | undefined, type: WidgetType) => string;
  serializeValue: (value: string | undefined, type: WidgetType) => string;
}
 
type FakeWidget = ComponentType<{ field: Field }> &
  SerializableWidget & {
    isFakeWidget: true;
  };
type RealWidget = ComponentType<WidgetProps> &
  SerializableWidget & {
    isFakeWidget: undefined;
  };
export type WidgetComponent = FakeWidget | RealWidget;
 
export function getInputClass(type: WidgetType) {
  let rv = "form-control";
  if (type.size === "small") {
    rv = "input-sm " + rv;
  } else if (type.size === "large") {
    rv = "input-lg " + rv;
  }
  return rv;
}