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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | 15x 13x 2x 2x 2x 36x 19x 49x 13x 13x 13x 11x 1x 10x 11x 13x 11x 14x 11x 5x 15x 13x 2x 2x 2x 2x 2x 2x 13x 13x 1x 12x 17x 17x 17x 17x 34x 45x | import React, { useCallback, useMemo, useState } from 'react';
import { useVariables } from '@ska-octopus-widget-sdk/widget-sdk';
import styles from './VariableSelectorWidget.module.css';
import type { CSSProperties } from 'react';
export interface VariableSelectorWidgetProps {
/** Options shown in every dropdown */
values: string[];
/** Only show these variable keys (if empty or undefined, show all) */
allowedVars?: string | string[];
/** Position of label relative to selector */
labelPosition?: 'left' | 'right' | 'top' | 'bottom';
/** Base text size for labels and dropdowns */
fontSize?: string;
}
/* Safely coerce any host-provided value to a string without throwing. */
function safeToString(v: unknown): string {
if (v === null || v === undefined) return '';
if (typeof v === 'string') return v;
try {
const asJson = JSON.stringify(v);
return typeof asJson === 'string' ? asJson : String(v);
} catch {
return String(v);
}
}
function syncSelectedOptionAttributes(select: HTMLSelectElement | null, selected: string) {
if (!select) return;
for (const option of Array.from(select.options)) {
option.toggleAttribute('selected', option.value === selected);
}
}
export default function VariableSelectorWidget({
values,
allowedVars,
labelPosition = 'left',
fontSize = '12px'
}: VariableSelectorWidgetProps) {
const [localValues, setLocalValues] = useState<Record<string, string>>({});
const { vars = {}, setVariable } = useVariables() as {
vars?: Record<string, unknown>;
setVariable: (key: string, val: string) => void;
};
const allowedVarKeys = useMemo(() => {
if (Array.isArray(allowedVars)) {
return allowedVars.map((item) => String(item ?? '').trim()).filter(Boolean);
}
const single = String(allowedVars ?? '').trim();
return single ? [single] : [];
}, [allowedVars]);
const entries = useMemo(() => {
const v = vars ?? {};
let keys = Object.keys(v).sort((a, b) => a.localeCompare(b));
if (allowedVarKeys.length > 0) {
keys = keys.filter((k) => allowedVarKeys.includes(k));
}
return keys.map((k) => [k, (v as any)[k]] as const);
}, [vars, allowedVarKeys]);
const handleChange = useCallback(
(e: React.ChangeEvent<HTMLSelectElement>) => {
const key = e.currentTarget.name;
const value = e.currentTarget.value;
setLocalValues((prev) => ({ ...prev, [key]: value }));
syncSelectedOptionAttributes(e.currentTarget, value);
try {
setVariable(key, value);
} catch {
// Swallow to avoid UI crashes
}
},
[setVariable]
);
const containerStyle = { fontSize } as CSSProperties;
if (entries.length === 0) {
return <div className={styles.empty}>No variables defined.</div>;
}
return (
<div className={styles.container} style={containerStyle}>
{entries.map(([k, v]) => {
const selected = localValues[k] ?? safeToString(v);
const base = Array.isArray(values) ? values : [];
const opts = base.includes(selected) ? base : [...base, selected];
return (
<div
key={k}
className={`${styles.row} ${styles[`layout${labelPosition}`]}`}
data-label-position={labelPosition}
>
<span className={styles.label}>{k}</span>
<select
className={styles.select}
name={k}
value={selected}
onChange={handleChange}
ref={(node) => syncSelectedOptionAttributes(node, selected)}
aria-label={`Value for ${k}`}
>
{opts.map((o) => (
<option key={`${k}::${o}`} value={o}>
{o === '' ? '(empty)' : o}
</option>
))}
</select>
</div>
);
})}
</div>
);
}
|