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

refactor: migrate styled-components usage of form components #1928

Merged
merged 8 commits into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
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
16 changes: 3 additions & 13 deletions src/components/SearchInput.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { useState } from 'react'
import styled from 'styled-components'

import { UseDebouncedSearch } from '~/hooks/useDebouncedSearch'
import { theme } from '~/styles'
import { tw } from '~/styles/utils'

import { Icon } from './designSystem'
import { TextInput } from './form'
Expand All @@ -17,8 +16,8 @@ export const SearchInput = ({ className, onChange, placeholder }: SearchInputPro
const [localValue, setLocalValue] = useState<string>('')

return (
<TextInputForSearch
className={className}
<TextInput
className={tw('max-w-60 [&_input]:h-10 [&_input]:!pl-3', className)}
ansmonjol marked this conversation as resolved.
Show resolved Hide resolved
placeholder={placeholder}
value={localValue}
onChange={(value) => {
Expand All @@ -32,12 +31,3 @@ export const SearchInput = ({ className, onChange, placeholder }: SearchInputPro
/>
)
}

const TextInputForSearch = styled(TextInput)`
max-width: 240px;

.MuiInputBase-inputAdornedStart {
height: 40px;
padding-left: ${theme.spacing(3)};
}
`
14 changes: 4 additions & 10 deletions src/components/creditNote/CreditNoteFormCalculation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,8 @@ export const CreditNoteFormCalculation = ({
_get(formikProps.errors, 'payBack.0.value') !== PayBackErrorEnum.maxRefund
}
>
<StyledTextInput
<AmountInputField
className="max-w-38 [&_input]:text-right"
name="payBack.0.value"
currency={currency}
formikProps={formikProps}
Expand Down Expand Up @@ -518,7 +519,8 @@ export const CreditNoteFormCalculation = ({
_get(formikProps.errors, 'payBack.1.value') !== PayBackErrorEnum.maxRefund
}
>
<StyledTextInput
<AmountInputField
className="max-w-38 [&_input]:text-right"
name="payBack.1.value"
currency={currency}
formikProps={formikProps}
Expand Down Expand Up @@ -608,14 +610,6 @@ const PayBackBlock = styled.div`
}
`

const StyledTextInput = styled(AmountInputField)`
max-width: 152px;

input {
text-align: right;
}
`

const InlineLabel = styled.div`
display: flex;
align-items: center;
Expand Down
2 changes: 1 addition & 1 deletion src/components/designSystem/Toasts/Toast.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
const AUTO_DISMISS_TIME = 6000

const containerStyles = cva(
'mt-4 box-border flex max-h-[300px] w-fit max-w-[360px] animate-enter items-center justify-start overflow-hidden rounded-xl px-4 py-3 text-white transition-all delay-[0ms] duration-[250ms] ease-in-out',
'mt-4 box-border flex max-h-[300px] w-fit max-w-[360px] animate-enter items-center justify-start overflow-hidden rounded-xl px-4 py-3 text-white transition-all delay-[0ms] duration-250 ease-in-out',
{
variants: {
severity: {
Expand All @@ -41,7 +41,7 @@
(time = AUTO_DISMISS_TIME) => {
if (!autoDismiss) return

// @ts-expect-error

Check warning on line 44 in src/components/designSystem/Toasts/Toast.tsx

View workflow job for this annotation

GitHub Actions / Run linters

Include a description after the "@ts-expect-error" directive to explain why the @ts-expect-error is necessary. The description must be 3 characters or longer
timeoutRef.current = setTimeout(() => {
setClosing(true)
}, time)
Expand Down
21 changes: 8 additions & 13 deletions src/components/form/ComboBox/ComboBoxInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ export const ComboBoxInput = ({
const { inputProps, InputProps, ...restParams } = params

return (
<StyledTextInput
<TextInput
onChange={(newVal) => {
// needed because useAutocomplete expect a DOM onChange listener...
inputProps.onChange({ target: { value: newVal } })
searchQuery && searchQuery(newVal)
}}
className={className}
className={tw('group/combobox-input', className)}
name={name}
placeholder={placeholder}
label={label}
Expand All @@ -56,9 +56,12 @@ export const ComboBoxInput = ({
{!disableClearable && (
<Button
// To make sure the "clear button" is displayed only on hover or focus
className={tw('MuiAutocomplete-clearIndicator', 'hidden', {
'MuiAutocomplete-clearIndicatorDirty': inputProps?.value,
})}
className={tw(
'MuiAutocomplete-clearIndicator',
'hidden',
inputProps?.value &&
'MuiAutocomplete-clearIndicatorDirty group-hover/combobox-input:flex',
)}
disabled={restParams.disabled}
size="small"
icon="close-circle-filled"
Expand Down Expand Up @@ -94,14 +97,6 @@ export const ComboBoxInput = ({
)
}

const StyledTextInput = styled(TextInput)`
&:hover {
.MuiAutocomplete-clearIndicatorDirty {
display: flex;
}
}
`

const StartAdornmentTypography = styled(Typography)`
> span:first-child {
margin-right: ${theme.spacing(2)};
Expand Down
8 changes: 7 additions & 1 deletion src/components/form/MultipleComboBox/MultipleComboBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import styled from 'styled-components'

import { Chip, Icon } from '~/components/designSystem'
import { useInternationalization } from '~/hooks/core/useInternationalization'
import { tw } from '~/styles/utils'

import { MultipleComboBoxItem } from './MultipleComboBoxItem'
import { MultipleComboBoxList } from './MultipleComboBoxList'
Expand Down Expand Up @@ -142,6 +143,11 @@ export const MultipleComboBox = ({
)
})
}}
componentsProps={{
clearIndicator: {
className: tw('size-6 rounded-lg'),
},
}}
clearIcon={<Icon name="close-circle-filled" />}
popupIcon={<Icon name="chevron-up-down" />}
noOptionsText={emptyText ?? translate('text_623b3acb8ee4e000ba87d082')}
Expand Down Expand Up @@ -229,7 +235,7 @@ const Container = styled.div`

/* Fix the placement of the adornment elements */
.MuiAutocomplete-endAdornment {
top: calc(50% - 19px);
top: calc(50% - 12px);
ansmonjol marked this conversation as resolved.
Show resolved Hide resolved
}

/* Make sure scursor is visible when overing svg */
Expand Down
28 changes: 28 additions & 0 deletions src/components/form/TextInput/TextInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { ChangeEvent, forwardRef, ReactNode, useCallback, useEffect, useState }

import { Button, Icon, Tooltip, Typography } from '~/components/designSystem'
import { useInternationalization } from '~/hooks/core/useInternationalization'
import { theme } from '~/styles'
import { tw } from '~/styles/utils'

export enum ValueFormatter {
Expand Down Expand Up @@ -34,6 +35,7 @@ export interface TextInputProps
description?: string
cleanable?: boolean
password?: boolean
variant?: 'outlined' | 'default'
value?: string | number
beforeChangeFormatter?: ValueFormatterType[] | ValueFormatterType
infoText?: string
Expand Down Expand Up @@ -131,6 +133,7 @@ export const TextInput = forwardRef<HTMLDivElement, TextInputProps>(
rows,
error,
cleanable = false,
variant = 'default',
InputProps,
type = 'text',
password,
Expand Down Expand Up @@ -248,6 +251,31 @@ export const TextInput = forwardRef<HTMLDivElement, TextInputProps>(
: {}),
...InputProps,
}}
sx={
variant === 'outlined'
? {
marginBottom: 0,
'& .MuiInputBase-formControl': {
borderRadius: 0,
},
'& .MuiOutlinedInput-notchedOutline': {
border: 'none',
},
'& .Mui-focused': {
zIndex: 1,
'& .MuiOutlinedInput-notchedOutline': {
border: `2px solid ${theme.palette.primary.main}`,
},
},
'& .Mui-error': {
'& .MuiOutlinedInput-notchedOutline': {
border: `2px solid ${theme.palette.error.main}`,
},
},
...props.sx,
}
: props.sx
}
{...props}
/>
{(helperText || error) && (
Expand Down
14 changes: 1 addition & 13 deletions src/components/form/TextInput/TextInputField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { FormikProps } from 'formik'
import _get from 'lodash/get'
import _isEqual from 'lodash/isEqual'
import { forwardRef, memo } from 'react'
import styled, { css } from 'styled-components'

import { TextInput, TextInputProps } from './TextInput'

Expand Down Expand Up @@ -30,8 +29,7 @@ export const TextInputField = memo(
const { values, errors, touched, handleBlur, setFieldValue } = formikProps

return (
<StyledTextInput
$displayErrorText={displayErrorText}
<TextInput
name={name}
value={_get(values, name)}
ref={ref}
Expand Down Expand Up @@ -67,13 +65,3 @@ export const TextInputField = memo(
)

TextInputField.displayName = 'TextInputField'

const StyledTextInput = styled(TextInput)<{ $displayErrorText?: boolean }>`
${({ $displayErrorText }) =>
!$displayErrorText &&
css`
.MuiTextField-root {
margin-bottom: 0 !important;
}
`}
`
9 changes: 2 additions & 7 deletions src/components/invoices/EditInvoiceDisplayName.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { useFormik } from 'formik'
import { forwardRef, useImperativeHandle, useRef, useState } from 'react'
import styled from 'styled-components'
import { object, string } from 'yup'

import { Button, Dialog, DialogRef } from '~/components/designSystem'
import { TextInputField } from '~/components/form'
import { InputMaybe } from '~/generated/graphql'
import { useInternationalization } from '~/hooks/core/useInternationalization'
import { theme } from '~/styles'

type EditInvoiceDisplayNameProps = {
invoiceDisplayName: InputMaybe<string> | undefined
Expand Down Expand Up @@ -76,10 +74,11 @@ export const EditInvoiceDisplayName = forwardRef<EditInvoiceDisplayNameRef>((_,
</>
)}
>
<Input
<TextInputField
// eslint-disable-next-line jsx-a11y/no-autofocus
autoFocus
cleanable
className="mb-8"
name="invoiceDisplayName"
label={translate('text_65018c8e5c6b626f030bcf26')}
placeholder={translate('text_65018c8e5c6b626f030bcf2a')}
Expand All @@ -91,7 +90,3 @@ export const EditInvoiceDisplayName = forwardRef<EditInvoiceDisplayNameRef>((_,
})

EditInvoiceDisplayName.displayName = 'forwardRef'

const Input = styled(TextInputField)`
margin-bottom: ${theme.spacing(8)};
`
12 changes: 2 additions & 10 deletions src/components/invoices/EditInvoiceItemDescriptionDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { object, string } from 'yup'
import { Button, Dialog, DialogRef } from '~/components/designSystem'
import { TextInputField } from '~/components/form'
import { useInternationalization } from '~/hooks/core/useInternationalization'
import { theme } from '~/styles'

const MAX_CHAR_LIMIT = 255

Expand Down Expand Up @@ -78,11 +77,12 @@ export const EditInvoiceItemDescriptionDialog = forwardRef<EditInvoiceItemDescri
</>
)}
>
<TextArea
<TextInputField
// eslint-disable-next-line jsx-a11y/no-autofocus
autoFocus
multiline
name="description"
className="mb-8 whitespace-pre-line"
rows="3"
label={translate('text_6453819268763979024ad011')}
error={formikProps.errors.description}
Expand All @@ -107,14 +107,6 @@ export const EditInvoiceItemDescriptionDialog = forwardRef<EditInvoiceItemDescri

EditInvoiceItemDescriptionDialog.displayName = 'forwardRef'

const TextArea = styled(TextInputField)`
margin-bottom: ${theme.spacing(8)};

textarea:first-child {
white-space: pre-line;
}
`

const TextInputHelper = styled.div`
display: flex;
justify-content: space-between;
Expand Down
9 changes: 2 additions & 7 deletions src/components/invoices/EditInvoicePaymentStatusDialog.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { gql } from '@apollo/client'
import { useFormik } from 'formik'
import { forwardRef, useImperativeHandle, useRef, useState } from 'react'
import styled from 'styled-components'
import { object, string } from 'yup'

import { Button, Dialog, DialogRef } from '~/components/designSystem'
Expand All @@ -16,7 +15,6 @@ import {
useUpdateInvoicePaymentStatusMutation,
} from '~/generated/graphql'
import { useInternationalization } from '~/hooks/core/useInternationalization'
import { theme } from '~/styles'

gql`
fragment InvoiceForUpdateInvoicePaymentStatus on Invoice {
Expand Down Expand Up @@ -120,7 +118,8 @@ export const UpdateInvoicePaymentStatusDialog = forwardRef<UpdateInvoicePaymentS
</>
)}
>
<StyledComboBoxField
<ComboBoxField
className="mb-8"
name="paymentStatus"
label={translate('text_63eba8c65a6c8043feee2a0f')}
data={Object.values(InvoicePaymentStatusTypeEnum).map((status) => ({
Expand All @@ -138,7 +137,3 @@ export const UpdateInvoicePaymentStatusDialog = forwardRef<UpdateInvoicePaymentS
)

UpdateInvoicePaymentStatusDialog.displayName = 'forwardRef'

const StyledComboBoxField = styled(ComboBoxField)`
margin-bottom: ${theme.spacing(8)};
`
7 changes: 2 additions & 5 deletions src/components/plans/ChargeAccordion.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,8 @@ export const ChargeAccordion = memo(
</Button>
) : (
<SpendingMinimumWrapper>
<SpendingMinimumInput
<AmountInput
className="flex-1"
ansmonjol marked this conversation as resolved.
Show resolved Hide resolved
id={`spending-minimum-input-${index}`}
beforeChangeFormatter={['positiveNumber', 'chargeDecimal']}
currency={currency}
Expand Down Expand Up @@ -1019,10 +1020,6 @@ const SpendingMinimumWrapper = styled.div`
align-items: center;
`

const SpendingMinimumInput = styled(AmountInput)`
flex: 1;
`

const InlineTaxInputWrapper = styled.div`
display: flex;
align-items: center;
Expand Down
Loading
Loading