From 66a0ee5d4e8c57af4b244ff1659961ef50594972 Mon Sep 17 00:00:00 2001 From: HarelM Date: Thu, 21 Dec 2023 09:23:06 +0200 Subject: [PATCH 01/13] Initial migration of jsx files --- src/components/{Doc.jsx => Doc.tsx} | 38 +++++++++++------- .../{InputButton.jsx => InputButton.tsx} | 30 +++++++------- .../{InputCheckbox.jsx => InputCheckbox.tsx} | 16 ++++---- .../{InputSelect.jsx => InputSelect.tsx} | 26 ++++++------ .../{InputString.jsx => InputString.tsx} | 40 ++++++++++--------- .../{SmallError.jsx => SmallError.tsx} | 13 +++--- 6 files changed, 84 insertions(+), 79 deletions(-) rename src/components/{Doc.jsx => Doc.tsx} (80%) rename src/components/{InputButton.jsx => InputButton.tsx} (53%) rename src/components/{InputCheckbox.jsx => InputCheckbox.tsx} (77%) rename src/components/{InputSelect.jsx => InputSelect.tsx} (53%) rename src/components/{InputString.jsx => InputString.tsx} (70%) rename src/components/{SmallError.jsx => SmallError.tsx} (54%) diff --git a/src/components/Doc.jsx b/src/components/Doc.tsx similarity index 80% rename from src/components/Doc.jsx rename to src/components/Doc.tsx index 3285165cf..c5db0c4d7 100644 --- a/src/components/Doc.jsx +++ b/src/components/Doc.tsx @@ -1,24 +1,33 @@ import React from 'react' -import PropTypes from 'prop-types' -export default class Doc extends React.Component { - static propTypes = { - fieldSpec: PropTypes.object.isRequired, +const headers = { + js: "JS", + android: "Android", + ios: "iOS", + macos: "macOS", +}; + +type DocProps = { + fieldSpec: { + doc?: string + values?: { + [key: string]: { + doc?: string + } + } + 'sdk-support'?: { + [key: string]: typeof headers + } } +}; +export default class Doc extends React.Component { render () { const {fieldSpec} = this.props; const {doc, values} = fieldSpec; const sdkSupport = fieldSpec['sdk-support']; - const headers = { - js: "JS", - android: "Android", - ios: "iOS", - macos: "macOS", - }; - const renderValues = ( !!values && // HACK: Currently we merge additional values into the style spec, so this is required @@ -61,10 +70,9 @@ export default class Doc extends React.Component { return ( {key} - {Object.keys(headers).map(k => { - const value = supportObj[k]; + {Object.keys(headers).map((k) => { if (supportObj.hasOwnProperty(k)) { - return {supportObj[k]}; + return {supportObj[k as keyof typeof headers]}; } else { return no; @@ -80,4 +88,4 @@ export default class Doc extends React.Component { ); } -} +} \ No newline at end of file diff --git a/src/components/InputButton.jsx b/src/components/InputButton.tsx similarity index 53% rename from src/components/InputButton.jsx rename to src/components/InputButton.tsx index e9c4b2351..bd8aaaa19 100644 --- a/src/components/InputButton.jsx +++ b/src/components/InputButton.tsx @@ -1,21 +1,20 @@ import React from 'react' -import PropTypes from 'prop-types' import classnames from 'classnames' -export default class InputButton extends React.Component { - static propTypes = { - "data-wd-key": PropTypes.string, - "aria-label": PropTypes.string, - onClick: PropTypes.func, - style: PropTypes.object, - className: PropTypes.string, - children: PropTypes.node, - disabled: PropTypes.bool, - type: PropTypes.string, - id: PropTypes.string, - title: PropTypes.string, - } +type InputButtonProps = { + "data-wd-key"?: string + "aria-label"?: string + onClick?(...args: unknown[]): unknown + style?: object + className?: string + children?: React.ReactNode + disabled?: boolean + type?: typeof HTMLButtonElement.prototype.type + id?: string + title?: string +}; +export default class InputButton extends React.Component { render() { return From c7c1139e8706975593869f8ff8599807aa7cb1e3 Mon Sep 17 00:00:00 2001 From: HarelM Date: Thu, 21 Dec 2023 22:23:31 +0200 Subject: [PATCH 13/13] More typescript migration --- package-lock.json | 10 +++ package.json | 1 + src/components/InputUrl.tsx | 2 + .../{ModalLoading.jsx => ModalLoading.tsx} | 22 +++--- .../{ModalOpen.jsx => ModalOpen.tsx} | 70 ++++++++++--------- 5 files changed, 62 insertions(+), 43 deletions(-) rename src/components/{ModalLoading.jsx => ModalLoading.tsx} (64%) rename src/components/{ModalOpen.jsx => ModalOpen.tsx} (82%) diff --git a/package-lock.json b/package-lock.json index 0ef80846a..24fa69199 100644 --- a/package-lock.json +++ b/package-lock.json @@ -78,6 +78,7 @@ "@types/react-autocomplete": "^1.8.9", "@types/react-color": "^3.0.10", "@types/react-dom": "^16.9.24", + "@types/react-file-reader-input": "^2.0.4", "@types/react-icon-base": "^2.1.6", "@types/uuid": "^9.0.7", "@vitejs/plugin-react": "^4.2.0", @@ -4917,6 +4918,15 @@ "@types/react": "^16" } }, + "node_modules/@types/react-file-reader-input": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/@types/react-file-reader-input/-/react-file-reader-input-2.0.4.tgz", + "integrity": "sha512-Jqrfn+w42j8t8Q3npMXXKPdk+reIM0UHLKVc3ykrA7q7bN3Z62SGhsClZX0+Edlqm66lcKwmDQl+WMm+Xor7Xg==", + "dev": true, + "dependencies": { + "@types/react": "*" + } + }, "node_modules/@types/react-icon-base": { "version": "2.1.6", "resolved": "https://registry.npmjs.org/@types/react-icon-base/-/react-icon-base-2.1.6.tgz", diff --git a/package.json b/package.json index a217705dd..fd9e96242 100644 --- a/package.json +++ b/package.json @@ -107,6 +107,7 @@ "@types/react-autocomplete": "^1.8.9", "@types/react-color": "^3.0.10", "@types/react-dom": "^16.9.24", + "@types/react-file-reader-input": "^2.0.4", "@types/react-icon-base": "^2.1.6", "@types/uuid": "^9.0.7", "@vitejs/plugin-react": "^4.2.0", diff --git a/src/components/InputUrl.tsx b/src/components/InputUrl.tsx index 2e22712a9..e8a9196ff 100644 --- a/src/components/InputUrl.tsx +++ b/src/components/InputUrl.tsx @@ -57,6 +57,8 @@ export type FieldUrlProps = { multi?: boolean required?: boolean 'aria-label'?: string + type?: string + className?: string }; type FieldUrlState = { diff --git a/src/components/ModalLoading.jsx b/src/components/ModalLoading.tsx similarity index 64% rename from src/components/ModalLoading.jsx rename to src/components/ModalLoading.tsx index 07303c3c3..f6f132e7b 100644 --- a/src/components/ModalLoading.jsx +++ b/src/components/ModalLoading.tsx @@ -1,19 +1,19 @@ import React from 'react' -import PropTypes from 'prop-types' import InputButton from './InputButton' import Modal from './Modal' -export default class ModalLoading extends React.Component { - static propTypes = { - isOpen: PropTypes.bool.isRequired, - onCancel: PropTypes.func.isRequired, - title: PropTypes.string.isRequired, - message: PropTypes.node.isRequired, - } +type ModalLoadingProps = { + isOpen: boolean + onCancel(...args: unknown[]): unknown + title: string + message: React.ReactNode +}; + - underlayOnClick(e) { +export default class ModalLoading extends React.Component { + underlayOnClick(e: Event) { // This stops click events falling through to underlying modals. e.stopPropagation(); } @@ -24,9 +24,9 @@ export default class ModalLoading extends React.Component { isOpen={this.props.isOpen} underlayClickExits={false} underlayProps={{ - onClick: (e) => underlayProps(e) + // @ts-ignore + onClick: (e: Event) => underlayProps(e) }} - closeable={false} title={this.props.title} onOpenToggle={() => this.props.onCancel()} > diff --git a/src/components/ModalOpen.jsx b/src/components/ModalOpen.tsx similarity index 82% rename from src/components/ModalOpen.jsx rename to src/components/ModalOpen.tsx index 6b542941e..da7aaeebf 100644 --- a/src/components/ModalOpen.jsx +++ b/src/components/ModalOpen.tsx @@ -1,25 +1,24 @@ -import React from 'react' -import PropTypes from 'prop-types' +import React, { FormEvent } from 'react' +import {MdFileUpload} from 'react-icons/md' +import {MdAddCircleOutline} from 'react-icons/md' +import FileReaderInput, { Result } from 'react-file-reader-input' + import ModalLoading from './ModalLoading' import Modal from './Modal' import InputButton from './InputButton' -import FileReaderInput from 'react-file-reader-input' import InputUrl from './InputUrl' -import {MdFileUpload} from 'react-icons/md' -import {MdAddCircleOutline} from 'react-icons/md' - import style from '../libs/style' import publicStyles from '../config/styles.json' -class PublicStyle extends React.Component { - static propTypes = { - url: PropTypes.string.isRequired, - thumbnailUrl: PropTypes.string.isRequired, - title: PropTypes.string.isRequired, - onSelect: PropTypes.func.isRequired, - } +type PublicStyleProps = { + url: string + thumbnailUrl: string + title: string + onSelect(...args: unknown[]): unknown +}; +class PublicStyle extends React.Component { render() { return
{ + constructor(props: ModalOpenProps) { super(props); this.state = { styleUrl: "" @@ -63,7 +69,7 @@ export default class ModalOpen extends React.Component { }) } - onCancelActiveRequest(e) { + onCancelActiveRequest(e: Event) { // Else the click propagates to the underlying modal if(e) e.stopPropagation(); @@ -76,12 +82,12 @@ export default class ModalOpen extends React.Component { } } - onStyleSelect = (styleUrl) => { + onStyleSelect = (styleUrl: string) => { this.clearError(); - let canceled; + let canceled: boolean = false; - const activeRequest = fetch(styleUrl, { + fetch(styleUrl, { mode: 'cors', credentials: "same-origin" }) @@ -123,13 +129,13 @@ export default class ModalOpen extends React.Component { }) } - onSubmitUrl = (e) => { + onSubmitUrl = (e: FormEvent) => { e.preventDefault(); this.onStyleSelect(this.state.styleUrl); } - onUpload = (_, files) => { - const [e, file] = files[0]; + onUpload = (_: any, files: Result[]) => { + const [_e, file] = files[0]; const reader = new FileReader(); this.clearError(); @@ -138,11 +144,11 @@ export default class ModalOpen extends React.Component { reader.onload = e => { let mapStyle; try { - mapStyle = JSON.parse(e.target.result) + mapStyle = JSON.parse(e.target?.result as string) } catch(err) { this.setState({ - error: err.toString() + error: (err as Error).toString() }); return; } @@ -161,7 +167,7 @@ export default class ModalOpen extends React.Component { this.props.onOpenToggle(); } - onChangeUrl = (url) => { + onChangeUrl = (url: string) => { this.setState({ styleUrl: url, }); @@ -200,7 +206,7 @@ export default class ModalOpen extends React.Component {

Upload Style

Upload a JSON style from your computer.

- + Upload
@@ -246,7 +252,7 @@ export default class ModalOpen extends React.Component { this.onCancelActiveRequest(e)} + onCancel={(e: Event) => this.onCancelActiveRequest(e)} message={"Loading: "+this.state.activeRequestUrl} />