Skip to content

Commit 01fe235

Browse files
MichaelDeBoeyCompuIves
authored andcommitted
Fix Preference's types (codesandbox#2971)
1 parent ca843ad commit 01fe235

File tree

13 files changed

+197
-183
lines changed

13 files changed

+197
-183
lines changed

packages/app/src/app/pages/common/Modals/PreferencesModal/Appearance/index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import { PreferenceText } from '@codesandbox/common/lib/components/Preference/PreferenceText';
2+
import themes from '@codesandbox/common/lib/themes';
13
import React from 'react';
4+
25
import { useOvermind } from 'app/overmind';
3-
import themes from '@codesandbox/common/lib/themes';
46

5-
import PreferenceText from '@codesandbox/common/lib/components/Preference/PreferenceText';
67
import {
78
Title,
89
SubContainer,

packages/app/src/app/pages/common/Modals/PreferencesModal/elements.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import { Preference } from '@codesandbox/common/lib/components/Preference';
12
import styled, { css } from 'styled-components';
2-
import Preference from '@codesandbox/common/lib/components/Preference';
33

44
export const SubContainer = styled.div`
55
color: ${props => props.theme.white};

packages/app/src/app/pages/common/Modals/ShareModal/elements.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import { Preference } from '@codesandbox/common/lib/components/Preference';
12
import styled from 'styled-components';
2-
import Preference from '@codesandbox/common/lib/components/Preference';
33

44
export const FilesContainer = styled.div`
55
max-height: 300px;
Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,31 @@
1-
import React from 'react';
2-
import Select from '../Select';
1+
import React, { ChangeEvent, FunctionComponent } from 'react';
32

4-
export type NameMapper = (param: string) => string;
3+
import Select from '../Select';
54

6-
export type Props = {
5+
type Props = {
6+
mapName?: (param: string) => string;
7+
options: string[];
78
setValue: (value: string) => void;
89
value: string;
9-
options: string[];
10-
mapName?: NameMapper;
1110
};
1211

13-
export default class PreferenceInput extends React.PureComponent<Props> {
14-
handleChange = (e: React.ChangeEvent<HTMLSelectElement>) => {
15-
const { value } = e.target;
16-
17-
this.props.setValue(value);
12+
export const PreferenceDropdown: FunctionComponent<Props> = ({
13+
mapName,
14+
options,
15+
setValue,
16+
value,
17+
}) => {
18+
const handleChange = ({ target }: ChangeEvent<HTMLSelectElement>) => {
19+
setValue(target.value);
1820
};
1921

20-
render() {
21-
const { value, options, mapName } = this.props;
22-
23-
return (
24-
<Select onChange={this.handleChange} value={value}>
25-
{options.map(op => (
26-
<option key={op} value={op}>
27-
{mapName ? mapName(op) : op}
28-
</option>
29-
))}
30-
</Select>
31-
);
32-
}
33-
}
22+
return (
23+
<Select onChange={handleChange} value={value}>
24+
{options.map(option => (
25+
<option key={option} value={option}>
26+
{mapName ? mapName(option) : option}
27+
</option>
28+
))}
29+
</Select>
30+
);
31+
};
Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,42 @@
1-
import React from 'react';
1+
import React, { FunctionComponent } from 'react';
2+
23
import KeybindingInput from './KeybindingInput';
34

4-
export type Props = {
5-
setValue: (value: Array<string[]>) => void;
6-
value: Array<string[]>;
5+
type Props = {
6+
setValue: (value: string[][]) => void;
7+
value: string[][];
78
};
89

9-
export default class PreferenceKeybinding extends React.PureComponent<Props> {
10-
setValue = index => value => {
11-
const result = [...this.props.value];
10+
export const PreferenceKeybinding: FunctionComponent<Props> = ({
11+
setValue: setValueProp,
12+
value: valueProp,
13+
...props
14+
}) => {
15+
const setValue = (index: number) => (value: string[]) => {
16+
const result = [...valueProp];
1217
result[index] = value;
1318

14-
this.props.setValue(result);
19+
setValueProp(result);
1520
};
1621

17-
render() {
18-
const { value } = this.props;
22+
return (
23+
<div>
24+
<KeybindingInput
25+
{...props}
26+
placeholder="First"
27+
setValue={setValue(0)}
28+
value={valueProp[0]}
29+
/>
30+
31+
{' - '}
1932

20-
return (
21-
<div>
22-
<KeybindingInput
23-
{...this.props}
24-
placeholder="First"
25-
value={value[0]}
26-
setValue={this.setValue(0)}
27-
/>
28-
{' - '}
29-
<KeybindingInput
30-
{...this.props}
31-
placeholder="Second"
32-
value={value.length === 2 && value[1]}
33-
setValue={this.setValue(1)}
34-
disabled={!value[0] || value[0].length === 0}
35-
/>
36-
</div>
37-
);
38-
}
39-
}
33+
<KeybindingInput
34+
{...props}
35+
disabled={!valueProp[0] || valueProp[0].length === 0}
36+
placeholder="Second"
37+
setValue={setValue(1)}
38+
value={valueProp.length === 2 && valueProp[1]}
39+
/>
40+
</div>
41+
);
42+
};
Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,32 @@
1-
import React from 'react';
2-
import { StyledInput } from './elements';
1+
import React, { ChangeEvent, ComponentProps, FunctionComponent } from 'react';
32

4-
export type Props = {
3+
import { Input } from './elements';
4+
5+
type Props = {
56
setValue: (value: number) => void;
6-
value: number;
77
step?: number;
8-
style?: React.CSSProperties;
9-
};
10-
11-
export default class PreferenceInput extends React.PureComponent<Props> {
12-
handleChange = e => {
13-
const { value } = e.target;
8+
value: number;
9+
} & Pick<ComponentProps<typeof Input>, 'style'>;
1410

15-
if (!Number.isNaN(+value)) {
16-
this.props.setValue(+value);
11+
export const PreferenceNumber: FunctionComponent<Props> = ({
12+
setValue,
13+
step,
14+
style,
15+
value,
16+
}) => {
17+
const handleChange = ({ target }: ChangeEvent<HTMLInputElement>) => {
18+
if (!Number.isNaN(+target.value)) {
19+
setValue(+target.value);
1720
}
1821
};
1922

20-
render() {
21-
const { value, style, step } = this.props;
22-
23-
return (
24-
<StyledInput
25-
step={step}
26-
style={{ width: '3rem', ...style }}
27-
type="number"
28-
value={value}
29-
onChange={this.handleChange}
30-
/>
31-
);
32-
}
33-
}
23+
return (
24+
<Input
25+
onChange={handleChange}
26+
step={step}
27+
style={{ width: '3rem', ...style }}
28+
type="number"
29+
value={value}
30+
/>
31+
);
32+
};
Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
1-
import React from 'react';
1+
import React, { FunctionComponent } from 'react';
2+
23
import Switch from '../Switch';
34

4-
export type Props = {
5+
type Props = {
6+
setValue: (value: boolean) => void;
57
value: boolean;
6-
setValue: (val: boolean) => void;
78
};
89

9-
export default class PreferenceSwitch extends React.Component<Props> {
10-
handleClick = () => {
11-
this.props.setValue(!this.props.value);
10+
export const PreferenceSwitch: FunctionComponent<Props> = ({
11+
setValue,
12+
value,
13+
}) => {
14+
const handleClick = () => {
15+
setValue(!value);
1216
};
1317

14-
render() {
15-
const { value } = this.props;
16-
return (
17-
<Switch
18-
onClick={this.handleClick}
19-
small
20-
style={{ width: '3rem' }}
21-
offMode
22-
secondary
23-
right={value}
24-
/>
25-
);
26-
}
27-
}
18+
return (
19+
<Switch
20+
offMode
21+
onClick={handleClick}
22+
right={value}
23+
secondary
24+
small
25+
style={{ width: '3rem' }}
26+
/>
27+
);
28+
};
Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,37 @@
1-
import React from 'react';
1+
import {
2+
ChangeEvent,
3+
ComponentProps,
4+
createElement,
5+
FunctionComponent,
6+
} from 'react';
7+
28
import Input, { TextArea } from '../Input';
39

4-
export type Props = {
5-
setValue: (value: string) => void;
6-
value: string;
7-
placeholder?: string;
8-
isTextArea?: boolean;
9-
style?: React.CSSProperties;
10+
type Props = {
1011
block?: boolean;
12+
isTextArea?: boolean;
13+
placeholder?: string;
1114
rows?: number;
12-
};
13-
14-
export default class PreferenceText extends React.PureComponent<Props> {
15-
handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
16-
const { value } = e.target;
15+
setValue: (value: string) => void;
16+
value: string;
17+
} & Pick<ComponentProps<typeof Input>, 'style'>;
1718

18-
this.props.setValue(value);
19+
export const PreferenceText: FunctionComponent<Props> = ({
20+
isTextArea,
21+
placeholder,
22+
setValue,
23+
value,
24+
...props
25+
}) => {
26+
const handleChange = ({ target }: ChangeEvent<HTMLInputElement>) => {
27+
setValue(target.value);
1928
};
2029

21-
render() {
22-
const { value, placeholder, isTextArea, ...props } = this.props;
23-
24-
return React.createElement(isTextArea ? TextArea : Input, {
25-
style: { width: '9rem' },
26-
value,
27-
placeholder,
28-
onChange: this.handleChange,
29-
...props,
30-
});
31-
}
32-
}
30+
return createElement(isTextArea ? TextArea : Input, {
31+
...props,
32+
style: { width: '9rem' },
33+
value,
34+
placeholder,
35+
onChange: handleChange,
36+
});
37+
};
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import styled from 'styled-components';
2-
import Input from '../Input';
2+
3+
import InputBase from '../Input';
34

45
export const Container = styled.div`
56
display: flex;
67
justify-content: space-between;
78
align-items: center;
89
`;
910

10-
export const StyledInput = styled(Input)`
11+
export const Input = styled(InputBase)`
1112
text-align: center;
1213
`;

packages/common/src/components/Preference/index.stories.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import React from 'react';
21
import { storiesOf } from '@storybook/react';
2+
import React from 'react';
3+
34
import { noop } from '../../test/mocks';
4-
import Preference from '.';
55
import { KEYBINDINGS } from '../../utils/keybindings';
66

7+
import { Preference } from '.';
8+
79
const stories = storiesOf('components/Preference', module);
810
const keyBindingKeys = Object.keys(KEYBINDINGS);
911

0 commit comments

Comments
 (0)