Skip to content
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

added priorityRepeat prop to Country #88

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
14 changes: 8 additions & 6 deletions src/CountryDropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ export default class CountryDropdown extends Component {
super(props);

this.state = {
countries: helpers.filterCountries(CountryRegionData, props.priorityOptions, props.whitelist, props.blacklist)
countries: helpers.filterCountries(CountryRegionData, props.priorityOptions, props.whitelist, props.blacklist, props.priorityRepeat)
};
}

getCountries () {
const { valueType, labelType } = this.props;

return this.state.countries.map(([countryName, countrySlug]) => (
<option value={(valueType === C.DISPLAY_TYPE_SHORT) ? countrySlug : countryName} key={countrySlug}>
return this.state.countries.map(([countryName, countrySlug], nr) => (
<option value={(valueType === C.DISPLAY_TYPE_SHORT) ? countrySlug : countryName} key={countrySlug + nr}>
{(labelType === C.DISPLAY_TYPE_SHORT) ? countrySlug : countryName}
</option>
));
Expand All @@ -37,7 +37,7 @@ export default class CountryDropdown extends Component {
render () {
// unused properties deliberately added so arbitraryProps gets populated with anything else the user specifies
const { name, id, classes, value, onChange, onBlur, disabled, showDefaultOption, defaultOptionLabel,
labelType, valueType, whitelist, blacklist, customOptions, priorityOptions, ...arbitraryProps } = this.props;
labelType, valueType, whitelist, blacklist, priorityRepeat, customOptions, priorityOptions, ...arbitraryProps } = this.props;

const attrs = {
...arbitraryProps,
Expand Down Expand Up @@ -77,7 +77,8 @@ CountryDropdown.propTypes = {
valueType: PropTypes.oneOf([C.DISPLAY_TYPE_FULL, C.DISPLAY_TYPE_SHORT]),
whitelist: PropTypes.array,
blacklist: PropTypes.array,
disabled: PropTypes.bool
disabled: PropTypes.bool,
priorityRepeat: PropTypes.bool
};
CountryDropdown.defaultProps = {
value: '',
Expand All @@ -93,5 +94,6 @@ CountryDropdown.defaultProps = {
valueType: C.DISPLAY_TYPE_FULL,
whitelist: [],
blacklist: [],
disabled: false
disabled: false,
priorityRepeat: false
};
7 changes: 4 additions & 3 deletions src/helpers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// reduces the subset of countries depending on whether the user specified a white/blacklist, and lists priority
// countries first
export const filterCountries = (countries, priorityCountries, whitelist, blacklist) => {
export const filterCountries = (countries, priorityCountries, whitelist, blacklist, priorityRepeat) => {
let countriesListedFirst = [];
let filteredCountries = countries;

Expand All @@ -19,8 +19,9 @@ export const filterCountries = (countries, priorityCountries, whitelist, blackli
countriesListedFirst.push(result);
}
});

filteredCountries = filteredCountries.filter(([, countrySlug]) => priorityCountries.indexOf(countrySlug) === -1);
if (!priorityRepeat) {
filteredCountries = filteredCountries.filter(([, countrySlug]) => priorityCountries.indexOf(countrySlug) === -1);
}
}

return countriesListedFirst.length ? [...countriesListedFirst, ...filteredCountries] : filteredCountries;
Expand Down