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

Add affix support on Select component #29

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
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
19 changes: 19 additions & 0 deletions packages/kubrick/src/Select/Select.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,23 @@ export const WithDescriptionBeforeInput: Story = {
render: Default.render,
};

export const WithAfffix: Story = {
args: {
'aria-label': 'Frequency',
description: 'Select the frequency for the site.',
label: '',
prefix: 'Frequency:',
suffix: 'Hz',
},
render(props) {
return (
<Select {...props}>
<Option value="80">80</Option>
<Option value="90">90</Option>
<Option value="100">100</Option>
</Select>
);
},
};

export default meta;
79 changes: 58 additions & 21 deletions packages/kubrick/src/Select/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,15 @@ interface SelectProps
descriptionArea?: 'after-input' | 'before-input';
label?: ReactNode;
name: string;
/**
* Content or element to dis before the input.
*/
prefix?: ReactNode;
selectedItem?: string;
/**
* Content or element to display after the input.
*/
suffix?: ReactNode;
}

function determineKey(props: OptionProps) {
Expand Down Expand Up @@ -84,7 +92,9 @@ export const Select = forwardRef<HTMLSelectElement, SelectProps>(
isRequired,
label,
name,
prefix,
selectedItem,
suffix,
} = props;
const ref = useObjectRef(forwardedRef);
const { clsx, componentProps, rootProps } = useProps('Select', props);
Expand Down Expand Up @@ -144,30 +154,57 @@ export const Select = forwardRef<HTMLSelectElement, SelectProps>(
: ''}
</label>
)}
<select
{...filterDOMProps(componentProps, { labelable: true })}
aria-describedby={selectProps['aria-describedby']}
aria-invalid={isInvalid || undefined}
aria-labelledby={selectProps['aria-labelledby']}
<div
className={clsx({
classNames: {
[classes.input]: true,
},
prefixedNames: 'input',
classNames: classes.inputWrapper,
prefixedNames: 'input-wrapper',
})}
disabled={isDisabled}
id={selectProps.id}
name={name}
onBlur={selectProps.onBlur}
onChange={(e) => state.setSelectedKey(e.target.value)}
onFocus={selectProps.onFocus}
ref={ref}
required={componentProps.isRequired}
tabIndex={componentProps.excludeFromTabOrder ? -1 : undefined}
value={state.selectedKey !== null ? state.selectedKey : undefined}
>
{children}
</select>
{prefix && (
<div
className={clsx({
classNames: classes.prefix,
prefixedNames: 'prefix',
})}
>
{prefix}
</div>
)}
<select
{...filterDOMProps(componentProps, { labelable: true })}
aria-describedby={selectProps['aria-describedby']}
aria-invalid={isInvalid || undefined}
aria-labelledby={selectProps['aria-labelledby']}
className={clsx({
classNames: {
[classes.input]: true,
},
prefixedNames: 'input',
})}
disabled={isDisabled}
id={selectProps.id}
name={name}
onBlur={selectProps.onBlur}
onChange={(e) => state.setSelectedKey(e.target.value)}
onFocus={selectProps.onFocus}
ref={ref}
required={componentProps.isRequired}
tabIndex={componentProps.excludeFromTabOrder ? -1 : undefined}
value={state.selectedKey !== null ? state.selectedKey : undefined}
>
{children}
</select>
{suffix && (
<div
className={clsx({
classNames: classes.suffix,
prefixedNames: 'suffix',
})}
>
{suffix}
</div>
)}
</div>
{errorMessageList.length >= 1 && (
<div
{...errorMessageProps}
Expand Down
Loading