Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@uppy/progress-bar: refactor to TypeScript #4921

Merged
merged 12 commits into from
Feb 28, 2024
2 changes: 2 additions & 0 deletions packages/@uppy/file-input/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

tsconfig.*
Original file line number Diff line number Diff line change
@@ -1,14 +1,40 @@
import { UIPlugin } from '@uppy/core'
import { h, type ComponentChild } from 'preact'
import type { CSSProperties } from 'preact/compat'

aduh95 marked this conversation as resolved.
Show resolved Hide resolved
import { UIPlugin, Uppy, type UIPluginOptions } from '@uppy/core'
import toArray from '@uppy/utils/lib/toArray'
import { h } from 'preact'
import type { Body, Meta } from '@uppy/utils/lib/UppyFile'

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore We don't want TS to generate types for the package.json
import packageJson from '../package.json'
import locale from './locale.js'
import locale from './locale.ts'


interface FileInputOptions extends UIPluginOptions {
target: HTMLElement | string | null
mifi marked this conversation as resolved.
Show resolved Hide resolved
pretty?: boolean,
inputName?: string,
}
interface FileInputState {
audioReady: boolean
recordingLengthSeconds: number
hasAudio: boolean
cameraError: null
audioSources: MediaDeviceInfo[]
currentDeviceId?: null | string | MediaStreamTrack
isRecording: boolean
showAudioSourceDropdown: boolean
[id: string]: unknown
}

