UNPKG

scriptable-testlab

Version:

A lightweight, efficient tool designed to manage and update scripts for Scriptable.

210 lines (188 loc) 5.04 kB
import {AbsUITableRow} from 'scriptable-abstract'; import {MockImage} from '../../media/image'; import {MockColor} from '../color'; import {MockUITableCell} from './ui-table-cell'; interface UITableRowState { isHeader: boolean; height: number; backgroundColor: MockColor | null; cellSpacing: number; cells: MockUITableCell[]; dismissOnSelect: boolean; onSelect: (() => void) | null; } /** * Mock implementation of Scriptable's UITableRow * Represents a row in a UITable */ export class MockUITableRow extends AbsUITableRow<UITableRowState> { constructor() { super({ isHeader: false, height: 44, // iOS 默认行高 backgroundColor: null, cellSpacing: 10, // iOS 默认间距 cells: [], dismissOnSelect: true, onSelect: null, }); } /** * Whether the row is a header row */ get isHeader(): boolean { return this.state.isHeader; } /** * Sets whether the row is a header row */ set isHeader(value: boolean) { if (typeof value !== 'boolean') { throw new Error('IsHeader must be a boolean'); } this.setState({isHeader: value}); } /** * Height of the row */ get height(): number { return this.state.height; } /** * Sets the height of the row */ set height(value: number) { if (typeof value !== 'number' || value <= 0) { throw new Error('Height must be a positive number'); } this.setState({height: value}); } /** * Background color of the row */ get backgroundColor(): MockColor | null { return this.state.backgroundColor; } /** * Sets the background color of the row */ set backgroundColor(value: MockColor | null) { this.setState({backgroundColor: value}); } /** * Spacing between cells */ get cellSpacing(): number { return this.state.cellSpacing; } /** * Sets the spacing between cells */ set cellSpacing(value: number) { if (typeof value !== 'number' || value < 0) { throw new Error('Cell spacing must be a non-negative number'); } this.setState({cellSpacing: value}); } /** * Whether to dismiss the table when the row is selected */ get dismissOnSelect(): boolean { return this.state.dismissOnSelect; } /** * Sets whether to dismiss the table when the row is selected */ set dismissOnSelect(value: boolean) { if (typeof value !== 'boolean') { throw new Error('DismissOnSelect must be a boolean'); } this.setState({dismissOnSelect: value}); } /** * Function to call when the row is selected */ get onSelect(): (() => void) | null { return this.state.onSelect; } /** * Sets the function to call when the row is selected */ set onSelect(value: (() => void) | null) { if (value !== null && typeof value !== 'function') { throw new Error('OnSelect must be a function or null'); } this.setState({onSelect: value}); } /** * Cells in the row */ get cells(): MockUITableCell[] { return this.state.cells; } /** * Adds a cell to the row * @param cell Cell to add to the row */ addCell(cell: MockUITableCell): void { if (!(cell instanceof MockUITableCell)) { throw new Error('Cell must be an instance of UITableCell'); } const cells = [...this.state.cells, cell]; this.setState({cells}); } /** * Adds a text cell to the row * @param title Optional title to show in the cell * @param subtitle Optional subtitle shown below the title in the cell * @returns The created cell */ addText(title?: string, subtitle?: string): MockUITableCell { // 使用 UITableCell 的静态工厂方法来创建文本单元格 const cell = MockUITableCell.text(title, subtitle); this.addCell(cell); return cell; } /** * Adds an image cell to the row * @param image Image to show in the cell * @returns The created cell */ addImage(image: MockImage): MockUITableCell { if (!image) { throw new Error('Image cannot be null'); } // 使用 UITableCell 的静态工厂方法来创建图片单元格 const cell = MockUITableCell.image(image); this.addCell(cell); return cell; } /** * Adds an image cell that loads from URL * @param url URL to image * @returns The created cell */ addImageAtURL(url: string): MockUITableCell { if (!url) { throw new Error('URL cannot be empty'); } // 使用 UITableCell 的静态工厂方法来创建图片单元格 const cell = MockUITableCell.imageAtURL(url); this.addCell(cell); return cell; } /** * Adds a button cell to the row * @param title Title of the button * @returns The created cell */ addButton(title: string): MockUITableCell { if (!title) { throw new Error('Button title cannot be empty'); } // 使用 UITableCell 的静态工厂方法来创建按钮单元格 const cell = MockUITableCell.button(title); this.addCell(cell); return cell; } }