Skip to content

Commit

Permalink
fix(framework): InputLeftElement and padding
Browse files Browse the repository at this point in the history
Give appropiate padding to the input element
depending on the width of InputLeftElement
  • Loading branch information
SebCodesTheWeb committed Nov 29, 2023
1 parent cbbad49 commit fa5dc1b
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 6 deletions.
10 changes: 4 additions & 6 deletions framework/lib/components/input/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import {
Input,
InputAddon,
InputGroup,
InputLeftAddon,
InputLeftElement,
InputRightAddon,
InputRightElement,
} from '@chakra-ui/react'

export {
Input,
InputGroup,
InputAddon,
InputLeftAddon,
InputRightAddon,
InputLeftElement,
InputRightElement,
}

export * from './input-left-element'
export * from './input'
export * from './input-group'
39 changes: 39 additions & 0 deletions framework/lib/components/input/input-group.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import React, {
Dispatch,
SetStateAction,
createContext,
forwardRef,
useMemo,
useState,
} from 'react'
import {
InputGroup as ChakraInputGroup,
InputGroupProps,
} from '@chakra-ui/react'

export const LeftElementContext = createContext<
| { leftWidth: number, setLeftWidth: Dispatch<SetStateAction<number>> }
| undefined
>(undefined)

export const InputGroup = forwardRef<HTMLDivElement, InputGroupProps>(
({ children, ...rest }, ref) => {
const [ leftWidth, setLeftWidth ] = useState(0)

const contextValue = useMemo(
() => ({
leftWidth,
setLeftWidth,
}),
[ leftWidth ]
)

return (
<LeftElementContext.Provider value={ contextValue }>
<ChakraInputGroup ref={ ref } { ...rest }>
{ children }
</ChakraInputGroup>
</LeftElementContext.Provider>
)
}
)
38 changes: 38 additions & 0 deletions framework/lib/components/input/input-left-element.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import React, { useContext, useEffect, useRef } from 'react'
import {
InputLeftElement as ChakraInputLeftElement,
InputLeftElementProps,
} from '@chakra-ui/react'
import { LeftElementContext } from './input-group'

export const InputLeftElement = ({
children,
...props
}: InputLeftElementProps) => {
const ref = useRef<HTMLDivElement>(null)
const contextValue = useContext(LeftElementContext)

useEffect(() => {
if (ref.current) {
const handleResize = () => {
if (contextValue) {
contextValue.setLeftWidth(ref.current!.offsetWidth)
}
}

handleResize()

const resizeObserver = new ResizeObserver(handleResize)
resizeObserver.observe(ref.current)

return () => resizeObserver.disconnect()
}
return () => null
}, [])

return (
<ChakraInputLeftElement ref={ ref } { ...props }>
{ children }
</ChakraInputLeftElement>
)
}
26 changes: 26 additions & 0 deletions framework/lib/components/input/input.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import React, { forwardRef, useContext, useRef } from 'react'
import {
Input as ChakraInput,
InputProps,
useMergeRefs,
useToken,
} from '@chakra-ui/react'
import { LeftElementContext } from './input-group'

export const Input = forwardRef<HTMLInputElement, InputProps>((props, ref) => {
const leftElement = useContext(LeftElementContext)
const inputRef = useRef(null)
const mergeRef = useMergeRefs(inputRef, ref)
const paddingInlineStart = useToken('space.padding-inline.input', 'default')
const leftWidth = leftElement ? leftElement.leftWidth : 0

return (
<ChakraInput
paddingInlineStart={ `${
leftWidth + parseFloat(paddingInlineStart) * 16
}px` }
ref={ mergeRef }
{ ...props }
/>
)
})

0 comments on commit fa5dc1b

Please sign in to comment.