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

add vira-dropdown to vira package #39

Merged
merged 6 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions cspell.config.cjs
electrovir marked this conversation as resolved.
Show resolved Hide resolved
electrovir marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ module.exports = {
words: [
...baseConfig.words,
'observavir',
'listbox',
],
};
10 changes: 8 additions & 2 deletions packages/vira-book/src/element-book/all-element-book-entries.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import {elementsBookPage} from './elements.book';
import {ViraDropdownItemPage} from './entries/dropdown/vira-dropdown-item.element.book';
import {dropdownPage} from './entries/dropdown/vira-dropdown.book';
import {viraDropdownPage} from './entries/dropdown/vira-dropdown.element.book';
import {iconsBookPage} from './entries/icons.book';
import {viraButtonBookPage} from './entries/vira-button.element.book';
import {viraCollapsibleBookPage} from './entries/vira-collapsible-wrapper.element.book';
Expand All @@ -9,12 +12,15 @@ import {viraLinkBookPage} from './entries/vira-link.element.book';

export const allElementBookEntries = [
elementsBookPage,

iconsBookPage,
dropdownPage,

viraButtonBookPage,
viraCollapsibleBookPage,
ViraDropdownItemPage,
viraDropdownPage,
viraIconBookPage,
viraImageBookPage,
viraInputBookPage,
viraLinkBookPage,
];
].sort((a, b) => a.title.localeCompare(b.title));
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import {BookPageControlTypeEnum, defineBookPage, definePageControl} from 'element-book';
electrovir marked this conversation as resolved.
Show resolved Hide resolved
import {CSSResult, HTMLTemplateResult, html} from 'element-vir';
import {ViraDropdownItem} from 'vira';
import {dropdownPage} from './vira-dropdown.book';

const examples: ReadonlyArray<{
electrovir marked this conversation as resolved.
Show resolved Hide resolved
title: string;
inputs?: typeof ViraDropdownItem.inputsType;
customStyle?: CSSResult;
electrovir marked this conversation as resolved.
Show resolved Hide resolved
customTemplate?: HTMLTemplateResult;
}> = [
{
title: 'unselected',
inputs: {
label: 'my label',
selected: false,
},
},
{
title: 'selected',
inputs: {
label: 'my label',
selected: true,
},
},
{
title: 'with custom child',
inputs: {
label: 'custom child',
selected: true,
},
customTemplate: html`
<b>This is custom</b>
`,
},
];

