Skip to content

Commit

Permalink
Merge pull request #466 from topotal/feature/disabled-list-item
Browse files Browse the repository at this point in the history
Add Disabled Props to List component
  • Loading branch information
nariakiraHara authored Dec 6, 2024
2 parents a43e894 + 791f6ed commit aef933f
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ interface Props<T = any> {
onHoverIn: (index: number) => void
onHoverOut: (index: number) => void
onPress?: (item: T) => void
itemDisabled?: (item: T) => boolean
}

export const Row = React.memo<Props>(({
Expand All @@ -21,14 +22,17 @@ export const Row = React.memo<Props>(({
disabledChangeBackground = false,
style,
renderItem,
itemDisabled,
onPress,
onHoverIn,
onHoverOut,
}) => {
const disabled = itemDisabled?.(item) ?? false
const { styles } = useStyles({
firstItem: index === 0,
hovered,
pressable: !!onPress,
disabled,
})

const handlePress = useCallback(() => {
Expand All @@ -51,6 +55,7 @@ export const Row = React.memo<Props>(({
return (
<Pressable
style={[styles.wrapper, style]}
disabled={disabled}
onPress={handlePress}
onHoverIn={handleHoverIn}
onHoverOut={handleHoverOut}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ interface Props {
firstItem: boolean
hovered: boolean
pressable: boolean
disabled: boolean
}


export const useStyles = ({
firstItem,
hovered,
pressable,
disabled,
}: Props) => {
const { color } = useTheme()

Expand All @@ -24,8 +27,10 @@ export const useStyles = ({
minHeight: 20,
borderTopWidth: firstItem ? 0 : 1,
borderColor: color.borderLight,
opacity: disabled ? 0.5 : 1,
backgroundColor: hovered ? color.background : color.surface,
cursor: pressable ? 'pointer' : 'default',
pointerEvents: disabled ? 'none' : 'auto',
},
}), [
color.background,
Expand All @@ -34,6 +39,7 @@ export const useStyles = ({
firstItem,
hovered,
pressable,
disabled,
])

return {
Expand Down
6 changes: 6 additions & 0 deletions packages/topotal-ui/src/components/List/index.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ DisabledChangeBackground.args = {
disabledChangeBackground: true,
}

export const ItemDisabled: Story<ListProps<Item>> = Template.bind({})
ItemDisabled.args = {
...defaultProps,
itemDisabled: (item) => item.name === 'item2',
}

export const DefaultEmptyView: Story<ListProps<Item>> = Template.bind({})
DefaultEmptyView.args = {
...defaultProps,
Expand Down
3 changes: 3 additions & 0 deletions packages/topotal-ui/src/components/List/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface Props<T> {
disabledChangeBackground?: boolean
keyExtractor?: (item: T, index: number) => string
onPressItem?: (item: T) => void
itemDisabled?: (item: T) => boolean
testID?: string
}

Expand All @@ -22,6 +23,7 @@ export const List = <T, >({
emptyView,
disabledChangeBackground = false,
renderItem,
itemDisabled,
keyExtractor,
onPressItem,
testID,
Expand Down Expand Up @@ -49,6 +51,7 @@ export const List = <T, >({
index={index}
hovered={index === hoveredIndex}
disabledChangeBackground={disabledChangeBackground}
itemDisabled={itemDisabled}
renderItem={renderItem}
onPress={onPressItem}
onHoverIn={handleHoverInRow}
Expand Down

0 comments on commit aef933f

Please sign in to comment.