Skip to content

Commit

Permalink
Added the search component (#52)
Browse files Browse the repository at this point in the history
# Pull Request

## 📖 Description

<!--- Provide some background and a description of your work. -->
Added the search foundation component.

This change also specifies an entry point for typings in the adaptive UI package

### 🎫 Issues

<!---
List and link relevant issues here using the keyword "closes"
if this PR will close an issue, eg. closes #411
-->
Continues with work on #31

## ✅ Checklist

### General

<!--- Review the list and put an x in the boxes that apply. -->

- [x] I have added tests for my changes.
- [x] I have tested my changes.
- [x] I have updated the project documentation to reflect my changes.

## ⏭ Next Steps

<!---
If there is relevant follow-up work to this PR, please list any existing issues or provide brief descriptions of what you would like to do next.
-->
- Continue adding foundation components
  • Loading branch information
janechu committed May 20, 2022
1 parent 0535d57 commit 7821e47
Show file tree
Hide file tree
Showing 15 changed files with 526 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "none",
"comment": "Added the search component",
"packageName": "@microsoft/fast-cli",
"email": "7559015+janechu@users.noreply.github.com",
"dependentChangeType": "none"
}
43 changes: 43 additions & 0 deletions packages/fast-cli/src/cli.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -511,5 +511,48 @@ test.describe("CLI", () => {
).not.toThrow();
});
});
test.describe("search", () => {
test.beforeAll(() => {
setup();
execSync(`cd ${tempDir} && npm run fast:init`);
setup();
execSync(`cd ${tempDir} && npm run fast:add-foundation-component:search`);
});
test.afterAll(() => {
teardown();
});
test("should copy files from the template", () => {
let files: Array<string> = [];

function testGeneratedFiles(folderName: string) {
const tempDirContents = fs.readdirSync(path.resolve(tempDir, "src/components/test-component", folderName));
const tempDirContentsWithFileTypes = fs.readdirSync(path.resolve(tempDir, "src/components/test-component", folderName), {
withFileTypes: true
});

for (let i = 0, contentLength = tempDirContents.length; i < contentLength; i++) {
if (tempDirContentsWithFileTypes[i].isDirectory()) {
testGeneratedFiles(tempDirContents[i]);
} else {
files.push(
folderName
? `${folderName}/${tempDirContents[i]}`
: tempDirContents[i]
);
}
}
}

testGeneratedFiles("");
expect(files).toEqual(expectedGeneratedComponentTemplateFiles);
});
test("should be able to run the build", () => {
expect(
() => {
execSync(`cd ${tempDir} && npm run build`);
}
).not.toThrow();
});
});
});
});
1 change: 1 addition & 0 deletions packages/fast-cli/src/components/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export const availableTemplates = [
"dialog",
"disclosure",
"number-field",
"search",
]
3 changes: 3 additions & 0 deletions packages/fast-cli/src/components/search/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Description

This template is used as a search component template from which a user can modify without having to do the initial scaffolding.
8 changes: 8 additions & 0 deletions packages/fast-cli/src/components/search/template/README.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { ComponentTemplateConfig } from "../../../utilities/template";

export default (config: ComponentTemplateConfig): string => `
# ${config.className}
An implementation of a [search](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input/search) as a form-connected web-component.
For more information on the building blocks used to create this component, please refer to https://www.fast.design/docs/components/search`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { ComponentTemplateConfig } from "../../../utilities/template";

export default (config: ComponentTemplateConfig): string =>
`import { template } from "./${config.tagName}.template.js";
import { styles } from "./${config.tagName}.styles.js";
export const definition = {
baseName: "${config.tagName}",
template,
styles,
};`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type { ComponentTemplateConfig } from "../../../utilities/template";

