Skip to content

Commit 7948fb0

Browse files
authored
feat: add suffix and prefix props on SfButton (#11)
1 parent e13e5ad commit 7948fb0

File tree

4 files changed

+64
-4
lines changed

4 files changed

+64
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export const createControlsOptions = <TConfig extends object>(config: TConfig, defaultOption?: keyof TConfig) => {
2+
const controlsOptions = Object.keys(config) as (keyof TConfig)[];
3+
4+
const getValue = <TLabel extends keyof TConfig>(label: TLabel) => config[label];
5+
6+
return {
7+
controlsOptions,
8+
defaultOption: defaultOption || controlsOptions[0],
9+
getValue,
10+
};
11+
};

apps/website/src/routes/examples/SfButton/index.tsx

+47-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { component$, useContext, useTask$ } from '@builder.io/qwik';
1+
import { component$, useContext, useSignal, useTask$ } from '@builder.io/qwik';
22
import {
33
SfButton,
44
SfButtonSize,
@@ -7,12 +7,27 @@ import {
77
SfIconSearch,
88
} from 'qwik-storefront-ui';
99
import { ComponentExample } from '../../../components/utils/ComponentExample';
10+
import { createControlsOptions } from '../../../components/utils/ControlsOptions';
1011
import { ControlsType } from '../../../components/utils/types';
1112
import { EXAMPLES_STATE } from '../layout';
1213

14+
15+
const prefixSlotOptions = createControlsOptions({
16+
none: undefined,
17+
'Search icon': <SfIconSearch />,
18+
});
19+
const suffixSlotOptions = createControlsOptions({
20+
none: undefined,
21+
'Lock icon': <SfIconLock />,
22+
});
23+
1324
export default component$(() => {
25+
const selectPrefix = useSignal<boolean>()
26+
const selectSuffix = useSignal<boolean>()
27+
1428
const examplesState = useContext(EXAMPLES_STATE);
1529

30+
1631
useTask$(() => {
1732
examplesState.data = {
1833
controls: [
@@ -21,6 +36,19 @@ export default component$(() => {
2136
modelName: 'slot',
2237
description: 'Only for demonstration purposes. Default slot',
2338
},
39+
{
40+
type: 'select',
41+
modelName: 'slotPrefix',
42+
description: 'slotPrefix',
43+
options: prefixSlotOptions.controlsOptions,
44+
45+
},
46+
{
47+
type: 'select',
48+
modelName: 'slotSuffix',
49+
description: 'slotSuffix',
50+
options: suffixSlotOptions.controlsOptions,
51+
},
2452
{
2553
type: 'text',
2654
modelName: 'as',
@@ -52,14 +80,31 @@ export default component$(() => {
5280
disabled: undefined,
5381
variant: SfButtonVariant.primary,
5482
size: SfButtonSize.base,
83+
slotPrefix: false,
84+
slotSuffix: false,
5585
square: undefined,
5686
},
5787
};
5888
});
5989

90+
useTask$(({ track }) => {
91+
track(() => examplesState.data.state);
92+
if (selectPrefix.value === null) return;
93+
selectPrefix.value = prefixSlotOptions.getValue(examplesState.data.state.slotPrefix)
94+
});
95+
96+
useTask$(({ track }) => {
97+
track(() => examplesState.data.state);
98+
if (selectSuffix.value === null) return;
99+
selectSuffix.value = suffixSlotOptions.getValue(examplesState.data.state.slotSuffix)
100+
});
101+
60102
return (
61103
<ComponentExample componentContainerClass="space-x-2">
62-
<SfButton {...examplesState.data.state} class="max-w-[200px]">
104+
<SfButton
105+
slotPrefix={selectPrefix.value}
106+
slotSuffix={selectSuffix.value}
107+
{...examplesState.data.state} class="max-w-[200px]">
63108
<div q:slot="prefix">
64109
<SfIconSearch />
65110
</div>

packages/qwik-storefront-ui/src/components/SfButton/SfButton.tsx

+4-2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ export const SfButton = component$<SfButtonProps>(
3232
ref,
3333
class: className,
3434
size = SfButtonSize.base,
35+
slotPrefix,
36+
slotSuffix,
3537
variant = SfButtonVariant.primary,
3638
square,
3739
...attributes
@@ -55,9 +57,9 @@ export const SfButton = component$<SfButtonProps>(
5557
data-testid="button"
5658
{...attributes}
5759
>
58-
<Slot name="prefix" />
60+
{slotPrefix && <Slot name="prefix" />}
5961
<Slot />
60-
<Slot name="suffix" />
62+
{slotSuffix && <Slot name="suffix" />}
6163
</Tag>
6264
);
6365
}

packages/qwik-storefront-ui/src/components/SfButton/types.ts

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ export type SfButtonProps = QwikIntrinsicElements['button'] &
1111
ref?: Signal<Element | undefined>;
1212
size?: `${SfButtonSize}`;
1313
variant?: `${SfButtonVariant}`;
14+
slotPrefix?: boolean,
15+
slotSuffix?: boolean,
1416
square?: boolean;
1517
onClick$?: PropFunction<
1618
(event: QwikMouseEvent<HTMLButtonElement, MouseEvent>) => void

0 commit comments

Comments
 (0)