-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract InternalSelect; add SelectInput
- Loading branch information
1 parent
2eb242a
commit d62ac4c
Showing
5 changed files
with
265 additions
and
208 deletions.
There are no files selected for viewing
136 changes: 136 additions & 0 deletions
136
packages/bento-design-system/src/SelectField/InternalSelect.tsx
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,136 @@ | ||
import Select, { MultiValue as MultiValueT, SingleValue as SingleValueT } from "react-select"; | ||
import { useDefaultMessages } from ".."; | ||
import { FieldProps } from "../Field/FieldProps"; | ||
import { BentoConfigProvider, useBentoConfig } from "../BentoConfigContext"; | ||
import { AriaLabelingProps, DOMProps } from "@react-types/shared"; | ||
import * as selectComponents from "./components"; | ||
import { useEffect, useRef } from "react"; | ||
import { BaseMultiProps, BaseSelectProps, BaseSingleProps, SelectOption } from "./types"; | ||
|
||
type MultiProps<A> = BaseMultiProps & | ||
Pick<FieldProps<A[]>, "autoFocus" | "disabled" | "name" | "onBlur" | "onChange" | "value">; | ||
|
||
type SingleProps<A> = BaseSingleProps & | ||
Pick< | ||
FieldProps<A | undefined>, | ||
"autoFocus" | "disabled" | "name" | "onBlur" | "onChange" | "value" | ||
>; | ||
|
||
type Props<A> = BaseSelectProps<A> & { | ||
fieldProps: AriaLabelingProps & DOMProps; | ||
validationState: "valid" | "invalid"; | ||
} & (SingleProps<A> | MultiProps<A>); | ||
|
||
export function InternalSelect<A>(props: Props<A>) { | ||
const dropdownConfig = useBentoConfig().dropdown; | ||
const { defaultMessages } = useDefaultMessages(); | ||
|
||
const menuPortalTarget = useRef<HTMLDivElement>(); | ||
useEffect(() => { | ||
if (!menuPortalTarget.current) { | ||
menuPortalTarget.current = document.createElement("div"); | ||
} | ||
document.body.appendChild(menuPortalTarget.current); | ||
|
||
return () => { | ||
if (document.body.contains(menuPortalTarget.current!)) { | ||
document.body.removeChild(menuPortalTarget.current!); | ||
} | ||
}; | ||
}, [menuPortalTarget]); | ||
|
||
const { | ||
fieldProps, | ||
validationState, | ||
value, | ||
onChange, | ||
options, | ||
onBlur, | ||
name, | ||
placeholder, | ||
disabled, | ||
isReadOnly, | ||
isMulti, | ||
noOptionsMessage, | ||
autoFocus, | ||
menuSize = dropdownConfig.defaultMenuSize, | ||
searchable, | ||
} = props; | ||
|
||
return ( | ||
// NOTE(gabro): SelectField has its own config for List, so we override it here using BentoConfigProvider | ||
<BentoConfigProvider value={{ list: dropdownConfig.list }}> | ||
<Select | ||
id={fieldProps.id} | ||
name={name} | ||
aria-label={fieldProps["aria-label"]} | ||
aria-labelledby={fieldProps["aria-labelledby"]} | ||
isDisabled={disabled} | ||
isReadOnly={isReadOnly || false} | ||
autoFocus={autoFocus} | ||
value={ | ||
isMulti | ||
? options.filter((o) => ((value ?? []) as readonly A[]).includes(o.value)) | ||
: options.find((o) => o.value === value) | ||
} | ||
onChange={(o) => { | ||
if (isMulti) { | ||
const multiValue = o as MultiValueT<SelectOption<A>>; | ||
onChange(multiValue.map((a) => a.value)); | ||
} else { | ||
const singleValue = o as SingleValueT<SelectOption<A>>; | ||
onChange(singleValue == null ? undefined : singleValue.value); | ||
} | ||
}} | ||
onBlur={onBlur} | ||
options={options | ||
.slice() // avoid mutating the original array | ||
.sort((a, b) => { | ||
// In case of multi-select, we display the selected options first | ||
if (isMulti) { | ||
const selectedValues = (value ?? []) as readonly A[]; | ||
const isSelected = (a: SelectOption<A>) => selectedValues.includes(a.value); | ||
if (isSelected(a) && !isSelected(b)) { | ||
return -1; | ||
} | ||
if (!isSelected(a) && isSelected(b)) { | ||
return 1; | ||
} | ||
} | ||
return 0; | ||
})} | ||
placeholder={placeholder} | ||
menuPortalTarget={menuPortalTarget.current} | ||
components={selectComponents} | ||
openMenuOnFocus | ||
styles={selectComponents.styles<SelectOption<A>>()} | ||
validationState={validationState} | ||
isMulti={isMulti} | ||
isClearable={false} | ||
noOptionsMessage={() => noOptionsMessage ?? defaultMessages.SelectField.noOptionsMessage} | ||
multiValueMessage={ | ||
props.isMulti && (!props.multiSelectMode || props.multiSelectMode === "summary") | ||
? props.multiValueMessage ?? defaultMessages.SelectField.multiOptionsSelected | ||
: undefined | ||
} | ||
closeMenuOnSelect={!isMulti} | ||
hideSelectedOptions={false} | ||
menuSize={menuSize} | ||
menuIsOpen={isReadOnly ? false : undefined} | ||
isSearchable={isReadOnly ? false : searchable ?? true} | ||
showMultiSelectBulkActions={isMulti ? props.showMultiSelectBulkActions : false} | ||
clearAllButtonLabel={ | ||
isMulti | ||
? props.clearAllButtonLabel ?? defaultMessages.SelectField.clearAllButtonLabel | ||
: undefined | ||
} | ||
selectAllButtonLabel={ | ||
isMulti | ||
? props.selectAllButtonLabel ?? defaultMessages.SelectField.selectAllButtonLabel | ||
: undefined | ||
} | ||
multiSelectMode={isMulti ? props.multiSelectMode : undefined} | ||
/> | ||
</BentoConfigProvider> | ||
); | ||
} |
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
Oops, something went wrong.