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 SelectSearch #929

Merged
merged 5 commits into from
Nov 27, 2023
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { StoryFn, Meta } from "@storybook/react";
import React, { useState } from "react";
import { SelectSearch } from "@thunderstore/cyberstorm/src/components/SelectSearch/SelectSearch";

const meta = {
title: "Cyberstorm/Components/SelectSearch",
component: SelectSearch,
} as Meta<typeof SelectSearch>;

const options = [
"Team 1",
"Team 2",
"Team 3",
"Team 4",
"Team 5",
"Team 6",
"Team 7",
"Team 8",
"Team 9",
"Team 10",
"Team 11",
"Team 12",
"Team 13",
"Team 14",
];

const defaultArgs = {
placeholder: "Select something",
options: options,
};

const Template: StoryFn<typeof SelectSearch> = (args) => {
const [selected, setSelected] = useState<string | undefined>(undefined);
const defaultProps = {
...args,
onChange: setSelected,
value: selected,
};
return (
<div>
<div style={{ color: "white" }}>Value in state: {selected}</div>
<SelectSearch {...defaultProps} />
</div>
);
};

const GreenSelectSearch = Template.bind({});
GreenSelectSearch.args = {
...defaultArgs,
color: "green",
};

const RedSelectSearch = Template.bind({});
RedSelectSearch.args = {
...defaultArgs,
color: "red",
};

export { meta as default, GreenSelectSearch, RedSelectSearch };
43 changes: 43 additions & 0 deletions packages/cyberstorm-forms/src/components/FormSelectSearch.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"use client";

import { z, ZodObject, ZodRawShape } from "zod";
import { Path, useController } from "react-hook-form";
import { SelectSearch } from "@thunderstore/cyberstorm";
import styles from "./FormTextInput.module.css";

export type FormSelectSearchProps<
Schema extends ZodObject<Z>,
Z extends ZodRawShape
> = {
// The schema is required to allow TS to infer valid values for the name field
schema: Schema;
name: Path<z.infer<Schema>>;
placeholder?: string;
options: string[];
};
export function FormSelectSearch<
Schema extends ZodObject<Z>,
Z extends ZodRawShape
>({ name, placeholder, options }: FormSelectSearchProps<Schema, Z>) {
const {
field,
fieldState: { isDirty, invalid, error },
formState: { isSubmitting, disabled },
} = useController({ name });

return (
<>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to use div here? E.g. with flex the error span can get placed oddly. Just thinking aloud here, not sure if it's necessary.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant replacing this <> fragment with a div, not the <span> inside. Now all the potential problems apply to that <div> as well since it's going to be rendered as a sibling to the <SelectSearch>

<SelectSearch
{...field}
ref={field.ref}
placeholder={placeholder}
color={isDirty || invalid ? (invalid ? "red" : "green") : undefined}
disabled={isSubmitting || disabled}
options={options}
/>
{error && <span className={styles.errorMessage}>{error.message}</span>}
</>
);
}

FormSelectSearch.displayName = "FormSelectSearch";
3 changes: 1 addition & 2 deletions packages/cyberstorm-forms/src/components/FormTextInput.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"use client";

import { z, ZodObject } from "zod";
import { ZodRawShape } from "zod/lib/types";
import { z, ZodObject, ZodRawShape } from "zod";
import { Path, useController } from "react-hook-form";
import { TextInput } from "@thunderstore/cyberstorm";
import styles from "./FormTextInput.module.css";
Expand Down
1 change: 1 addition & 0 deletions packages/cyberstorm-forms/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { useFormToaster } from "./useFormToaster";
export { FormSubmitButton } from "./components/FormSubmitButton";
export { FormSelectSearch } from "./components/FormSelectSearch";
export { FormTextInput } from "./components/FormTextInput";
export { CreateTeamForm } from "./forms/CreateTeamForm";
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
.root {
position: relative;
display: flex;
flex-direction: column;
gap: var(--gap--16);
justify-content: flex-end;
width: auto;
min-height: 6rem;
color: var(--color-text--tertiary);
}

.selected {
display: flex;
flex-flow: row wrap;
gap: var(--space--8);
}

.search {
position: relative;
display: flex;
flex-direction: column;
width: auto;
height: 2.75rem;
color: var(--color-text--tertiary);
}

.inputContainer {
display: flex;
flex-direction: row;
align-items: center;
width: 100%;
border: var(--border-width--2) solid var(--border-color);

border-radius: var(--border-radius--8);
background-color: var(--color-surface--4);

--border-color: transparent;

transition: ease-out 300ms;
}

.input {
width: 100%;
margin: var(--space--10) var(--space--14);
font-weight: var(--font-weight-medium);
font-size: var(--font-size--l);
line-height: normal;
background-color: transparent;
}

.inputContainer:hover {
--border-color: var(--color-border--highlight);
}

.inputContainer:focus-within {
color: var(--color-text--default);
background-color: var(--color-black);

--border-color: var(--color-border--highlight);
}

.input::placeholder {
color: var(--color-text--tertiary);
}

.inputContainer[data-color="red"] {
--border-color: var(--color-red--5);
}

.inputContainer[data-color="red"]:hover {
--border-color: var(--color-red--3);
}

.inputContainer[data-color="green"] {
--border-color: var(--color-cyber-green--50);
}

.inputContainer[data-color="green"]:hover {
--border-color: var(--color-cyber-green--80);
}

.clearSearch {
width: 3rem;
height: 100%;
color: #c6c3ff;
background: transparent;
opacity: 0.5;
}

.showMenuButton {
width: 3rem;
height: 100%;
color: #9c9cc4;
background: transparent;
}

.inputButtonDivider {
width: 0.063rem;
height: 1.375rem;
background: #4343a3;
}

.menu {
position: absolute;
top: 3.25rem;
z-index: 9999;
display: flex;
flex-direction: column;
gap: var(--gap--4);
width: 100%;
min-height: 1.5rem;
max-height: 12rem;
padding: var(--space--8) 0;
border: var(--space--px) var(--color-surface--6) solid;
border-radius: var(--border-radius--8);
overflow: hidden;
overflow-y: auto !important;
color: var(--text-color);
background-color: var(--color-surface--2);
box-shadow: var(--box-shadow-default);
visibility: hidden;

--text-color: var(--color-white);
--bg-color: var(--color-surface--4);
}

.menu:where(.visible) {
visibility: visible;
}

.menuLabel {
font-weight: var(--font-weight-medium);
}

.multiSelectItemWrapper {
padding: var(--space--12) var(--space--16);
}

.multiSelectItemWrapper:focus {
background-color: var(--color-surface--6);
}
Loading
Loading