Skip to content

Commit

Permalink
Add icon prop to form pills, set icon size, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ElisaShapiro committed Jul 22, 2024
1 parent c9e9590 commit 46bfc4f
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 12 deletions.
4 changes: 4 additions & 0 deletions playbook/app/pb_kits/playbook/pb_form_pill/_form_pill.scss
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ $form_pill_colors: (
outline: $primary solid 2px;
outline-offset: -1px;
}
.pb_form_pill_icon {
height: 12px !important;
width: 12px !important;
}
&.small {
height: fit-content;
height: -moz-fit-content;
Expand Down
11 changes: 11 additions & 0 deletions playbook/app/pb_kits/playbook/pb_form_pill/_form_pill.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,15 @@ test('displays size variant', () => {
)
const kit = screen.getByTestId('formpill')
expect(kit).toHaveClass(`pb_form_pill_kit_primary small none`)
});

test('displays icon', () => {
render(
<FormPill
data={{ testid: testId }}
icon={"test"}
/>
)
const kit = screen.getByTestId('formpill')
expect(kit).toHaveClass(`pb_form_pill_kit_primary_icon none`)
});
13 changes: 11 additions & 2 deletions playbook/app/pb_kits/playbook/pb_form_pill/_form_pill.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type FormPillProps = {
color?: "primary" | "neutral",
data?: {[key: string]: string},
tabIndex?: number,
icon?: string,
closeProps?: {
onClick?: React.MouseEventHandler<HTMLSpanElement>,
onMouseDown?: React.MouseEventHandler<HTMLSpanElement>,
Expand All @@ -42,9 +43,12 @@ const FormPill = (props: FormPillProps): React.ReactElement => {
color = "primary",
data = {},
tabIndex,
icon = "",
} = props

const iconClass = icon ? "_icon" : ""
const css = classnames(
`pb_form_pill_kit_${color}`,
`pb_form_pill_kit_${color}${iconClass}`,
globalProps(props),
className,
size === 'small' ? 'small' : null,
Expand Down Expand Up @@ -76,7 +80,12 @@ const FormPill = (props: FormPillProps): React.ReactElement => {
/>
</>
}

{icon &&
<Icon
className="pb_form_pill_icon"
icon={icon}
/>
}
{text &&
<Title
className="pb_form_pill_tag"
Expand Down
22 changes: 13 additions & 9 deletions playbook/app/pb_kits/playbook/pb_form_pill/form_pill.html.erb
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
<%= content_tag(:div, id: object.id, data: object.data, class: object.classname + object.size_class, tabindex: object.tabindex, **combined_html_options) do %>
<% if object.name.present? %>
<%= pb_rails("avatar", props: { name: object.name, image_url: object.avatar_url, size: "xs" }) %>
<%= pb_rails("title", props: { text: object.name, size: 4, classname: "pb_form_pill_text" }) %>
<% elsif object.text.present? %>
<%= pb_rails("title", props: { text: object.text, size: 4, classname: "pb_form_pill_tag" }) %>
<% end %>

<%= pb_rails("body", props: { classname: "pb_form_pill_close" }) do %>
<%= pb_rails("icon", props: { icon: 'times' , fixed_width: true }) %>
<% end %>
<% end %>
<%= pb_rails("title", props: { text: object.name, size: 4, classname: "pb_form_pill_text" }) %>
<% elsif object.text.present? %>
<% if object.icon.present? %>
<%= pb_rails("icon", props: { classname: "pb_form_pill_icon", icon: object.icon }) %>
<% end %>
<% if object.text.present? %>
<%= pb_rails("title", props: { text: object.text, size: 4, classname: "pb_form_pill_tag" }) %>
<% end %>
<% end %>
<%= pb_rails("body", props: { classname: "pb_form_pill_close" }) do %>
<%= pb_rails("icon", props: { icon: 'times' , fixed_width: true }) %>
<% end %>
<% end %>
7 changes: 6 additions & 1 deletion playbook/app/pb_kits/playbook/pb_form_pill/form_pill.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ class FormPill < Playbook::KitBase
values: %w[primary neutral],
default: "primary"
prop :tabindex
prop :icon

def classname
generate_classname("pb_form_pill_kit", color, name, text, text_transform)
generate_classname("pb_form_pill_kit", color, icon_class, name, text, text_transform)
end

def display_text
Expand All @@ -27,6 +28,10 @@ def display_text
def size_class
size == "small" ? " small" : ""
end

def icon_class
icon ? "icon" : nil
end
end
end
end
2 changes: 2 additions & 0 deletions playbook/spec/pb_kits/playbook/kits/form_pill_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
it { is_expected.to define_prop(:avatar_url) }
it { is_expected.to define_prop(:size) }
it { is_expected.to define_prop(:color) }
it { is_expected.to define_prop(:icon) }

describe "#classname" do
it "returns namespaced class name", :aggregate_failures do
expect(subject.new({}).classname).to eq "pb_form_pill_kit_primary_none"
expect(subject.new(classname: "additional_class").classname).to eq "pb_form_pill_kit_primary_none additional_class"
expect(subject.new(color: "neutral").classname).to eq "pb_form_pill_kit_neutral_none"
expect(subject.new(icon: "user").classname).to eq "pb_form_pill_kit_primary_icon_none"
end
end
end

0 comments on commit 46bfc4f

Please sign in to comment.