Skip to content

Commit

Permalink
fix: nonce field styles (#2563)
Browse files Browse the repository at this point in the history
* fix: nonce field styles

* fix: prevent segments from adding option
  • Loading branch information
iamacook authored Oct 2, 2023
1 parent 5d5466d commit 0bbd714
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions src/components/tx-flow/common/TxNonce/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
ListSubheader,
type ListSubheaderProps,
} from '@mui/material'
import { createFilterOptions } from '@mui/material/Autocomplete'
import { Controller, useForm } from 'react-hook-form'

import { SafeTxContext } from '@/components/tx-flow/SafeTxProvider'
Expand Down Expand Up @@ -81,13 +82,22 @@ const NonceFormOption = memo(function NonceFormOption({
)
})

const getFieldMinWidth = (value: string): string => {
const getFieldMinWidth = (value: string, showRecommendedNonceButton = false): string => {
const MIN_CHARS = 5
const MAX_WIDTH = '200px'
const ADORNMENT_PADDING = '24px'

return `clamp(calc(${MIN_CHARS}ch + 6px), calc(${Math.max(MIN_CHARS, value.length)}ch + 6px), ${MAX_WIDTH})`
const clamped = `clamp(calc(${MIN_CHARS}ch + 6px), calc(${Math.max(MIN_CHARS, value.length)}ch + 6px), ${MAX_WIDTH})`

if (showRecommendedNonceButton) {
return `calc(${clamped} + ${ADORNMENT_PADDING})`
}

return clamped
}

const filter = createFilterOptions<string>()

enum TxNonceFormFieldNames {
NONCE = 'nonce',
}
Expand Down Expand Up @@ -169,6 +179,19 @@ const TxNonceForm = ({ nonce, recommendedNonce }: { nonce: string; recommendedNo
}}
options={[recommendedNonce, ...previousNonces]}
getOptionLabel={(option) => option.toString()}
filterOptions={(options, params) => {
const filtered = filter(options, params)

// Prevent segments from showing recommended, e.g. if recommended is 250, don't show for 2, 5 or 25
const shouldShow = !recommendedNonce.includes(params.inputValue)
const isQueued = options.some((option) => params.inputValue === option)

if (params.inputValue !== '' && !isQueued && shouldShow) {
filtered.push(recommendedNonce)
}

return filtered
}}
renderOption={(props, option) => {
const isRecommendedNonce = option === recommendedNonce
const isInitialPreviousNonce = option === previousNonces[0]
Expand Down Expand Up @@ -213,7 +236,7 @@ const TxNonceForm = ({ nonce, recommendedNonce }: { nonce: string; recommendedNo
},
])}
sx={{
minWidth: getFieldMinWidth(field.value),
minWidth: getFieldMinWidth(field.value, showRecommendedNonceButton),
}}
/>
</Tooltip>
Expand Down

0 comments on commit 0bbd714

Please sign in to comment.