Skip to content

Commit

Permalink
Merge pull request #67 from fortanix/input-with-tags
Browse files Browse the repository at this point in the history
Refactor Input with tags to display tags below
  • Loading branch information
nighto authored Dec 20, 2024
2 parents 45f71ca + 3800968 commit e795650
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,21 @@
@layer baklava.components {
.bk-input-field-with-tags {
@include bk.component-base(input-field-with-tags);
}

display: flex;
flex-direction: column;
gap: 6px;
padding-bottom: 1px;
border-bottom: 1px solid bk.$theme-form-rule-default;

.bk-input-field-with-tags__label {
@include bk.font(bk.$font-family-body, bk.$font-weight-semibold);
cursor: default;
}

.bk-input-field-with-tags__control {
--empty: ; // Prevent empty class from being removed
}

&:focus-within {
border-bottom-color: bk.$theme-form-rule-focused;
.bk-input-field-with-tags__label {
@include bk.font(bk.$font-family-body, bk.$font-weight-semibold);
cursor: default;
margin-bottom: bk.$spacing-2;
}

input {
outline: none !important;
}
}
.bk-input-field-with-tags__control {
width: 100%;
}

.bk-input-field-with-tags__container {
.bk-input-field-with-tags__tags {
display: flex;
flex-flow: row wrap;
gap: bk.$spacing-2;
}

.bk-input-field-with-tags__input-container {
flex-grow: 1;

input {
width: 100%;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,19 @@ export const InputWithTags: Story = {
};

return (
<Card>
<InputFieldWithTags
tags={tags}
value={inputText}
onUpdate={handleUpdate}
onUpdateTags={handleUpdateTags}
placeholder=""
/>
</Card>
<div>
<p>Enter creates new tags; backspace removes tags.</p>
<Card style={{width: '350px'}}>
<InputFieldWithTags
tags={tags}
value={inputText}
label={'Input with tags'}
onUpdate={handleUpdate}
onUpdateTags={handleUpdateTags}
placeholder="Placeholder"
/>
</Card>
</div>
);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export const InputFieldWithTags = (props: InputFieldWithTagsProps) => {

const controlId = React.useId();
const formContext = useFormContext();
const inputRef = React.useRef<React.ComponentRef<'input'>>(null);

const onChange = (e: React.ChangeEvent<HTMLInputElement>) => {
// first handle supplied onChange, if exists
Expand All @@ -80,6 +81,7 @@ export const InputFieldWithTags = (props: InputFieldWithTagsProps) => {
onUpdateTags(tags.slice(0,-1));
}
if (e.key === 'Enter' && value !== '') {
e.preventDefault();
onUpdateTags([...tags, value.trim()]);
onUpdate('');
}
Expand All @@ -89,6 +91,8 @@ export const InputFieldWithTags = (props: InputFieldWithTagsProps) => {
const onRemoveTag = (index: number) => {
if (onUpdateTags) {
onUpdateTags(tags.filter((_, idx) => idx !== index));
const inputEl = inputRef.current;
inputEl?.focus();
}
};

Expand All @@ -110,23 +114,21 @@ export const InputFieldWithTags = (props: InputFieldWithTagsProps) => {
{label}
</label>
}
<div className={cl['bk-input-field-with-tags__container']}>
<Input
{...inputProps}
id={controlId}
form={formContext.formId}
className={cx(cl['bk-input-field-with-tags__control'], inputProps.className)}
onChange={onChange}
onKeyDown={onKeyDown}
value={value}
ref={inputRef}
/>
<div className={cl['bk-input-field-with-tags__tags']}>
{tags && (
// biome-ignore lint/suspicious/noArrayIndexKey: no other unique identifier available
tags.map((tag, idx) => <Tag key={idx} content={tag} onRemove={() => onRemoveTag(idx)}/>)
)}
<div className={cl['bk-input-field-with-tags__input-container']}>
<Input
{...inputProps}
unstyled={true}
id={controlId}
form={formContext.formId}
className={cx(cl['bk-input-field-with-tags__control'], inputProps.className)}
onChange={onChange}
onKeyDown={onKeyDown}
value={value}
/>
</div>
</div>
</div>
);
Expand Down
1 change: 1 addition & 0 deletions src/components/text/Tag/Tag.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
align-items: center;
font-size: bk.$font-size-xs;
padding: bk.$size-2 bk.$spacing-2 bk.$size-3;
flex-shrink: 0;

&.bk-tag--with-close-button {
padding-right: 0;
Expand Down
5 changes: 4 additions & 1 deletion src/components/text/Tag/Tag.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import { classNames as cx, type ComponentProps } from '../../../util/componentUtil.ts';
import * as React from 'react';

import { Button } from '../../actions/Button/Button.tsx';
import { Icon } from '../../graphics/Icon/Icon.tsx';

import cl from './Tag.module.scss';
Expand Down Expand Up @@ -46,7 +47,9 @@ export const Tag = (props: TagProps) => {
>
{content}
{onRemove && (
<Icon icon="cross" className={cl['bk-tag__icon']} onClick={onRemove}/>
<Button unstyled onPress={onRemove}>
<Icon icon="cross" className={cl['bk-tag__icon']}/>
</Button>
)}
</div>
);
Expand Down

0 comments on commit e795650

Please sign in to comment.