Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: nonce field styles #2563

Merged
merged 3 commits into from
Oct 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading