Skip to content

Commit

Permalink
fix: fix properties and methods table and fix some style
Browse files Browse the repository at this point in the history
  • Loading branch information
yvmunayev committed May 25, 2020
1 parent cd2bf76 commit 5f02147
Show file tree
Hide file tree
Showing 13 changed files with 57 additions and 37 deletions.
4 changes: 4 additions & 0 deletions library/styleguideComponents/PropsTable/bodyRows.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ function renderValue(value, caption, row) {
}
}

if (caption === 'Default' && type.name === 'array') {
return <div className="react-rainbow-defualt-array">{value}</div>;
}

return value;
}

Expand Down
6 changes: 6 additions & 0 deletions library/styleguideComponents/PropsTable/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,10 @@

.react-rainbow-prop-description {
white-space: normal;
}

.react-rainbow-defualt-array {
white-space: normal;
max-width: 300px;
text-align: justify;
}
2 changes: 2 additions & 0 deletions src/components/PhoneInput/countriesDropdown/hooks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ export { default as useFilterCountries } from './useFilterCountries';
export { default as useKeyboardNavigation } from './useKeyboardNavigation';
export { default as useScrollControls } from './useScrollControls';
export { default as useSimulatedLoading } from './useSimulatedLoading';
export { default as useItemsRef } from './useItemsRef';
export { default as useHandleCountryChange } from './useHandleCountryChange';
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { useCallback } from 'react';

export default function useHandleCountryChange(scrollableRef, onCountryChange, setQuery) {
return useCallback(
newCountry => {
setQuery('');
scrollableRef.current.scrollTo(0, 0);
onCountryChange(newCountry);
},
[onCountryChange, scrollableRef, setQuery],
);
}
10 changes: 10 additions & 0 deletions src/components/PhoneInput/countriesDropdown/hooks/useItemsRef.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { useRef, useEffect } from 'react';

export default function useItemsRef(length) {
const itemsRef = useRef([]);
useEffect(() => {
itemsRef.current = itemsRef.current.slice(0, length);
}, [length]);

return itemsRef;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ import isOptionVisible from '../../../Picklist/helpers/isOptionVisible';
import calculateScrollOffset from '../../../Picklist/helpers/calculateScrollOffset';

export default function useKeyboardNavigation(
country,
list,
ref,
scrollableRef,
itemsRef,
onEnter,
handleCountryChange,
setFocusIndex,
) {
const activeIndex = useRef(0);
Expand Down Expand Up @@ -64,14 +65,14 @@ export default function useKeyboardNavigation(
[DOWN_KEY]: () => moveToOption(getNewIndex(active + 1, length)),
[HOME_KEY]: () => moveToOption(0),
[END_KEY]: () => moveToOption(length - 1),
[ENTER_KEY]: () => onEnter(event, active),
[ENTER_KEY]: () => handleCountryChange(list[active]),
[ESCAPE_KEY]: () => setFocusIndex(0),
[TAB_KEY]: () => setFocusIndex(2),
};
keyHandlerMap[event.keyCode]();
}
},
[length, moveToOption, onEnter, setFocusIndex],
[handleCountryChange, length, list, moveToOption, setFocusIndex],
);

useEffect(() => {
Expand All @@ -86,7 +87,7 @@ export default function useKeyboardNavigation(
handleActiveChange(0);
scrollableRef.current.scrollTo(0, 0);
activeIndex.current = 0;
}, [handleActiveChange, itemsRef, length, scrollableRef]);
}, [country, handleActiveChange, itemsRef, length, scrollableRef]);

