Skip to content

Fix issue with editable prop not working on Picker component #1697

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 1 commit into from
Nov 28, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
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