@sproutsocial/racine 11.3.0-beta.2 → 11.3.0-beta.5
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/__flow__/Input/index.js +64 -12
- package/__flow__/Input/index.stories.js +33 -1
- package/__flow__/Input/index.test.js +28 -0
- package/commonjs/Input/index.js +52 -12
- package/lib/Input/index.js +51 -12
- package/package.json +1 -1
package/__flow__/Input/index.js
CHANGED
|
@@ -3,6 +3,7 @@ import * as React from "react";
|
|
|
3
3
|
import Container, { Accessory } from "./styles";
|
|
4
4
|
import Button from "../Button";
|
|
5
5
|
import Icon from "../Icon";
|
|
6
|
+
import styled from "styled-components";
|
|
6
7
|
|
|
7
8
|
type TypeProps = {
|
|
8
9
|
/** ID of the form element, should match the "for" value of the associated label */
|
|
@@ -43,10 +44,11 @@ type TypeProps = {
|
|
|
43
44
|
/** Props to spread onto the underlying input element */
|
|
44
45
|
inputProps?: any,
|
|
45
46
|
/** Used to get a reference to the underlying element */
|
|
46
|
-
innerRef?:
|
|
47
|
+
innerRef?: {| current: HTMLInputElement | null |},
|
|
47
48
|
onBlur?: (e: SyntheticFocusEvent<HTMLInputElement>) => void,
|
|
48
49
|
onChange?: (e: SyntheticInputEvent<HTMLInputElement>, value: string) => void,
|
|
49
|
-
/**
|
|
50
|
+
/** Input.ClearButton onClick callback. Required when using <Input type="search"/> or <Input.ClearButton/>.
|
|
51
|
+
The component handles returning focus to Input after onClear is called only. You must reset "value" yourself.*/
|
|
50
52
|
onClear?: (e: SyntheticEvent<HTMLButtonElement>) => void,
|
|
51
53
|
onFocus?: (e: SyntheticFocusEvent<HTMLInputElement>) => void,
|
|
52
54
|
onKeyDown?: (
|
|
@@ -70,27 +72,70 @@ type TypeProps = {
|
|
|
70
72
|
// Using Context so that Input's Input.ClearButton-specific props can be passed to Input.ClearButton,
|
|
71
73
|
// regardless of whether it is manually included as elemAfter or automatically included for type="search" Inputs.
|
|
72
74
|
type TypeInputContext = $Shape<{
|
|
75
|
+
onClear?: (e: SyntheticEvent<HTMLButtonElement>) => void,
|
|
73
76
|
handleClear: (e: SyntheticEvent<HTMLButtonElement>) => void,
|
|
74
77
|
clearButtonLabel: string,
|
|
75
78
|
hasValue: boolean,
|
|
79
|
+
size: "large" | "small" | "default",
|
|
76
80
|
}>;
|
|
77
81
|
|
|
78
82
|
const InputContext = React.createContext<TypeInputContext>({});
|
|
79
83
|
|
|
84
|
+
const StyledButton = styled(Button)`
|
|
85
|
+
&:hover,
|
|
86
|
+
&:active {
|
|
87
|
+
color: ${(props) =>
|
|
88
|
+
props.theme.utils.interact(props.theme.colors.icon.base)};
|
|
89
|
+
}
|
|
90
|
+
`;
|
|
91
|
+
|
|
80
92
|
const ClearButton = () => {
|
|
81
|
-
const {
|
|
82
|
-
|
|
93
|
+
const {
|
|
94
|
+
onClear,
|
|
95
|
+
handleClear,
|
|
96
|
+
clearButtonLabel,
|
|
97
|
+
hasValue,
|
|
98
|
+
size: inputSize,
|
|
99
|
+
} = React.useContext(InputContext);
|
|
83
100
|
|
|
84
101
|
// Hide the button when there is no text to clear.
|
|
85
102
|
if (!hasValue) {
|
|
86
103
|
return null;
|
|
87
104
|
}
|
|
105
|
+
|
|
106
|
+
// Log a warning and hide the button when no onClear callback is provided.
|
|
107
|
+
// If we called handleClear with no onClear prop, all the button would do is focus the Input.
|
|
108
|
+
if (!onClear) {
|
|
109
|
+
console.warn(
|
|
110
|
+
"Warning: No onClear prop provided to Input when using Input.ClearButton. Omitting Input.ClearButton."
|
|
111
|
+
);
|
|
112
|
+
return null;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// Warn if clearButtonLabel is not included, so that the unlocalized fallback will not be mistaken for a proper label.
|
|
116
|
+
if (!clearButtonLabel) {
|
|
117
|
+
console.warn(
|
|
118
|
+
"Warning: clearButtonLabel prop is required when using Input.ClearButton. Please pass a localized clearButtonLabel to Input."
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// Reduce Button padding for size small Inputs so that the Button won't go outside the bounds of the Input.
|
|
123
|
+
// This adjustment is handled automatically for default and large Inputs via Button's size. There is no "small" Button.
|
|
124
|
+
const py = inputSize === "small" ? 100 : undefined;
|
|
125
|
+
const px = inputSize === "small" ? 200 : undefined;
|
|
126
|
+
const buttonSize = inputSize === "small" ? "default" : inputSize;
|
|
127
|
+
|
|
88
128
|
return (
|
|
89
|
-
<
|
|
90
|
-
{
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
129
|
+
<StyledButton
|
|
130
|
+
onClick={handleClear}
|
|
131
|
+
size={buttonSize}
|
|
132
|
+
py={py}
|
|
133
|
+
px={px}
|
|
134
|
+
title={clearButtonLabel || "Clear"}
|
|
135
|
+
color="icon.base"
|
|
136
|
+
>
|
|
137
|
+
<Icon name="circlex" />
|
|
138
|
+
</StyledButton>
|
|
94
139
|
);
|
|
95
140
|
};
|
|
96
141
|
|
|
@@ -110,6 +155,7 @@ class Input extends React.Component<TypeProps> {
|
|
|
110
155
|
type: "text",
|
|
111
156
|
size: "default",
|
|
112
157
|
appearance: "primary",
|
|
158
|
+
innerRef: React.createRef<HTMLInputElement>(),
|
|
113
159
|
};
|
|
114
160
|
|
|
115
161
|
static ClearButton = ClearButton;
|
|
@@ -117,8 +163,10 @@ class Input extends React.Component<TypeProps> {
|
|
|
117
163
|
handleBlur = (e: SyntheticFocusEvent<HTMLInputElement>) =>
|
|
118
164
|
this.props.onBlur?.(e);
|
|
119
165
|
|
|
120
|
-
handleClear = (e: SyntheticEvent<HTMLButtonElement>) =>
|
|
166
|
+
handleClear = (e: SyntheticEvent<HTMLButtonElement>) => {
|
|
167
|
+
this.props.innerRef.current?.focus();
|
|
121
168
|
this.props.onClear?.(e);
|
|
169
|
+
};
|
|
122
170
|
|
|
123
171
|
handleChange = (e: SyntheticInputEvent<HTMLInputElement>) =>
|
|
124
172
|
this.props.onChange?.(e, e.currentTarget.value);
|
|
@@ -166,6 +214,7 @@ class Input extends React.Component<TypeProps> {
|
|
|
166
214
|
inputProps = {},
|
|
167
215
|
qa = {},
|
|
168
216
|
appearance,
|
|
217
|
+
size,
|
|
169
218
|
...rest
|
|
170
219
|
} = this.props;
|
|
171
220
|
|
|
@@ -178,7 +227,7 @@ class Input extends React.Component<TypeProps> {
|
|
|
178
227
|
// Add default elemBefore and elemAfter elements if type is search.
|
|
179
228
|
const elementBefore =
|
|
180
229
|
type === "search" && !elemBefore ? (
|
|
181
|
-
<Icon name="search" ariaHidden />
|
|
230
|
+
<Icon name="search" ariaHidden color="icon.base" />
|
|
182
231
|
) : (
|
|
183
232
|
elemBefore
|
|
184
233
|
);
|
|
@@ -194,14 +243,17 @@ class Input extends React.Component<TypeProps> {
|
|
|
194
243
|
invalid={!!isInvalid}
|
|
195
244
|
warning={hasWarning}
|
|
196
245
|
appearance={appearance}
|
|
246
|
+
size={size}
|
|
197
247
|
// $FlowIssue - upgrade v0.112.0
|
|
198
248
|
{...rest}
|
|
199
249
|
>
|
|
200
250
|
<InputContext.Provider
|
|
201
251
|
value={{
|
|
202
252
|
handleClear: this.handleClear,
|
|
203
|
-
clearButtonLabel,
|
|
204
253
|
hasValue: !!value,
|
|
254
|
+
clearButtonLabel,
|
|
255
|
+
onClear,
|
|
256
|
+
size,
|
|
205
257
|
}}
|
|
206
258
|
>
|
|
207
259
|
{elementBefore && <Accessory before>{elementBefore}</Accessory>}
|
|
@@ -156,6 +156,38 @@ searchInput.story = {
|
|
|
156
156
|
name: "Search Input",
|
|
157
157
|
};
|
|
158
158
|
|
|
159
|
+
export const smallSearchInput = () => (
|
|
160
|
+
<Input
|
|
161
|
+
type="search"
|
|
162
|
+
size="small"
|
|
163
|
+
placeholder={text("placeholder", "Please enter a value...")}
|
|
164
|
+
value={text("value", "val")}
|
|
165
|
+
onClear={() => window.alert("Cleared!")}
|
|
166
|
+
clearButtonLabel={text("clearButtonLabel", "Clear search")}
|
|
167
|
+
ariaLabel={text("ariaLabel", "Descriptive label goes here")}
|
|
168
|
+
/>
|
|
169
|
+
);
|
|
170
|
+
|
|
171
|
+
smallSearchInput.story = {
|
|
172
|
+
name: "Small Search Input",
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
export const largeSearchInput = () => (
|
|
176
|
+
<Input
|
|
177
|
+
type="search"
|
|
178
|
+
size="large"
|
|
179
|
+
placeholder={text("placeholder", "Please enter a value...")}
|
|
180
|
+
value={text("value", "val")}
|
|
181
|
+
onClear={() => window.alert("Cleared!")}
|
|
182
|
+
clearButtonLabel={text("clearButtonLabel", "Clear search")}
|
|
183
|
+
ariaLabel={text("ariaLabel", "Descriptive label goes here")}
|
|
184
|
+
/>
|
|
185
|
+
);
|
|
186
|
+
|
|
187
|
+
largeSearchInput.story = {
|
|
188
|
+
name: "Large Search Input",
|
|
189
|
+
};
|
|
190
|
+
|
|
159
191
|
export const nonSearchClearButtonInput = () => {
|
|
160
192
|
return (
|
|
161
193
|
<Input
|
|
@@ -171,7 +203,7 @@ export const nonSearchClearButtonInput = () => {
|
|
|
171
203
|
};
|
|
172
204
|
|
|
173
205
|
nonSearchClearButtonInput.story = {
|
|
174
|
-
name: "Input.ClearButton usage",
|
|
206
|
+
name: "Manual Input.ClearButton usage",
|
|
175
207
|
};
|
|
176
208
|
|
|
177
209
|
export const autofocus = () => (
|
|
@@ -85,6 +85,20 @@ describe("Input", () => {
|
|
|
85
85
|
expect(getByRole("button")).toBeTruthy();
|
|
86
86
|
});
|
|
87
87
|
|
|
88
|
+
it("should not render a clear button for search Inputs if there is no text", () => {
|
|
89
|
+
const { queryByRole } = render(
|
|
90
|
+
<Input
|
|
91
|
+
id="name"
|
|
92
|
+
name="name"
|
|
93
|
+
value=""
|
|
94
|
+
type="search"
|
|
95
|
+
onClear={jest.fn()}
|
|
96
|
+
clearButtonLabel="Clear search"
|
|
97
|
+
/>
|
|
98
|
+
);
|
|
99
|
+
expect(queryByRole("button")).toBeFalsy();
|
|
100
|
+
});
|
|
101
|
+
|
|
88
102
|
it("should not override an elemAfter prop if passed", () => {
|
|
89
103
|
const { getByText, queryByTitle } = render(
|
|
90
104
|
<Input
|
|
@@ -189,6 +203,20 @@ describe("Input", () => {
|
|
|
189
203
|
expect(getByRole("button")).toBeTruthy();
|
|
190
204
|
});
|
|
191
205
|
|
|
206
|
+
it("should not render a clear button if there is no text", () => {
|
|
207
|
+
const { queryByRole } = render(
|
|
208
|
+
<Input
|
|
209
|
+
id="name"
|
|
210
|
+
name="name"
|
|
211
|
+
value=""
|
|
212
|
+
type="text"
|
|
213
|
+
elemAfter={<Input.ClearButton />}
|
|
214
|
+
clearButtonLabel="Clear search"
|
|
215
|
+
/>
|
|
216
|
+
);
|
|
217
|
+
expect(queryByRole("button")).toBeFalsy();
|
|
218
|
+
});
|
|
219
|
+
|
|
192
220
|
it("should use the fallback title if clearButtonLabel is not provided", () => {
|
|
193
221
|
const { getByTitle } = render(
|
|
194
222
|
<Input
|
package/commonjs/Input/index.js
CHANGED
|
@@ -11,7 +11,9 @@ var _Button = _interopRequireDefault(require("../Button"));
|
|
|
11
11
|
|
|
12
12
|
var _Icon = _interopRequireDefault(require("../Icon"));
|
|
13
13
|
|
|
14
|
-
var
|
|
14
|
+
var _styledComponents = _interopRequireDefault(require("styled-components"));
|
|
15
|
+
|
|
16
|
+
var _excluded = ["autoComplete", "autoFocus", "disabled", "readOnly", "isInvalid", "hasWarning", "id", "name", "placeholder", "type", "required", "value", "elemBefore", "elemAfter", "maxLength", "ariaLabel", "ariaDescribedby", "clearButtonLabel", "innerRef", "onBlur", "onChange", "onClear", "onFocus", "onKeyDown", "onKeyUp", "onPaste", "inputProps", "qa", "appearance", "size"];
|
|
15
17
|
|
|
16
18
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
17
19
|
|
|
@@ -28,23 +30,52 @@ function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.crea
|
|
|
28
30
|
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
|
|
29
31
|
|
|
30
32
|
var InputContext = /*#__PURE__*/React.createContext({});
|
|
33
|
+
var StyledButton = (0, _styledComponents.default)(_Button.default).withConfig({
|
|
34
|
+
displayName: "Input__StyledButton",
|
|
35
|
+
componentId: "sc-1ck1dnm-0"
|
|
36
|
+
})(["&:hover,&:active{color:", ";}"], function (props) {
|
|
37
|
+
return props.theme.utils.interact(props.theme.colors.icon.base);
|
|
38
|
+
});
|
|
31
39
|
|
|
32
40
|
var ClearButton = function ClearButton() {
|
|
33
41
|
var _React$useContext = React.useContext(InputContext),
|
|
42
|
+
onClear = _React$useContext.onClear,
|
|
34
43
|
handleClear = _React$useContext.handleClear,
|
|
35
44
|
clearButtonLabel = _React$useContext.clearButtonLabel,
|
|
36
|
-
hasValue = _React$useContext.hasValue
|
|
45
|
+
hasValue = _React$useContext.hasValue,
|
|
46
|
+
inputSize = _React$useContext.size; // Hide the button when there is no text to clear.
|
|
37
47
|
|
|
38
48
|
|
|
39
49
|
if (!hasValue) {
|
|
40
50
|
return null;
|
|
41
|
-
}
|
|
51
|
+
} // Log a warning and hide the button when no onClear callback is provided.
|
|
52
|
+
// If we called handleClear with no onClear prop, all the button would do is focus the Input.
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
if (!onClear) {
|
|
56
|
+
console.warn("Warning: No onClear prop provided to Input when using Input.ClearButton. Omitting Input.ClearButton.");
|
|
57
|
+
return null;
|
|
58
|
+
} // Warn if clearButtonLabel is not included, so that the unlocalized fallback will not be mistaken for a proper label.
|
|
42
59
|
|
|
43
|
-
|
|
44
|
-
|
|
60
|
+
|
|
61
|
+
if (!clearButtonLabel) {
|
|
62
|
+
console.warn("Warning: clearButtonLabel prop is required when using Input.ClearButton. Please pass a localized clearButtonLabel to Input.");
|
|
63
|
+
} // Reduce Button padding for size small Inputs so that the Button won't go outside the bounds of the Input.
|
|
64
|
+
// This adjustment is handled automatically for default and large Inputs via Button's size. There is no "small" Button.
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
var py = inputSize === "small" ? 100 : undefined;
|
|
68
|
+
var px = inputSize === "small" ? 200 : undefined;
|
|
69
|
+
var buttonSize = inputSize === "small" ? "default" : inputSize;
|
|
70
|
+
return /*#__PURE__*/React.createElement(StyledButton, {
|
|
71
|
+
onClick: handleClear,
|
|
72
|
+
size: buttonSize,
|
|
73
|
+
py: py,
|
|
74
|
+
px: px,
|
|
75
|
+
title: clearButtonLabel || "Clear",
|
|
76
|
+
color: "icon.base"
|
|
45
77
|
}, /*#__PURE__*/React.createElement(_Icon.default, {
|
|
46
|
-
name: "circlex"
|
|
47
|
-
title: clearButtonLabel || "Clear"
|
|
78
|
+
name: "circlex"
|
|
48
79
|
}));
|
|
49
80
|
}; // Used for positioning elementAfter. This logic will detect if the element is a ClearButton,
|
|
50
81
|
// regardless of whether it was manually passed as elemAfter or automatically added to a search Input.
|
|
@@ -75,7 +106,10 @@ var Input = /*#__PURE__*/function (_React$Component) {
|
|
|
75
106
|
};
|
|
76
107
|
|
|
77
108
|
_this.handleClear = function (e) {
|
|
78
|
-
|
|
109
|
+
var _this$props$innerRef$;
|
|
110
|
+
|
|
111
|
+
(_this$props$innerRef$ = _this.props.innerRef.current) == null ? void 0 : _this$props$innerRef$.focus();
|
|
112
|
+
_this.props.onClear == null ? void 0 : _this.props.onClear(e);
|
|
79
113
|
};
|
|
80
114
|
|
|
81
115
|
_this.handleChange = function (e) {
|
|
@@ -136,6 +170,7 @@ var Input = /*#__PURE__*/function (_React$Component) {
|
|
|
136
170
|
_this$props$qa = _this$props.qa,
|
|
137
171
|
qa = _this$props$qa === void 0 ? {} : _this$props$qa,
|
|
138
172
|
appearance = _this$props.appearance,
|
|
173
|
+
size = _this$props.size,
|
|
139
174
|
rest = _objectWithoutPropertiesLoose(_this$props, _excluded); // Convert autoComplete from a boolean prop to a string value.
|
|
140
175
|
|
|
141
176
|
|
|
@@ -148,7 +183,8 @@ var Input = /*#__PURE__*/function (_React$Component) {
|
|
|
148
183
|
|
|
149
184
|
var elementBefore = type === "search" && !elemBefore ? /*#__PURE__*/React.createElement(_Icon.default, {
|
|
150
185
|
name: "search",
|
|
151
|
-
ariaHidden: true
|
|
186
|
+
ariaHidden: true,
|
|
187
|
+
color: "icon.base"
|
|
152
188
|
}) : elemBefore; // Do not add a ClearButton if no onClear callback is provided or if an elemAfter prop was passed.
|
|
153
189
|
|
|
154
190
|
var elementAfter = type === "search" && onClear && !elemAfter ? /*#__PURE__*/React.createElement(ClearButton, null) : elemAfter;
|
|
@@ -158,13 +194,16 @@ var Input = /*#__PURE__*/function (_React$Component) {
|
|
|
158
194
|
disabled: disabled,
|
|
159
195
|
invalid: !!isInvalid,
|
|
160
196
|
warning: hasWarning,
|
|
161
|
-
appearance: appearance
|
|
197
|
+
appearance: appearance,
|
|
198
|
+
size: size // $FlowIssue - upgrade v0.112.0
|
|
162
199
|
|
|
163
200
|
}, rest), /*#__PURE__*/React.createElement(InputContext.Provider, {
|
|
164
201
|
value: {
|
|
165
202
|
handleClear: this.handleClear,
|
|
203
|
+
hasValue: !!value,
|
|
166
204
|
clearButtonLabel: clearButtonLabel,
|
|
167
|
-
|
|
205
|
+
onClear: onClear,
|
|
206
|
+
size: size
|
|
168
207
|
}
|
|
169
208
|
}, elementBefore && /*#__PURE__*/React.createElement(_styles.Accessory, {
|
|
170
209
|
before: true
|
|
@@ -207,7 +246,8 @@ Input.defaultProps = {
|
|
|
207
246
|
disabled: false,
|
|
208
247
|
type: "text",
|
|
209
248
|
size: "default",
|
|
210
|
-
appearance: "primary"
|
|
249
|
+
appearance: "primary",
|
|
250
|
+
innerRef: /*#__PURE__*/React.createRef()
|
|
211
251
|
};
|
|
212
252
|
Input.ClearButton = ClearButton;
|
|
213
253
|
Input.ClearButton.displayName = "Input.ClearButton";
|
package/lib/Input/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
var _excluded = ["autoComplete", "autoFocus", "disabled", "readOnly", "isInvalid", "hasWarning", "id", "name", "placeholder", "type", "required", "value", "elemBefore", "elemAfter", "maxLength", "ariaLabel", "ariaDescribedby", "clearButtonLabel", "innerRef", "onBlur", "onChange", "onClear", "onFocus", "onKeyDown", "onKeyUp", "onPaste", "inputProps", "qa", "appearance"];
|
|
1
|
+
var _excluded = ["autoComplete", "autoFocus", "disabled", "readOnly", "isInvalid", "hasWarning", "id", "name", "placeholder", "type", "required", "value", "elemBefore", "elemAfter", "maxLength", "ariaLabel", "ariaDescribedby", "clearButtonLabel", "innerRef", "onBlur", "onChange", "onClear", "onFocus", "onKeyDown", "onKeyUp", "onPaste", "inputProps", "qa", "appearance", "size"];
|
|
2
2
|
|
|
3
3
|
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
|
|
4
4
|
|
|
@@ -12,24 +12,54 @@ import * as React from "react";
|
|
|
12
12
|
import Container, { Accessory } from "./styles";
|
|
13
13
|
import Button from "../Button";
|
|
14
14
|
import Icon from "../Icon";
|
|
15
|
+
import styled from "styled-components";
|
|
15
16
|
var InputContext = /*#__PURE__*/React.createContext({});
|
|
17
|
+
var StyledButton = styled(Button).withConfig({
|
|
18
|
+
displayName: "Input__StyledButton",
|
|
19
|
+
componentId: "sc-1ck1dnm-0"
|
|
20
|
+
})(["&:hover,&:active{color:", ";}"], function (props) {
|
|
21
|
+
return props.theme.utils.interact(props.theme.colors.icon.base);
|
|
22
|
+
});
|
|
16
23
|
|
|
17
24
|
var ClearButton = function ClearButton() {
|
|
18
25
|
var _React$useContext = React.useContext(InputContext),
|
|
26
|
+
onClear = _React$useContext.onClear,
|
|
19
27
|
handleClear = _React$useContext.handleClear,
|
|
20
28
|
clearButtonLabel = _React$useContext.clearButtonLabel,
|
|
21
|
-
hasValue = _React$useContext.hasValue
|
|
29
|
+
hasValue = _React$useContext.hasValue,
|
|
30
|
+
inputSize = _React$useContext.size; // Hide the button when there is no text to clear.
|
|
22
31
|
|
|
23
32
|
|
|
24
33
|
if (!hasValue) {
|
|
25
34
|
return null;
|
|
26
|
-
}
|
|
35
|
+
} // Log a warning and hide the button when no onClear callback is provided.
|
|
36
|
+
// If we called handleClear with no onClear prop, all the button would do is focus the Input.
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
if (!onClear) {
|
|
40
|
+
console.warn("Warning: No onClear prop provided to Input when using Input.ClearButton. Omitting Input.ClearButton.");
|
|
41
|
+
return null;
|
|
42
|
+
} // Warn if clearButtonLabel is not included, so that the unlocalized fallback will not be mistaken for a proper label.
|
|
27
43
|
|
|
28
|
-
|
|
29
|
-
|
|
44
|
+
|
|
45
|
+
if (!clearButtonLabel) {
|
|
46
|
+
console.warn("Warning: clearButtonLabel prop is required when using Input.ClearButton. Please pass a localized clearButtonLabel to Input.");
|
|
47
|
+
} // Reduce Button padding for size small Inputs so that the Button won't go outside the bounds of the Input.
|
|
48
|
+
// This adjustment is handled automatically for default and large Inputs via Button's size. There is no "small" Button.
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
var py = inputSize === "small" ? 100 : undefined;
|
|
52
|
+
var px = inputSize === "small" ? 200 : undefined;
|
|
53
|
+
var buttonSize = inputSize === "small" ? "default" : inputSize;
|
|
54
|
+
return /*#__PURE__*/React.createElement(StyledButton, {
|
|
55
|
+
onClick: handleClear,
|
|
56
|
+
size: buttonSize,
|
|
57
|
+
py: py,
|
|
58
|
+
px: px,
|
|
59
|
+
title: clearButtonLabel || "Clear",
|
|
60
|
+
color: "icon.base"
|
|
30
61
|
}, /*#__PURE__*/React.createElement(Icon, {
|
|
31
|
-
name: "circlex"
|
|
32
|
-
title: clearButtonLabel || "Clear"
|
|
62
|
+
name: "circlex"
|
|
33
63
|
}));
|
|
34
64
|
}; // Used for positioning elementAfter. This logic will detect if the element is a ClearButton,
|
|
35
65
|
// regardless of whether it was manually passed as elemAfter or automatically added to a search Input.
|
|
@@ -60,7 +90,10 @@ var Input = /*#__PURE__*/function (_React$Component) {
|
|
|
60
90
|
};
|
|
61
91
|
|
|
62
92
|
_this.handleClear = function (e) {
|
|
63
|
-
|
|
93
|
+
var _this$props$innerRef$;
|
|
94
|
+
|
|
95
|
+
(_this$props$innerRef$ = _this.props.innerRef.current) == null ? void 0 : _this$props$innerRef$.focus();
|
|
96
|
+
_this.props.onClear == null ? void 0 : _this.props.onClear(e);
|
|
64
97
|
};
|
|
65
98
|
|
|
66
99
|
_this.handleChange = function (e) {
|
|
@@ -121,6 +154,7 @@ var Input = /*#__PURE__*/function (_React$Component) {
|
|
|
121
154
|
_this$props$qa = _this$props.qa,
|
|
122
155
|
qa = _this$props$qa === void 0 ? {} : _this$props$qa,
|
|
123
156
|
appearance = _this$props.appearance,
|
|
157
|
+
size = _this$props.size,
|
|
124
158
|
rest = _objectWithoutPropertiesLoose(_this$props, _excluded); // Convert autoComplete from a boolean prop to a string value.
|
|
125
159
|
|
|
126
160
|
|
|
@@ -133,7 +167,8 @@ var Input = /*#__PURE__*/function (_React$Component) {
|
|
|
133
167
|
|
|
134
168
|
var elementBefore = type === "search" && !elemBefore ? /*#__PURE__*/React.createElement(Icon, {
|
|
135
169
|
name: "search",
|
|
136
|
-
ariaHidden: true
|
|
170
|
+
ariaHidden: true,
|
|
171
|
+
color: "icon.base"
|
|
137
172
|
}) : elemBefore; // Do not add a ClearButton if no onClear callback is provided or if an elemAfter prop was passed.
|
|
138
173
|
|
|
139
174
|
var elementAfter = type === "search" && onClear && !elemAfter ? /*#__PURE__*/React.createElement(ClearButton, null) : elemAfter;
|
|
@@ -143,13 +178,16 @@ var Input = /*#__PURE__*/function (_React$Component) {
|
|
|
143
178
|
disabled: disabled,
|
|
144
179
|
invalid: !!isInvalid,
|
|
145
180
|
warning: hasWarning,
|
|
146
|
-
appearance: appearance
|
|
181
|
+
appearance: appearance,
|
|
182
|
+
size: size // $FlowIssue - upgrade v0.112.0
|
|
147
183
|
|
|
148
184
|
}, rest), /*#__PURE__*/React.createElement(InputContext.Provider, {
|
|
149
185
|
value: {
|
|
150
186
|
handleClear: this.handleClear,
|
|
187
|
+
hasValue: !!value,
|
|
151
188
|
clearButtonLabel: clearButtonLabel,
|
|
152
|
-
|
|
189
|
+
onClear: onClear,
|
|
190
|
+
size: size
|
|
153
191
|
}
|
|
154
192
|
}, elementBefore && /*#__PURE__*/React.createElement(Accessory, {
|
|
155
193
|
before: true
|
|
@@ -192,7 +230,8 @@ Input.defaultProps = {
|
|
|
192
230
|
disabled: false,
|
|
193
231
|
type: "text",
|
|
194
232
|
size: "default",
|
|
195
|
-
appearance: "primary"
|
|
233
|
+
appearance: "primary",
|
|
234
|
+
innerRef: /*#__PURE__*/React.createRef()
|
|
196
235
|
};
|
|
197
236
|
Input.ClearButton = ClearButton;
|
|
198
237
|
Input.ClearButton.displayName = "Input.ClearButton";
|