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(DSW-2774) add select option and option group #2278

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
6 changes: 6 additions & 0 deletions .changeset/hip-trees-worry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@justeattakeaway/pie-select": minor
"pie-storybook": minor
---

[Added] - pie select option
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ build/**
dist/
custom-elements.json
**/src/react.ts
**/src/**/react.ts
**/stats.html

# Yalc
Expand Down
17 changes: 10 additions & 7 deletions apps/pie-storybook/stories/pie-select.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,19 @@ const Template = ({
?disabled="${disabled}"
size="${ifDefined(size)}"
assistiveText="${ifDefined(assistiveText)}"
status=${ifDefined(status)}>
${showLeadingIcon ? html`<icon-placeholder slot="leadingIcon"></icon-placeholder>` : nothing}
status=${ifDefined(status)}>
${showLeadingIcon ? html`<icon-placeholder slot="leadingIcon"></icon-placeholder>` : nothing}
<pie-select-option>Option 1</pie-select-option>
<pie-select-option>Option 2</pie-select-option>
<pie-select-option>Option 3</pie-select-option>
</pie-select>
`;
`;

const WithLabelTemplate: TemplateFunction<SelectProps> = (props: SelectProps) => html`
<p>Please note, the label is a separate component. See <pie-link href="/?path=/story/form-label">pie-form-label</pie-link>.</p>
<pie-form-label for="${ifDefined(props.name)}">Label</pie-form-label>
${Template(props)}
`;
<p>Please note, the label is a separate component. See <pie-link href="/?path=/story/form-label">pie-form-label</pie-link>.</p>
<pie-form-label for="${ifDefined(props.name)}">Label</pie-form-label>
${Template(props)}
`;

export const Default = createStory<SelectProps>(Template, defaultArgs)();
export const Labelled = createStory<SelectProps>(WithLabelTemplate, defaultArgs)();
17 changes: 14 additions & 3 deletions packages/components/pie-select/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,22 @@ export class PieSelect extends RtlMixin(LitElement) implements SelectProps {
@state()
private _hasLeadingIcon = false;

private _handleDefaultSlotChange () {
this.requestUpdate();
}

private _handleLeadingIconSlotchange () {
this._hasLeadingIcon = Boolean(this._leadingIconSlot.length);
}

private renderItems () {
return html`
${[...this.children]
.filter((el) => el.matches('pie-select-option'))
.map(({ textContent }) => html`<option>${textContent}</option>`)}
`;
}

private renderAssistiveText () {
if (!this.assistiveText) {
return nothing;
Expand Down Expand Up @@ -113,11 +125,10 @@ export class PieSelect extends RtlMixin(LitElement) implements SelectProps {
aria-describedby=${ifDefined(assistiveText ? assistiveTextIdValue : undefined)}
aria-invalid=${status === 'error' ? 'true' : 'false'}
aria-errormessage="${ifDefined(status === 'error' ? assistiveTextIdValue : undefined)}">
<option value="dog">Dog</option>
<option value="cat">Cat</option>
<option value="hamster">Hamster</option>
${this.renderItems()}
</select>
<icon-chevron-down size='s' class='c-select-trailingIcon'></icon-chevron-down>
<slot @slotchange=${this._handleDefaultSlotChange} style="display: none"></slot>
</div>
${this.renderAssistiveText()}
`;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import type React from 'react';

export type ReactBaseType = React.HTMLAttributes<HTMLOptionElement>
19 changes: 19 additions & 0 deletions packages/components/pie-select/src/pie-select-option/defs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { type ComponentDefaultProps } from '@justeattakeaway/pie-webc-core';

export interface SelectOptionProps {
/**
* Same as the HTML disabled attribute - indicates whether the select is disabled.
*/
disabled?: boolean;
/**
* The value of the select option (used as a key/value pair in HTML forms with `name`).
*/
value: string;
}

type SelectOptionDefaultProps = ComponentDefaultProps<SelectOptionProps>;

export const selectOptionDefaultProps: SelectOptionDefaultProps = {
disabled: false,
value: '',
};
25 changes: 25 additions & 0 deletions packages/components/pie-select/src/pie-select-option/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { LitElement } from 'lit';
import { property } from 'lit/decorators.js';
import { defineCustomElement } from '@justeattakeaway/pie-webc-core';
import { selectOptionDefaultProps, type SelectOptionProps } from './defs';

const componentSelector = 'pie-select-option';

/**
* @tagname pie-select-option
*/
export class PieSelectOption extends LitElement implements SelectOptionProps {
@property({ type: Boolean })
public disabled = selectOptionDefaultProps.disabled;

@property({ type: String })
public value = selectOptionDefaultProps.value;
}

defineCustomElement(componentSelector, PieSelectOption);

declare global {
interface HTMLElementTagNameMap {
[componentSelector]: PieSelectOption;
}
}
60 changes: 58 additions & 2 deletions packages/components/pie-select/vite.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,59 @@
import viteConfig from '@justeattakeaway/pie-components-config/vite.config';
import { defineConfig } from 'vite';
import dts from 'vite-plugin-dts';
import { visualizer } from 'rollup-plugin-visualizer';

import { deepmerge } from 'deepmerge-ts';

// https://vitejs.dev/config/
const sharedConfig = ({ build = {}, plugins = [], ...rest }) => defineConfig({
build: deepmerge({
lib: {
entry: {
index: 'src/index.ts',
react: 'src/react.ts',
'pie-select-option/index': 'src/pie-select-option/index.ts',
'pie-select-option/react': 'src/pie-select-option/react.ts',
},
formats: ['es'],
},
rollupOptions: {
external: (id) => {
if (['react', '@lit/react'].includes(id) || /^lit/.test(id)) {
return true;
}

if (id.startsWith('@justeattakeaway/pie-')) {
console.info(`Excluding ${id} from the bundle`);
return true;
}

return false;
},
},
}, build),
test: {
dir: '.',
environment: 'jsdom',
globals: true,
include: [
'./src/__tests__/**/*.{spec,test}.{js,ts}',
'./test/unit/**/*.{spec,test}.{js,ts}',
],
exclude: ['**/node_modules/**'],
},
plugins: deepmerge([dts({
insertTypesEntry: true,
outputDir: 'dist',
entryRoot: 'src',
rollupTypes: false,
}),
visualizer({
gzipSize: true,
brotliSize: true,
})], plugins),

...rest,
});

export default sharedConfig;

export default viteConfig;
Loading