Skip to content

Commit

Permalink
correcciones componente select
Browse files Browse the repository at this point in the history
  • Loading branch information
Damian Morales committed Jan 5, 2024
1 parent b6e4ecf commit de5d318
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 23 deletions.
16 changes: 9 additions & 7 deletions src/components/Select/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Select from './';
jest.spyOn(React, 'useEffect').mockImplementation((f) => f());

const useStateSpy = jest.spyOn(React, 'useState');
const inputRefSpy = jest.spyOn(React, 'useRef');
const setInputValueSpy = jest.fn();
const setSelectedOptions = jest.fn();
const setFilteredOptionsSpy = jest.fn();
Expand All @@ -28,6 +29,8 @@ const validProps = {
onFocusSpy: jest.fn(),
};

const measureMock = jest.fn((callback) => callback(0, 0, 100, 50, 0, 0));

const validMeasures = {width: 0, pageY: 0, pageX: 0};

describe('Select component', () => {
Expand All @@ -44,13 +47,17 @@ describe('Select component', () => {
.mockReturnValueOnce([validOptions, setFilteredOptionsSpy])
.mockReturnValueOnce([false, setIsShowedDropdownSpy])
.mockReturnValueOnce([validMeasures, setDropdownMeasures]);
inputRefSpy.mockReturnValueOnce({current: {measure: measureMock}});
const {root} = create(
<Select options={validProps.options} label={validProps.label} isDisabled={true} />
);
const ChevronComponent = root.findByType(Icon);
ChevronComponent.props.onPress();

jest.spyOn(React, 'useEffect').mockImplementation((f) => f());

expect(setIsShowedDropdownSpy).not.toBeCalled();
expect(setDropdownMeasures).toBeCalled();
});
});

Expand Down Expand Up @@ -104,9 +111,7 @@ describe('Select component', () => {
.mockReturnValueOnce([true, setIsShowedDropdownSpy])
.mockReturnValueOnce([validMeasures, setDropdownMeasures]);

const {root} = create(
<Select options={validProps.options} label={validProps.label} isSearchable />
);
const {root} = create(<Select options={validProps.options} label={validProps.label} />);
const InputComponent = root.findByType(TextInput);
InputComponent.props.onFocus();
InputComponent.props.onChangeText(5);
Expand All @@ -123,9 +128,7 @@ describe('Select component', () => {
.mockReturnValueOnce([true, setIsShowedDropdownSpy])
.mockReturnValueOnce([validMeasures, setDropdownMeasures]);

const {root} = create(
<Select options={validProps.options} label={validProps.label} isSearchable />
);
const {root} = create(<Select options={validProps.options} label={validProps.label} />);
const InputComponent = root.findByType(TextInput);
InputComponent.props.onFocus();
InputComponent.props.onChangeText('');
Expand All @@ -146,7 +149,6 @@ describe('Select component', () => {
<Select
options={validProps.options}
label={validProps.label}
isSearchable
onFocus={validProps.onFocusSpy}
/>
);
Expand Down
28 changes: 12 additions & 16 deletions src/components/Select/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ interface SelectProps {
optionStyles?: {};
placeholder?: string;
inputProps?: TextInput;
isSearchable?: boolean;
isMulti?: boolean;
isDisabled?: boolean;
noOptionsMessage?: string;
Expand All @@ -68,7 +67,6 @@ const Select: FC<SelectProps> = ({
placeholder = '',
optionStyles = {},
inputProps = {},
isSearchable = false,
isMulti = false,
isDisabled = false,
noOptionsMessage = 'no options',
Expand Down Expand Up @@ -114,11 +112,9 @@ const Select: FC<SelectProps> = ({
};

const handleOnFocus = () => {
if (!isSearchable || !isShowedOptions || isMulti || variantOptions === VariantOptions.Modal) {
Keyboard.dismiss();
}
onFocus();
Keyboard.dismiss();
setIsShowedOptions(true);
onFocus();
};

const setSingleOption = (option: Option) => {
Expand Down Expand Up @@ -175,6 +171,14 @@ const Select: FC<SelectProps> = ({
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [hasDefaultValue, value]);

useEffect(() => {
if (inputRef.current) {
inputRef.current.measure((x, y, width, height, pageX, pageY) =>
setDropdownMeasures({width, pageX, pageY: pageY - 15})
);
}
}, [isShowedOptions]);

const styles = StyleSheet.create({
wrapper: {
width: '100%',
Expand Down Expand Up @@ -226,14 +230,6 @@ const Select: FC<SelectProps> = ({
},
});

useEffect(() => {
if (inputRef.current) {
inputRef.current.measure((x, y, width, height, pageX, pageY) =>
setDropdownMeasures({width, pageX, pageY: pageY - 15})
);
}
}, [isShowedOptions]);

return (
<View style={styles.wrapper} {...props}>
<View style={styles.wrapperInput}>
Expand All @@ -259,8 +255,8 @@ const Select: FC<SelectProps> = ({
style={styles.input}
value={inputValue}
placeholder={isMoveLabel && placeholder}
showSoftInputOnFocus={!isMulti && isSearchable}
caretHidden={!isSearchable}
showSoftInputOnFocus={false}
caretHidden={true}
keyboardType={keyboardType}
editable={!isDisabled}
onFocus={handleOnFocus}
Expand Down

0 comments on commit de5d318

Please sign in to comment.