Skip to content

Commit

Permalink
feat: Add TextSwitch component (#566)
Browse files Browse the repository at this point in the history
  • Loading branch information
dogmar authored Jan 23, 2024
1 parent 26285f8 commit f65fc90
Show file tree
Hide file tree
Showing 4 changed files with 508 additions and 33 deletions.
69 changes: 37 additions & 32 deletions src/components/ComboBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ function ComboBox({
titleContent,
showClearButton,
chips,
inputContent,
onDeleteChip: onDeleteChipProp,
...props
}: ComboBoxProps) {
Expand Down Expand Up @@ -358,6 +359,7 @@ function ComboBox({

const buttonRef = useRef(null)
const inputRef = useRef(null)
const inputInnerRef = useRef(null)
const listBoxRef = useRef(null)
const popoverRef = useRef(null)

Expand Down Expand Up @@ -420,7 +422,7 @@ function ComboBox({

if (dir === 0) return

if (elt instanceof HTMLInputElement) {
if (elt === inputInnerRef.current && elt instanceof HTMLInputElement) {
if (elt.selectionStart !== 0 || dir !== -1) {
return
}
Expand All @@ -440,7 +442,7 @@ function ComboBox({

if (dir === 1) {
if (!chip.nextElementSibling) {
inputRef.current?.querySelector('input')?.focus()
inputInnerRef.current?.focus()
} else {
chip?.nextElementSibling
?.querySelector(`[${CHIP_CLOSE_ATTR_KEY}]`)
Expand All @@ -458,36 +460,37 @@ function ComboBox({

outerInputProps = useMemo(
() => ({
...(!isEmpty(chips)
? {
inputContent: (
<InputChipList
ref={chipListRef}
onKeyDown={handleKeyDown}
>
{chips.map((chipProps) => (
<Chip
size="small"
condensed
truncateWidth={100}
truncateEdge="start"
closeButton
tooltip
onClick={onChipClick}
closeButtonProps={{
onClick: () => {
onDeleteChip?.(chipProps?.key)
},
'aria-label': `Remove ${chipProps.key}`,
}}
{...{ [CHIP_ATTR_KEY]: chipProps?.key }}
{...chipProps}
/>
))}
</InputChipList>
),
}
: {}),
inputContent: (
<>
{inputContent}
{!isEmpty(chips) && (
<InputChipList
ref={chipListRef}
onKeyDown={handleKeyDown}
>
{chips.map((chipProps) => (
<Chip
size="small"
condensed
truncateWidth={100}
truncateEdge="start"
closeButton
tooltip
onClick={onChipClick}
closeButtonProps={{
onClick: () => {
onDeleteChip?.(chipProps?.key)
},
'aria-label': `Remove ${chipProps.key}`,
}}
{...{ [CHIP_ATTR_KEY]: chipProps?.key }}
{...chipProps}
/>
))}
</InputChipList>
)}
</>
),
...(onDeleteChipProp
? {
onDeleteInputContent: () =>
Expand All @@ -502,6 +505,7 @@ function ComboBox({
[
chips,
handleKeyDown,
inputContent,
onDeleteChip,
onDeleteChipProp,
outerInputProps,
Expand All @@ -512,6 +516,7 @@ function ComboBox({
return (
<ComboBoxInner>
<ComboBoxInput
inputRef={inputInnerRef}
inputProps={{
...inputProps,
onKeyDown: (e: KeyboardEvent<HTMLInputElement>) => {
Expand Down
Loading

0 comments on commit f65fc90

Please sign in to comment.