Skip to content

Feat/color picker add full color to result #2651

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions demo/src/screens/componentScreens/ColorPickerScreen.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import _ from 'lodash';
import React, {Component} from 'react';
import {StyleSheet, ScrollView} from 'react-native';
import {Colors, View, Text, ColorPicker, ColorPalette, ColorName} from 'react-native-ui-lib';
import {Colors, View, Text, ColorPicker, ColorPalette, ColorName, ColorExtraData} from 'react-native-ui-lib';
import {renderMultipleSegmentOptions} from '../ExampleScreenPresenter';

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

onValueChange = (value: string, options: object) => {
this.setState({color: value, textColor: options ? _.get(options, 'tintColor') : undefined, paletteChange: false});
onValueChange = (color: string, extraData: ColorExtraData) => {
this.setState({color, textColor: extraData?.tintColor, paletteChange: false});
};

onPaletteValueChange = (value: string, options: object) => {
this.setState({color: value, textColor: options ? _.get(options, 'tintColor') : undefined, paletteChange: true});
onPaletteValueChange = (color: string, extraData: ColorExtraData) => {
this.setState({color, textColor: extraData?.tintColor, paletteChange: true});
};

render() {
Expand Down
2 changes: 1 addition & 1 deletion src/components/colorPalette/ColorPalette.api.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
},
{
"name": "onValueChange",
"type": "(value: string, options: object) => void",
"type": "(color: string, extraData: ColorExtraData) => void",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the props is named 'onValueChange' I'd leave it as 'value'. Also since the user probably already extracting 'value' here

"description": "Invoked once when value changes by selecting one of the swatches in the palette"
},
{"name": "swatchStyle", "type": "ViewStyle", "description": "Style to pass all the ColorSwatches in the palette"},
Expand Down
8 changes: 4 additions & 4 deletions src/components/colorPalette/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import View from '../view';
import Carousel from '../carousel';
import ScrollBar from '../scrollBar';
import PageControl from '../pageControl';
import ColorSwatch, {SWATCH_SIZE, SWATCH_MARGIN} from '../colorSwatch';
import ColorSwatch, {ColorSwatchProps, ColorExtraData, SWATCH_SIZE, SWATCH_MARGIN} from '../colorSwatch';

interface Props {
/**
Expand Down Expand Up @@ -50,7 +50,7 @@ interface Props {
/**
* Invoked once when value changes by selecting one of the swatches in the palette
*/
onValueChange?: (value: string, options: object) => void;
onValueChange?: ColorSwatchProps['onPress'];
style?: StyleProp<ViewStyle>;
testID?: string;
/**
Expand Down Expand Up @@ -261,8 +261,8 @@ class ColorPalette extends PureComponent<Props, State> {
this.setState({currentPage: index});
};

onValueChange = (value: string, options: object) => {
this.props.onValueChange?.(value, options);
onValueChange = (color: string, extraData: ColorExtraData) => {
this.props.onValueChange?.(color, extraData);
};

getHorizontalMargins = (index: number) => {
Expand Down
2 changes: 1 addition & 1 deletion src/components/colorPicker/colorPicker.api.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
},
{
"name": "onValueChange",
"type": "(value: string, options: object) => void",
"type": "(color: string, extraData: ColorExtraData) => void",
"description": "Callback for the picker's color palette change"
},
{
Expand Down
2 changes: 1 addition & 1 deletion src/components/colorSwatch/colorSwatch.api.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
{"name": "color", "type": "string", "description": "The color of the ColorSwatch"},
{"name": "selected", "type": "boolean", "description": "Is the initial state is selected"},
{"name": "animated", "type": "boolean", "description": "Is first render should be animated"},
{"name": "onPress", "type": "(value: string, options: object) => void", "description": "Callback from press event"},
{"name": "onPress", "type": "(color: string, extraData: ColorExtraData) => void", "description": "Callback from press event"},
{"name": "index", "type": "number", "description": "The index of the Swatch if in array"},
{"name": "style", "type": "ViewStyle", "description": "Component's style"},
{"name": "testID", "type": "string", "description": "The test id for e2e tests"},
Expand Down
15 changes: 13 additions & 2 deletions src/components/colorSwatch/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ import View from '../view';
import TouchableOpacity from '../touchableOpacity';
import Image from '../image';

export interface ColorExtraData {
index?: number;
tintColor?: string;
/**
* The color result with 6 characters (#FFFFFF and never #FFF)
*/
hexString: string;
}

interface Props {
/**
* The identifier value of the ColorSwatch in a ColorSwatch palette.
Expand All @@ -32,7 +41,7 @@ interface Props {
/**
* onPress callback
*/
onPress?: (value: string, options: object) => void;
onPress?: (color: string, extraData: ColorExtraData) => void;
index?: number;
style?: StyleProp<ViewStyle>;
testID?: string;
Expand Down Expand Up @@ -111,7 +120,9 @@ class ColorSwatch extends PureComponent<Props> {
onPress = () => {
const {color = '', value, index} = this.props;
const tintColor = this.getTintColor(value);
this.props.onPress?.(value || color, {tintColor, index});
const result = value || color;
const hexString = Colors.getHexString(result);
this.props.onPress?.(result, {tintColor, index, hexString});
};

getTintColor(color?: string) {
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export {default as Chip, ChipProps} from './components/chip';
export {default as ColorPicker, ColorPickerProps} from './components/colorPicker';
export {default as ColorPalette, ColorPaletteProps} from './components/colorPalette';
export {default as ColorSliderGroup, ColorSliderGroupProps} from './components/slider/ColorSliderGroup';
export {default as ColorSwatch, ColorSwatchProps} from './components/colorSwatch';
export {default as ColorSwatch, ColorSwatchProps, ColorExtraData} from './components/colorSwatch';
export {default as ConnectionStatusBar, ConnectionStatusBarProps} from './components/connectionStatusBar';
export {default as Dash, DashProps} from './components/dash';
export {default as DateTimePicker, DateTimePickerProps, DateTimePickerMode} from './components/dateTimePicker';
Expand Down