Skip to content

Commit b5a7902

Browse files
authored
Feat/color picker add full color to result (#2651)
* ColorPicker - small fixes * ColorSwatch - refactor result * Add hexString * Rename to value * Rename to value 2 * Rename to ColorInfo
1 parent 1bed591 commit b5a7902

File tree

7 files changed

+26
-15
lines changed

7 files changed

+26
-15
lines changed

demo/src/screens/componentScreens/ColorPickerScreen.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import _ from 'lodash';
22
import React, {Component} from 'react';
33
import {StyleSheet, ScrollView} from 'react-native';
4-
import {Colors, View, Text, ColorPicker, ColorPalette, ColorName} from 'react-native-ui-lib';
4+
import {Colors, View, Text, ColorPicker, ColorPalette, ColorName, ColorInfo} from 'react-native-ui-lib';
55
import {renderMultipleSegmentOptions} from '../ExampleScreenPresenter';
66

77
interface State {
@@ -44,12 +44,12 @@ export default class ColorPickerScreen extends Component<{}, State> {
4444
this.setState({color, textColor, customColors: _.clone(customColors), paletteChange: false});
4545
};
4646

47-
onValueChange = (value: string, options: object) => {
48-
this.setState({color: value, textColor: options ? _.get(options, 'tintColor') : undefined, paletteChange: false});
47+
onValueChange = (value: string, colorInfo: ColorInfo) => {
48+
this.setState({color: value, textColor: colorInfo?.tintColor, paletteChange: false});
4949
};
5050

51-
onPaletteValueChange = (value: string, options: object) => {
52-
this.setState({color: value, textColor: options ? _.get(options, 'tintColor') : undefined, paletteChange: true});
51+
onPaletteValueChange = (value: string, colorInfo: ColorInfo) => {
52+
this.setState({color: value, textColor: colorInfo?.tintColor, paletteChange: true});
5353
};
5454

5555
render() {

src/components/colorPalette/ColorPalette.api.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
},
3232
{
3333
"name": "onValueChange",
34-
"type": "(value: string, options: object) => void",
34+
"type": "(value: string, colorInfo: ColorInfo) => void",
3535
"description": "Invoked once when value changes by selecting one of the swatches in the palette"
3636
},
3737
{"name": "swatchStyle", "type": "ViewStyle", "description": "Style to pass all the ColorSwatches in the palette"},

src/components/colorPalette/index.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import View from '../view';
88
import Carousel from '../carousel';
99
import ScrollBar from '../scrollBar';
1010
import PageControl from '../pageControl';
11-
import ColorSwatch, {SWATCH_SIZE, SWATCH_MARGIN} from '../colorSwatch';
11+
import ColorSwatch, {ColorSwatchProps, ColorInfo, SWATCH_SIZE, SWATCH_MARGIN} from '../colorSwatch';
1212

1313
interface Props {
1414
/**
@@ -50,7 +50,7 @@ interface Props {
5050
/**
5151
* Invoked once when value changes by selecting one of the swatches in the palette
5252
*/
53-
onValueChange?: (value: string, options: object) => void;
53+
onValueChange?: ColorSwatchProps['onPress'];
5454
style?: StyleProp<ViewStyle>;
5555
testID?: string;
5656
/**
@@ -261,8 +261,8 @@ class ColorPalette extends PureComponent<Props, State> {
261261
this.setState({currentPage: index});
262262
};
263263

264-
onValueChange = (value: string, options: object) => {
265-
this.props.onValueChange?.(value, options);
264+
onValueChange = (value: string, colorInfo: ColorInfo) => {
265+
this.props.onValueChange?.(value, colorInfo);
266266
};
267267

268268
getHorizontalMargins = (index: number) => {

src/components/colorPicker/colorPicker.api.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
},
2323
{
2424
"name": "onValueChange",
25-
"type": "(value: string, options: object) => void",
25+
"type": "(value: string, colorInfo: ColorInfo) => void",
2626
"description": "Callback for the picker's color palette change"
2727
},
2828
{

src/components/colorSwatch/colorSwatch.api.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
{"name": "color", "type": "string", "description": "The color of the ColorSwatch"},
1717
{"name": "selected", "type": "boolean", "description": "Is the initial state is selected"},
1818
{"name": "animated", "type": "boolean", "description": "Is first render should be animated"},
19-
{"name": "onPress", "type": "(value: string, options: object) => void", "description": "Callback from press event"},
19+
{"name": "onPress", "type": "(value: string, colorInfo: ColorInfo) => void", "description": "Callback from press event"},
2020
{"name": "index", "type": "number", "description": "The index of the Swatch if in array"},
2121
{"name": "style", "type": "ViewStyle", "description": "Component's style"},
2222
{"name": "testID", "type": "string", "description": "The test id for e2e tests"},

src/components/colorSwatch/index.tsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ import View from '../view';
77
import TouchableOpacity from '../touchableOpacity';
88
import Image from '../image';
99

10+
export interface ColorInfo {
11+
index?: number;
12+
tintColor?: string;
13+
/**
14+
* The color result with 6 characters (#FFFFFF and never #FFF)
15+
*/
16+
hexString: string;
17+
}
18+
1019
interface Props {
1120
/**
1221
* The identifier value of the ColorSwatch in a ColorSwatch palette.
@@ -32,7 +41,7 @@ interface Props {
3241
/**
3342
* onPress callback
3443
*/
35-
onPress?: (value: string, options: object) => void;
44+
onPress?: (value: string, colorInfo: ColorInfo) => void;
3645
index?: number;
3746
style?: StyleProp<ViewStyle>;
3847
testID?: string;
@@ -111,7 +120,9 @@ class ColorSwatch extends PureComponent<Props> {
111120
onPress = () => {
112121
const {color = '', value, index} = this.props;
113122
const tintColor = this.getTintColor(value);
114-
this.props.onPress?.(value || color, {tintColor, index});
123+
const result = value || color;
124+
const hexString = Colors.getHexString(result);
125+
this.props.onPress?.(result, {tintColor, index, hexString});
115126
};
116127

117128
getTintColor(color?: string) {

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export {default as Chip, ChipProps} from './components/chip';
6565
export {default as ColorPicker, ColorPickerProps} from './components/colorPicker';
6666
export {default as ColorPalette, ColorPaletteProps} from './components/colorPalette';
6767
export {default as ColorSliderGroup, ColorSliderGroupProps} from './components/slider/ColorSliderGroup';
68-
export {default as ColorSwatch, ColorSwatchProps} from './components/colorSwatch';
68+
export {default as ColorSwatch, ColorSwatchProps, ColorInfo} from './components/colorSwatch';
6969
export {default as ConnectionStatusBar, ConnectionStatusBarProps} from './components/connectionStatusBar';
7070
export {default as Dash, DashProps} from './components/dash';
7171
export {default as DateTimePicker, DateTimePickerProps, DateTimePickerMode} from './components/dateTimePicker';

0 commit comments

Comments
 (0)