Skip to content

Commit

Permalink
Improve typings of exising components and migrated a few more
Browse files Browse the repository at this point in the history
  • Loading branch information
HarelM committed Dec 22, 2023
1 parent 1318dd2 commit 6af3eee
Show file tree
Hide file tree
Showing 12 changed files with 180 additions and 187 deletions.
12 changes: 6 additions & 6 deletions src/components/InputArray.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ import InputString from './InputString'
import InputNumber from './InputNumber'

export type FieldArrayProps = {
value: string[] | number[]
value: (string | number | undefined)[]
type?: string
length?: number
default?: string[] | number[]
onChange?(...args: unknown[]): unknown
default?: (string | number | undefined)[]
onChange?(value: (string | number | undefined)[] | undefined): unknown
'aria-label'?: string
label?: string
};

type FieldArrayState = {
value: string[] | number[]
value: (string | number | undefined)[]
initialPropsValue: unknown[]
}

Expand Down Expand Up @@ -59,7 +59,7 @@ export default class FieldArray extends React.Component<FieldArrayProps, FieldAr
});
}

changeValue(idx: number, newValue: string) {
changeValue(idx: number, newValue: string | number | undefined) {
const value = this.state.value.slice(0);
value[idx] = newValue;

Expand Down Expand Up @@ -93,7 +93,7 @@ export default class FieldArray extends React.Component<FieldArrayProps, FieldAr
default={containsValues || !this.props.default ? undefined : this.props.default[i] as number}
value={value[i] as number}
required={containsValues ? true : false}
onChange={this.changeValue.bind(this, i)}
onChange={(v) => this.changeValue(i, v)}
aria-label={this.props['aria-label'] || this.props.label}
/>
} else {
Expand Down
8 changes: 4 additions & 4 deletions src/components/InputDynamicArray.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import InputUrl from './InputUrl'


export type FieldDynamicArrayProps = {
value?: (string | number)[]
value?: (string | number | undefined)[]
type?: 'url' | 'number' | 'enum' | 'string'
default?: (string | number)[]
onChange?(...args: unknown[]): unknown
default?: (string | number | undefined)[]
onChange?(values: (string | number | undefined)[] | undefined): unknown
style?: object
fieldSpec?: {
values?: any
Expand All @@ -25,7 +25,7 @@ export type FieldDynamicArrayProps = {


export default class FieldDynamicArray extends React.Component<FieldDynamicArrayProps> {
changeValue(idx: number, newValue: string | number) {
changeValue(idx: number, newValue: string | number | undefined) {
const values = this.values.slice(0)
values[idx] = newValue
if (this.props.onChange) this.props.onChange(values)
Expand Down
2 changes: 1 addition & 1 deletion src/components/InputNumber.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export type InputNumberProps = {
default?: number
min?: number
max?: number
onChange?(...args: unknown[]): unknown
onChange?(value: number | undefined): unknown
allowRange?: boolean
rangeStep?: number
wdKey?: string
Expand Down
2 changes: 1 addition & 1 deletion src/components/InputSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export type InputSelectProps = {
"data-wd-key"?: string
options: [string, any][] | string[]
style?: object
onChange(...args: unknown[]): unknown
onChange(value: string): unknown
title?: string
'aria-label'?: string
};
Expand Down
18 changes: 9 additions & 9 deletions src/components/InputSpec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import capitalize from 'lodash.capitalize'
const iconProperties = ['background-pattern', 'fill-pattern', 'line-pattern', 'fill-extrusion-pattern', 'icon-image']

export type SpecFieldProps = {
onChange?(...args: unknown[]): unknown
onChange?(fieldName: string | undefined, value: number | undefined | (string | number | undefined)[]): unknown
fieldName?: string
fieldSpec: {
fieldSpec?: {
default?: unknown
type: 'number' | 'enum' | 'resolvedImage' | 'formatted' | 'string' | 'color' | 'boolean' | 'array'
type?: 'number' | 'enum' | 'resolvedImage' | 'formatted' | 'string' | 'color' | 'boolean' | 'array'
minimum?: number
maximum?: number
values?: unknown[]
Expand All @@ -29,9 +29,9 @@ export type SpecFieldProps = {
/** Override the style of the field */
style?: object
'aria-label'?: string
error: unknown[]
label: string
action: ReactElement
error?: unknown[]
label?: string
action?: ReactElement
};

/** Display any field from the Maplibre GL style spec and
Expand All @@ -47,12 +47,12 @@ export default class SpecField extends React.Component<SpecFieldProps> {
action: this.props.action,
style: this.props.style,
value: this.props.value,
default: this.props.fieldSpec.default,
default: this.props.fieldSpec?.default,
name: this.props.fieldName,
onChange: (newValue: string) => this.props.onChange!(this.props.fieldName, newValue),
onChange: (newValue: number | undefined | (string | number | undefined)[]) => this.props.onChange!(this.props.fieldName, newValue),
'aria-label': this.props['aria-label'],
}
switch(this.props.fieldSpec.type) {
switch(this.props.fieldSpec?.type) {
case 'number': return (
<InputNumber
{...commonProps as InputNumberProps}
Expand Down
4 changes: 2 additions & 2 deletions src/components/InputString.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ export type InputStringProps = {
value?: string
style?: object
default?: string
onChange?(...args: unknown[]): unknown
onInput?(...args: unknown[]): unknown
onChange?(value: string | undefined): unknown
onInput?(value: string | undefined): unknown
multi?: boolean
required?: boolean
disabled?: boolean
Expand Down
2 changes: 1 addition & 1 deletion src/components/ModalSources.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ class AddSource extends React.Component<AddSourceProps, AddSourceState> {
['image', 'Image'],
['video', 'Video'],
]}
onChange={(mode: EditorMode) => this.setState({mode: mode, source: this.defaultSource(mode)})}
onChange={mode => this.setState({mode: mode as EditorMode, source: this.defaultSource(mode as EditorMode)})}
value={this.state.mode as string}
/>
<ModalSourcesTypeEditor
Expand Down
4 changes: 2 additions & 2 deletions src/components/SpecField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ type SpecFieldProps = InputFieldSpecProps & {

export default class SpecField extends React.Component<SpecFieldProps> {
render() {
const fieldType = this.props.fieldSpec.type;
const fieldType = this.props.fieldSpec?.type;

const typeBlockFn = typeMap[fieldType];
const typeBlockFn = typeMap[fieldType!];

let TypeBlock;
if (typeBlockFn) {
Expand Down
Loading

0 comments on commit 6af3eee

Please sign in to comment.