Skip to content

Commit

Permalink
Fix issue with editable prop not working on Picker component
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanshar committed Nov 27, 2021
1 parent fb74062 commit b1fe026
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 9 deletions.
8 changes: 8 additions & 0 deletions generatedTypes/src/incubator/expandableOverlay/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ export declare type ExpandableOverlayProps = TouchableOpacityProps & PropsWithCh
* A custom overlay to render instead of Modal or Dialog components
*/
renderCustomOverlay?: (props: RenderCustomOverlayProps) => React.ReactElement | undefined;
/**
* Disabled opening expandable overlay
*/
disabled?: boolean;
}>;
interface ExpandableOverlayMethods {
openExpandable: () => void;
Expand Down Expand Up @@ -71,6 +75,10 @@ declare const _default: React.ForwardRefExoticComponent<TouchableOpacityProps &
* A custom overlay to render instead of Modal or Dialog components
*/
renderCustomOverlay?: ((props: RenderCustomOverlayProps) => React.ReactElement<any, string | React.JSXElementConstructor<any>> | undefined) | undefined;
/**
* Disabled opening expandable overlay
*/
disabled?: boolean | undefined;
} & {
children?: React.ReactNode;
} & React.RefAttributes<ExpandableOverlayMethods>>;
Expand Down
41 changes: 37 additions & 4 deletions src/components/picker/__tests__/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import {Picker} from '../index';
import React, {useState} from 'react';
import {fireEvent, render} from '@testing-library/react-native';
import Picker, {Picker as _Picker} from '../index';

const countries = [
{label: 'Israel', value: 'IL'},
Expand All @@ -11,15 +13,46 @@ const countries = [
describe('Picker', () => {
describe('getLabel', () => {
it('should get label of a simple item', () => {
let uut = new Picker({value: countries[2]});
let uut = new _Picker({value: countries[2]});
expect(uut.getLabelValueText()).toEqual(countries[2].label);
uut = new Picker({value: countries[3]});
uut = new _Picker({value: countries[3]});
expect(uut.getLabelValueText()).toEqual(countries[3].label);
});

it('should get label out of an array of items', () => {
const uut = new Picker({value: [countries[2], countries[4]]});
const uut = new _Picker({value: [countries[2], countries[4]]});
expect(uut.getLabelValueText()).toEqual(`${countries[2].label}, ${countries[4].label}`);
});
});

describe('Picker - Render Tests', () => {
it('should open picker overlay after pressing the picker', () => {
const renderTree = render(<TestCase/>);
const picker = renderTree.getByTestId('picker');
const pickerOverlay = renderTree.getByTestId('picker.overlay');
expect(pickerOverlay.props.visible).toBe(false);
fireEvent.press(picker);
expect(pickerOverlay.props.visible).toBe(true);
});

it('should not open picker overlay after pressing when picker is disabled', () => {
const renderTree = render(<TestCase editable={false}/>);
const picker = renderTree.getByTestId('picker');
const pickerOverlay = renderTree.getByTestId('picker.overlay');
expect(pickerOverlay.props.visible).toBe(false);
fireEvent.press(picker);
expect(pickerOverlay.props.visible).toBe(false);
});
});
});

const TestCase = props => {
const [value, setValue] = useState(props.value);
return (
<Picker testID="picker" {...props} onChange={setValue} value={value}>
{countries.map(country => {
return <Picker.Item key={country.value} value={country.value} label={country.label}/>;
})}
</Picker>
);
};
7 changes: 5 additions & 2 deletions src/components/picker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,8 @@ class Picker extends Component {
enableModalBlur,
topBarProps,
pickerModalProps,
value
value,
editable
} = this.props;

if (useNativePicker) {
Expand Down Expand Up @@ -489,15 +490,17 @@ class Picker extends Component {
modalProps={modalProps}
expandableContent={this.renderExpandableModal()}
renderCustomOverlay={renderCustomModal ? this.renderCustomModal : undefined}
testID={renderCustomModal ? testID : undefined}
testID={testID}
{...customPickerProps}
disabled={editable === false}
>
{renderPicker ? (
renderPicker(value, this.getLabel(value))
) : (
<TextField
ref={forwardedRef}
{...textInputProps}
testID={`${testID}.input`}
containerStyle={[paddings, margins, positionStyle, containerStyle]}
{...this.getAccessibilityInfo()}
importantForAccessibility={'no-hide-descendants'}
Expand Down
12 changes: 9 additions & 3 deletions src/incubator/expandableOverlay/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ export type ExpandableOverlayProps = TouchableOpacityProps &
* A custom overlay to render instead of Modal or Dialog components
*/
renderCustomOverlay?: (props: RenderCustomOverlayProps) => React.ReactElement | undefined;
/**
* Disabled opening expandable overlay
*/
disabled?: boolean;
}>;

interface ExpandableOverlayMethods {
Expand All @@ -59,6 +63,8 @@ const ExpandableOverlay = (props: ExpandableOverlayProps, ref: any) => {
showTopBar,
topBarProps,
renderCustomOverlay,
disabled,
testID,
...others
} = props;
const [visible, setExpandableVisible] = useState(false);
Expand All @@ -79,7 +85,7 @@ const ExpandableOverlay = (props: ExpandableOverlayProps, ref: any) => {

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

const renderDialog = () => {
return (
<Dialog {...dialogProps} visible={visible} onDismiss={closeExpandable}>
<Dialog testID={`${testID}.overlay`} {...dialogProps} visible={visible} onDismiss={closeExpandable}>
{expandableContent}
</Dialog>
);
Expand All @@ -108,7 +114,7 @@ const ExpandableOverlay = (props: ExpandableOverlayProps, ref: any) => {
};

return (
<TouchableOpacity {...others} onPress={openExpandable}>
<TouchableOpacity {...others} onPress={openExpandable} disabled={disabled} testID={testID}>
<View pointerEvents="none">{children}</View>
{renderOverlay()}
</TouchableOpacity>
Expand Down

0 comments on commit b1fe026

Please sign in to comment.