Skip to content
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

fix(SelectInput): Prevent placeholder from being selectable in SelectInput #1482

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions packages/fuselage/src/components/SelectInput/SelectInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ export const SelectInput = forwardRef(function SelectInput(
{ children, multiple, placeholder, onChange, ...props }: SelectInputProps,
ref: Ref<HTMLElement>,
) {
// State to track if the placeholder should be visible
const [isPlaceholderVisible, setPlaceholderVisible] = useState(
!props.value && !props.defaultValue,
);

// Update placeholder visibility based on the selected value
const handleChange = useCallback(
(event) => {
setPlaceholderVisible(!event.currentTarget.value);
Expand All @@ -37,24 +40,29 @@ export const SelectInput = forwardRef(function SelectInput(
children={children}
{...props}
multiple
type='select'
type="select"
onChange={handleChange}
/>
);
}

return (
<InputBox
// Conditionally set placeholder visibility based on state
placeholderVisible={isPlaceholderVisible ? !!placeholder : undefined}
ref={ref}
{...props}
addon={<Icon name='chevron-down' size='x20' />}
type='select'
addon={<Icon name="chevron-down" size="x20" />}
type="select"
onChange={handleChange}
>
{placeholder && (
<InputBox.Placeholder value=''>{placeholder}</InputBox.Placeholder>
{/* Render a disabled, hidden placeholder option if no value is selected */}
{placeholder && isPlaceholderVisible && (
<option value="" disabled hidden>
{placeholder}
</option>
)}
{/* Render the provided children options */}
{children}
</InputBox>
);
Expand Down