Skip to content

Commit

Permalink
chore: fix ci
Browse files Browse the repository at this point in the history
  • Loading branch information
AtsushiM committed Jan 30, 2025
1 parent 686df8f commit d1ab3aa
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 31 deletions.
60 changes: 35 additions & 25 deletions packages/smarthr-ui/src/components/Browser/Browser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@ import { ItemNode, ItemNodeLike, RootNode } from './models'
import { getElementIdFromNode } from './utils'

const optionsListWrapper = tv({
base: [
'smarthr-ui-Browser shr-flex shr-flex-row shr-flex-nowrap shr-min-h-[355px]',
'[&[data-column-length="0"]]:shr-justify-center [&[data-column-length="0"]]:shr-items-center',
'[&[data-column-length="1"]]:[&>div]:shr-flex-1',
'[&[data-column-length="2"]]:[&>div:nth-child(1)]:shr-flex-1 [&[data-column-length="2"]]:[&>div:nth-child(2)]:shr-flex-[2]',
'[&[data-column-length="3"]]:[&>div]:shr-flex-1',
],
base: 'smarthr-ui-Browser shr-flex shr-flex-row shr-flex-nowrap shr-min-h-[355px]',
variants: {
columnCount: {
0: 'shr-justify-center shr-items-center',
1: '[&>div]:shr-flex-1',
2: '[&>div:nth-child(1)]:shr-flex-1 [&>div:nth-child(2)]:shr-flex-[2]',
3: '[&>div]:shr-flex-1',
},
},
defaultVariants: {
columnCount: 0,
},
})

