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(framework): InputLeftElement and padding #196

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
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 }
/>
)
})