|
| 1 | +import { |
| 2 | + QComboBox, |
| 3 | + NodeWidget, |
| 4 | + QSize, |
| 5 | + QVariant, |
| 6 | + SizeAdjustPolicy, |
| 7 | + InsertPolicy, |
| 8 | + QIcon |
| 9 | +} from "@nodegui/nodegui"; |
| 10 | +import { ViewProps, setViewProps } from "../View/RNView"; |
| 11 | +import { RNWidget } from "../config"; |
| 12 | +import { throwUnsupported } from "../../utils/helpers"; |
| 13 | + |
| 14 | +export interface ComboBoxProps extends ViewProps { |
| 15 | + items?: ComboBoxItem[]; |
| 16 | + count?: number; |
| 17 | + iconSize?: QSize; |
| 18 | + frame?: boolean; |
| 19 | + currentIndex?: number; |
| 20 | + currentData?: QVariant; |
| 21 | + currentText?: string; |
| 22 | + duplicatesEnabled?: boolean; |
| 23 | + editable?: boolean; |
| 24 | + insertPolicy?: InsertPolicy; |
| 25 | + maxCount?: number; |
| 26 | + maxVisibleItems?: number; |
| 27 | + minimumContentsLength?: number; |
| 28 | + modelColumn?: number; |
| 29 | + sizeAdjustPolicy?: SizeAdjustPolicy; |
| 30 | +} |
| 31 | + |
| 32 | +type ComboBoxItem = { |
| 33 | + text: string; |
| 34 | + icon?: QIcon; |
| 35 | + userData?: QVariant; |
| 36 | +}; |
| 37 | + |
| 38 | +const setComboBoxProps = ( |
| 39 | + widget: RNComboBox, |
| 40 | + newProps: ComboBoxProps, |
| 41 | + oldProps: ComboBoxProps |
| 42 | +) => { |
| 43 | + const setter: ComboBoxProps = { |
| 44 | + set items(items: ComboBoxItem[]) { |
| 45 | + widget.clear(); |
| 46 | + items.forEach(item => { |
| 47 | + widget.addItem(item.icon, item.text, item.userData); |
| 48 | + }); |
| 49 | + }, |
| 50 | + set count(count: number) { |
| 51 | + widget.setProperty("count", count); |
| 52 | + }, |
| 53 | + set iconSize(iconSize: QSize) { |
| 54 | + widget.setProperty("iconSize", iconSize); |
| 55 | + }, |
| 56 | + set frame(frame: boolean) { |
| 57 | + widget.setProperty("frame", frame); |
| 58 | + }, |
| 59 | + set currentIndex(currentIndex: number) { |
| 60 | + widget.setProperty("currentIndex", currentIndex); |
| 61 | + }, |
| 62 | + set currentData(value: QVariant) { |
| 63 | + widget.setProperty("currentData", value); |
| 64 | + }, |
| 65 | + set currentText(text: string) { |
| 66 | + widget.setProperty("currentText", text); |
| 67 | + }, |
| 68 | + set duplicatesEnabled(enabled: boolean) { |
| 69 | + widget.setProperty("duplicatesEnabled", enabled); |
| 70 | + }, |
| 71 | + set editable(enabled: boolean) { |
| 72 | + widget.setProperty("editable", enabled); |
| 73 | + }, |
| 74 | + set insertPolicy(policy: InsertPolicy) { |
| 75 | + widget.setProperty("insertPolicy", policy); |
| 76 | + }, |
| 77 | + set maxCount(count: number) { |
| 78 | + widget.setProperty("maxCount", count); |
| 79 | + }, |
| 80 | + set maxVisibleItems(count: number) { |
| 81 | + widget.setProperty("maxVisibleItems", count); |
| 82 | + }, |
| 83 | + set minimumContentsLength(count: number) { |
| 84 | + widget.setProperty("minimumContentsLength", count); |
| 85 | + }, |
| 86 | + set modelColumn(column: number) { |
| 87 | + widget.setProperty("modelColumn", column); |
| 88 | + }, |
| 89 | + set sizeAdjustPolicy(policy: SizeAdjustPolicy) { |
| 90 | + widget.setProperty("sizeAdjustPolicy", policy); |
| 91 | + } |
| 92 | + }; |
| 93 | + Object.assign(setter, newProps); |
| 94 | + setViewProps(widget, newProps, oldProps); |
| 95 | +}; |
| 96 | + |
| 97 | +/** |
| 98 | + * @ignore |
| 99 | + */ |
| 100 | +export class RNComboBox extends QComboBox implements RNWidget { |
| 101 | + setProps(newProps: ComboBoxProps, oldProps: ComboBoxProps): void { |
| 102 | + setComboBoxProps(this, newProps, oldProps); |
| 103 | + } |
| 104 | + appendInitialChild(child: NodeWidget): void { |
| 105 | + throwUnsupported(this); |
| 106 | + } |
| 107 | + appendChild(child: NodeWidget): void { |
| 108 | + throwUnsupported(this); |
| 109 | + } |
| 110 | + insertBefore(child: NodeWidget, beforeChild: NodeWidget): void { |
| 111 | + throwUnsupported(this); |
| 112 | + } |
| 113 | + removeChild(child: NodeWidget): void { |
| 114 | + throwUnsupported(this); |
| 115 | + } |
| 116 | + static tagName = "combobox"; |
| 117 | +} |
0 commit comments