Skip to content

Commit 7d8a422

Browse files
authored
ColorSwatch new unavailable mode (#2425)
* ColorSwatch new unavilable mode * new size prop for color swatch * Fixed review notes * Size prop added to the api.json * Removed diagonalLine from ColorSwatch index
1 parent a1985b7 commit 7d8a422

File tree

3 files changed

+47
-22
lines changed

3 files changed

+47
-22
lines changed

demo/src/screens/componentScreens/ColorSwatchScreen.tsx

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ export default class ColorSwatchScreen extends Component {
5151
this.setState({color2: value});
5252
};
5353

54+
unavailableOnPress = () => {
55+
console.log(`Pressed on unavailable color swatch!`);
56+
};
57+
5458
render() {
5559
const {color, color1, color2, selected} = this.state;
5660

@@ -61,10 +65,16 @@ export default class ColorSwatchScreen extends Component {
6165
Single ColorSwatch
6266
</Text>
6367
<View row>
64-
<ColorSwatch selected={selected} onPress={this.onPress}/>
6568
<View>
66-
<ColorSwatch selected color={Colors.$backgroundMajorLight}/>
67-
<Text>Disabled</Text>
69+
<ColorSwatch selected={selected} onPress={this.onPress}/>
70+
</View>
71+
<View>
72+
<ColorSwatch color={Colors.$backgroundMajorLight}/>
73+
<Text text90R>Disabled</Text>
74+
</View>
75+
<View>
76+
<ColorSwatch unavailable onPress={this.unavailableOnPress} color={Colors.yellow10}/>
77+
<Text text90R>Unavailable</Text>
6878
</View>
6979
</View>
7080

src/components/colorSwatch/colorSwatch.api.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
{"name": "onPress", "type": "(value: string, options: object) => 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"},
22-
{"name": "testID", "type": "string", "description": "The test id for e2e tests"}
22+
{"name": "testID", "type": "string", "description": "The test id for e2e tests"},
23+
{"name": "unavailable", "type": "boolean", "description": "Is the initial state is unavailable"},
24+
{"name": "size", "type": "number", "description": "Color Swatch size"}
2325
],
24-
"snippet": [
25-
"<ColorSwatch color={Colors.red30$1} selected={true$2} onPress={() => console.log('pressed')$3}/>"
26-
]
26+
"snippet": ["<ColorSwatch color={Colors.red30$1} selected={true$2} onPress={() => console.log('pressed')$3}/>"]
2727
}

src/components/colorSwatch/index.tsx

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ interface Props {
2121
* Is the initial state is selected
2222
*/
2323
selected?: boolean;
24+
/**
25+
* Is the initial state is unavailable
26+
*/
27+
unavailable?: boolean;
2428
/**
2529
* Is first render should be animated
2630
*/
@@ -32,6 +36,10 @@ interface Props {
3236
index?: number;
3337
style?: StyleProp<ViewStyle>;
3438
testID?: string;
39+
/**
40+
* Color swatch size
41+
*/
42+
size?: number;
3543
}
3644
export type ColorSwatchProps = Props;
3745

@@ -133,7 +141,7 @@ class ColorSwatch extends PureComponent<Props> {
133141
};
134142

135143
renderContent() {
136-
const {style, color, onPress, ...others} = this.props;
144+
const {style, color, onPress, unavailable, size = DEFAULT_SIZE, ...others} = this.props;
137145
const {isSelected} = this.state;
138146
const Container = onPress ? TouchableOpacity : View;
139147
const tintColor = this.getTintColor(color);
@@ -147,21 +155,25 @@ class ColorSwatch extends PureComponent<Props> {
147155
throttleTime={0}
148156
hitSlop={{top: 10, bottom: 10, left: 10, right: 10}}
149157
onPress={this.onPress}
150-
style={[this.styles.container, style]}
158+
style={[this.styles.container, {width: size, height: size, borderRadius: size / 2}, style]}
151159
onLayout={this.onLayout}
152160
{...accessibilityInfo}
153161
>
154162
{Colors.isTransparent(color) && (
155163
<Image source={transparentImage} style={this.styles.transparentImage} resizeMode={'cover'}/>
156164
)}
157-
<Animated.Image
158-
source={Assets.icons.check}
159-
style={{
160-
tintColor,
161-
opacity: isSelected,
162-
transform: [{scaleX: isSelected}, {scaleY: isSelected}]
163-
}}
164-
/>
165+
{unavailable ? (
166+
<View style={[this.styles.unavailable, {backgroundColor: tintColor}]}/>
167+
) : (
168+
<Animated.Image
169+
source={Assets.icons.check}
170+
style={{
171+
tintColor,
172+
opacity: isSelected,
173+
transform: [{scaleX: isSelected}, {scaleY: isSelected}]
174+
}}
175+
/>
176+
)}
165177
</Container>
166178
);
167179
}
@@ -196,12 +208,9 @@ function createStyles({color = Colors.grey30}) {
196208
return StyleSheet.create({
197209
container: {
198210
backgroundColor: color,
199-
width: DEFAULT_SIZE,
200-
height: DEFAULT_SIZE,
201-
borderRadius: DEFAULT_SIZE / 2,
202-
margin: SWATCH_MARGIN,
203211
borderWidth: color === 'transparent' ? undefined : 1,
204-
borderColor: Colors.rgba(Colors.$outlineDisabledHeavy, 0.2)
212+
borderColor: Colors.rgba(Colors.$outlineDisabledHeavy, 0.2),
213+
margin: SWATCH_MARGIN
205214
},
206215
transparentImage: {
207216
...StyleSheet.absoluteFillObject,
@@ -210,6 +219,12 @@ function createStyles({color = Colors.grey30}) {
210219
borderWidth: 1,
211220
borderRadius: BorderRadiuses.br100,
212221
borderColor: Colors.rgba(Colors.$outlineDisabledHeavy, 0.2)
222+
},
223+
unavailable: {
224+
height: '100%',
225+
width: 3,
226+
transform: [{rotate: '45deg'}],
227+
opacity: 0.7
213228
}
214229
});
215230
}

0 commit comments

Comments
 (0)