-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
98 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* text eol=lf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,41 @@ | ||
import EntitiesSearch from '@types'; | ||
import { Set } from 'immutable'; | ||
import React, { JSX } from 'react'; | ||
import Select from 'react-select'; | ||
import Select, { SingleValue } from 'react-select'; | ||
|
||
import { matchOptionValues } from '../utils/match-option-values'; | ||
import { onChangeControlOptionsHandle } from '../utils/on-change-control-options-handle'; | ||
|
||
export function PostTypeSelect<V>( | ||
props: EntitiesSearch.PostTypeSelect<V> | ||
): JSX.Element | null { | ||
const matchedValues = matchOptionValues(Set([props.value]), props.options); | ||
|
||
const onChange = (values: Set<V> | null) => | ||
props.onChange(values?.first() ?? null); | ||
|
||
return ( | ||
<Select | ||
isMulti={false} | ||
value={props.value} | ||
value={matchedValues?.first() ?? null} | ||
options={props.options.toArray()} | ||
onChange={props.onChange} | ||
onChange={(options) => { | ||
if (isNonNullableValue(options)) { | ||
onChange(null); | ||
return; | ||
} | ||
|
||
onChangeControlOptionsHandle( | ||
onChange, | ||
Set(options ? [options] : []) | ||
); | ||
}} | ||
/> | ||
); | ||
} | ||
|
||
function isNonNullableValue<V>( | ||
value: SingleValue<EntitiesSearch.ControlOption<V>> | ||
): value is NonNullable<EntitiesSearch.ControlOption<V>> { | ||
return value !== null && value.value !== null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import EntitiesSearch from '@types'; | ||
import { Set } from 'immutable'; | ||
|
||
export const matchOptionValues = <V>( | ||
value: Set<V> | null, | ||
options: Set<EntitiesSearch.ControlOption<V>> | ||
): Set<EntitiesSearch.ControlOption<V>> | null => { | ||
if (!value) return null; | ||
return options.filter((option) => value?.has(option.value)); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import EntitiesSearch from '@types'; | ||
import { Set } from 'immutable'; | ||
|
||
import { isControlOption } from './is-control-option'; | ||
|
||
/** | ||
* @internal | ||
* @param handler | ||
* @param options | ||
*/ | ||
export const onChangeControlOptionsHandle = <V>( | ||
handler: (values: Set<V> | null) => void, | ||
options: Set<EntitiesSearch.ControlOption<V>> | null | ||
): void => { | ||
if (options === null) return; | ||
const controlOptions = Set(options).filter(isControlOption); | ||
const values = controlOptions.map((option) => option.value); | ||
handler(values.size > 0 ? values : null); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters