Skip to content

Commit b1fe026

Browse files
committed
Fix issue with editable prop not working on Picker component
1 parent fb74062 commit b1fe026

File tree

4 files changed

+59
-9
lines changed

4 files changed

+59
-9
lines changed

generatedTypes/src/incubator/expandableOverlay/index.d.ts

+8
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ export declare type ExpandableOverlayProps = TouchableOpacityProps & PropsWithCh
3737
* A custom overlay to render instead of Modal or Dialog components
3838
*/
3939
renderCustomOverlay?: (props: RenderCustomOverlayProps) => React.ReactElement | undefined;
40+
/**
41+
* Disabled opening expandable overlay
42+
*/
43+
disabled?: boolean;
4044
}>;
4145
interface ExpandableOverlayMethods {
4246
openExpandable: () => void;
@@ -71,6 +75,10 @@ declare const _default: React.ForwardRefExoticComponent<TouchableOpacityProps &
7175
* A custom overlay to render instead of Modal or Dialog components
7276
*/
7377
renderCustomOverlay?: ((props: RenderCustomOverlayProps) => React.ReactElement<any, string | React.JSXElementConstructor<any>> | undefined) | undefined;
78+
/**
79+
* Disabled opening expandable overlay
80+
*/
81+
disabled?: boolean | undefined;
7482
} & {
7583
children?: React.ReactNode;
7684
} & React.RefAttributes<ExpandableOverlayMethods>>;

src/components/picker/__tests__/index.spec.js

+37-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import {Picker} from '../index';
1+
import React, {useState} from 'react';
2+
import {fireEvent, render} from '@testing-library/react-native';
3+
import Picker, {Picker as _Picker} from '../index';
24

35
const countries = [
46
{label: 'Israel', value: 'IL'},
@@ -11,15 +13,46 @@ const countries = [
1113
describe('Picker', () => {
1214
describe('getLabel', () => {
1315
it('should get label of a simple item', () => {
14-
let uut = new Picker({value: countries[2]});
16+
let uut = new _Picker({value: countries[2]});
1517
expect(uut.getLabelValueText()).toEqual(countries[2].label);
16-
uut = new Picker({value: countries[3]});
18+
uut = new _Picker({value: countries[3]});
1719
expect(uut.getLabelValueText()).toEqual(countries[3].label);
1820
});
1921

2022
it('should get label out of an array of items', () => {
21-
const uut = new Picker({value: [countries[2], countries[4]]});
23+
const uut = new _Picker({value: [countries[2], countries[4]]});
2224
expect(uut.getLabelValueText()).toEqual(`${countries[2].label}, ${countries[4].label}`);
2325
});
2426
});
27+
28+
describe('Picker - Render Tests', () => {
29+
it('should open picker overlay after pressing the picker', () => {
30+
const renderTree = render(<TestCase/>);
31+
const picker = renderTree.getByTestId('picker');
32+
const pickerOverlay = renderTree.getByTestId('picker.overlay');
33+
expect(pickerOverlay.props.visible).toBe(false);
34+
fireEvent.press(picker);
35+
expect(pickerOverlay.props.visible).toBe(true);
36+
});
37+
38+
it('should not open picker overlay after pressing when picker is disabled', () => {
39+
const renderTree = render(<TestCase editable={false}/>);
40+
const picker = renderTree.getByTestId('picker');
41+
const pickerOverlay = renderTree.getByTestId('picker.overlay');
42+
expect(pickerOverlay.props.visible).toBe(false);
43+
fireEvent.press(picker);
44+
expect(pickerOverlay.props.visible).toBe(false);
45+
});
46+
});
2547
});
48+
49+
const TestCase = props => {
50+
const [value, setValue] = useState(props.value);
51+
return (
52+
<Picker testID="picker" {...props} onChange={setValue} value={value}>
53+
{countries.map(country => {
54+
return <Picker.Item key={country.value} value={country.value} label={country.label}/>;
55+
})}
56+
</Picker>
57+
);
58+
};

src/components/picker/index.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,8 @@ class Picker extends Component {
447447
enableModalBlur,
448448
topBarProps,
449449
pickerModalProps,
450-
value
450+
value,
451+
editable
451452
} = this.props;
452453

453454
if (useNativePicker) {
@@ -489,15 +490,17 @@ class Picker extends Component {
489490
modalProps={modalProps}
490491
expandableContent={this.renderExpandableModal()}
491492
renderCustomOverlay={renderCustomModal ? this.renderCustomModal : undefined}
492-
testID={renderCustomModal ? testID : undefined}
493+
testID={testID}
493494
{...customPickerProps}
495+
disabled={editable === false}
494496
>
495497
{renderPicker ? (
496498
renderPicker(value, this.getLabel(value))
497499
) : (
498500
<TextField
499501
ref={forwardedRef}
500502
{...textInputProps}
503+
testID={`${testID}.input`}
501504
containerStyle={[paddings, margins, positionStyle, containerStyle]}
502505
{...this.getAccessibilityInfo()}
503506
importantForAccessibility={'no-hide-descendants'}

src/incubator/expandableOverlay/index.tsx

+9-3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ export type ExpandableOverlayProps = TouchableOpacityProps &
4242
* A custom overlay to render instead of Modal or Dialog components
4343
*/
4444
renderCustomOverlay?: (props: RenderCustomOverlayProps) => React.ReactElement | undefined;
45+
/**
46+
* Disabled opening expandable overlay
47+
*/
48+
disabled?: boolean;
4549
}>;
4650

4751
interface ExpandableOverlayMethods {
@@ -59,6 +63,8 @@ const ExpandableOverlay = (props: ExpandableOverlayProps, ref: any) => {
5963
showTopBar,
6064
topBarProps,
6165
renderCustomOverlay,
66+
disabled,
67+
testID,
6268
...others
6369
} = props;
6470
const [visible, setExpandableVisible] = useState(false);
@@ -79,7 +85,7 @@ const ExpandableOverlay = (props: ExpandableOverlayProps, ref: any) => {
7985

8086
const renderModal = () => {
8187
return (
82-
<Modal {...modalProps} visible={visible} onDismiss={closeExpandable}>
88+
<Modal testID={`${testID}.overlay`} {...modalProps} visible={visible} onDismiss={closeExpandable}>
8389
{showTopBar && <Modal.TopBar onDone={closeExpandable} {...topBarProps}/>}
8490
{expandableContent}
8591
</Modal>
@@ -88,7 +94,7 @@ const ExpandableOverlay = (props: ExpandableOverlayProps, ref: any) => {
8894

8995
const renderDialog = () => {
9096
return (
91-
<Dialog {...dialogProps} visible={visible} onDismiss={closeExpandable}>
97+
<Dialog testID={`${testID}.overlay`} {...dialogProps} visible={visible} onDismiss={closeExpandable}>
9298
{expandableContent}
9399
</Dialog>
94100
);
@@ -108,7 +114,7 @@ const ExpandableOverlay = (props: ExpandableOverlayProps, ref: any) => {
108114
};
109115

110116
return (
111-
<TouchableOpacity {...others} onPress={openExpandable}>
117+
<TouchableOpacity {...others} onPress={openExpandable} disabled={disabled} testID={testID}>
112118
<View pointerEvents="none">{children}</View>
113119
{renderOverlay()}
114120
</TouchableOpacity>

0 commit comments

Comments
 (0)