@khanacademy/wonder-blocks-search-field 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,10 @@
1
+ # @khanacademy/wonder-blocks-search-field
2
+
3
+ ## 1.0.0
4
+ ### Major Changes
5
+
6
+ - 2d9198ff: Create SearchField component
7
+
8
+ ### Minor Changes
9
+
10
+ - 80aebdc2: Adds `wonder-blocks-search-field` package
@@ -0,0 +1,139 @@
1
+ import _extends from '@babel/runtime/helpers/extends';
2
+ import _objectWithoutPropertiesLoose from '@babel/runtime/helpers/objectWithoutPropertiesLoose';
3
+ import * as React from 'react';
4
+ import * as ReactDOM from 'react-dom';
5
+ import { StyleSheet } from 'aphrodite';
6
+ import { styles as styles$1 } from '@khanacademy/wonder-blocks-typography';
7
+ import { View } from '@khanacademy/wonder-blocks-core';
8
+ import IconButton from '@khanacademy/wonder-blocks-icon-button';
9
+ import { TextField } from '@khanacademy/wonder-blocks-form';
10
+ import Icon, { icons } from '@khanacademy/wonder-blocks-icon';
11
+ import Color from '@khanacademy/wonder-blocks-color';
12
+ import Spacing from '@khanacademy/wonder-blocks-spacing';
13
+
14
+ // The default labels that will be used by different components
15
+ const defaultLabels = {
16
+ clearSearch: "Clear search"
17
+ };
18
+
19
+ const _excluded = ["clearAriaLabel", "disabled", "light", "id", "value", "placeholder", "style", "testId", "onClick", "onChange", "onFocus", "onBlur"];
20
+
21
+ /**
22
+ * Search Field. A TextField with a search icon on its left side
23
+ * and an X icon on its right side.
24
+ *
25
+ * ### Usage
26
+ * ```jsx
27
+ * import {SearchField} from "@khanacademy/wonder-blocks-search-field";
28
+ *
29
+ * const [value, setValue] = React.useState("");
30
+ *
31
+ * const handleChange = (newValue: string) => {
32
+ * setValue(newValue);
33
+ * };
34
+ *
35
+ * <SearchField
36
+ * id="some-id"
37
+ * value={value}
38
+ * onChange={handleChange}
39
+ * />
40
+ * ```
41
+ */
42
+ function SearchField(props) {
43
+ const {
44
+ clearAriaLabel = defaultLabels.clearSearch,
45
+ disabled = false,
46
+ light = false,
47
+ id,
48
+ value,
49
+ placeholder,
50
+ style,
51
+ testId,
52
+ onClick,
53
+ onChange,
54
+ onFocus,
55
+ onBlur
56
+ } = props,
57
+ otherProps = _objectWithoutPropertiesLoose(props, _excluded);
58
+
59
+ const handleClear = () => {
60
+ // Empty the search text.
61
+ onChange(""); // Focus back on the text field since the clear button disappears after
62
+ // the field is cleared.
63
+
64
+ const currentField = ReactDOM.findDOMNode(document.getElementById(id));
65
+ currentField.focus();
66
+ };
67
+
68
+ const maybeRenderClearIconButton = () => {
69
+ if (!value.length) {
70
+ return null;
71
+ }
72
+
73
+ return /*#__PURE__*/React.createElement(IconButton, {
74
+ icon: icons.dismiss,
75
+ kind: "tertiary",
76
+ onClick: handleClear,
77
+ style: styles.dismissIcon,
78
+ "aria-label": clearAriaLabel
79
+ });
80
+ };
81
+
82
+ return /*#__PURE__*/React.createElement(View, {
83
+ onClick: onClick,
84
+ style: [styles.inputContainer, style]
85
+ }, /*#__PURE__*/React.createElement(Icon, {
86
+ icon: icons.search,
87
+ size: "medium",
88
+ color: Color.offBlack64,
89
+ style: styles.searchIcon,
90
+ "aria-hidden": "true"
91
+ }), /*#__PURE__*/React.createElement(TextField, _extends({
92
+ id: id,
93
+ type: "text",
94
+ disabled: disabled,
95
+ light: light,
96
+ onChange: onChange,
97
+ onFocus: onFocus,
98
+ onBlur: onBlur,
99
+ placeholder: placeholder,
100
+ value: value,
101
+ style: [styles.inputStyleReset, styles$1.LabelMedium],
102
+ testId: testId
103
+ }, otherProps)), maybeRenderClearIconButton());
104
+ }
105
+ const styles = StyleSheet.create({
106
+ inputContainer: {
107
+ boxSizing: "border-box",
108
+ flexDirection: "row",
109
+ borderRadius: Spacing.xxxSmall_4,
110
+ alignItems: "center",
111
+ height: 40
112
+ },
113
+ searchIcon: {
114
+ marginLeft: Spacing.xSmall_8,
115
+ marginRight: Spacing.xSmall_8,
116
+ position: "absolute"
117
+ },
118
+ dismissIcon: {
119
+ margin: 0,
120
+ position: "absolute",
121
+ right: 0,
122
+ ":hover": {
123
+ border: "none"
124
+ }
125
+ },
126
+ inputStyleReset: {
127
+ display: "flex",
128
+ flex: 1,
129
+ "::placeholder": {
130
+ color: Color.offBlack64
131
+ },
132
+ width: "100%",
133
+ color: "inherit",
134
+ paddingLeft: Spacing.large_24 + Spacing.medium_16,
135
+ paddingRight: Spacing.large_24 + Spacing.medium_16
136
+ }
137
+ });
138
+
139
+ export { SearchField as default };