From b1fe02608672ce2d4ebf40357c27220edbff96d4 Mon Sep 17 00:00:00 2001 From: Ethan Sharabi Date: Sat, 27 Nov 2021 08:29:26 +0200 Subject: [PATCH] Fix issue with editable prop not working on Picker component --- .../incubator/expandableOverlay/index.d.ts | 8 ++++ src/components/picker/__tests__/index.spec.js | 41 +++++++++++++++++-- src/components/picker/index.js | 7 +++- src/incubator/expandableOverlay/index.tsx | 12 ++++-- 4 files changed, 59 insertions(+), 9 deletions(-) diff --git a/generatedTypes/src/incubator/expandableOverlay/index.d.ts b/generatedTypes/src/incubator/expandableOverlay/index.d.ts index 8f25eadaff..b68283f28e 100644 --- a/generatedTypes/src/incubator/expandableOverlay/index.d.ts +++ b/generatedTypes/src/incubator/expandableOverlay/index.d.ts @@ -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; @@ -71,6 +75,10 @@ declare const _default: React.ForwardRefExoticComponent React.ReactElement> | undefined) | undefined; + /** + * Disabled opening expandable overlay + */ + disabled?: boolean | undefined; } & { children?: React.ReactNode; } & React.RefAttributes>; diff --git a/src/components/picker/__tests__/index.spec.js b/src/components/picker/__tests__/index.spec.js index 3f173e7455..f084d89f88 100644 --- a/src/components/picker/__tests__/index.spec.js +++ b/src/components/picker/__tests__/index.spec.js @@ -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'}, @@ -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(); + 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(); + 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 ( + + {countries.map(country => { + return ; + })} + + ); +}; diff --git a/src/components/picker/index.js b/src/components/picker/index.js index 8578403f8c..fd014535c1 100644 --- a/src/components/picker/index.js +++ b/src/components/picker/index.js @@ -447,7 +447,8 @@ class Picker extends Component { enableModalBlur, topBarProps, pickerModalProps, - value + value, + editable } = this.props; if (useNativePicker) { @@ -489,8 +490,9 @@ 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)) @@ -498,6 +500,7 @@ class Picker extends Component { React.ReactElement | undefined; + /** + * Disabled opening expandable overlay + */ + disabled?: boolean; }>; interface ExpandableOverlayMethods { @@ -59,6 +63,8 @@ const ExpandableOverlay = (props: ExpandableOverlayProps, ref: any) => { showTopBar, topBarProps, renderCustomOverlay, + disabled, + testID, ...others } = props; const [visible, setExpandableVisible] = useState(false); @@ -79,7 +85,7 @@ const ExpandableOverlay = (props: ExpandableOverlayProps, ref: any) => { const renderModal = () => { return ( - + {showTopBar && } {expandableContent} @@ -88,7 +94,7 @@ const ExpandableOverlay = (props: ExpandableOverlayProps, ref: any) => { const renderDialog = () => { return ( - + {expandableContent} ); @@ -108,7 +114,7 @@ const ExpandableOverlay = (props: ExpandableOverlayProps, ref: any) => { }; return ( - + {children} {renderOverlay()}