-
Notifications
You must be signed in to change notification settings - Fork 734
Migrate Slider, GradientSlider and ColorSliderGroup components to TypeScript. #1408
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
Changes from 12 commits
c401dbc
bbd0fb6
bff80ca
d346e6b
d438e82
de74f45
56ecb36
a0876f5
b9cc4e7
c597440
2b809c9
970924b
320d621
cf0735e
1876e01
3dc00ff
e464abc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,70 @@ | ||
import React, {PureComponent, GetDerivedStateFromProps} from 'react'; | ||
import {StyleProp, ViewStyle, TextStyle} from 'react-native'; | ||
import _ from 'lodash'; | ||
import PropTypes from 'prop-types'; | ||
import React, {PureComponent} from 'react'; | ||
import {asBaseComponent} from '../../commons'; | ||
import GradientSlider from './GradientSlider'; | ||
import {asBaseComponent} from '../../commons/new'; | ||
import GradientSlider, {GradientSliderTypes} from './GradientSlider'; | ||
import SliderGroup from './context/SliderGroup'; | ||
import Text from '../text'; | ||
|
||
type SliderOnValueChange = (value: string) => void; | ||
|
||
/** | ||
* @description: A Gradient Slider component | ||
* @example: https://github.com/wix/react-native-ui-lib/blob/master/demo/src/screens/componentScreens/SliderScreen.js | ||
* @gif: https://github.com/wix/react-native-ui-lib/blob/master/demo/showcase/ColorSliderGroup/ColorSliderGroup.gif?raw=true | ||
*/ | ||
class ColorSliderGroup extends PureComponent { | ||
static displayName = 'ColorSliderGroup'; | ||
|
||
static propTypes = { | ||
/** | ||
export type ColorSliderGroupProps = { | ||
/** | ||
* The gradient color | ||
*/ | ||
initialColor: PropTypes.string.isRequired, | ||
/** | ||
initialColor: string; | ||
/** | ||
* Callback for onValueChange returns the new hex color | ||
*/ | ||
onValueChange: PropTypes.func, | ||
/** | ||
onValueChange?: SliderOnValueChange; | ||
/** | ||
* Group container style | ||
*/ | ||
containerStyle: PropTypes.oneOfType([PropTypes.object, PropTypes.number, PropTypes.array]), | ||
/** | ||
containerStyle?: StyleProp<ViewStyle>; | ||
/** | ||
* Sliders style | ||
*/ | ||
sliderContainerStyle: PropTypes.oneOfType([PropTypes.object, PropTypes.number, PropTypes.array]), | ||
/** | ||
sliderContainerStyle?: StyleProp<ViewStyle>; | ||
/** | ||
* Show the sliders labels (defaults are: Hue, Lightness, Saturation) | ||
*/ | ||
showLabels: PropTypes.bool, | ||
/** | ||
showLabels?: boolean; | ||
/** | ||
* In case you would like to change the default labels (translations etc.), you can provide | ||
* this prop with a map to the relevant labels ({hue: ..., lightness: ..., saturation: ...}). | ||
*/ | ||
labels: PropTypes.object, | ||
/** | ||
labels?: {[key in GradientSliderTypes]: string}; | ||
/** | ||
* The labels style | ||
*/ | ||
labelsStyle: PropTypes.oneOfType([PropTypes.object, PropTypes.number, PropTypes.array]) | ||
} | ||
labelsStyle?: StyleProp<TextStyle>; | ||
/** | ||
* If true the component will have accessibility features enabled | ||
*/ | ||
accessible?: boolean; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added |
||
}; | ||
|
||
interface ColorSliderGroupState { | ||
initialColor: string; | ||
} | ||
|
||
/** | ||
* @description: A Gradient Slider component | ||
* @example: https://github.com/wix/react-native-ui-lib/blob/master/demo/src/screens/componentScreens/SliderScreen.tsx | ||
* @gif: https://github.com/wix/react-native-ui-lib/blob/master/demo/showcase/ColorSliderGroup/ColorSliderGroup.gif?raw=true | ||
*/ | ||
class ColorSliderGroup extends PureComponent<ColorSliderGroupProps, ColorSliderGroupState> { | ||
static displayName = 'ColorSliderGroup'; | ||
|
||
static defaultProps = { | ||
labels: {hue: 'Hue', lightness: 'Lightness', saturation: 'Saturation'} | ||
} | ||
}; | ||
|
||
state = { | ||
initialColor: this.props.initialColor | ||
} | ||
|
||
static getDerivedStateFromProps(nextProps, prevState) { | ||
static getDerivedStateFromProps: GetDerivedStateFromProps<ColorSliderGroupProps, ColorSliderGroupState> = (nextProps, prevState) => { | ||
if (prevState.initialColor !== nextProps.initialColor) { | ||
return { | ||
initialColor: nextProps.initialColor | ||
|
@@ -64,16 +73,16 @@ class ColorSliderGroup extends PureComponent { | |
return null; | ||
} | ||
|
||
onValueChange = (value) => { | ||
onValueChange = (value: string) => { | ||
_.invoke(this.props, 'onValueChange', value); | ||
} | ||
|
||
renderSlider = (type) => { | ||
renderSlider = (type: GradientSliderTypes) => { | ||
const {sliderContainerStyle, showLabels, labelsStyle, accessible, labels} = this.props; | ||
|
||
return ( | ||
<> | ||
{showLabels && ( | ||
{showLabels && labels && ( | ||
<Text dark30 text80 style={labelsStyle} accessible={accessible}> | ||
{labels[type]} | ||
</Text> | ||
|
@@ -97,4 +106,4 @@ class ColorSliderGroup extends PureComponent { | |
} | ||
} | ||
|
||
export default asBaseComponent(ColorSliderGroup); | ||
export default asBaseComponent<ColorSliderGroupProps, typeof ColorSliderGroup>(ColorSliderGroup); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,95 @@ | ||
import React, {Component} from 'react'; | ||
import {StyleProp, ViewStyle} from 'react-native'; | ||
import _ from 'lodash'; | ||
import PropTypes from 'prop-types'; | ||
import tinycolor from 'tinycolor2'; | ||
import React, {Component} from 'react'; | ||
import {HueGradient, LightnessGradient, SaturationGradient, Gradient} from 'react-native-color'; | ||
import {Colors} from '../../style'; | ||
import {asBaseComponent} from '../../commons'; | ||
import {asBaseComponent} from '../../commons/new'; | ||
import Slider from './index'; | ||
import {SliderContextProps} from './context/SliderContext'; | ||
import asSliderGroupChild from './context/asSliderGroupChild'; | ||
|
||
type SliderOnValueChange = (value: string, alfa: number) => void; | ||
|
||
const GRADIENT_TYPES = { | ||
DEFAULT: 'default', | ||
HUE: 'hue', | ||
LIGHTNESS: 'lightness', | ||
SATURATION: 'saturation' | ||
}; | ||
|
||
/** | ||
* @description: A Gradient Slider component | ||
* @example: https://github.com/wix/react-native-ui-lib/blob/master/demo/src/screens/componentScreens/SliderScreen.js | ||
* @gif: https://github.com/wix/react-native-ui-lib/blob/master/demo/showcase/GradientSlider/GradientSlider.gif?raw=true | ||
*/ | ||
class GradientSlider extends Component { | ||
static displayName = 'GradientSlider'; | ||
export enum GradientSliderTypes { | ||
DEFAULT = 'default', | ||
HUE = 'hue', | ||
LIGHTNESS = 'lightness', | ||
SATURATION = 'saturation' | ||
} | ||
|
||
static propTypes = { | ||
/** | ||
export type GradientSliderProps = { | ||
/** | ||
* The gradient color | ||
*/ | ||
color: PropTypes.string, | ||
/** | ||
color?: string; | ||
/** | ||
* The gradient type (default, hue, lightness, saturation) | ||
*/ | ||
type: PropTypes.oneOf(Object.values(GRADIENT_TYPES)), | ||
/** | ||
type?: GradientSliderTypes; | ||
/** | ||
* The gradient steps | ||
*/ | ||
gradientSteps: PropTypes.number, | ||
/** | ||
gradientSteps?: number; | ||
/** | ||
* Callback for onValueChange, returns the updated color | ||
*/ | ||
onValueChange: PropTypes.func | ||
} | ||
onValueChange?: SliderOnValueChange; | ||
/** | ||
* If true the component will have accessibility features enabled | ||
*/ | ||
accessible?: boolean; | ||
/** | ||
* The container style | ||
*/ | ||
containerStyle?: StyleProp<ViewStyle>; | ||
/** | ||
* If true the Slider will be disabled and will appear in disabled color | ||
*/ | ||
disabled?: boolean; | ||
} | ||
|
||
static defaultProps = { | ||
type: GRADIENT_TYPES.DEFAULT, | ||
gradientSteps: 120 | ||
} | ||
type GradientSliderComponentProps = { | ||
/** | ||
* Context of the slider group | ||
*/ | ||
sliderContext: SliderContextProps; | ||
} & GradientSliderProps & typeof defaultProps; | ||
|
||
|
||
interface GradientSliderState { | ||
color: tinycolor.ColorFormats.HSLA; | ||
prevColor: string | undefined; | ||
} | ||
|
||
static types = GRADIENT_TYPES; | ||
const defaultProps = { | ||
type: GradientSliderTypes.DEFAULT, | ||
gradientSteps: 120, | ||
color: Colors.blue30 | ||
}; | ||
|
||
constructor(props) { | ||
/** | ||
* @description: A Gradient Slider component | ||
* @example: https://github.com/wix/react-native-ui-lib/blob/master/demo/src/screens/componentScreens/SliderScreen.tsx | ||
* @gif: https://github.com/wix/react-native-ui-lib/blob/master/demo/showcase/GradientSlider/GradientSlider.gif?raw=true | ||
*/ | ||
class GradientSlider extends Component<GradientSliderComponentProps, GradientSliderState> { | ||
static displayName = 'GradientSlider'; | ||
|
||
static defaultProps = defaultProps; | ||
|
||
static types = GradientSliderTypes; | ||
|
||
constructor(props: GradientSliderComponentProps) { | ||
super(props); | ||
|
||
this.state = { | ||
prevColor: props.color, | ||
color: props.color ? Colors.getHSL(props.color) : undefined | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Leaving |
||
color: Colors.getHSL(props.color) | ||
}; | ||
} | ||
|
||
static getDerivedStateFromProps(nextProps, prevState) { | ||
static getDerivedStateFromProps(nextProps: GradientSliderComponentProps, prevState: GradientSliderState) { | ||
if (prevState.prevColor !== nextProps.color) { | ||
return { | ||
color: Colors.getHSL(nextProps.color), | ||
|
@@ -72,10 +102,11 @@ class GradientSlider extends Component { | |
getColor() { | ||
const {color} = this.state; | ||
const {value} = this.props.sliderContext; | ||
|
||
return value || color; | ||
} | ||
|
||
getStepColor = (i) => { | ||
getStepColor = (i: number) => { | ||
const color = this.getColor(); | ||
return tinycolor({...color, a: i}).toHslString(); | ||
} | ||
|
@@ -118,12 +149,12 @@ class GradientSlider extends Component { | |
); | ||
} | ||
|
||
onValueChange = (value, alpha) => { | ||
onValueChange = (value: string, alpha: number) => { | ||
// alpha returns for type.DEFAULT | ||
_.invoke(this.props, 'onValueChange', value, alpha); | ||
} | ||
|
||
updateColor(color) { | ||
updateColor(color: tinycolor.ColorFormats.HSLA) { | ||
if (!_.isEmpty(this.props.sliderContext)) { | ||
_.invoke(this.props.sliderContext, 'setValue', color); | ||
} else { | ||
|
@@ -133,22 +164,22 @@ class GradientSlider extends Component { | |
} | ||
} | ||
|
||
updateAlpha = a => { | ||
updateAlpha = (a: number) => { | ||
const color = this.getColor(); | ||
this.updateColor({...color, a}); | ||
}; | ||
|
||
updateHue = h => { | ||
updateHue = (h: number) => { | ||
const color = this.getColor(); | ||
this.updateColor({...color, h}); | ||
}; | ||
|
||
updateLightness = l => { | ||
updateLightness = (l: number) => { | ||
const color = this.getColor(); | ||
this.updateColor({...color, l}); | ||
}; | ||
|
||
updateSaturation = s => { | ||
updateSaturation = (s: number) => { | ||
const color = this.getColor(); | ||
this.updateColor({...color, s}); | ||
}; | ||
|
@@ -164,19 +195,19 @@ class GradientSlider extends Component { | |
let onValueChange = this.updateAlpha; | ||
|
||
switch (type) { | ||
case GRADIENT_TYPES.HUE: | ||
case GradientSliderTypes.HUE: | ||
step = 1; | ||
maximumValue = 359; | ||
value = color.h; | ||
renderTrack = this.renderHueGradient; | ||
onValueChange = this.updateHue; | ||
break; | ||
case GRADIENT_TYPES.LIGHTNESS: | ||
case GradientSliderTypes.LIGHTNESS: | ||
value = color.l; | ||
renderTrack = this.renderLightnessGradient; | ||
onValueChange = this.updateLightness; | ||
break; | ||
case GRADIENT_TYPES.SATURATION: | ||
case GradientSliderTypes.SATURATION: | ||
value = color.s; | ||
renderTrack = this.renderSaturationGradient; | ||
onValueChange = this.updateSaturation; | ||
|
@@ -201,4 +232,4 @@ class GradientSlider extends Component { | |
} | ||
} | ||
|
||
export default asBaseComponent(asSliderGroupChild(GradientSlider)); | ||
export default asBaseComponent<GradientSliderComponentProps, typeof GradientSlider>(asSliderGroupChild(GradientSlider)); |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This shows as an error in TS compile, however I don't see a reason for it, so I removed it.