-
Notifications
You must be signed in to change notification settings - Fork 7
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
style(inputsearch): change default html clear icon #607
Changes from all commits
c4096a8
58e8be7
0197fed
d1dcf20
cbfab43
2ec4550
2c12150
e8b456d
0dad97c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,6 @@ import { | |
AutocompleteInputChangeReason, | ||
AutocompleteRenderInputParams, | ||
AutocompleteRenderOptionState, | ||
InputAdornment, | ||
AutocompleteProps as MuiAutocompleteProps, | ||
Popper, | ||
PopperProps, | ||
|
@@ -13,6 +12,7 @@ import { noop } from "src/common/utils"; | |
import ButtonIcon from "../ButtonIcon"; | ||
import { IconProps } from "../Icon"; | ||
import { InputSearchProps } from "../InputSearch"; | ||
import { StyledInputAdornment } from "../InputSearch/style"; | ||
import MenuItem, { IconNameToSmallSizes } from "../MenuItem"; | ||
import { | ||
InputBaseWrapper, | ||
|
@@ -171,13 +171,34 @@ const Autocomplete = < | |
*/ | ||
...params.InputProps.ref, | ||
endAdornment: ( | ||
<InputAdornment position="end"> | ||
<StyledInputAdornment position="end"> | ||
{/** | ||
* (masoudmansdon): Because the Autocomplete component overrides the | ||
* InputSearch's endAdornment, we must also include the clear IconButton here. | ||
*/} | ||
{inputValue && ( | ||
<ButtonIcon | ||
aria-label="clear-button" | ||
className="input-search-clear-icon" | ||
onClick={clearInput} | ||
sdsType="primary" | ||
sdsSize="small" | ||
sdsIconProps={{ | ||
sdsType: "iconButton", | ||
}} | ||
sdsIcon="xMark" | ||
/> | ||
)} | ||
<ButtonIcon | ||
aria-label="search-button" | ||
sdsType="secondary" | ||
sdsSize="small" | ||
sdsIconProps={{ | ||
sdsType: "interactive", | ||
}} | ||
sdsIcon="search" | ||
/> | ||
</InputAdornment> | ||
</StyledInputAdornment> | ||
), | ||
inputProps: params.inputProps, | ||
}} | ||
|
@@ -217,6 +238,20 @@ const Autocomplete = < | |
/> | ||
); | ||
|
||
function clearInput() { | ||
setInputValue(""); | ||
/** | ||
* (masoudmanson): Because we are manually firing this event, | ||
* we must build a onChange event to transmit the updated value to onChange listeners. | ||
*/ | ||
if (onInputChange) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because we are manually firing this event, we must build a onChange event to transmit the updated value to onChange listeners. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's add this context comment in the code too please 🙏 |
||
onInputChange( | ||
{ target: { value: "" } } as React.ChangeEvent<HTMLInputElement>, | ||
"", | ||
"clear" | ||
); | ||
} | ||
|
||
function defaultGetOptionLabel( | ||
option: T | AutocompleteFreeSoloValueMapping<FreeSolo> | ||
): string { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
import { | ||
InputAdornment, | ||
TextFieldProps as RawTextFieldSearchProps, | ||
} from "@mui/material"; | ||
import { TextFieldProps as RawTextFieldSearchProps } from "@mui/material"; | ||
import React, { forwardRef, useState } from "react"; | ||
import ButtonIcon from "../ButtonIcon"; | ||
import { InputSearchExtraProps, StyledLabel, StyledSearchBase } from "./style"; | ||
import { | ||
InputSearchExtraProps, | ||
StyledInputAdornment, | ||
StyledLabel, | ||
StyledSearchBase, | ||
} from "./style"; | ||
|
||
export interface AccessibleInputSearchProps { | ||
label: string; | ||
|
@@ -50,6 +52,23 @@ const InputSearch = forwardRef<HTMLDivElement, InputSearchProps>( | |
if (onChange) onChange(event); | ||
}; | ||
|
||
const clearInput = () => { | ||
setValue(""); | ||
setHasValue(false); | ||
|
||
if (handleSubmit) handleSubmit(""); | ||
|
||
/** | ||
* (masoudmanson): Because we are manually firing this event, | ||
* we must build a onChange event to transmit the updated value to onChange listeners. | ||
*/ | ||
if (onChange) { | ||
onChange({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as the Autocomplete component, Because we are manually firing this event, we must build a onChange event to transmit the updated value to onChange listeners. |
||
target: { value: "" }, | ||
} as React.ChangeEvent<HTMLInputElement>); | ||
} | ||
}; | ||
|
||
const localHandleSubmit = () => { | ||
if (handleSubmit) handleSubmit(value); | ||
}; | ||
|
@@ -77,7 +96,18 @@ const InputSearch = forwardRef<HTMLDivElement, InputSearchProps>( | |
// passed to mui Input | ||
InputProps={{ | ||
endAdornment: ( | ||
<InputAdornment position="end"> | ||
<StyledInputAdornment position="end"> | ||
<ButtonIcon | ||
aria-label="clear-button" | ||
className="input-search-clear-icon" | ||
onClick={clearInput} | ||
sdsType="primary" | ||
sdsSize="small" | ||
sdsIconProps={{ | ||
sdsType: "iconButton", | ||
}} | ||
sdsIcon="xMark" | ||
/> | ||
<ButtonIcon | ||
aria-label="search-button" | ||
onClick={localHandleSubmit} | ||
|
@@ -88,7 +118,7 @@ const InputSearch = forwardRef<HTMLDivElement, InputSearchProps>( | |
}} | ||
sdsIcon="search" | ||
/> | ||
</InputAdornment> | ||
</StyledInputAdornment> | ||
), | ||
}} | ||
type="search" | ||
|
@@ -100,8 +130,13 @@ const InputSearch = forwardRef<HTMLDivElement, InputSearchProps>( | |
sdsStyle={sdsStyle} | ||
sdsStage={hasValue ? "userInput" : "default"} | ||
onChange={handleChange} | ||
onKeyPress={handleKeyPress} | ||
onKeyDown={handleKeyPress} | ||
intent={intent} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The onKeyPress event has been deprecated and replaced by the newer onKeyDown event. |
||
/** | ||
* (masoudmanson): This prevents the browser's default auto completion | ||
* menu from being displayed for the InputSearch. | ||
*/ | ||
autoComplete="off" | ||
{...rest} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's add this comment in the code too for context! |
||
/> | ||
</> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
import { css, SerializedStyles } from "@emotion/react"; | ||
import { | ||
buttonBaseClasses, | ||
InputAdornment, | ||
inputAdornmentClasses, | ||
inputBaseClasses, | ||
outlinedInputClasses, | ||
|
@@ -21,6 +23,7 @@ export interface InputSearchExtraProps extends CommonThemeProps { | |
intent?: "default" | "error" | "warning"; | ||
sdsStyle?: "rounded" | "square"; | ||
sdsStage?: "default" | "userInput"; | ||
value?: string; | ||
} | ||
|
||
const sdsPropNames = ["sdsStyle", "sdsStage", "intent", "handleSubmit"]; | ||
|
@@ -104,7 +107,7 @@ export const StyledSearchBase = styled(TextField, { | |
}, | ||
})` | ||
${(props: InputSearchExtraProps) => { | ||
const { intent, disabled, sdsStyle } = props; | ||
const { intent, disabled, sdsStyle, value } = props; | ||
const spacings = getSpaces(props); | ||
const borders = getBorders(props); | ||
const colors = getColors(props); | ||
|
@@ -116,14 +119,29 @@ export const StyledSearchBase = styled(TextField, { | |
min-width: 120px; | ||
display: block; | ||
|
||
[type="search"]::-webkit-search-cancel-button { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This part hides the default HTML clear icon. |
||
-webkit-appearance: none; | ||
appearance: none; | ||
} | ||
|
||
& .input-search-clear-icon { | ||
opacity: 0; | ||
margin-right: ${spacings?.s}px; | ||
} | ||
|
||
.${outlinedInputClasses.root} { | ||
.${outlinedInputClasses.notchedOutline} { | ||
border: ${borders?.gray[400]}; | ||
} | ||
|
||
&:hover .input-search-clear-icon, | ||
&:focus-within .input-search-clear-icon { | ||
opacity: ${value ? 1 : 0}; | ||
} | ||
} | ||
|
||
.${inputBaseClasses.inputSizeSmall} { | ||
padding: ${spacings?.xs}px 0 ${spacings?.xs}px ${spacings?.l}px; | ||
padding: ${spacings?.xs}px ${spacings?.l}px; | ||
height: 34px; | ||
box-sizing: border-box; | ||
background-color: #fff; | ||
|
@@ -139,8 +157,11 @@ export const StyledSearchBase = styled(TextField, { | |
border: ${borders?.primary[400]}; | ||
} | ||
|
||
.${inputAdornmentClasses.root} svg { | ||
color: ${colors?.primary[400]}; | ||
.${inputAdornmentClasses.root} .${buttonBaseClasses.root}:last-of-type { | ||
cursor: default; | ||
svg { | ||
color: ${colors?.primary[400]}; | ||
} | ||
} | ||
} | ||
|
||
|
@@ -151,3 +172,7 @@ export const StyledSearchBase = styled(TextField, { | |
`; | ||
}} | ||
`; | ||
|
||
export const StyledInputAdornment = styled(InputAdornment)` | ||
position: relative; | ||
`; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because the Autocomplete component overrides the InputSearch's endAdornment, we must also include the clear IconButton here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
feels like it's worth adding this comment in the code!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahh, sure :D