Skip to content

Commit

Permalink
Merge pull request #2436 from graphcommerce-org/fix/various
Browse files Browse the repository at this point in the history
Added ref forwarding for the inputRef
  • Loading branch information
paales authored Nov 28, 2024
2 parents 7f17a21 + 55f94c5 commit d7b3d58
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/calm-sheep-relate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphcommerce/ecommerce-ui': patch
---

Added ref forwarding for the inputRef
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ import type {
SxProps,
Theme,
} from '@mui/material'
import { Checkbox, FormControl, FormControlLabel, FormGroup, FormHelperText } from '@mui/material'
import {
Checkbox,
FormControl,
FormControlLabel,
FormGroup,
FormHelperText,
useForkRef,
} from '@mui/material'

export type CheckboxElementProps<T extends FieldValues> = Omit<CheckboxProps, 'name'> & {
label?: FormControlLabelProps['label']
Expand Down Expand Up @@ -62,7 +69,7 @@ export function CheckboxElement<TFieldValues extends FieldValues>(
<Checkbox
{...rest}
{...field}
inputRef={ref}
inputRef={useForkRef(ref, rest.inputRef)}
color={rest.color || 'primary'}
sx={{
...(Array.isArray(sx) ? sx : [sx]),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import {
IconSvg,
extendableComponent,
iconMin,
iconPlus,
IconSvg,
responsiveVal,
} from '@graphcommerce/next-ui'
import type { ControllerProps, FieldValues } from '@graphcommerce/react-hook-form'
import { Controller, useController } from '@graphcommerce/react-hook-form'
import { i18n } from '@lingui/core'
import type { IconButtonProps, SxProps, TextFieldProps, Theme } from '@mui/material'
import { Fab, TextField } from '@mui/material'
import { Fab, TextField, useForkRef } from '@mui/material'

export type NumberFieldElementProps<T extends FieldValues = FieldValues> = Omit<
TextFieldProps,
Expand Down Expand Up @@ -44,7 +44,7 @@ export function NumberFieldElement<T extends FieldValues>(props: NumberFieldElem
variant = 'outlined',
disabled,
shouldUnregister,
...textFieldProps
...rest
} = props

const classes = withState({ size })
Expand All @@ -69,22 +69,22 @@ export function NumberFieldElement<T extends FieldValues>(props: NumberFieldElem

return (
<TextField
{...textFieldProps}
{...rest}
{...field}
inputRef={ref}
inputRef={useForkRef(ref, rest.inputRef)}
value={value ?? ''}
onChange={(ev) => {
const newValue = (ev.target as HTMLInputElement).valueAsNumber
onChange(Number.isNaN(newValue) ? '' : newValue)
textFieldProps.onChange?.(ev)
rest.onChange?.(ev)
}}
variant={variant}
required={required}
error={invalid}
helperText={error ? error.message : textFieldProps.helperText}
helperText={error ? error.message : rest.helperText}
size={size}
type='number'
className={`${textFieldProps.className ?? ''} ${classes.quantity}`}
className={`${rest.className ?? ''} ${classes.quantity}`}
sx={[
{
width: responsiveVal(90, 120),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { ControllerProps, FieldValues } from '@graphcommerce/react-hook-for
import { useController } from '@graphcommerce/react-hook-form'
import { i18n } from '@lingui/core'
import type { TextFieldProps } from '@mui/material'
import { MenuItem, TextField } from '@mui/material'
import { MenuItem, TextField, useForkRef } from '@mui/material'

type OptionBase = { id: string | number; label: string | number }

Expand Down Expand Up @@ -63,7 +63,7 @@ export function SelectElement<TFieldValues extends FieldValues, O extends Option
{...rest}
value={value ?? ''}
{...field}
inputRef={ref}
inputRef={useForkRef(ref, rest.inputRef)}
onChange={(event) => {
let item: number | string | O | undefined = event.target.value
if (type === 'number') item = Number(item)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { FieldValues, UseControllerProps } from '@graphcommerce/react-hook-
import { emailPattern, useController } from '@graphcommerce/react-hook-form'
import { i18n } from '@lingui/core'
import type { TextFieldProps } from '@mui/material'
import { TextField } from '@mui/material'
import { TextField, useForkRef } from '@mui/material'
import React, { useState } from 'react'

export type TextFieldElementProps<T extends FieldValues = FieldValues> = Omit<
Expand Down Expand Up @@ -68,7 +68,7 @@ export function TextFieldElement<TFieldValues extends FieldValues>({
onChange(type === 'number' && ev.target.value ? Number(ev.target.value) : ev.target.value)
rest.onChange?.(ev)
}}
inputRef={ref}
inputRef={useForkRef(ref, rest.inputRef)}
required={required}
type={type}
error={Boolean(error) || rest.error}
Expand Down

0 comments on commit d7b3d58

Please sign in to comment.