Skip to content
This repository was archived by the owner on Mar 9, 2023. It is now read-only.

Commit 23ea396

Browse files
Merge pull request #59 from ljmotta/issue-58
Use TypeScript strict mode
2 parents e3a93c2 + 25ebf47 commit 23ea396

19 files changed

+252
-215
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,5 @@ dist
104104
.tern-port
105105

106106
# Custom
107-
reproductions
107+
reproductions
108+
.idea/

src/AutoField.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,10 @@ export default function AutoField(originalProps: AutoFieldProps) {
5656
invariant(component, 'Unsupported field type: %s', fieldType);
5757
}
5858
}
59-
60-
return 'options' in component && component.options?.kind === 'leaf'
59+
60+
return component &&
61+
'options' in component &&
62+
component.options?.kind === 'leaf'
6163
? createElement(component.Component, props)
62-
: createElement(component, originalProps);;
64+
: createElement(component!, originalProps);
6365
}

src/AutoFields.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ export default function AutoFields({
2323
element!,
2424
props,
2525
(fields ?? schema.getSubfields())
26-
.filter(field => !omitFields!.includes(field))
27-
.map(field => createElement(autoField!, { key: field, name: field })),
26+
.filter((field) => !omitFields!.includes(field))
27+
.map((field) => createElement(autoField!, { key: field, name: field }))
2828
);
2929
}
3030

src/BaseForm.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ function Patternfly(parent: any): any {
2020
return _;
2121
}
2222

23-
export default Patternfly(BaseForm);
23+
export default Patternfly(BaseForm);

src/BoolField.tsx

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,43 @@
11
import React from 'react';
2-
import { Checkbox, CheckboxProps, Switch } from '@patternfly/react-core';
3-
import { connectField, filterDOMProps } from 'uniforms/es5';
2+
import {
3+
Checkbox,
4+
CheckboxProps,
5+
Switch,
6+
SwitchProps,
7+
} from '@patternfly/react-core';
8+
import { connectField, FieldProps, filterDOMProps } from 'uniforms/es5';
49

5-
export type BoolFieldProps = {
6-
appearance?: 'checkbox' | 'switch';
7-
label?: string;
8-
legend?: string;
9-
onChange?: (value: any) => void;
10-
transform?: (label?: string) => string;
11-
disabled: boolean;
12-
} & Omit<CheckboxProps, 'isDisabled'>;
10+
enum ComponentType {
11+
checkbox = 'checkbox',
12+
switch = 'switch',
13+
}
1314

