Skip to content

Commit 1022068

Browse files
authored
Merge pull request #10989 from TylerAPfledderer/fix/input-misalignment
fix(Input): fix misalignment and styling states
2 parents 3f73bb2 + 973f741 commit 1022068

File tree

4 files changed

+39
-50
lines changed

4 files changed

+39
-50
lines changed

src/@chakra-ui/gatsby-plugin/components/Input.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { inputAnatomy } from "@chakra-ui/anatomy"
2+
import { cssVar } from "@chakra-ui/react"
23
import {
34
createMultiStyleConfigHelpers,
45
defineStyle,
@@ -8,6 +9,9 @@ import { defineMergeStyles, inputDefaultTheme } from "./components.utils"
89
const { defineMultiStyleConfig, definePartsStyle } =
910
createMultiStyleConfigHelpers(inputAnatomy.keys)
1011

12+
// From the default theme
13+
const $height = cssVar("input-height")
14+
1115
const baseStyle = definePartsStyle((props) => {
1216
const {
1317
focusBorderColor: fc = "primaryHover",
@@ -19,10 +23,10 @@ const baseStyle = definePartsStyle((props) => {
1923
inputDefaultTheme.variants?.outline(props),
2024
{
2125
field: {
22-
borderColor: "currentColor",
2326
borderRadius: "base",
2427
outline: "3px solid transparent",
2528
lineHeight: 1,
29+
px: "2",
2630
_placeholder: {
2731
color: "disabled",
2832
opacity: 1,
@@ -34,9 +38,6 @@ const baseStyle = definePartsStyle((props) => {
3438
boxShadow: "none",
3539
},
3640
_hover: null, // override default
37-
_groupHover: {
38-
borderColor: "primary.hover",
39-
},
4041
_invalid: {
4142
borderColor: ec,
4243
boxShadow: "none",
@@ -46,10 +47,14 @@ const baseStyle = definePartsStyle((props) => {
4647
opacity: 1,
4748
},
4849
"&:not(:disabled)": {
50+
borderColor: "body.base",
4951
_active: {
5052
bg: "background.highlight",
5153
borderColor: "primary.highContrast",
5254
},
55+
"[data-group] &:hover, &:hover": {
56+
borderColor: "primary.hover",
57+
},
5358
},
5459
},
5560
element: {
@@ -84,12 +89,10 @@ const baseStyle = definePartsStyle((props) => {
8489

8590
const size = {
8691
md: defineStyle({
87-
h: 10.5,
88-
px: 2,
92+
[$height.variable]: "sizes.10.5",
8993
}),
9094
sm: defineStyle({
91-
h: 8,
92-
px: 1,
95+
[$height.variable]: "sizes.8",
9396
}),
9497
}
9598

src/components/Input/Input.stories.tsx

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import * as React from "react"
22
import { VStack } from "@chakra-ui/react"
33
import { Meta, StoryObj } from "@storybook/react"
44
import Input from "."
5-
import { MdSearch } from "react-icons/md"
65
import { BsSlashSquare } from "react-icons/bs"
76

87
type InputType = typeof Input
@@ -19,34 +18,19 @@ type Story = StoryObj<InputType>
1918
export const Sizes: Story = {
2019
render: () => (
2120
<VStack width="154px">
22-
<Input
23-
leftElement={<MdSearch />}
24-
rightElement={<BsSlashSquare />}
25-
placeholder="Search"
26-
/>
27-
<Input
28-
leftElement={<MdSearch />}
29-
rightElement={<BsSlashSquare />}
30-
placeholder="Search"
31-
size="sm"
32-
/>
21+
<Input rightIcon={<BsSlashSquare />} placeholder="Search" />
22+
<Input rightIcon={<BsSlashSquare />} placeholder="Search" size="sm" />
3323
</VStack>
3424
),
3525
}
3626

3727
export const ElementVariations: Story = {
3828
render: () => (
3929
<VStack width="258px" spacing={4}>
40-
<Input
41-
leftElement={<MdSearch />}
42-
rightElement={<BsSlashSquare />}
43-
placeholder="input text"
44-
/>
45-
<Input leftElement={<MdSearch />} placeholder="input text" />
4630
<Input placeholder="input text" />
47-
<Input rightElement={<BsSlashSquare />} placeholder="input text" />
31+
<Input rightIcon={<BsSlashSquare />} placeholder="input text" />
4832
<Input
49-
rightElement={<BsSlashSquare />}
33+
rightIcon={<BsSlashSquare />}
5034
placeholder="input text"
5135
isDisabled
5236
/>

src/components/Input/index.tsx

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,36 @@ import {
33
Input as ChakraInput,
44
InputGroup,
55
InputGroupProps,
6-
InputLeftElement,
76
InputProps as ChakraInputProps,
87
InputRightElement,
98
} from "@chakra-ui/react"
109

11-
interface InputProps extends ChakraInputProps {
10+
type CommonProps = ChakraInputProps
11+
12+
type NoIconProps = CommonProps & { rightIcon?: never }
13+
14+
type WithIconProps = CommonProps & {
1215
/**
13-
* The Element or Icon used to render `InputLeftElement`
16+
* The Icon used to render `InputRightElement` on the right side of the input
1417
*/
15-
leftElement?: React.ReactNode
18+
rightIcon: JSX.Element
1619
/**
17-
* The Element or Icon used to render `InputRightElement`
20+
* Primarily for style props to be applied to the wrapper
1821
*/
19-
rightElement?: React.ReactNode
2022
inputGroupProps?: InputGroupProps
2123
}
2224

25+
type InputProps = NoIconProps | WithIconProps
26+
27+
function Input(props: NoIconProps): JSX.Element
28+
function Input(props: WithIconProps): JSX.Element
2329
function Input(props: InputProps) {
24-
if ("leftElement" in props || "rightElement" in props) {
25-
const { size, inputGroupProps, leftElement, rightElement, ...rest } = props
30+
if (props.rightIcon) {
31+
const { size, inputGroupProps, rightIcon: Icon, ...rest } = props
2632
return (
2733
<InputGroup size={size} {...inputGroupProps}>
2834
<ChakraInput data-peer {...rest} />
29-
{leftElement ? <InputLeftElement children={leftElement} /> : null}
30-
{rightElement ? <InputRightElement children={rightElement} /> : null}
35+
<InputRightElement children={Icon} />
3136
</InputGroup>
3237
)
3338
}

src/pages/languages.tsx

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -91,18 +91,15 @@ const LanguagesPage = ({ location }: PageProps<Queries.LanguagesPageQuery>) => {
9191
value={keyword}
9292
placeholder={searchString}
9393
onChange={(e) => setKeyword(e.target.value)}
94-
rightElement={
95-
keyword !== "" && (
96-
<IconButton
97-
icon={<MdClose />}
98-
onClick={resetKeyword}
99-
position="absolute"
100-
insetInlineEnd={1}
101-
aria-label={t("clear")}
102-
variant="ghost"
103-
_hover={{ svg: { fill: "primary" } }}
104-
/>
105-
)
94+
rightIcon={
95+
<IconButton
96+
icon={<MdClose />}
97+
onClick={resetKeyword}
98+
display={keyword === "" ? "none" : undefined}
99+
aria-label={t("clear")}
100+
variant="ghost"
101+
_hover={{ svg: { fill: "primary" } }}
102+
/>
106103
}
107104
/>
108105
</Box>

0 commit comments

Comments
 (0)