export const ViraDropdownItemPage = defineBookPage({
title: ViraDropdownItem.tagName,
parent: dropdownPage,
controls: {
Selected: definePageControl({
controlType: BookPageControlTypeEnum.Dropdown,
initValue: '',
options: [
'',
'all',
'none',
],
}),
Label: definePageControl({
controlType: BookPageControlTypeEnum.Text,
initValue: '',
}),
},
elementExamplesCallback({defineExample}) {
examples.forEach((example) => {
defineExample({
title: example.title,
stateInitStatic: {
selected: example.inputs?.selected || [],
},
renderCallback({controls}) {
const finalInputs: typeof ViraDropdownItem.inputsType = {
label: controls.Label || example.inputs?.label || '',
selected: controls.Selected
? controls.Selected === 'all'
: !!example.inputs?.selected,
};

if (example.customTemplate) {
return html`
<${ViraDropdownItem.assign(finalInputs)}>
${example.customTemplate}
</${ViraDropdownItem}>
`;
} else {
return html`
<${ViraDropdownItem.assign(finalInputs)}></${ViraDropdownItem}>
`;
}
},
});
});
},
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {defineBookPage} from 'element-book';
import {elementsBookPage} from '../../elements.book';

export const dropdownPage = defineBookPage({
parent: elementsBookPage,
title: 'Dropdown',
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
import {isTruthy} from '@augment-vir/common';
import {BookPageControlTypeEnum, defineBookPage, definePageControl} from 'element-book';
import {CSSResult, html, listen} from 'element-vir';
import {ViraDropdown, ViraDropdownOption, allIconsByName} from 'vira';
import {dropdownPage} from './vira-dropdown.book';

const exampleDropdownOptions: ReadonlyArray<Readonly<ViraDropdownOption>> = [
{
label: 'Option 1',
id: 1,
},
{
label: 'Option 2',
id: 2,
},
{
label: 'Option 3',
id: 3,
},
{
label: 'Really really super duper long option',
id: 4,
},
{
label: 'Really really super duper long option',
id: 5,
},
{
label: 'Really really super duper long option',
id: 6,
},
{
label: 'Really really super duper long option',
id: 7,
},
{
label: "Really really super duper long it just keeps going because it's so long option",
id: 8,
},
];

const examples: ReadonlyArray<{
title: string;
inputs?: Partial<typeof ViraDropdown.inputsType>;
customStyle?: CSSResult;
}> = [
{
title: 'default',
},
{
title: 'disabled',
inputs: {
isDisabled: true,
},
},
{
title: 'multi select',
inputs: {
isMultiSelect: true,
},
},
{
title: 'long selection',
inputs: {
selected: [8],
},
},
{
title: 'with custom template',
inputs: {
selected: [8],
options: [
...exampleDropdownOptions,
{
id: 42,
label: 'custom template',
template: html`
<select>
<option selected>NESTED SELECT!!!</option>
<option>this is a terrible idea</option>
<option>pls don't do this</option>
</select>
`,
},
],
},
},
{
title: 'with disabled item',
inputs: {
selected: [8],
options: [
...exampleDropdownOptions,
{
id: 42,
label: 'this is disabled',
disabled: true,
},
],
},
},
// {
electrovir marked this conversation as resolved.
Show resolved Hide resolved
// title: 'forced open',
// inputs: {
// z_debug_forceOpenState: true,
// selected: [1],
// },
// },
];

export const viraDropdownPage = defineBookPage({
title: ViraDropdown.tagName,
parent: dropdownPage,
controls: {
Selected: definePageControl({
controlType: BookPageControlTypeEnum.Dropdown,
initValue: '',
options: [
'',
...exampleDropdownOptions.map((option) => option.label),
],
}),
Prefix: definePageControl({
controlType: BookPageControlTypeEnum.Text,
initValue: '',
}),
'Force State': definePageControl({
controlType: BookPageControlTypeEnum.Dropdown,
options: [
'',
'force open',
'force closed',
],
initValue: '',
}),
'Multi Select': definePageControl({
controlType: BookPageControlTypeEnum.Dropdown,
options: [
'',
'all',
'none',
],
initValue: '',
}),
Icon: definePageControl({
controlType: BookPageControlTypeEnum.Dropdown,
initValue: '',
options: [
'',
...Object.keys(allIconsByName),
],
}),
Disabled: definePageControl({
controlType: BookPageControlTypeEnum.Dropdown,
options: [
'',
'all',
'none',
],
initValue: '',
}),
},
elementExamplesCallback({defineExample}) {
examples.forEach((example) => {
defineExample({
title: example.title,
stateInitStatic: {
selected: example.inputs?.selected || [],
},
renderCallback({state, updateState, controls}) {
const finalInputs: typeof ViraDropdown.inputsType = {
options: example.inputs?.options || exampleDropdownOptions,
selected: controls.Selected
? [
exampleDropdownOptions.find(
(option) => option.label === controls.Selected,
)?.id,
].filter(isTruthy)
: state.selected,
buttonPrefix: controls.Prefix || example.inputs?.buttonPrefix,
isDisabled: controls.Disabled
? controls.Disabled === 'all'
: example.inputs?.isDisabled,
icon: controls.Icon
? allIconsByName[controls.Icon as keyof typeof allIconsByName]
: example.inputs?.icon,
isMultiSelect: controls['Multi Select']
? controls['Multi Select'] === 'all'
: example.inputs?.isMultiSelect,
z_debug_forceOpenState: controls['Force State']
? controls['Force State'] === 'force open'
: example.inputs?.z_debug_forceOpenState,
};

return html`
<${ViraDropdown.assign(finalInputs)}
${listen(ViraDropdown.events.selectedChange, (event) => {
updateState({selected: event.detail});
})}
></${ViraDropdown}>
`;
},
});
});
},
});
2 changes: 2 additions & 0 deletions packages/vira/src/elements/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
export * from './define-vira-element';
export * from './vira-button.element';
export * from './vira-collapsible-wrapper.element';
export * from './vira-dropdown-item.element';
export * from './vira-dropdown.element';
export * from './vira-icon.element';
export * from './vira-image.element';
export * from './vira-input.element';
Expand Down
Loading
Loading