14-
const Bool = ({
15+
export type BoolFieldProps = FieldProps<
16+
boolean,
17+
CheckboxProps & SwitchProps,
18+
{
19+
appearance?: ComponentType;
20+
inputRef: React.RefObject<Switch | Checkbox> &
21+
React.RefObject<HTMLInputElement>;
22+
}
23+
>;
24+
25+
function Bool({
26+
appearance,
1527
disabled,
1628
id,
1729
inputRef,
1830
label,
1931
name,
2032
onChange,
2133
value,
22-
toggle,
2334
...props
24-
}) => {
25-
const Component = (props.appearance === 'switch') ? Switch : Checkbox;
35+
}: BoolFieldProps) {
36+
const Component = appearance === ComponentType.switch ? Switch : Checkbox;
2637
return (
2738
<div {...filterDOMProps(props)}>
2839
<Component
29-
isChecked={!!value}
40+
isChecked={value || false}
3041
isDisabled={disabled}
3142
id={id}
3243
name={name}
@@ -36,10 +47,8 @@ const Bool = ({
3647
/>
3748
</div>
3849
);
39-
};
50+
}
4051

41-
Bool.defaultProps = {
42-
appearance: 'checkbox'
43-
};
52+
Bool.defaultProps = { appearance: ComponentType.checkbox };
4453

4554
export default connectField(Bool);

src/DateField.tsx

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import React, { Ref } from 'react';
1+
import React, { FormEvent } from 'react';
2+
import { connectField, FieldProps } from 'uniforms/es5';
23
import { TextInput, TextInputProps } from '@patternfly/react-core';
3-
import { connectField } from 'uniforms/es5';
44

55
import wrapField from './wrapField';
66

77
const DateConstructor = (typeof global === 'object' ? global : window).Date;
8-
const dateFormat = value => value && value.toISOString().slice(0, -8);
9-
const dateParse = (timestamp, onChange) => {
8+
const dateFormat = (value?: Date) => value && value.toISOString().slice(0, -8);
9+
const dateParse = (timestamp: number, onChange: DateFieldProps['onChange']) => {
1010
const date = new DateConstructor(timestamp);
1111
if (date.getFullYear() < 10000) {
1212
onChange(date);
@@ -15,20 +15,20 @@ const dateParse = (timestamp, onChange) => {
1515
}
1616
};
1717

18-
export type DateFieldProps = {
19-
id: string;
20-
inputRef?: Ref<HTMLInputElement>;
21-
onChange: (value?: string) => void;
22-
value?: string;
23-
disabled: boolean;
24-
error?: boolean;
25-
} & Omit<TextInputProps, 'isDisabled'>;
18+
export type DateFieldProps = FieldProps<
19+
Date,
20+
TextInputProps,
21+
{
22+
inputRef: React.RefObject<HTMLInputElement>;
23+
max?: Date;
24+
min?: Date;
25+
}
26+
>;
2627

2728
function Date(props: DateFieldProps) {
28-
29-
const onChange = (value, event) => {
30-
props.disabled || dateParse(event.target.valueAsNumber, props.onChange)
31-
}
29+
const onChange = (value: string, event: FormEvent<HTMLInputElement>) =>
30+
props.disabled ||
31+
dateParse((event.target as any).valueAsNumber, props.onChange);
3232

3333
return wrapField(
3434
props,
@@ -43,7 +43,7 @@ function Date(props: DateFieldProps) {
4343
ref={props.inputRef}
4444
type="datetime-local"
4545
value={dateFormat(props.value) ?? ''}
46-
/>,
46+
/>
4747
);
4848
}
4949

src/ListAddField.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from 'react';
22
import cloneDeep from 'lodash/cloneDeep';
33
import { Button, ButtonProps } from '@patternfly/react-core';
44
import { PlusCircleIcon } from '@patternfly/react-icons';
5-
import { useField, FieldProps, filterDOMProps, joinName } from 'uniforms/es5';
5+
import { useField, filterDOMProps, joinName } from 'uniforms/es5';
66

77
export type ListAddFieldProps = {
88
initialCount?: number;
@@ -23,7 +23,7 @@ function ListAdd({
2323
const parent = useField<{ maxCount?: number }, unknown[]>(
2424
parentName,
2525
{},
26-
{ absoluteName: true },
26+
{ absoluteName: true }
2727
)[0];
2828

2929
const limitNotReached =
@@ -32,12 +32,12 @@ function ListAdd({
3232
return (
3333
<Button
3434
variant="plain"
35-
style={{ paddingLeft: '0', paddingRight: '0'}}
35+
style={{ paddingLeft: '0', paddingRight: '0' }}
3636
disabled={!limitNotReached}
3737
onClick={() => {
3838
!disabled &&
39-
limitNotReached &&
40-
parent.onChange(parent.value!.concat([cloneDeep(value)]));
39+
limitNotReached &&
40+
parent.onChange(parent.value!.concat([cloneDeep(value)]));
4141
}}
4242
{...filterDOMProps(props)}
4343
>

src/ListDelField.tsx

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,14 @@ export type ListDelFieldProps = {
99
value?: unknown;
1010
} & ButtonProps;
1111

12-
function ListDel({
13-
name,
14-
disabled,
15-
...props
16-
}: ListDelFieldProps) {
12+
function ListDel({ name, disabled, ...props }: ListDelFieldProps) {
1713
const nameParts = joinName(null, name);
1814
const nameIndex = +nameParts[nameParts.length - 1];
1915
const parentName = joinName(nameParts.slice(0, -1));
2016
const parent = useField<{ minCount?: number }, unknown[]>(
2117
parentName,
2218
{},
23-
{ absoluteName: true },
19+
{ absoluteName: true }
2420
)[0];
2521

2622
const limitNotReached =
@@ -30,13 +26,11 @@ function ListDel({
3026
<Button
3127
disabled={!limitNotReached || disabled}
3228
variant="plain"
33-
style={{ paddingLeft: '0', paddingRight: '0'}}
29+
style={{ paddingLeft: '0', paddingRight: '0' }}
3430
onClick={() => {
3531
const value = parent.value!.slice();
3632
value.splice(nameIndex, 1);
37-
!disabled &&
38-
limitNotReached &&
39-
parent.onChange(value);
33+
!disabled && limitNotReached && parent.onChange(value);
4034
}}
4135
{...filterDOMProps(props)}
4236
>

src/ListField.tsx

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,16 @@
1-
import React, { Children, HTMLProps, ReactNode, isValidElement, cloneElement } from 'react';
1+
import React, {
2+
Children,
3+
ReactNode,
4+
isValidElement,
5+
cloneElement,
6+
} from 'react';
27
import { Tooltip, Split, SplitItem } from '@patternfly/react-core';
38
import { OutlinedQuestionCircleIcon } from '@patternfly/react-icons';
4-
import { HTMLFieldProps, connectField, filterDOMProps, joinName } from 'uniforms/es5';
9+
import { HTMLFieldProps, connectField, filterDOMProps } from 'uniforms/es5';
510

611
import ListItemField from './ListItemField';
712
import ListAddField from './ListAddField';
8-
import { ListDelField } from '.';
9-
10-
// export type ListFieldProps = {
11-
// value: unknown[];
12-
// children?: ReactNode;
13-
// addIcon?: any;
14-
// error?: boolean;
15-
// info?: boolean;
16-
// errorMessage?: string;
17-
// initialCount?: number;
18-
// itemProps?: {};
19-
// labelCol?: any;
20-
// label: string;
21-
// wrapperCol?: any;
22-
// name: string;
23-
// showInlineError?: boolean;
24-
// } & Omit<HTMLProps<HTMLDivElement>, 'children' | 'name'>;
13+
import ListDelField from './ListDelField';
2514

2615
export type ListFieldProps = HTMLFieldProps<
2716
unknown[],
@@ -84,17 +73,17 @@ function ListField({
8473
</Split>
8574

8675
<div>
87-
{value?.map((item, itemIndex) =>
88-
Children.map(children, (child, childIndex) =>
89-
isValidElement(child)
90-
? cloneElement(child, {
91-
key: `${itemIndex}-${childIndex}`,
92-
name: child.props.name?.replace('$', '' + itemIndex),
93-
...itemProps,
94-
})
95-
: child,
96-
),
97-
)}
76+
{value?.map((item, itemIndex) =>
77+
Children.map(children, (child, childIndex) =>
78+
isValidElement(child)
79+
? cloneElement(child, {
80+
key: `${itemIndex}-${childIndex}`,
81+
name: child.props.name?.replace('$', '' + itemIndex),
82+
...itemProps,
83+
})
84+
: child
85+
)
86+
)}
9887
</div>
9988
</div>
10089
);

src/ListItemField.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ export type ListItemFieldProps = {
1313

1414
export default function ListItemField(props: ListItemFieldProps) {
1515
return (
16-
<div style={{ marginBottom: '1rem'}}>
16+
<div style={{ marginBottom: '1rem' }}>
1717
{props.children ? (
18-
Children.map(props.children as JSX.Element, child =>
18+
Children.map(props.children as JSX.Element, (child) =>
1919
React.cloneElement(child, {
2020
name: joinName(props.name, child.props.name),
2121
label: null,
22-
}),
22+
})
2323
)
2424
) : (
2525
<AutoField {...props} />
2626
)}
2727
</div>
2828
);
29-
}
29+
}

0 commit comments

Comments
 (0)