-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: replace html selects with Select component (#1637)
Replaces all the places we used raw html select components with the Select component. This was orignally needed for themeing so we could re-style the Select component, but we came up with an alternate solution.
- Loading branch information
Showing
15 changed files
with
195 additions
and
184 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,54 @@ | ||
import React, { useCallback } from 'react'; | ||
import classNames from 'classnames'; | ||
import { useForwardedRef } from '@deephaven/react-hooks'; | ||
|
||
export type SelectProps = { | ||
children: React.ReactNode; | ||
onBlur?: React.FocusEventHandler<HTMLSelectElement>; | ||
type baseSelectProps = Omit<React.HTMLProps<HTMLSelectElement>, 'onChange'>; | ||
|
||
export type SelectProps = baseSelectProps & { | ||
onChange: (value: string) => void; | ||
className?: string; | ||
defaultValue?: string; | ||
name?: string; | ||
value?: string; | ||
disabled?: boolean; | ||
'data-testid'?: string; | ||
}; | ||
|
||
function Select({ | ||
children, | ||
onBlur, | ||
onChange, | ||
className, | ||
defaultValue, | ||
name, | ||
value, | ||
disabled, | ||
'data-testid': dataTestId, | ||
}: SelectProps): JSX.Element { | ||
const handleChange = useCallback( | ||
event => { | ||
onChange(event.target.value); | ||
}, | ||
[onChange] | ||
); | ||
|
||
return ( | ||
<select | ||
className={classNames('custom-select', className)} | ||
onBlur={onBlur} | ||
onChange={handleChange} | ||
defaultValue={defaultValue} | ||
value={value} | ||
name={name} | ||
disabled={disabled} | ||
data-testid={dataTestId} | ||
> | ||
{children} | ||
</select> | ||
); | ||
} | ||
/** | ||
* A custom select component with styling, which is a wrapper around the | ||
* native select element. | ||
* @param props.onChange returns a string value and not the event | ||
*/ | ||
|
||
const Select = React.forwardRef<HTMLSelectElement, SelectProps>( | ||
(props, forwardedRef) => { | ||
const { | ||
children, | ||
className, | ||
onChange, | ||
'data-testid': dataTestId, | ||
...rest | ||
} = props; | ||
|
||
const ref = useForwardedRef<HTMLSelectElement>(forwardedRef); | ||
|
||
const handleChange = useCallback( | ||
(event: React.ChangeEvent<HTMLSelectElement>): void => { | ||
onChange(event.target.value); | ||
}, | ||
[onChange] | ||
); | ||
|
||
return ( | ||
<select | ||
ref={ref} | ||
className={classNames('custom-select', className)} | ||
onChange={handleChange} | ||
data-testid={dataTestId} | ||
// eslint-disable-next-line react/jsx-props-no-spreading | ||
{...rest} | ||
> | ||
{children} | ||
</select> | ||
); | ||
} | ||
); | ||
|
||
Select.displayName = 'Select'; | ||
|
||
export default Select; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.