useEffect(() => {
const currentItem = itemsRef.current[activeIndex.current];
Expand Down
36 changes: 12 additions & 24 deletions src/components/PhoneInput/countriesDropdown/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import React, { useRef, useEffect, useCallback, memo } from 'react';
import React, { useRef, memo } from 'react';
import PropTypes from 'prop-types';
import { useFilterCountries, useKeyboardNavigation, useScrollControls } from './hooks';
import {
useFilterCountries,
useKeyboardNavigation,
useScrollControls,
useItemsRef,
useHandleCountryChange,
} from './hooks';
import {
StyledScrollable,
StyledUl,
Expand Down Expand Up @@ -30,35 +36,17 @@ const CountriesDropdown = memo(props => {

const scrollableRef = useRef();
const [query, countriesFiltered, setQuery] = useFilterCountries(countries, country);

const handleCountryChange = useCallback(
newCountry => {
setQuery('');
scrollableRef.current.scrollTo(0, 0);
onCountryChange(newCountry);
},
[onCountryChange, setQuery],
);

const onEnter = useCallback((event, index) => handleCountryChange(countriesFiltered[index]), [
handleCountryChange,
countriesFiltered,
]);

const itemsRef = useRef([]);
useEffect(() => {
itemsRef.current = itemsRef.current.slice(0, countriesFiltered.length);
}, [countriesFiltered.length]);

const itemsRef = useItemsRef(countriesFiltered.length);
const handleCountryChange = useHandleCountryChange(scrollableRef, onCountryChange, setQuery);
const handleActiveChange = useKeyboardNavigation(
country,
countriesFiltered,
searchRef,
scrollableRef,
itemsRef,
onEnter,
handleCountryChange,
setFocusIndex,
);

const {
showScrollUp,
showScrollDown,
Expand Down
2 changes: 1 addition & 1 deletion src/components/PhoneInput/hooks/useHandleCountryChange.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ export default function useHandleCountryChange(phone, onChange, setFocusIndex, i
});
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[onChange, isOpen],
[isOpen],
);
}
4 changes: 3 additions & 1 deletion src/components/PhoneInput/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,10 @@ const PhoneInput = React.forwardRef((props, ref) => {
searchRef,
inputRef,
);
const isOpen = focusIndex === 1;
const handleFocus = useHandleFocus(focusIndex, onFocus, setFocusIndex);
const handleBlur = useHandleBlur(focusIndex, onBlur);

const isOpen = focusIndex === 1;
const handleCountryChange = useHandleCountryChange(phone, onChange, setFocusIndex, isOpen);

function handlePhoneChange(event) {
Expand Down Expand Up @@ -245,6 +246,7 @@ PhoneInput.propTypes = {
style: PropTypes.object,
/** The id of the outer element. */
id: PropTypes.string,
/** Specifies the available countries for selection. */
countries: PropTypes.array,
};

Expand Down
2 changes: 0 additions & 2 deletions src/components/PhoneInput/styled/flagContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import styled from 'styled-components';
import attachThemeAttrs from '../../../styles/helpers/attachThemeAttrs';

const StyledFlagContainer = attachThemeAttrs(styled.div)`
display: flex;
flex: 0 0 auto;
padding-right: 12px;
border-right: 1px solid ${props => props.palette.text.header};
${props =>
Expand Down
5 changes: 2 additions & 3 deletions src/components/PhoneInput/styled/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ export const StyledCountryCode = attachThemeAttrs(styled.div)`
`;

export const StyledFlagIcon = styled.img`
width: 20px;
height: 20px;
flex: 0 0 auto;
width: 28px;
height: 100%;
`;

export { default as StyledIndicator } from './indicator';
Expand Down
1 change: 0 additions & 1 deletion src/components/PhoneInput/styled/indicator.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const StyledIndicator = styled.span`
z-index: 2;
margin-top: -4px;
margin-left: 8px;
flex: 0 0 auto;
&::after {
content: '';
Expand Down
1 change: 0 additions & 1 deletion src/components/PhoneInput/styled/trigger.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ const StyledTrigger = attachThemeAttrs(styled.button)`
font-weight: 300;
font-size: ${FONT_SIZE_TEXT_LARGE};
border-radius: ${BORDER_RADIUS_2} 0 0 ${BORDER_RADIUS_2};
display: flex;
:hover,
:focus,
Expand Down

0 comments on commit 5f02147

Please sign in to comment.