Skip to content

Commit

Permalink
Fix country sorting for antd area select. (#67)
Browse files Browse the repository at this point in the history
* Fix country sorting for antd area select.

* Remove filterSort from AreaSelect lite.
  • Loading branch information
firflant authored Nov 10, 2022
1 parent a4a533d commit 509f7bc
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
3 changes: 1 addition & 2 deletions src/area-select.lite.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Select, { SelectProps } from 'rc-select';
import { OptionProps } from 'rc-select/es/Option';
import React, { useContext } from 'react';
import { configContext } from './config';
import { filterOption, filterSort } from './shared';
import { filterOption } from './shared';

export interface AreaSelectProps extends SelectProps<any> {
optionProps?: OptionProps;
Expand All @@ -21,7 +21,6 @@ export const AreaSelect = ({
dropdownMatchSelectWidth={false}
optionLabelProp="label"
filterOption={filterOption}
filterSort={filterSort}
{...selectProps}
>
{areas.map((item) => {
Expand Down
3 changes: 1 addition & 2 deletions src/area-select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Select } from 'antd';
import { OptionProps, SelectProps } from 'antd/es/select';
import React, { useContext } from 'react';
import { configContext } from './config';
import { filterOption, filterSort } from './shared';
import { filterOption } from './shared';

export interface AreaSelectProps extends SelectProps<any> {
optionProps?: OptionProps;
Expand All @@ -28,7 +28,6 @@ export const AreaSelect = ({
dropdownMatchSelectWidth={false}
optionLabelProp="label"
filterOption={filterOption}
filterSort={filterSort}
{...selectProps}
>
{areas.map((item) => {
Expand Down
16 changes: 15 additions & 1 deletion src/config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,27 @@ export type AreaMapper = (value: Area, index: number, array: Area[]) => Area;
export type AreaSorter = (a: Area, b: Area) => number;
const defaultAreaFilter: AreaFilter = () => true;
const defaultAreaMapper: AreaMapper = (area) => area;
const defaultAreaSorter: AreaSorter = (a, b) => {
if (a.name && b.name) {
const nameA = a.name.toUpperCase();
const nameB = b.name.toUpperCase();
if (nameA < nameB) {
return -1;
}
if (nameA > nameB) {
return 1;
}
return 0;
}
return 0;
};

export const ConfigProvider = ({
children,
locale = {},
areaFilter = defaultAreaFilter,
areaMapper = defaultAreaMapper,
areaSorter,
areaSorter = defaultAreaSorter,
}: {
children: ReactNode;
locale?: any;
Expand Down
6 changes: 0 additions & 6 deletions src/shared.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@ export const filterOption: SelectProps['filterOption'] = (input, option) => {
return keyHasAllChars;
};

export const filterSort: SelectProps['filterSort'] = (a, b) => {
const keyA = a.key as string;
const keyB = b.key as string;
return keyA.length - keyB.length;
};

export const usePhoneInput = ({
isControlled,
defaultValue,
Expand Down

0 comments on commit 509f7bc

Please sign in to comment.