Skip to content

Commit d4a21e1

Browse files
Infra/color picker to ts (#1235)
* ColorPicker, ColorPalette, ColorSwatch and ColorPickerDialog - migrate to TS * removing unused type * types for demo screens * fix generated types * Update src/components/colorPicker/ColorPickerDialog.tsx use optional chaining instead of _.get() Co-authored-by: Ethan Sharabi <[email protected]> * fix PR comments * removing 'height' prop passed to Dialog component * generating types * Colors.getHSL param to optional * generating types Co-authored-by: Ethan Sharabi <[email protected]>
1 parent 4accb01 commit d4a21e1

File tree

17 files changed

+578
-338
lines changed

17 files changed

+578
-338
lines changed

demo/src/screens/componentScreens/ColorPickerScreen.js renamed to demo/src/screens/componentScreens/ColorPickerScreen.tsx

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ import {StyleSheet, ScrollView} from 'react-native';
44
import {Colors, View, Text, ColorPicker, ColorPalette, ColorName} from 'react-native-ui-lib';
55

66

7+
interface Props {}
8+
interface State {
9+
color: string,
10+
textColor: string,
11+
customColors: string[],
12+
paletteChange: boolean
13+
}
14+
715
const INITIAL_COLOR = Colors.blue30;
816
const colors = [
917
'#20303C', '#43515C', '#66737C', '#858F96', '#A3ABB0', '#C2C7CB', '#E0E3E5', '#F2F4F5',
@@ -17,34 +25,30 @@ const colors = [
1725
];
1826

1927

20-
export default class ColorPickerScreen extends Component {
21-
constructor(props) {
22-
super(props);
23-
24-
this.state = {
25-
color: INITIAL_COLOR,
26-
textColor: Colors.white,
27-
customColors: [],
28-
paletteChange: false
29-
};
30-
}
28+
export default class ColorPickerScreen extends Component<Props, State> {
29+
state: State = {
30+
color: INITIAL_COLOR,
31+
textColor: Colors.white,
32+
customColors: [],
33+
paletteChange: false
34+
};
3135

3236
onDismiss = () => {
3337
console.log(`screen onDismiss`);
3438
}
3539

36-
onSubmit = (color, textColor) => {
40+
onSubmit = (color: string, textColor: string) => {
3741
const {customColors} = this.state;
3842
customColors.push(color);
3943
this.setState({color, textColor, customColors: _.clone(customColors), paletteChange: false});
4044
}
4145

42-
onValueChange = (value, options) => {
43-
this.setState({color: value, textColor: options ? options.tintColor : undefined, paletteChange: false});
46+
onValueChange = (value: string, options: object) => {
47+
this.setState({color: value, textColor: options ? _.get(options, 'tintColor') : undefined, paletteChange: false});
4448
}
4549

46-
onPaletteValueChange = (value, options) => {
47-
this.setState({color: value, textColor: options ? options.tintColor : undefined, paletteChange: true});
50+
onPaletteValueChange = (value: string, options: object) => {
51+
this.setState({color: value, textColor: options ? _.get(options, 'tintColor') : undefined, paletteChange: true});
4852
}
4953

5054
render() {
@@ -74,7 +78,7 @@ export default class ColorPickerScreen extends Component {
7478
Theme Color
7579
</Text>
7680
<Text marginL-20>Choose a color for your place’s theme.</Text>
77-
<ColorPalette value={paletteValue} onValueChange={this.onPaletteValueChange} colors={colors} />
81+
<ColorPalette value={paletteValue} onValueChange={this.onPaletteValueChange} colors={colors}/>
7882
<Text marginL-20 marginT-16>
7983
Custom Colors
8084
</Text>

demo/src/screens/componentScreens/ColorSwatchScreen.js renamed to demo/src/screens/componentScreens/ColorSwatchScreen.tsx

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,34 @@ import {Constants, Colors, View, Text, ColorSwatch, ColorPalette} from 'react-na
55

66

77
export default class ColorSwatchScreen extends Component {
8-
constructor(props) {
9-
super(props);
10-
11-
this.colors = ['transparent', Colors.green30, Colors.yellow30, Colors.red30];
12-
this.mainColors = ['#66737C', '#459FED', '#1D5382', '#3CC7C5', '#65C888', '#FAAD4D', '#F27052', '#F2564D', '#B13DAC', '#733CA6', '#79838A', '#5847FF', '#00BBF2', '#00CD8B', '#FF563D', '#ffb600'];
13-
this.allColors = _.filter(Object.values(Colors), _.isString);
8+
colors = ['transparent', Colors.green30, Colors.yellow30, Colors.red30];
9+
mainColors = ['#66737C', '#459FED', '#1D5382', '#3CC7C5', '#65C888', '#FAAD4D', '#F27052', '#F2564D', '#B13DAC', '#733CA6', '#79838A', '#5847FF', '#00BBF2', '#00CD8B', '#FF563D', '#ffb600'];
10+
allColors = _.filter(Object.values(Colors), _.isString);
1411

15-
this.state = {
16-
color: this.colors[0],
17-
color1: this.mainColors[this.mainColors.length - 1],
18-
color2: this.allColors[20],
19-
selected: false
20-
};
21-
}
12+
state = {
13+
color: this.colors[0],
14+
color1: this.mainColors[this.mainColors.length - 1],
15+
color2: this.allColors[20],
16+
selected: false
17+
};
2218

2319
onPress = () => {
2420
this.setState({selected: !this.state.selected});
2521
}
2622

27-
onValueChange = (value) => {
23+
onValueChange = (value: string) => {
2824
this.setState({color: value});
2925
}
30-
onValueChange1 = (value) => {
26+
onValueChange1 = (value: string) => {
3127
this.setState({color1: value});
3228
}
33-
onValueChange2 = (value) => {
29+
onValueChange2 = (value: string) => {
3430
this.setState({color2: value});
3531
}
3632

3733
render() {
3834
const {color, color1, color2, selected} = this.state;
39-
35+
4036
return (
4137
<ScrollView style={{backgroundColor: Colors.dark80}}>
4238
<View flex center useSafeArea>
@@ -48,16 +44,29 @@ export default class ColorSwatchScreen extends Component {
4844
<Text>Disabled</Text>
4945
</View>
5046
</View>
51-
47+
5248
<Text marginT-20 text60 dark10>ColorPalette</Text>
5349
<Text marginB-10 text70 style={{color}}>Selected Color: {color}</Text>
54-
<ColorPalette value={color} onValueChange={this.onValueChange} colors={this.colors}/>
50+
<ColorPalette
51+
value={color}
52+
onValueChange={this.onValueChange}
53+
colors={this.colors}
54+
/>
5555

5656
<Text margin-10 text60 dark10>Scrollable</Text>
57-
<ColorPalette value={color1} onValueChange={this.onValueChange1} colors={this.mainColors}/>
58-
57+
<ColorPalette
58+
value={color1}
59+
onValueChange={this.onValueChange1}
60+
colors={this.mainColors}
61+
/>
62+
5963
<Text margin-10 text60 dark10>Pagination</Text>
60-
<ColorPalette numberOfRows={!Constants.isTablet ? 4 : undefined} value={color2} onValueChange={this.onValueChange2} colors={this.allColors}/>
64+
<ColorPalette
65+
numberOfRows={!Constants.isTablet ? 4 : undefined}
66+
value={color2}
67+
onValueChange={this.onValueChange2}
68+
colors={this.allColors}
69+
/>
6170
</View>
6271
</ScrollView>
6372
);
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import React from 'react';
2+
import { StyleProp, ViewStyle } from 'react-native';
3+
interface Props {
4+
/**
5+
* Array of colors to render in the palette
6+
*/
7+
colors: string[];
8+
/**
9+
* Style to pass the palette container
10+
*/
11+
containerStyle?: StyleProp<ViewStyle>;
12+
/**
13+
* The container margins
14+
*/
15+
containerWidth?: number;
16+
/**
17+
* Whether to use pagination when number of colors exceeds the number of rows
18+
*/
19+
usePagination?: boolean;
20+
/**
21+
* Whether the colors pagination scrolls in a loop
22+
*/
23+
loop?: boolean;
24+
/**
25+
* The number of color rows from 2 to 5
26+
*/
27+
numberOfRows?: number;
28+
/**
29+
* Style to pass all the ColorSwatches in the palette
30+
*/
31+
swatchStyle?: StyleProp<ViewStyle>;
32+
/**
33+
* The value of the selected swatch
34+
*/
35+
value?: string;
36+
/**
37+
* The index of the item to animate at first render (default is last)
38+
*/
39+
animatedIndex?: number;
40+
/**
41+
* Invoked once when value changes by selecting one of the swatches in the palette
42+
*/
43+
onValueChange?: (value: string, options: object) => void;
44+
style?: StyleProp<ViewStyle>;
45+
testID?: string;
46+
}
47+
export declare type ColorPaletteProps = Props;
48+
declare const _default: React.ComponentClass<Props & {
49+
useCustomTheme?: boolean | undefined;
50+
}, any>;
51+
export default _default;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import React from 'react';
2+
import { StyleProp, ViewStyle } from 'react-native';
3+
import { DialogProps } from '../dialog';
4+
interface Props extends DialogProps {
5+
/**
6+
* The initial color to pass the picker dialog
7+
*/
8+
initialColor?: string;
9+
/**
10+
* onSubmit callback for the picker dialog color change
11+
*/
12+
onSubmit?: () => void;
13+
/**
14+
* Props to pass the Dialog component // TODO: deprecate 'dialogProps' prop
15+
*/
16+
dialogProps?: object;
17+
/**
18+
* Additional styling for the color preview text.
19+
*/
20+
previewInputStyle?: StyleProp<ViewStyle>;
21+
/**
22+
* Accessibility labels as an object of strings, ex. {addButton: 'add custom color using hex code', dismissButton: 'dismiss', doneButton: 'done', input: 'custom hex color code'}
23+
*/
24+
/**
25+
* Ok (v) button color
26+
*/
27+
doneButtonColor?: string;
28+
accessibilityLabels?: {
29+
dismissButton?: string;
30+
doneButton?: string;
31+
input?: string;
32+
};
33+
}
34+
export declare type ColorPickerDialogProps = Props;
35+
declare const _default: React.ComponentClass<Props & {
36+
useCustomTheme?: boolean | undefined;
37+
}, any>;
38+
export default _default;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import React from 'react';
2+
import { StyleProp, ViewStyle } from 'react-native';
3+
interface Props {
4+
/**
5+
* The identifier value of the ColorSwatch in a ColorSwatch palette.
6+
* Must be different than other ColorSwatches in the same group
7+
*/
8+
value?: string;
9+
/**
10+
* The color of the ColorSwatch
11+
*/
12+
color?: string;
13+
/**
14+
* Is the initial state is selected
15+
*/
16+
selected?: boolean;
17+
/**
18+
* Is first render should be animated
19+
*/
20+
animated?: boolean;
21+
/**
22+
* onPress callback
23+
*/
24+
onPress?: (value: string, options: object) => void;
25+
index?: number;
26+
style?: StyleProp<ViewStyle>;
27+
testID?: string;
28+
}
29+
export declare type ColorSwatchProps = Props;
30+
export declare const SWATCH_MARGIN = 12;
31+
export declare const SWATCH_SIZE: number;
32+
declare const _default: React.ComponentClass<Props & {
33+
useCustomTheme?: boolean | undefined;
34+
}, any>;
35+
export default _default;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import React from 'react';
2+
import { StyleProp, ViewStyle } from 'react-native';
3+
import { ColorPickerDialogProps } from './ColorPickerDialog';
4+
interface Props extends ColorPickerDialogProps {
5+
/**
6+
* Array of colors for the picker's color palette (hex values)
7+
*/
8+
colors: string[];
9+
/**
10+
* The value of the selected swatch // TODO: rename prop 'selectedValue'
11+
*/
12+
value?: string;
13+
/**
14+
* The index of the item to animate at first render (default is last)
15+
*/
16+
animatedIndex?: number;
17+
/**
18+
* onValueChange callback for the picker's color palette change
19+
*/
20+
onValueChange?: (value: string, options: object) => void;
21+
/**
22+
* Accessibility labels as an object of strings, ex.
23+
* {
24+
* addButton: 'add custom color using hex code',
25+
* dismissButton: 'dismiss',
26+
* doneButton: 'done',
27+
* input: 'custom hex color code'
28+
* }
29+
*/
30+
accessibilityLabels: {
31+
addButton?: string;
32+
dismissButton?: string;
33+
doneButton?: string;
34+
input?: string;
35+
};
36+
style?: StyleProp<ViewStyle>;
37+
testID?: string;
38+
}
39+
export declare type ColorPickerProps = Props;
40+
declare const _default: React.ComponentClass<Props & {
41+
useCustomTheme?: boolean | undefined;
42+
}, any>;
43+
export default _default;

generatedTypes/incubator/TextField/index.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ declare const _default: React.ComponentClass<(Partial<Record<"margin" | "marginL
9292
/**
9393
* A single or multiple validator. Can be a string (required, email) or custom function.
9494
*/
95-
validate?: "number" | Function | "required" | "email" | "url" | "price" | Validator[] | undefined;
95+
validate?: "number" | Function | "email" | "required" | "url" | "price" | Validator[] | undefined;
9696
/**
9797
* Should validate when the TextField mounts
9898
*/
@@ -147,7 +147,7 @@ declare const _default: React.ComponentClass<(Partial<Record<"margin" | "marginL
147147
/**
148148
* A single or multiple validator. Can be a string (required, email) or custom function.
149149
*/
150-
validate?: "number" | Function | "required" | "email" | "url" | "price" | Validator[] | undefined;
150+
validate?: "number" | Function | "email" | "required" | "url" | "price" | Validator[] | undefined;
151151
/**
152152
* Should validate when the TextField mounts
153153
*/
@@ -202,7 +202,7 @@ declare const _default: React.ComponentClass<(Partial<Record<"margin" | "marginL
202202
/**
203203
* A single or multiple validator. Can be a string (required, email) or custom function.
204204
*/
205-
validate?: "number" | Function | "required" | "email" | "url" | "price" | Validator[] | undefined;
205+
validate?: "number" | Function | "email" | "required" | "url" | "price" | Validator[] | undefined;
206206
/**
207207
* Should validate when the TextField mounts
208208
*/
@@ -257,7 +257,7 @@ declare const _default: React.ComponentClass<(Partial<Record<"margin" | "marginL
257257
/**
258258
* A single or multiple validator. Can be a string (required, email) or custom function.
259259
*/
260-
validate?: "number" | Function | "required" | "email" | "url" | "price" | Validator[] | undefined;
260+
validate?: "number" | Function | "email" | "required" | "url" | "price" | Validator[] | undefined;
261261
/**
262262
* Should validate when the TextField mounts
263263
*/

0 commit comments

Comments
 (0)