type Props = {
Expand All @@ -36,9 +41,11 @@ const DECORATOR_DEFAULT_TEXTS = {
type DecoratorKeyTypes = keyof typeof DECORATOR_DEFAULT_TEXTS

export const Browser: FC<Props> = ({ value, items, decorators, onSelectItem }) => {
const style = useMemo(() => optionsListWrapper(), [])
const decorated = useDecorators<DecoratorKeyTypes>(DECORATOR_DEFAULT_TEXTS, decorators)
const rootNode = useMemo(() => RootNode.from({ children: items }), [items])
const columns = useMemo(() => rootNode.toViewData(value), [rootNode, value])

const style = useMemo(() => optionsListWrapper({ columnCount: columns.length as 0 | 1 | 2 | 3 }), [columns.length])
const decorated = useDecorators<DecoratorKeyTypes>(DECORATOR_DEFAULT_TEXTS, decorators)

const selectedNode = useMemo(() => {
if (value) {
Expand All @@ -47,39 +54,43 @@ export const Browser: FC<Props> = ({ value, items, decorators, onSelectItem }) =
return
}, [rootNode, value])

const columns = useMemo(() => rootNode.toViewData(value), [rootNode, value])

// FIXME: focusメソッドのfocusVisibleが主要ブラウザでサポートされたら使うようにしたい(現状ではマウスクリックでもfocusのoutlineが出てしまう)
// https://developer.mozilla.org/ja/docs/Web/API/HTMLElement/focus
const handleKeyDown: KeyboardEventHandler = useCallback(
(e) => {
let target: ItemNodeLike | null = null
if (!selectedNode) {
return
}

let target: ItemNode | undefined = undefined

switch (e.key) {
case 'ArrowUp': {
if (selectedNode) {
target = selectedNode.getPrev() ?? selectedNode.parent?.getLastChild()
}
target = selectedNode.getPrev() ?? selectedNode.parent?.getLastChild()

break
}
case 'ArrowDown': {
if (selectedNode) {
target = selectedNode.getNext() ?? selectedNode.parent?.getFirstChild()
}
target = selectedNode.getNext() ?? selectedNode.parent?.getFirstChild()

break
}
case 'ArrowLeft':
target = selectedNode?.parent
case 'ArrowLeft': {
const node = selectedNode.parent

if (node instanceof ItemNode) {
target = node
}

break
}
case 'ArrowRight':
case 'Enter':
case ' ':
target = selectedNode?.getFirstChild()
case ' ': {
target = selectedNode.getFirstChild()

break
}
}

if (target) {
Expand All @@ -96,14 +107,13 @@ export const Browser: FC<Props> = ({ value, items, decorators, onSelectItem }) =
<div
role="application"
onKeyDown={handleKeyDown}
data-column-length={columns.length}
className={style}
>
{columns.length > 0 ? (
columns.map((items, index) => (
columns.map((colItems, index) => (
<BrowserColumn
key={index}
items={items}
items={colItems}
index={index}
value={value}
onSelectItem={onSelectItem}
Expand Down
6 changes: 4 additions & 2 deletions packages/smarthr-ui/src/components/Browser/BrowserColumn.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { FC, useMemo } from 'react'
import React, { FC } from 'react'

import { BrowserItem } from './BrowserItem'
import { ItemNode } from './models'
Expand All @@ -17,6 +17,7 @@ export const BrowserColumn: FC<Props> = ({ items, index: columnIndex, value, onS
<ul className="shr-list-none shr-px-0.25 shr-py-0.5" id={getColumnId(columnIndex)}>
{items.map((item, rowIndex) => (
<ListItem
key={rowIndex}
itemValue={item.value}
itemLabel={item.label}
itemHasChildren={item.children.length > 0}
Expand All @@ -31,10 +32,11 @@ export const BrowserColumn: FC<Props> = ({ items, index: columnIndex, value, onS
)

const ListItem = React.memo<
Pick<Props, 'value' | 'columnIndex' | 'onSelectItem'> & {
Pick<Props, 'value' | 'onSelectItem'> & {
itemValue: ItemNode['value']
itemLabel: ItemNode['label']
itemHasChildren: boolean
columnIndex: Props['index']
rowIndex: number
}
>(({ itemValue, itemLabel, itemHasChildren, value, columnIndex, rowIndex, onSelectItem }) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/smarthr-ui/src/components/Browser/BrowserItem.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { FC, KeyboardEventHandler, useCallback, useMemo } from 'react'
import React, { KeyboardEventHandler, useMemo } from 'react'
import { tv } from 'tailwind-variants'

import { FaAngleRightIcon } from '../Icon'
Expand Down
13 changes: 13 additions & 0 deletions packages/smarthr-ui/src/components/Browser/models/ItemNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export class ItemNode {
this.value = value
this.label = label
this.context = new NodeContext(this)

for (const child of children) {
this.append(child)
}
Expand All @@ -43,28 +44,35 @@ export class ItemNode {
if (this.#getDepth() >= NODE_MAX_DEPTH) {
throw new Error(`Cannot append to a node deeper than ${NODE_MAX_DEPTH}`)
}

this.context.append(that.context)
}

getNext(): ItemNode | undefined {
if (!this.parent) {
return
}

const index = this.#getIndex()

if (index === -1) {
return
}

return this.parent.children[index + 1]
}

getPrev(): ItemNode | undefined {
if (!this.parent) {
return
}

const index = this.#getIndex()

if (index === -1) {
return
}

return this.parent.children[index - 1]
}

Expand All @@ -80,6 +88,7 @@ export class ItemNode {
const ancestors: ItemNode[] = []

let current: Node | undefined = this.parent

while (current instanceof ItemNode) {
ancestors.unshift(current)
current = current.parent
Expand All @@ -92,15 +101,18 @@ export class ItemNode {
if (!this.parent) {
return []
}

return this.parent.children
}

findByValue(value: string): ItemNode | undefined {
if (this.value === value) {
return this
}

for (const child of this.children) {
const found = child.findByValue(value)

if (found) {
return found
}
Expand All @@ -112,6 +124,7 @@ export class ItemNode {
if (!this.parent) {
return -1
}

return this.parent.children.indexOf(this)
}

Expand Down
4 changes: 4 additions & 0 deletions packages/smarthr-ui/src/components/Browser/models/RootNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export class RootNode {

constructor(children: ItemNode[]) {
this.context = new NodeContext(this)

for (const child of children) {
this.append(child)
}
Expand Down Expand Up @@ -38,10 +39,12 @@ export class RootNode {
findByValue(value: string): ItemNode | undefined {
for (const child of this.children) {
const found = child.findByValue(value)

if (found) {
return found
}
}

return
}

Expand All @@ -55,6 +58,7 @@ export class RootNode {
}

const pivot = this.findByValue(value)

if (!pivot) {
return [this.children]
}
Expand Down
4 changes: 1 addition & 3 deletions packages/smarthr-ui/src/components/Browser/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
import { ItemNode } from './models'

export const getElementIdFromNode = (node: ItemNode) => `radio-${node.value}`
export const getElementIdFromNode = (value: string) => `radio-${value}`

0 comments on commit d1ab3aa

Please sign in to comment.