Skip to content

Commit 0a94d48

Browse files
SaraVieirasiddharthkp
authored andcommitted
Add Search Input (codesandbox#3297)
* add more tests cases * use token instead of color
1 parent 0923d0c commit 0a94d48

File tree

7 files changed

+136
-3
lines changed

7 files changed

+136
-3
lines changed

packages/common/src/design-language/theme.ts

+10
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ const theme = {
77
fontWeights,
88

99
// we use a 4 point grid
10+
// 0 - 0
11+
// 1 - 4
12+
// 2 - 8
13+
// 3 - 12
14+
// 4 - 16
15+
// 5 - 24
16+
// 6 - 28
17+
// 7 - 32
18+
// 8 - 36
19+
// 9 - 40
1020
space: [0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40],
1121
sizes: [0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40],
1222

packages/components/.storybook/config.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ const GlobalStyle = createGlobalStyle`
3333
${global};
3434
html body {
3535
font-family: 'Inter', sans-serif;
36-
3736
background-color: ${props =>
3837
// @ts-ignore
3938
props.theme.colors.sideBar.background} !important;

packages/components/src/components/Input/index.stories.tsx

+8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React from 'react';
2+
import { action } from '@storybook/addon-actions';
23
import { Input } from '.';
34

45
export default {
@@ -12,3 +13,10 @@ export const Placeholder = () => <Input placeholder="Your name" />;
1213
export const Label = () => (
1314
<Input label="Your full name" placeholder="John Doe" />
1415
);
16+
export const onChange = () => (
17+
<Input
18+
label="Your full name"
19+
placeholder="John Doe"
20+
onChange={action('input change')}
21+
/>
22+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import React from 'react';
2+
import { action } from '@storybook/addon-actions';
3+
import { SearchInput } from '.';
4+
5+
export default {
6+
title: 'components/SearchInput',
7+
component: SearchInput,
8+
};
9+
10+
export const Basic = () => <SearchInput />;
11+
export const Placeholder = () => (
12+
<SearchInput placeholder="Search For Dependencies" />
13+
);
14+
export const Label = () => (
15+
<SearchInput label="Search For Dependencies" placeholder="react" />
16+
);
17+
18+
export const onChange = () => (
19+
<SearchInput
20+
onChange={action('search input change')}
21+
label="Search For Dependencies"
22+
placeholder="react"
23+
/>
24+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import React from 'react';
2+
import styled from 'styled-components';
3+
import css from '@styled-system/css';
4+
import VisuallyHidden from '@reach/visually-hidden';
5+
import { uniqueId } from 'lodash-es';
6+
import { Text } from '../Text';
7+
import { InputComponent } from '../Input';
8+
import { Element } from '../Element';
9+
10+
const SearchIconBase = props => (
11+
<svg fill="none" width="12" height="12" {...props}>
12+
<path
13+
fillRule="evenodd"
14+
clipRule="evenodd"
15+
d="M6.966 7.932a4.15 4.15 0 01-2.69.993C1.916 8.925 0 6.927 0 4.463 0 1.998 1.915 0 4.277 0s4.276 1.998 4.276 4.463c0 1.063-.356 2.04-.951 2.806L12 11.86l-.635.663-4.399-4.59zm.689-3.47c0 1.947-1.513 3.525-3.378 3.525C2.41 7.987.899 6.41.899 4.463.899 2.516 2.41.938 4.277.938c1.865 0 3.378 1.578 3.378 3.525z"
16+
/>
17+
</svg>
18+
);
19+
20+
export const SearchInputComponent = styled(InputComponent)(
21+
css({
22+
paddingLeft: 5,
23+
24+
'::-ms-clear, ::-ms-reveal': {
25+
display: 'none',
26+
width: 0,
27+
height: 0,
28+
},
29+
30+
'::-webkit-search-cancel-button': {
31+
display: 'none',
32+
},
33+
})
34+
);
35+
36+
export const SearchIcon = styled(SearchIconBase)(
37+
css({
38+
position: 'absolute',
39+
top: '50%',
40+
transform: 'translateY(-50%)',
41+
left: 1,
42+
43+
path: {
44+
fill: 'input.placeholderForeground',
45+
},
46+
})
47+
);
48+
49+
interface ISearchProps extends React.InputHTMLAttributes<HTMLInputElement> {
50+
label?: string;
51+
}
52+
53+
export const SearchInput: React.FC<ISearchProps> = ({ label, ...props }) => {
54+
const id = props.id || uniqueId('form_');
55+
56+
return (
57+
<>
58+
{props.placeholder && !label ? (
59+
<VisuallyHidden>
60+
<label htmlFor={id}>{props.placeholder}</label>
61+
</VisuallyHidden>
62+
) : null}
63+
<Text
64+
as="label"
65+
size={2}
66+
marginBottom={2}
67+
htmlFor={id}
68+
style={{ display: 'block' }}
69+
>
70+
{label}
71+
</Text>
72+
<Element style={{ position: 'relative' }}>
73+
<SearchIcon />
74+
<SearchInputComponent id={id} {...props} type="search" />
75+
</Element>
76+
</>
77+
);
78+
};

packages/components/src/components/Textarea/index.stories.tsx

+12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React from 'react';
2+
import { action } from '@storybook/addon-actions';
23
import { Textarea } from '.';
34

45
export default {
@@ -30,3 +31,14 @@ export const MaxLength = () => (
3031
<Textarea maxLength={10} label="Your full name" placeholder="John Doe" />
3132
</Wrapper>
3233
);
34+
35+
export const onChange = () => (
36+
<Wrapper>
37+
<Textarea
38+
maxLength={10}
39+
label="Your full name"
40+
placeholder="John Doe"
41+
onChange={action('textarea change')}
42+
/>
43+
</Wrapper>
44+
);

packages/components/src/components/Textarea/index.tsx

+4-2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ const Count = styled.div<{ limit: boolean }>(({ limit }) =>
4343
export const Textarea: React.FC<ITextareaProps> = ({
4444
label,
4545
maxLength,
46+
onChange,
47+
onKeyPress,
4648
...props
4749
}) => {
4850
const id = props.id || uniqueId('form_');
@@ -57,7 +59,7 @@ export const Textarea: React.FC<ITextareaProps> = ({
5759

5860
// eslint-disable-next-line consistent-return
5961
const update = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
60-
if (props.onChange) props.onChange(e);
62+
if (onChange) onChange(e);
6163
if (maxLength) {
6264
const trimmedText = e.target.value.substring(0, maxLength);
6365
setValue(trimmedText);
@@ -68,7 +70,7 @@ export const Textarea: React.FC<ITextareaProps> = ({
6870
};
6971

7072
const keyPress = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
71-
if (props.onKeyPress) props.onKeyPress(e);
73+
if (onKeyPress) onKeyPress(e);
7274
if (maxLength) {
7375
if (maxLength <= wordCount) {
7476
return false;

0 commit comments

Comments
 (0)