export default class FileInput extends UIPlugin {
export default class FileInput<M extends Meta, B extends Body> extends
UIPlugin<FileInputOptions, M, B, FileInputState> {
static VERSION = packageJson.version
mifi marked this conversation as resolved.
Show resolved Hide resolved

constructor (uppy, opts) {
input: HTMLInputElement | null

constructor (uppy: Uppy<M, B>, opts?: FileInputOptions) {
super(uppy, opts)
this.id = this.opts.id || 'FileInput'
this.title = 'File Input'
Expand All @@ -33,7 +59,7 @@ export default class FileInput extends UIPlugin {
this.handleClick = this.handleClick.bind(this)
}

addFiles (files) {
addFiles (files: File[]): void {
const descriptors = files.map((file) => ({
source: this.id,
name: file.name,
Expand All @@ -48,9 +74,9 @@ export default class FileInput extends UIPlugin {
}
}

handleInputChange (event) {
handleInputChange (event: Parameters<React.ChangeEventHandler<HTMLInputElement>>[0]): void {
this.uppy.log('[FileInput] Something selected through input...')
mifi marked this conversation as resolved.
Show resolved Hide resolved
const files = toArray(event.target.files)
const files = toArray((event.target as HTMLInputElement | null)?.files ?? [])
this.addFiles(files)

// We clear the input after a file is selected, because otherwise
Expand All @@ -59,16 +85,17 @@ export default class FileInput extends UIPlugin {
// ___Why not use value="" on <input/> instead?
// Because if we use that method of clearing the input,
// Chrome will not trigger change if we drop the same file twice (Issue #768).
// @ts-expect-error yes
event.target.value = null // eslint-disable-line no-param-reassign
}

handleClick () {
this.input.click()
handleClick (): void {
this.input?.click()
}

render () {
render (): ComponentChild {
/* http://tympanus.net/codrops/2015/09/15/styling-customizing-file-inputs-smart-way/ */
Murderlon marked this conversation as resolved.
Show resolved Hide resolved
const hiddenInputStyle = {
const hiddenInputStyle: CSSProperties = {
width: '0.1px',
height: '0.1px',
opacity: 0,
Expand All @@ -78,13 +105,13 @@ export default class FileInput extends UIPlugin {
}

const { restrictions } = this.uppy.opts
const accept = restrictions.allowedFileTypes ? restrictions.allowedFileTypes.join(',') : null
const accept = restrictions.allowedFileTypes ? restrictions.allowedFileTypes.join(',') : undefined

return (
<div className="uppy-FileInput-container">
<input
className="uppy-FileInput-input"
style={this.opts.pretty && hiddenInputStyle}
style={this.opts.pretty ? hiddenInputStyle : undefined}
type="file"
name={this.opts.inputName}
onChange={this.handleInputChange}
Expand All @@ -106,14 +133,14 @@ export default class FileInput extends UIPlugin {
)
}

install () {
install (): void {
const { target } = this.opts
if (target) {
this.mount(target, this)
}
}

uninstall () {
uninstall (): void {
this.unmount()
}
}
1 change: 0 additions & 1 deletion packages/@uppy/file-input/src/index.js

This file was deleted.

1 change: 1 addition & 0 deletions packages/@uppy/file-input/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './FileInput.tsx'
35 changes: 35 additions & 0 deletions packages/@uppy/file-input/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"extends": "../../../tsconfig.shared",
"compilerOptions": {
"noImplicitAny": false,
Murderlon marked this conversation as resolved.
Show resolved Hide resolved
"outDir": "./lib",
"paths": {
"@uppy/utils/lib/*": [
"../utils/src/*"
],
"@uppy/core": [
"../core/src/index.js"
],
"@uppy/core/lib/*": [
"../core/src/*"
]
},
"resolveJsonModule": false,
"rootDir": "./src",
"skipLibCheck": true
},
"include": [
"./src/**/*.*"
],
"exclude": [
"./src/**/*.test.ts"
],
"references": [
{
"path": "../utils/tsconfig.build.json"
},
{
"path": "../core/tsconfig.build.json"
}
]
}
30 changes: 30 additions & 0 deletions packages/@uppy/file-input/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"extends": "../../../tsconfig.shared",
"compilerOptions": {
"emitDeclarationOnly": false,
Murderlon marked this conversation as resolved.
Show resolved Hide resolved
"noEmit": true,
"paths": {
"@uppy/utils/lib/*": [
"../utils/src/*"
],
"@uppy/core": [
"../core/src/index.js"
],
"@uppy/core/lib/*": [
"../core/src/*"
]
}
},
"include": [
"./package.json",
"./src/**/*.*"
],
"references": [
{
"path": "../utils/tsconfig.build.json"
},
{
"path": "../core/tsconfig.build.json"
}
]
}
2 changes: 2 additions & 0 deletions packages/@uppy/progress-bar/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

mifi marked this conversation as resolved.
Show resolved Hide resolved
tsconfig.*
Original file line number Diff line number Diff line change
@@ -1,16 +1,38 @@
import { h } from 'preact'
import { UIPlugin } from '@uppy/core'
import { h, type ComponentChild } from 'preact'
import { UIPlugin, Uppy, type UIPluginOptions } from '@uppy/core'
import type { Body, Meta } from '@uppy/utils/lib/UppyFile'

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore We don't want TS to generate types for the package.json
import packageJson from '../package.json'


interface ProgressBarOptions extends UIPluginOptions {
target?: HTMLElement | string
mifi marked this conversation as resolved.
Show resolved Hide resolved
hideAfterFinish?: boolean,
mifi marked this conversation as resolved.
Show resolved Hide resolved
fixed?: boolean,
}
interface ProgressBarState {
audioReady: boolean
recordingLengthSeconds: number
hasAudio: boolean
cameraError: null
audioSources: MediaDeviceInfo[]
currentDeviceId?: null | string | MediaStreamTrack
isRecording: boolean
showAudioSourceDropdown: boolean
[id: string]: unknown
}

/**
* Progress bar
*
*/
export default class ProgressBar extends UIPlugin {
export default class ProgressBar<M extends Meta, B extends Body>
extends UIPlugin<ProgressBarOptions, M, B, ProgressBarState> {
static VERSION = packageJson.version
mifi marked this conversation as resolved.
Show resolved Hide resolved

constructor (uppy, opts) {
constructor (uppy: Uppy<M, B>, opts?: ProgressBarOptions) {
super(uppy, opts)
this.id = this.opts.id || 'ProgressBar'
this.title = 'Progress Bar'
Expand All @@ -29,7 +51,7 @@ export default class ProgressBar extends UIPlugin {
this.render = this.render.bind(this)
}

render (state) {
render (state: ProgressBarState): ComponentChild {
const progress = state.totalProgress || 0
Murderlon marked this conversation as resolved.
Show resolved Hide resolved
mifi marked this conversation as resolved.
Show resolved Hide resolved
// before starting and after finish should be hidden if specified in the options
const isHidden = (progress === 0 || progress === 100) && this.opts.hideAfterFinish
Expand All @@ -45,14 +67,14 @@ export default class ProgressBar extends UIPlugin {
)
}

install () {
install (): void {
const { target } = this.opts
if (target) {
this.mount(target, this)
}
}

uninstall () {
uninstall (): void {
this.unmount()
}
}
1 change: 0 additions & 1 deletion packages/@uppy/progress-bar/src/index.js

This file was deleted.

1 change: 1 addition & 0 deletions packages/@uppy/progress-bar/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './ProgressBar.tsx'
35 changes: 35 additions & 0 deletions packages/@uppy/progress-bar/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"extends": "../../../tsconfig.shared",
"compilerOptions": {
"noImplicitAny": false,
Murderlon marked this conversation as resolved.
Show resolved Hide resolved
"outDir": "./lib",
"paths": {
"@uppy/utils/lib/*": [
"../utils/src/*"
],
"@uppy/core": [
"../core/src/index.js"
],
"@uppy/core/lib/*": [
"../core/src/*"
]
},
"resolveJsonModule": false,
"rootDir": "./src",
"skipLibCheck": true
},
"include": [
"./src/**/*.*"
],
"exclude": [
"./src/**/*.test.ts"
],
"references": [
{
"path": "../utils/tsconfig.build.json"
},
{
"path": "../core/tsconfig.build.json"
}
]
}
30 changes: 30 additions & 0 deletions packages/@uppy/progress-bar/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"extends": "../../../tsconfig.shared",
"compilerOptions": {
"emitDeclarationOnly": false,
Murderlon marked this conversation as resolved.
Show resolved Hide resolved
"noEmit": true,
"paths": {
"@uppy/utils/lib/*": [
"../utils/src/*"
],
"@uppy/core": [
"../core/src/index.js"
],
"@uppy/core/lib/*": [
"../core/src/*"
]
}
},
"include": [
"./package.json",
"./src/**/*.*"
],
"references": [
{
"path": "../utils/tsconfig.build.json"
},
{
"path": "../core/tsconfig.build.json"
}
]
}