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

feat: add suffix and prefix props on SfButton #11

Merged
merged 1 commit into from
Oct 10, 2023
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
11 changes: 11 additions & 0 deletions apps/website/src/components/utils/ControlsOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const createControlsOptions = <TConfig extends object>(config: TConfig, defaultOption?: keyof TConfig) => {
const controlsOptions = Object.keys(config) as (keyof TConfig)[];

const getValue = <TLabel extends keyof TConfig>(label: TLabel) => config[label];

return {
controlsOptions,
defaultOption: defaultOption || controlsOptions[0],
getValue,
};
};
49 changes: 47 additions & 2 deletions apps/website/src/routes/examples/SfButton/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { component$, useContext, useTask$ } from '@builder.io/qwik';
import { component$, useContext, useSignal, useTask$ } from '@builder.io/qwik';
import {
SfButton,
SfButtonSize,
Expand All @@ -7,12 +7,27 @@ import {
SfIconSearch,
} from 'qwik-storefront-ui';
import { ComponentExample } from '../../../components/utils/ComponentExample';
import { createControlsOptions } from '../../../components/utils/ControlsOptions';
import { ControlsType } from '../../../components/utils/types';
import { EXAMPLES_STATE } from '../layout';


const prefixSlotOptions = createControlsOptions({
none: undefined,
'Search icon': <SfIconSearch />,
});
const suffixSlotOptions = createControlsOptions({
none: undefined,
'Lock icon': <SfIconLock />,
});

export default component$(() => {
const selectPrefix = useSignal<boolean>()
const selectSuffix = useSignal<boolean>()

const examplesState = useContext(EXAMPLES_STATE);


useTask$(() => {
examplesState.data = {
controls: [
Expand All @@ -21,6 +36,19 @@ export default component$(() => {
modelName: 'slot',
description: 'Only for demonstration purposes. Default slot',
},
{
type: 'select',
modelName: 'slotPrefix',
description: 'slotPrefix',
options: prefixSlotOptions.controlsOptions,

},
{
type: 'select',
modelName: 'slotSuffix',
description: 'slotSuffix',
options: suffixSlotOptions.controlsOptions,
},
{
type: 'text',
modelName: 'as',
Expand Down Expand Up @@ -52,14 +80,31 @@ export default component$(() => {
disabled: undefined,
variant: SfButtonVariant.primary,
size: SfButtonSize.base,
slotPrefix: false,
slotSuffix: false,
square: undefined,
},
};
});

useTask$(({ track }) => {
track(() => examplesState.data.state);
if (selectPrefix.value === null) return;
selectPrefix.value = prefixSlotOptions.getValue(examplesState.data.state.slotPrefix)
});

useTask$(({ track }) => {
track(() => examplesState.data.state);
if (selectSuffix.value === null) return;
selectSuffix.value = suffixSlotOptions.getValue(examplesState.data.state.slotSuffix)
});

return (
<ComponentExample componentContainerClass="space-x-2">
<SfButton {...examplesState.data.state} class="max-w-[200px]">
<SfButton
slotPrefix={selectPrefix.value}
slotSuffix={selectSuffix.value}
{...examplesState.data.state} class="max-w-[200px]">
<div q:slot="prefix">
<SfIconSearch />
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export const SfButton = component$<SfButtonProps>(
ref,
class: className,
size = SfButtonSize.base,
slotPrefix,
slotSuffix,
variant = SfButtonVariant.primary,
square,
...attributes
Expand All @@ -55,9 +57,9 @@ export const SfButton = component$<SfButtonProps>(
data-testid="button"
{...attributes}
>
<Slot name="prefix" />
{slotPrefix && <Slot name="prefix" />}
<Slot />
<Slot name="suffix" />
{slotSuffix && <Slot name="suffix" />}
</Tag>
);
}
Expand Down
2 changes: 2 additions & 0 deletions packages/qwik-storefront-ui/src/components/SfButton/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export type SfButtonProps = QwikIntrinsicElements['button'] &
ref?: Signal<Element | undefined>;
size?: `${SfButtonSize}`;
variant?: `${SfButtonVariant}`;
slotPrefix?: boolean,
slotSuffix?: boolean,
square?: boolean;
onClick$?: PropFunction<
(event: QwikMouseEvent<HTMLButtonElement, MouseEvent>) => void
Expand Down