Skip to content

Commit

Permalink
fix: simplify and correctly render tool filter tab buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
robinpyon committed Jan 12, 2024
1 parent 19f5b7d commit 4fb90bf
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 40 deletions.
53 changes: 16 additions & 37 deletions src/tool/scheduleFilters/ScheduleFilter.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {StateLink} from 'sanity/router'
import {red, white} from '@sanity/color'
import {Box, Flex, Tab, Text} from '@sanity/ui'
import {useStateLink} from 'sanity/router'
import {Button, Inline, Text} from '@sanity/ui'
import React from 'react'
import {SCHEDULE_STATE_DICTIONARY} from '../../constants'
import {Schedule, ScheduleState} from '../../types'
Expand All @@ -13,54 +12,34 @@ interface Props {
}

const ScheduleFilter = (props: Props) => {
const {selected, schedules, state, ...rest} = props
const {selected, schedules, state} = props

const count = useFilteredSchedules(schedules, state).length

const hasItems = count > 0

const critical = state === 'cancelled'
const criticalCount = state === 'cancelled' && hasItems

const {href, onClick} = useStateLink({
state: {state},
})

return (
<Tab
// @ts-expect-error actually, this as property works but is missing in the typings
as={StateLink}
id={state}
paddingX={1}
paddingY={2}
<Button
as="a"
href={href}
mode="bleed"
onClick={onClick}
selected={selected}
state={{state}}
tone={critical ? 'critical' : 'default'}
{...rest}
>
<Flex align="center" paddingX={1}>
<Inline space={2}>
<Text size={2} weight="medium">
{SCHEDULE_STATE_DICTIONARY[state].title}
</Text>
{/*
HACK: when there are no items, we still render in with hidden visibility to
preserve correct tab height / vertical padding.
*/}
<Box
marginLeft={count > 0 ? 2 : 0}
style={{
background: criticalCount ? red[500].hex : 'transparent',
color: criticalCount ? white.hex : 'inherit',
border: 'none',
boxShadow: 'none',
borderRadius: '2px',
visibility: hasItems ? 'visible' : 'hidden',
padding: hasItems ? '0.25em 0.4em' : '0.25em 0',
width: hasItems ? 'auto' : 0,
}}
>
<Text size={1} style={{color: criticalCount ? white.hex : 'inherit'}}>
{count}
</Text>
</Box>
</Flex>
</Tab>
{hasItems && <Text size={1}>{count}</Text>}
</Inline>
</Button>
)
}

Expand Down
6 changes: 3 additions & 3 deletions src/tool/scheduleFilters/ScheduleFilters.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {useRouter} from 'sanity/router'
import {CheckmarkIcon, CloseIcon, SelectIcon} from '@sanity/icons'
import {Box, Button, Label, Menu, MenuButton, MenuItem, TabList} from '@sanity/ui'
import {Box, Button, Flex, Label, Menu, MenuButton, MenuItem} from '@sanity/ui'
import {format} from 'date-fns'
import React from 'react'
import {SCHEDULE_FILTERS, SCHEDULE_STATE_DICTIONARY} from '../../constants'
Expand Down Expand Up @@ -86,7 +86,7 @@ export const ScheduleFilters = (props: ScheduleFiltersProps) => {
)}

{scheduleState && (
<TabList space={2}>
<Flex gap={2}>
{SCHEDULE_FILTERS.map((filter) => (
<ScheduleFilter
key={filter}
Expand All @@ -95,7 +95,7 @@ export const ScheduleFilters = (props: ScheduleFiltersProps) => {
state={filter}
/>
))}
</TabList>
</Flex>
)}
</Box>
</>
Expand Down

0 comments on commit 4fb90bf

Please sign in to comment.