Fix param types

This commit is contained in:
Francisco Hodge 2021-12-10 00:10:55 -08:00
parent 5eaefbe342
commit 91843de873
6 changed files with 21 additions and 21 deletions

View File

@ -1,6 +1,6 @@
import "./css/Keyboard.css"; import "./css/Keyboard.css";
import PhysicalKeyboard from "../services/PhysicalKeyboard"; import PhysicalKeyboard from "../services/PhysicalKeyboard";
import { KeyboardOptions, KeyboardInput, KeyboardButtonElements, KeyboardHandlerEvent, KeyboardElement, KeyboardParams } from "../interfaces"; import { KeyboardOptions, KeyboardInput, KeyboardButtonElements, KeyboardHandlerEvent, KeyboardElement } from "../interfaces";
import CandidateBox from "./CandidateBox"; import CandidateBox from "./CandidateBox";
/** /**
* Root class for simple-keyboard. * Root class for simple-keyboard.
@ -42,11 +42,11 @@ declare class SimpleKeyboard {
* Creates an instance of SimpleKeyboard * Creates an instance of SimpleKeyboard
* @param {Array} params If first parameter is a string, it is considered the container class. The second parameter is then considered the options object. If first parameter is an object, it is considered the options object. * @param {Array} params If first parameter is a string, it is considered the container class. The second parameter is then considered the options object. If first parameter is an object, it is considered the options object.
*/ */
constructor(...params: KeyboardParams); constructor(selectorOrOptions?: string | HTMLDivElement | KeyboardOptions, keyboardOptions?: KeyboardOptions);
/** /**
* parseParams * parseParams
*/ */
handleParams: (params: KeyboardParams) => { handleParams: (selectorOrOptions?: string | HTMLDivElement | KeyboardOptions | undefined, keyboardOptions?: KeyboardOptions | undefined) => {
keyboardDOMClass: string; keyboardDOMClass: string;
keyboardDOM: KeyboardElement; keyboardDOM: KeyboardElement;
options: Partial<KeyboardOptions | undefined>; options: Partial<KeyboardOptions | undefined>;

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -15,7 +15,6 @@ export interface KeyboardButtonAttributes {
export interface KeyboardInput { export interface KeyboardInput {
[key: string]: string; [key: string]: string;
} }
export declare type KeyboardParams = [KeyboardOptions] | [string | HTMLDivElement, KeyboardOptions];
export declare type CandidateBoxParams = { export declare type CandidateBoxParams = {
utilities: Utilities; utilities: Utilities;
}; };

View File

@ -10,7 +10,6 @@ import {
KeyboardButtonElements, KeyboardButtonElements,
KeyboardHandlerEvent, KeyboardHandlerEvent,
KeyboardElement, KeyboardElement,
KeyboardParams,
} from "../interfaces"; } from "../interfaces";
import CandidateBox from "./CandidateBox"; import CandidateBox from "./CandidateBox";
@ -51,14 +50,17 @@ class SimpleKeyboard {
* Creates an instance of SimpleKeyboard * Creates an instance of SimpleKeyboard
* @param {Array} params If first parameter is a string, it is considered the container class. The second parameter is then considered the options object. If first parameter is an object, it is considered the options object. * @param {Array} params If first parameter is a string, it is considered the container class. The second parameter is then considered the options object. If first parameter is an object, it is considered the options object.
*/ */
constructor(...params: KeyboardParams) { constructor(
selectorOrOptions?: string | HTMLDivElement | KeyboardOptions,
keyboardOptions?: KeyboardOptions
) {
if (typeof window === "undefined") return; if (typeof window === "undefined") return;
const { const {
keyboardDOMClass, keyboardDOMClass,
keyboardDOM, keyboardDOM,
options = {}, options = {},
} = this.handleParams(params); } = this.handleParams(selectorOrOptions, keyboardOptions);
/** /**
* Initializing Utilities * Initializing Utilities
@ -232,7 +234,8 @@ class SimpleKeyboard {
* parseParams * parseParams
*/ */
handleParams = ( handleParams = (
params: KeyboardParams selectorOrOptions?: string | HTMLDivElement | KeyboardOptions,
keyboardOptions?: KeyboardOptions
): { ): {
keyboardDOMClass: string; keyboardDOMClass: string;
keyboardDOM: KeyboardElement; keyboardDOM: KeyboardElement;
@ -246,29 +249,29 @@ class SimpleKeyboard {
* If first parameter is a string: * If first parameter is a string:
* Consider it as an element's class * Consider it as an element's class
*/ */
if (typeof params[0] === "string") { if (typeof selectorOrOptions === "string") {
keyboardDOMClass = params[0].split(".").join(""); keyboardDOMClass = selectorOrOptions.split(".").join("");
keyboardDOM = document.querySelector( keyboardDOM = document.querySelector(
`.${keyboardDOMClass}` `.${keyboardDOMClass}`
) as KeyboardElement; ) as KeyboardElement;
options = params[1]; options = keyboardOptions;
/** /**
* If first parameter is an KeyboardElement * If first parameter is an KeyboardElement
* Consider it as the keyboard DOM element * Consider it as the keyboard DOM element
*/ */
} else if (params[0] instanceof HTMLDivElement) { } else if (selectorOrOptions instanceof HTMLDivElement) {
/** /**
* This element must have a class, otherwise throw * This element must have a class, otherwise throw
*/ */
if (!params[0].className) { if (!selectorOrOptions.className) {
console.warn("Any DOM element passed as parameter must have a class."); console.warn("Any DOM element passed as parameter must have a class.");
throw new Error("KEYBOARD_DOM_CLASS_ERROR"); throw new Error("KEYBOARD_DOM_CLASS_ERROR");
} }
keyboardDOMClass = params[0].className.split(" ")[0]; keyboardDOMClass = selectorOrOptions.className.split(" ")[0];
keyboardDOM = params[0]; keyboardDOM = selectorOrOptions;
options = params[1]; options = keyboardOptions;
/** /**
* Otherwise, search for .simple-keyboard DOM element * Otherwise, search for .simple-keyboard DOM element
@ -278,7 +281,7 @@ class SimpleKeyboard {
keyboardDOM = document.querySelector( keyboardDOM = document.querySelector(
`.${keyboardDOMClass}` `.${keyboardDOMClass}`
) as KeyboardElement; ) as KeyboardElement;
options = params[0]; options = selectorOrOptions;
} }
return { return {

View File

@ -21,8 +21,6 @@ export interface KeyboardInput {
[key: string]: string [key: string]: string
} }
export type KeyboardParams = [KeyboardOptions] | [string | HTMLDivElement, KeyboardOptions];
export type CandidateBoxParams = { export type CandidateBoxParams = {
utilities: Utilities utilities: Utilities
} }