|
| 1 | +import { registerComponent } from "../config"; |
| 2 | +import { QSpinBox } from "@nodegui/nodegui"; |
| 3 | +import { ViewProps, setProps as setViewProps } from "../View"; |
| 4 | + |
| 5 | +type Range = { |
| 6 | + minimum: number; |
| 7 | + maximum: number; |
| 8 | +}; |
| 9 | + |
| 10 | +export interface SpinBoxProps extends ViewProps { |
| 11 | + prefix?: string; |
| 12 | + suffix?: string; |
| 13 | + singleStep?: number; |
| 14 | + range?: Range; |
| 15 | + value?: number; |
| 16 | +} |
| 17 | + |
| 18 | +export const setProps = ( |
| 19 | + widget: QSpinBox, |
| 20 | + newProps: SpinBoxProps, |
| 21 | + oldProps: SpinBoxProps |
| 22 | +) => { |
| 23 | + const setter: SpinBoxProps = { |
| 24 | + set prefix(prefix: string) { |
| 25 | + widget.setPrefix(prefix); |
| 26 | + }, |
| 27 | + set suffix(suffix: string) { |
| 28 | + widget.setSuffix(suffix); |
| 29 | + }, |
| 30 | + set singleStep(step: number) { |
| 31 | + widget.setSingleStep(step); |
| 32 | + }, |
| 33 | + set range(range: Range) { |
| 34 | + widget.setRange(range.minimum, range.maximum); |
| 35 | + }, |
| 36 | + set value(value: number) { |
| 37 | + widget.setValue(value); |
| 38 | + } |
| 39 | + }; |
| 40 | + Object.assign(setter, newProps); |
| 41 | + setViewProps(widget, newProps, oldProps); |
| 42 | +}; |
| 43 | + |
| 44 | +export const SpinBox = registerComponent<SpinBoxProps>({ |
| 45 | + id: "SpinBox", |
| 46 | + getContext() { |
| 47 | + return {}; |
| 48 | + }, |
| 49 | + shouldSetTextContent: () => { |
| 50 | + return true; |
| 51 | + }, |
| 52 | + createInstance: newProps => { |
| 53 | + const widget = new QSpinBox(); |
| 54 | + setProps(widget, newProps, {}); |
| 55 | + return widget; |
| 56 | + }, |
| 57 | + finalizeInitialChildren: () => { |
| 58 | + return false; |
| 59 | + }, |
| 60 | + commitMount: (instance, newProps, internalInstanceHandle) => { |
| 61 | + return; |
| 62 | + }, |
| 63 | + prepareUpdate: ( |
| 64 | + instance, |
| 65 | + oldProps, |
| 66 | + newProps, |
| 67 | + rootContainerInstance, |
| 68 | + hostContext |
| 69 | + ) => { |
| 70 | + return true; |
| 71 | + }, |
| 72 | + commitUpdate: (instance, updatePayload, oldProps, newProps, finishedWork) => { |
| 73 | + setProps(instance as QSpinBox, newProps, oldProps); |
| 74 | + } |
| 75 | +}); |
0 commit comments