|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2024 Siemens AG |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: MIT |
| 5 | + * |
| 6 | + * This source code is licensed under the MIT license found in the |
| 7 | + * LICENSE file in the root directory of this source tree. |
| 8 | + */ |
| 9 | +import type { ArgTypes, Meta, StoryObj } from '@storybook/web-components'; |
| 10 | +import type { Components } from '@siemens/ix/components'; |
| 11 | +import { genericRender, makeArgTypes } from './utils/generic-render'; |
| 12 | +import { action } from '@storybook/addon-actions'; |
| 13 | + |
| 14 | +type Element = Components.IxCheckbox & { |
| 15 | + defaultSlot: string; |
| 16 | + ['checked-change']: any; |
| 17 | + validation: string; |
| 18 | + 'text-on': string; |
| 19 | +}; |
| 20 | + |
| 21 | +type GroupElement = Components.IxCheckboxGroup & { |
| 22 | + label: string; |
| 23 | + defaultSlot: string; |
| 24 | +}; |
| 25 | + |
| 26 | +const CheckboxRender = (args: Element) => { |
| 27 | + const container = genericRender('ix-checkbox', args); |
| 28 | + const ixCheckbox = container.querySelector( |
| 29 | + 'ix-checkbox' |
| 30 | + ) as HTMLIxCheckboxElement; |
| 31 | + ixCheckbox.addEventListener('checkedChange', action('checkedChange')); |
| 32 | + return container; |
| 33 | +}; |
| 34 | + |
| 35 | +const CheckboxGroupRender = (args: GroupElement) => { |
| 36 | + const container = genericRender('ix-checkbox-group', args); |
| 37 | + const checkboxGroup = container.querySelector( |
| 38 | + 'ix-checkbox-group' |
| 39 | + ) as HTMLIxCheckboxGroupElement; |
| 40 | + checkboxGroup.setAttribute('label', args.label || 'Group'); |
| 41 | + |
| 42 | + const checkbox1 = document.createElement('ix-checkbox'); |
| 43 | + checkbox1.setAttribute('label', 'Checkbox 1'); |
| 44 | + checkbox1.setAttribute('name', 'checkbox1'); |
| 45 | + checkbox1.addEventListener('checkedChange', action('checkbox1Change')); |
| 46 | + |
| 47 | + const checkbox2 = document.createElement('ix-checkbox'); |
| 48 | + checkbox2.setAttribute('label', 'Checkbox 2'); |
| 49 | + checkbox2.setAttribute('name', 'checkbox2'); |
| 50 | + checkbox2.addEventListener('checkedChange', action('checkbox2Change')); |
| 51 | + |
| 52 | + checkboxGroup.appendChild(checkbox1); |
| 53 | + checkboxGroup.appendChild(checkbox2); |
| 54 | + container.appendChild(checkboxGroup); |
| 55 | + |
| 56 | + return container; |
| 57 | +}; |
| 58 | + |
| 59 | +const meta = { |
| 60 | + title: 'Example/Checkbox', |
| 61 | + tags: [], |
| 62 | + render: (args) => CheckboxRender(args), |
| 63 | + argTypes: makeArgTypes<Partial<ArgTypes<Element>>>('ix-checkbox', { |
| 64 | + validation: { |
| 65 | + control: { type: 'select' }, |
| 66 | + }, |
| 67 | + }), |
| 68 | + parameters: { |
| 69 | + design: { |
| 70 | + type: 'figma', |
| 71 | + url: 'https://www.figma.com/design/r2nqdNNXXZtPmWuVjIlM1Q/iX-Components?node-id=42365-150769&p=f&t=eGUQESg89t8bPyiB-0', |
| 72 | + }, |
| 73 | + }, |
| 74 | +} satisfies Meta<Element>; |
| 75 | + |
| 76 | +export default meta; |
| 77 | +type Story = StoryObj<Element>; |
| 78 | +type GroupStory = StoryObj<GroupElement>; |
| 79 | + |
| 80 | +// More on writing stories with args: https://storybook.js.org/docs/writing-stories/args |
| 81 | +export const Default: Story = { |
| 82 | + args: { |
| 83 | + disabled: false, |
| 84 | + label: 'Checkbox', |
| 85 | + }, |
| 86 | +}; |
| 87 | + |
| 88 | +export const Disabled: Story = { |
| 89 | + args: { |
| 90 | + disabled: true, |
| 91 | + label: 'Checkbox', |
| 92 | + }, |
| 93 | +}; |
| 94 | + |
| 95 | +export const Group: GroupStory = { |
| 96 | + render: (args) => CheckboxGroupRender(args), |
| 97 | + args: { |
| 98 | + label: 'Checkbox Group', |
| 99 | + }, |
| 100 | +}; |
0 commit comments