UNPKG

@teaui/core

Version:

A high-level terminal UI library for Node

105 lines 3.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Radio = exports.Checkbox = void 0; const Container_1 = require("../Container"); const Text_1 = require("./Text"); const geometry_1 = require("../geometry"); const events_1 = require("../events"); const UI_1 = require("../UI"); class Checkbox extends Container_1.Container { #value = false; #hotKey; #onChange; #textView; constructor(props) { super(props); this.#textView = new Text_1.Text({ alignment: 'center' }); this.add(this.#textView); this.#update(props); } get value() { return this.#value; } set value(value) { if (value === this.#value) { return; } this.#value = value; this.invalidateRender(); } childTheme(view) { return (0, UI_1.childTheme)(super.childTheme(view), this.isPressed, this.isHover); } update(props) { this.#update(props); super.update(props); } #update({ title, hotKey, value, onChange }) { const styledText = hotKey ? (0, events_1.styleTextForHotKey)(title ?? '', hotKey) : title; this.#textView.text = styledText ?? ''; this.#value = value; this.#hotKey = hotKey; this.#onChange = onChange; } get title() { return this.#textView?.text; } set title(value) { const styledText = this.#hotKey ? (0, events_1.styleTextForHotKey)(value ?? '', this.#hotKey) : value; this.#textView.text = styledText ?? ''; this.invalidateSize(); } naturalSize(available) { return super.naturalSize(available).grow(CHECKBOX_WIDTH, 0); } receiveMouse(event, system) { super.receiveMouse(event, system); if ((0, events_1.isMouseClicked)(event)) { this.#value = !this.#value; this.#onChange?.(this.#value); } } render(viewport) { if (viewport.isEmpty) { return super.render(viewport); } viewport.registerMouse(['mouse.button.left', 'mouse.move']); const uiStyle = this.theme.ui({ isPressed: this.isPressed, isHover: this.isHover, }); viewport.paint(uiStyle); const boxWidth = CHECKBOX_WIDTH; const naturalSize = super.naturalSize(viewport.contentSize.shrink(boxWidth, 0)); const offset = new geometry_1.Point(boxWidth, Math.round((viewport.contentSize.height - naturalSize.height) / 2)); const box = this.boxChars()[this.#value ? 'checked' : 'unchecked']; viewport.write(box, geometry_1.Point.zero, uiStyle); viewport.clipped(new geometry_1.Rect(offset, naturalSize), uiStyle, inside => { super.render(inside); }); } boxChars() { return BOX.checkbox; } } exports.Checkbox = Checkbox; class Radio extends Checkbox { boxChars() { return BOX.radio; } } exports.Radio = Radio; const BOX = { checkbox: { unchecked: '☐ ', checked: '◼︎ ', }, radio: { unchecked: '◯ ', checked: '⦿ ', }, }; const CHECKBOX_WIDTH = 2; //# sourceMappingURL=Checkbox.js.map