All files / packages/app/features/playground inputs-showcase.tsx

0% Statements 0/45
0% Branches 0/8
0% Functions 0/22
0% Lines 0/41

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206                                                                                                                                                                                                                                                                                                                                                                                                                           
import React from "react";
 
import { Autocomplete } from "~components/autocomplete";
import { Button } from "~components/button";
import { Checkbox } from "~components/checkbox";
import { DateInput } from "~components/date-input";
import { FileInput } from "~components/file-input";
import { Form } from "~components/form";
import { Icon } from "~components/icons";
import { Input } from "~components/input";
import { NumberInput } from "~components/number-input";
import { Slider } from "~components/slider";
import { Text } from "~components/text";
import { View } from "~components/view";
 
import { Group, Section } from "./showcase-section";
 
const ITEMS = ["Apple", "Banana", "Cherry"];
 
const AutocompleteShowcase = () => {
	const [inputValue, setInputValue] = React.useState("");
	const [selectedKey, setSelectedKey] = React.useState<string | null>(null);
	return (
		<Section title="Autocomplete">
			<Autocomplete
				label="Fruit"
				placeholder="Search fruit"
				emptyContent="No fruit found"
				inputValue={inputValue}
				onInputChange={setInputValue}
				selectedKey={selectedKey}
				onSelectionChange={setSelectedKey}
				onClear={() => {
					setInputValue("");
					setSelectedKey(null);
				}}
			>
				{[
					{
						key: "fruit",
						title: "Fruit",
						items: ITEMS.map((item) => ({
							key: item,
							textValue: item,
							children: <Text>{item}</Text>,
						})),
					},
				]}
			</Autocomplete>
			<Text>Selected: {selectedKey ?? "none"}</Text>
		</Section>
	);
};
 
const CheckboxShowcase = () => {
	const [selected, setSelected] = React.useState<Record<string, boolean>>({
		primary: true,
	});
	return (
		<Section title="Checkbox">
			<View className="flex flex-row flex-wrap gap-3">
				{(["default", "primary", "success", "warning", "danger"] as const).map(
					(color) => (
						<Checkbox
							key={color}
							color={color}
							isSelected={Boolean(selected[color])}
							onValueChange={(value) =>
								setSelected((previous) => ({ ...previous, [color]: value }))
							}
						>
							{color}
						</Checkbox>
					),
				)}
				<Checkbox isIndeterminate>Indeterminate</Checkbox>
				<Checkbox isDisabled>Disabled</Checkbox>
				<Checkbox icon={<Icon name="check" className="size-4" />}>
					Custom icon
				</Checkbox>
			</View>
		</Section>
	);
};
 
const DateInputShowcase = () => {
	const [date, setDate] = React.useState<Temporal.PlainDate | undefined>();
	return (
		<Section title="DateInput">
			<DateInput value={date} onValueChange={setDate} />
			<Text>Selected: {date?.toString() ?? "none"}</Text>
		</Section>
	);
};
 
const FileInputShowcase = () => {
	const clickRef = React.useRef<() => void>(() => undefined);
	const [image, setImage] = React.useState("");
	return (
		<Section title="FileInput">
			<FileInput onClickRef={clickRef} onFileUpdate={setImage} />
			<Button onPress={() => clickRef.current()}>Choose an image</Button>
			<Text>{image ? "File loaded" : "No file selected"}</Text>
		</Section>
	);
};
 
const FormShowcase = () => {
	const [submits, setSubmits] = React.useState(0);
	const formId = React.useId();
	return (
		<Section title="Form">
			<Form id={formId} onSubmit={() => setSubmits((count) => count + 1)}>
				<Button type="submit">Submit inside form</Button>
			</Form>
			<Button type="submit" form={formId}>
				Submit outside form
			</Button>
			<Text>Submissions: {submits}</Text>
		</Section>
	);
};
 
const InputShowcase = () => {
	const [values, setValues] = React.useState<Record<string, string>>({});
	return (
		<Section title="Input">
			{(["bordered", "flat"] as const).map((variant) =>
				(
					[
						"default",
						"primary",
						"secondary",
						"success",
						"warning",
						"danger",
					] as const
				).map((color) => {
					const key = `${variant}-${color}`;
					return (
						<Input
							key={key}
							label={`${color} / ${variant}`}
							variant={variant}
							color={color}
							placeholder="Type here"
							value={values[key] ?? ""}
							isClearable
							onValueChange={(value) =>
								setValues((previous) => ({ ...previous, [key]: value }))
							}
						/>
					);
				}),
			)}
			<Input label="Disabled" isDisabled />
			<Input label="With description" description="Example description" />
		</Section>
	);
};
 
const NumberInputShowcase = () => {
	const [value, setValue] = React.useState(0);
	return (
		<Section title="NumberInput">
			<NumberInput
				value={value}
				onValueChange={setValue}
				fractionDigits={2}
				minValue={0}
			/>
			<Text>Value: {value}</Text>
		</Section>
	);
};
 
const SliderShowcase = () => {
	const [value, setValue] = React.useState(5);
	return (
		<Section title="Slider">
			<Slider
				label="Amount"
				minValue={0}
				maxValue={10}
				step={1}
				value={value}
				onChange={setValue}
			/>
			<Text>Value: {value}</Text>
		</Section>
	);
};
 
export const InputsShowcase = () => (
	<Group title="Inputs">
		<AutocompleteShowcase />
		<CheckboxShowcase />
		<DateInputShowcase />
		<FileInputShowcase />
		<FormShowcase />
		<InputShowcase />
		<NumberInputShowcase />
		<SliderShowcase />
	</Group>
);