diff --git a/apps/website/src/components/utils/ControlsOptions.ts b/apps/website/src/components/utils/ControlsOptions.ts new file mode 100644 index 0000000..7e10317 --- /dev/null +++ b/apps/website/src/components/utils/ControlsOptions.ts @@ -0,0 +1,11 @@ +export const createControlsOptions = (config: TConfig, defaultOption?: keyof TConfig) => { + const controlsOptions = Object.keys(config) as (keyof TConfig)[]; + + const getValue = (label: TLabel) => config[label]; + + return { + controlsOptions, + defaultOption: defaultOption || controlsOptions[0], + getValue, + }; +}; \ No newline at end of file diff --git a/apps/website/src/routes/examples/SfButton/index.tsx b/apps/website/src/routes/examples/SfButton/index.tsx index 097857d..94236d8 100644 --- a/apps/website/src/routes/examples/SfButton/index.tsx +++ b/apps/website/src/routes/examples/SfButton/index.tsx @@ -1,4 +1,4 @@ -import { component$, useContext, useTask$ } from '@builder.io/qwik'; +import { component$, useContext, useSignal, useTask$ } from '@builder.io/qwik'; import { SfButton, SfButtonSize, @@ -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': , +}); +const suffixSlotOptions = createControlsOptions({ + none: undefined, + 'Lock icon': , +}); + export default component$(() => { + const selectPrefix = useSignal() + const selectSuffix = useSignal() + const examplesState = useContext(EXAMPLES_STATE); + useTask$(() => { examplesState.data = { controls: [ @@ -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', @@ -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 ( - +
diff --git a/packages/qwik-storefront-ui/src/components/SfButton/SfButton.tsx b/packages/qwik-storefront-ui/src/components/SfButton/SfButton.tsx index 08008d6..724c822 100644 --- a/packages/qwik-storefront-ui/src/components/SfButton/SfButton.tsx +++ b/packages/qwik-storefront-ui/src/components/SfButton/SfButton.tsx @@ -32,6 +32,8 @@ export const SfButton = component$( ref, class: className, size = SfButtonSize.base, + slotPrefix, + slotSuffix, variant = SfButtonVariant.primary, square, ...attributes @@ -55,9 +57,9 @@ export const SfButton = component$( data-testid="button" {...attributes} > - + {slotPrefix && } - + {slotSuffix && } ); } diff --git a/packages/qwik-storefront-ui/src/components/SfButton/types.ts b/packages/qwik-storefront-ui/src/components/SfButton/types.ts index 84ba47d..7e908f8 100644 --- a/packages/qwik-storefront-ui/src/components/SfButton/types.ts +++ b/packages/qwik-storefront-ui/src/components/SfButton/types.ts @@ -11,6 +11,8 @@ export type SfButtonProps = QwikIntrinsicElements['button'] & ref?: Signal; size?: `${SfButtonSize}`; variant?: `${SfButtonVariant}`; + slotPrefix?: boolean, + slotSuffix?: boolean, square?: boolean; onClick$?: PropFunction< (event: QwikMouseEvent) => void