-
Notifications
You must be signed in to change notification settings - Fork 2
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
feat: Add ability to target headers by name #360
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { Control, Controller, FieldValues, Path } from 'react-hook-form' | ||
import { StyledReactSelect } from '../StyledReactSelect' | ||
import { ComponentProps, ReactNode } from 'react' | ||
|
||
type Option = { label: ReactNode; value: string; disabled?: boolean } | ||
|
||
interface ControlledSelectProps<T extends FieldValues, O extends Option> { | ||
name: Path<T> | ||
control: Control<T> | ||
options: O[] | ||
selectProps?: ComponentProps<typeof StyledReactSelect<O>> | ||
onChange?: (value?: O['value']) => void | ||
} | ||
|
||
export function ControlledReactSelect<T extends FieldValues, O extends Option>({ | ||
name, | ||
control, | ||
options, | ||
onChange, | ||
selectProps = {}, | ||
}: ControlledSelectProps<T, O>) { | ||
return ( | ||
<Controller | ||
name={name} | ||
control={control} | ||
render={({ field }) => ( | ||
<StyledReactSelect | ||
onChange={(option) => | ||
onChange ? onChange(option?.value) : field.onChange(option?.value) | ||
} | ||
onBlur={field.onBlur} | ||
value={options.find((option) => option.value === field.value)} | ||
isDisabled={field.disabled} | ||
options={options} | ||
{...selectProps} | ||
/> | ||
)} | ||
/> | ||
) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { StylesConfig, Theme } from 'react-select' | ||
|
||
export function getThemeConfig(theme: Theme) { | ||
return { | ||
...theme, | ||
spacing: { | ||
...theme.spacing, | ||
baseUnit: 2, | ||
menuGutter: 2, | ||
}, | ||
|
||
colors: { | ||
...theme.colors, | ||
primary: 'var(--focus-8)', // active border | ||
primary50: 'var(--accent-9)', // focus item | ||
primary25: 'var(--accent-9)', // item hover | ||
neutral0: 'var(--color-surface)', // input background | ||
neutral20: 'var(--gray-a7)', // border | ||
neutral30: 'var(--gray-a8)', // border hover | ||
neutral50: 'var(--gray-a10)', // placeholder | ||
neutral60: 'var(--gray-12)', // caret | ||
neutral80: 'var(--gray-12)', // input text | ||
}, | ||
} | ||
} | ||
|
||
export function getStylesConfig<Option>(): StylesConfig<Option> { | ||
return { | ||
control: (provided, state) => ({ | ||
...provided, | ||
height: 'var(--space-6)', | ||
minHeight: 'auto', | ||
paddingLeft: 'var(--space-3)', | ||
paddingRight: 'var(--space-3)', | ||
boxShadow: state.menuIsOpen ? 'none' : provided.boxShadow, | ||
borderColor: state.menuIsOpen ? 'var(--gray-a8)' : provided.borderColor, | ||
'&:hover': { | ||
borderColor: 'var(--gray-a8)', | ||
}, | ||
}), | ||
valueContainer: (provided) => ({ | ||
...provided, | ||
padding: 0, | ||
}), | ||
singleValue: (provided) => ({ | ||
...provided, | ||
margin: 0, | ||
}), | ||
menu: (provided) => ({ | ||
...provided, | ||
backgroundColor: 'var(--color-panel-solid)', | ||
}), | ||
menuPortal: (provided) => ({ | ||
...provided, | ||
zIndex: 40, | ||
}), | ||
menuList: (provided) => ({ | ||
...provided, | ||
padding: 'var(--space-2)', | ||
paddingRight: 'var(--space-3)', | ||
}), | ||
option: (provided, state) => ({ | ||
...provided, | ||
background: state.isFocused ? 'var(--accent-9)' : 'transparent', | ||
color: state.isFocused ? 'var(--accent-contrast)' : undefined, | ||
display: 'flex', | ||
alignItems: 'center', | ||
position: 'relative', | ||
paddingLeft: 'var(--space-5)', | ||
paddingRight: 'var(--space-6)', | ||
borderRadius: 'var(--radius-2)', | ||
}), | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { ComponentProps, useMemo } from 'react' | ||
import Select, { components, OptionProps } from 'react-select' | ||
import { ChevronDownIcon, ThickCheckIcon } from '@radix-ui/themes' | ||
import { getStylesConfig, getThemeConfig } from './StyledReactSelect.styles' | ||
|
||
export function StyledReactSelect<Option>( | ||
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. Far from ideal integration of ReactSelect into Radix, it doesn't support all the props Radix's select does and has static size, but this iteration I've focused on blending it into Radix styles. |
||
props: ComponentProps<typeof Select<Option>> | ||
) { | ||
const styles = useMemo(() => getStylesConfig<Option>(), []) | ||
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. Can this be moved to just before the component function? 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 only reason is passing that generic type, was getting TS errors without it :( |
||
|
||
return ( | ||
<div css={{ fontSize: 'var(--font-size-2)' }}> | ||
<Select | ||
menuPlacement="auto" | ||
menuPosition="fixed" | ||
styles={styles} | ||
components={{ | ||
IndicatorSeparator: null, | ||
DropdownIndicator: DropdownIndicator, | ||
Option: OptionComponent<Option>, | ||
}} | ||
theme={getThemeConfig} | ||
{...props} | ||
/> | ||
</div> | ||
) | ||
} | ||
|
||
function OptionComponent<Option>({ children, ...props }: OptionProps<Option>) { | ||
return ( | ||
<components.Option {...props}> | ||
<div | ||
css={{ | ||
position: 'absolute', | ||
left: 0, | ||
width: 'var(--space-5)', | ||
display: 'inline-flex', | ||
alignItems: 'center', | ||
justifyContent: 'center', | ||
}} | ||
> | ||
{props.isSelected && <ThickCheckIcon />} | ||
</div> | ||
|
||
{children} | ||
</components.Option> | ||
) | ||
} | ||
|
||
function DropdownIndicator() { | ||
return <ChevronDownIcon className="rt-SelectIcon" /> | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './StyledReactSelect' |
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.
Not related to the feature, but added this as QoL to avoid closing the sidebar every time I did cmd+r to refresh the app.