Skip to content

Commit

Permalink
feat: display path
Browse files Browse the repository at this point in the history
  • Loading branch information
bitterteasweetorange committed Nov 10, 2023
1 parent 08ed895 commit 7c4d429
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 22 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mui-easy-cascader",
"version": "0.3.3",
"version": "0.3.4",
"main": "dist/index.js",
"module": "dist/index.es.js",
"types": "dist/src/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion src/EasyCascaderColumn/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export function EasyCascaderColumn<T extends EasyCascaderBaseNode>({
{getNodeLabel(node)}
{endAdornment?.(node)}
</ListItemText>
{node.childrenId && (
{node.childrenId && node.childrenId.length !== 0 && (
<KeyboardArrowRight color={selected ? 'primary' : 'disabled'} />
)}
</MenuItem>
Expand Down
14 changes: 14 additions & 0 deletions src/EasyCascaderInput/index.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,17 @@ export const Defalut = () => {
/>
)
}

export const DisplayPath = () => {
const [value, onChange] = useState<MockObject | null>(mockObjectNodes[2])

return (
<EasyCascaderInput<MockObject>
data={mockObjectNodes}
getNodeLabel={(node) => node.name}
value={value}
onChange={onChange}
displayPath
/>
)
}
55 changes: 35 additions & 20 deletions src/EasyCascaderInput/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { TextField } from '@mui/material'
import { RefObject, useRef, useState } from 'react'
import { Box, TextField } from '@mui/material'
import { RefObject, useMemo, useRef, useState } from 'react'
import { EasyCascader } from 'src/EasyCascader'
import { EasyList } from 'src/EasyList'
import { EasyPopper } from 'src/EasyPopper'
import { useDebounce } from 'use-debounce'
import { EasyCascaderInputProps, EasyCascaderBaseNode, EasyId } from '../types'
import { EasyCascaderBaseNode, EasyCascaderInputProps, EasyId } from '../types'

export function EasyCascaderInput<T extends EasyCascaderBaseNode>(
props: EasyCascaderInputProps<T>,
Expand All @@ -23,6 +23,7 @@ export function EasyCascaderInput<T extends EasyCascaderBaseNode>(
disabled,
required,
helperText,
displayPath,
...duplication
} = props

Expand All @@ -45,6 +46,18 @@ export function EasyCascaderInput<T extends EasyCascaderBaseNode>(
}
}

const textValue = useMemo(() => {
if (value === null) return ''
if (!displayPath) return getNodeLabel(value)
const text = [...(value.pathId || []), value.id]
?.map((id) => {
const node = data.find((node) => node.id === id)
return node ? getNodeLabel(node) : ''
})
.join(' / ')
return text
}, [data, getNodeLabel, value, displayPath])

return (
<>
<TextField
Expand All @@ -61,8 +74,8 @@ export function EasyCascaderInput<T extends EasyCascaderBaseNode>(
onSelectedId(value?.id ?? null)
}}
ref={anchorRef}
placeholder={isSearch && value ? getNodeLabel(value) : ''}
value={isSearch ? search : value === null ? '' : getNodeLabel(value)}
placeholder={isSearch && value ? textValue : ''}
value={isSearch ? search : textValue}
onChange={(e) => {
setSearch(e.target.value)
}}
Expand All @@ -76,21 +89,23 @@ export function EasyCascaderInput<T extends EasyCascaderBaseNode>(
setSearch('')
}}
>
{debouncedSearch ? (
<EasyList
{...duplication}
search={debouncedSearch}
setSearch={setSearch}
selectedId={selectedId}
setSelectedId={setSelectedId}
/>
) : (
<EasyCascader<T>
{...duplication}
selectedId={selectedId}
setSelectedId={setSelectedId}
/>
)}
<Box>
{debouncedSearch ? (
<EasyList
{...duplication}
search={debouncedSearch}
setSearch={setSearch}
selectedId={selectedId}
setSelectedId={setSelectedId}
/>
) : (
<EasyCascader<T>
{...duplication}
selectedId={selectedId}
setSelectedId={setSelectedId}
/>
)}
</Box>
</EasyPopper>
</>
)
Expand Down
1 change: 1 addition & 0 deletions src/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const mockObjectNodes: MockObject[] = [
id: 1,
name: 'childre-0',
pathId: [0],
childrenId: [],
},
{
id: 2,
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type EasyCascaderInputProps<T extends EasyCascaderBaseNode> =
helperText?: ReactNode
required?: boolean
disabled?: boolean
displayPath?: boolean
}

export type EasyId = number | string
Expand Down

0 comments on commit 7c4d429

Please sign in to comment.