Skip to content

Commit

Permalink
Clean up code
Browse files Browse the repository at this point in the history
  • Loading branch information
ai committed Jan 9, 2024
1 parent 768c71c commit f9b6d06
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 28 deletions.
24 changes: 10 additions & 14 deletions view/card/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,20 @@ function initInput(type: 'a' | 'c' | 'h' | 'l'): HTMLInputElement {
text.setAttribute('aria-valuenow', String(angleInRange))
})

text.addEventListener('spin', e => {
text.addEventListener('spin', (e: SpinEvent) => {
let { max, min, step } = getInputMeta(text)

let value = computeExpression(text.value)

switch ((e as SpinEvent).detail.action) {
case 'increase':
value = value + step
break
case 'decrease':
value = value - step
break
case 'setMaximum':
value = max
break
case 'setMinimum':
value = min
break
let action = e.detail
if (action === 'increase') {
value = value + step
} else if (action === 'decrease') {
value = value - step
} else if (action === 'setMaximum') {
value = max
} else if (action === 'setMinimum') {
value = min
}

let parsedValue = bindedClamp(value, { max, min })
Expand Down
25 changes: 11 additions & 14 deletions view/field/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ function removeByPosition(str: string, position: number): string {
return str.slice(0, position) + str.slice(position + 1)
}

type SpinAction = 'decrease' | 'increase' | 'setMaximum' | 'setMinimum'

export interface SpinEvent extends Event {
detail: {
action: 'decrease' | 'increase' | 'setMaximum' | 'setMinimum'
}
detail?: SpinAction
}

let currentNotifyCb = (): void => {}
Expand All @@ -117,10 +117,10 @@ function useSpinButton(input: HTMLInputElement): void {
let decrease = increase.nextElementSibling as HTMLButtonElement
let pattern = new RegExp(input.getAttribute('pattern')!)

function changeNotice(type: SpinEvent['detail']['action']): void {
function changeNotice(type: SpinAction): void {
if (/[0-9]/.test(input.value.charAt(input.value.length - 1))) {
setValid(input)
input.dispatchEvent(new CustomEvent('spin', { detail: { action: type } }))
input.dispatchEvent(new CustomEvent<SpinAction>('spin', { detail: type }))
} else {
setInvalid(input, 'Invalid number')
}
Expand Down Expand Up @@ -162,15 +162,12 @@ function useSpinButton(input: HTMLInputElement): void {
function onKeyPressed(e: KeyboardEvent): void {
onKeyDown(e)

switch (e.key) {
case 'ArrowUp':
e.preventDefault()
changeNotice('increase')
break
case 'ArrowDown':
e.preventDefault()
changeNotice('decrease')
break
if (e.key === 'ArrowUp') {
e.preventDefault()
changeNotice('increase')
} else if (e.key === 'ArrowDown') {
e.preventDefault()
changeNotice('decrease')
}
}

Expand Down

0 comments on commit f9b6d06

Please sign in to comment.