export default (config: ComponentTemplateConfig): string => `
import { expect, test } from "@playwright/test";
import { fixtureURL } from "@microsoft/fast-cli/dist/esm/utilities/playwright.js";
test.describe("${config.tagName}", () => {
const fixture = fixtureURL("${config.tagName}");
test.beforeEach(async ({ page }) => {
await page.goto(fixture);
});
test("should load the fixture URL", async ({ page }) => {
const pageUrl = page.url();
expect(pageUrl).toBe(\`http://localhost:3000/\${fixture}\`);
});
test("should contain the component in the URL", async ({ page }) => {
const element = page.locator("${config.tagName}");
await expect(element).not.toBeNull();
});
});
`
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { ComponentTemplateConfig } from "../../../utilities/template";

export default (config: ComponentTemplateConfig): string => `
import Template from "./fixtures/base.html";
import "./define.js";
export default {
title: "${config.tagName}",
};
export const ${config.className} = () => Template;
`;
260 changes: 260 additions & 0 deletions packages/fast-cli/src/components/search/template/component.styles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,260 @@
import type { ComponentTemplateConfig } from "../../../utilities/template";

export default (config: ComponentTemplateConfig): string =>
`import { css, ElementStyles } from "@microsoft/fast-element";
import {
DesignToken,
disabledCursor,
display,
focusVisible,
forcedColorsStylesheetBehavior,
FoundationElementTemplate,
TextFieldOptions,
} from "@microsoft/fast-foundation";
import { SystemColors } from "@microsoft/fast-web-utilities";
import {
accentFillActive,
accentFillHover,
accentFillRest,
bodyFont,
controlCornerRadius,
density,
designUnit,
disabledOpacity,
fillColor,
focusStrokeOuter,
heightNumber,
neutralFillHover,
neutralFillInputHover,
neutralFillInputRest,
neutralFillRecipe,
neutralFillStealthActive,
neutralFillStealthHover,
neutralFillStealthRecipe,
neutralForegroundRest,
neutralStrokeRest,
strokeWidth,
typeRampBaseFontSize,
typeRampBaseLineHeight,
} from "@microsoft/adaptive-ui";
import type { Swatch } from "@microsoft/adaptive-ui/dist/dts/color/swatch.d.js"
const clearButtonHover = DesignToken.create<Swatch>("clear-button-hover").withDefault(
(target: HTMLElement) => {
const buttonRecipe = neutralFillStealthRecipe.getValueFor(target);
const inputRecipe = neutralFillRecipe.getValueFor(target);
return buttonRecipe.evaluate(target, inputRecipe.evaluate(target).hover).hover;
}
);
const clearButtonActive = DesignToken.create<Swatch>("clear-button-active").withDefault(
(target: HTMLElement) => {
const buttonRecipe = neutralFillStealthRecipe.getValueFor(target);
const inputRecipe = neutralFillRecipe.getValueFor(target);
return buttonRecipe.evaluate(target, inputRecipe.evaluate(target).hover).active;
}
);
/**
* Styles for ${config.className}
* @public
*/
export const styles: FoundationElementTemplate<ElementStyles> = (
context,
definition
) =>
css\`
\${display("inline-block")} :host {
font-family: \${bodyFont};
outline: none;
user-select: none;
}
.root {
box-sizing: border-box;
position: relative;
display: flex;
flex-direction: row;
color: \${neutralForegroundRest};
background: \${neutralFillInputRest};
border-radius: calc(\${controlCornerRadius} * 1px);
border: calc(\${strokeWidth} * 1px) solid \${accentFillRest};
height: calc(\${heightNumber} * 1px);
align-items: baseline;
}
.control {
-webkit-appearance: none;
font: inherit;
background: transparent;
border: 0;
color: inherit;
height: calc(100% - 4px);
width: 100%;
margin-top: auto;
margin-bottom: auto;
border: none;
padding: 0 calc(\${designUnit} * 2px + 1px);
font-size: \${typeRampBaseFontSize};
line-height: \${typeRampBaseLineHeight};
}
.control::-webkit-search-cancel-button {
-webkit-appearance: none;
}
.control:hover,
.control:\${focusVisible},
.control:disabled,
.control:active {
outline: none;
}
.clear-button {
height: calc(100% - 2px);
opacity: 0;
margin: 1px;
background: transparent;
color: \${neutralForegroundRest};
fill: currentcolor;
border: none;
border-radius: calc(\${controlCornerRadius} * 1px);
min-width: calc(\${heightNumber} * 1px);
font-size: \${typeRampBaseFontSize};
line-height: \${typeRampBaseLineHeight};
outline: none;
font-family: \${bodyFont};
padding: 0 calc((10 + (\${designUnit} * 2 * \${density})) * 1px);
}
.clear-button:hover {
background: \${neutralFillStealthHover};
}
.clear-button:active {
background: \${neutralFillStealthActive};
}
:host([appearance="filled"]) .clear-button:hover {
background: \${clearButtonHover};
}
:host([appearance="filled"]) .clear-button:active {
background: \${clearButtonActive};
}
.input-wrapper {
display: flex;
position: relative;
width: 100%;
height: 100%;
}
.label {
display: block;
color: \${neutralForegroundRest};
cursor: pointer;
font-size: \${typeRampBaseFontSize};
line-height: \${typeRampBaseLineHeight};
margin-bottom: 4px;
}
.label__hidden {
display: none;
visibility: hidden;
}
.input-wrapper,
.start,
.end {
align-self: center;
}
.start,
.end {
display: flex;
margin: 1px;
fill: currentcolor;
}
::slotted([slot="end"]) {
height: 100%
}
.end {
margin-inline-end: 1px;
height: calc(100% - 2px);
}
::slotted(svg) {
/* TODO: adaptive typography https://github.com/microsoft/fast/issues/2432 */
width: 16px;
height: 16px;
margin-inline-end: 11px;
margin-inline-start: 11px;
margin-top: auto;
margin-bottom: auto;
}
:host(:hover:not([disabled])) .root {
background: \${neutralFillInputHover};
border-color: \${accentFillHover};
}
:host(:active:not([disabled])) .root {
background: \${neutralFillInputHover};
border-color: \${accentFillActive};
}
:host(:focus-within:not([disabled])) .root {
border-color: \${focusStrokeOuter};
box-shadow: 0 0 0 1px \${focusStrokeOuter} inset;
}
.clear-button__hidden {
opacity: 0;
}
:host(:hover:not([disabled], [readOnly])) .clear-button,
:host(:active:not([disabled], [readOnly])) .clear-button,
:host(:focus-within:not([disabled], [readOnly])) .clear-button {
opacity: 1;
}
:host(:hover:not([disabled], [readOnly])) .clear-button__hidden,
:host(:active:not([disabled], [readOnly])) .clear-button__hidden,
:host(:focus-within:not([disabled], [readOnly])) .clear-button__hidden {
opacity: 0;
}
:host([appearance="filled"]) .root {
background: \${fillColor};
}
:host([appearance="filled"]:hover:not([disabled])) .root {
background: \${neutralFillHover};
}
:host([disabled]) .label,
:host([readonly]) .label,
:host([readonly]) .control,
:host([disabled]) .control {
cursor: \${disabledCursor};
}
:host([disabled]) {
opacity: \${disabledOpacity};
}
:host([disabled]) .control {
border-color: \${neutralStrokeRest};
}
\`.withBehaviors(
forcedColorsStylesheetBehavior(
css\`
.root,
:host([appearance="filled"]) .root {
forced-color-adjust: none;
background: \${SystemColors.Field};
border-color: \${SystemColors.FieldText};
}
:host(:hover:not([disabled])) .root,
:host([appearance="filled"]:hover:not([disabled])) .root,
:host([appearance="filled"]:hover) .root {
background: \${SystemColors.Field};
border-color: \${SystemColors.Highlight};
}
.start,
.end {
fill: currentcolor;
}
:host([disabled]) {
opacity: 1;
}
:host([disabled]) .root,
:host([appearance="filled"]:hover[disabled]) .root {
border-color: \${SystemColors.GrayText};
background: \${SystemColors.Field};
}
:host(:focus-within:enabled) .root {
border-color: \${SystemColors.Highlight};
box-shadow: 0 0 0 1px \${SystemColors.Highlight} inset;
}
input::placeholder {
color: \${SystemColors.GrayText};
}
\`
)
);`;

0 comments on commit 7821e47

Please sign in to comment.