@solid-primitives/controlled-props
Version:
The primitives in this package allow you to create controlls for component props.
113 lines (88 loc) • 3.35 kB
Markdown
<p>
<img width="100%" src="https://assets.solidjs.com/banner?type=Primitives&background=tiles&project=Controlled%20Props" alt="Solid Primitives Controlled Props">
</p>
[](https://bundlephobia.com/package/@solid-primitives/controlled-props)
[](https://www.npmjs.com/package/@solid-primitives/controlled-props)
[](https://github.com/solidjs-community/solid-primitives#contribution-process)
Library of helpers for creating your own component prototyping tool. _(a Storybook alternative if you will)_ The primitives in this package allow you to create controlls for component props.
```bash
npm install @solid-primitives/controlled-props
yarn add @solid-primitives/controlled-props
pnpm add @solid-primitives/controlled-props
```
If you are using [solid-start](https://github.com/solidjs/solid-start) with SSR, you may see this error comming form the `@solid-primitives/props` package.
```
TypeError: web.template is not a function
```
To prevent this, add `"@solid-primitives/props"` entry to `noExternal` field in your vite config, like so:
```tsx
export default defineConfig({
plugins: [solid()],
ssr: {
// It allows Vite to preprocess the package
noExternal: ["@solid-primitives/props"],
},
});
```
Primitive that provides controllable props signals like knobs/controls for simple component testing
You can either create a single prop:
```ts
// Second argument can be initialValue for boolean, number, string:
const [string, setString, stringField] = createControlledProp("stringValue", "test");
// Arrays or enums can be provided in an options object:
const [language, setLanguage, languageField] = createControlledProp(
"language",
{ initialValue: "en", options: ["de", "en", "fr", "it"] as const }
// If you want your array to be able to influence the setter/getter types, use `as const`.
);
enum Currency {
AUD,
GBP,
EUR,
USD,
CHF,
JPY,
CNY
}
const [currency, setCurrency, currencyField] = createControlledProp("currency", {
initialValue: Currency.USD,
options: Currency
});
return { languageField(); };
```
or multiple props in one call:
```ts
enum Test { One, Two, Three };
const languages = ['de', 'en', 'fr', 'it'] as const;
const [props, fields] = createControlledProps({
boolean: true,
number: 42,
string: 'text',
array: { initialValue: 'en', options: languages },
enum: { initialValue: Test.Three, options: Test }
});
props == {
boolean: Accessor<boolean>,
setBoolean: Setter<boolean>,
number: Accessor<number>,
setNumber: Setter<number>,
string: Accessor<string>,
setString: Setter<string>,
array: Accessor<string>,
setArray: Setter<string>,
enum: Accessor<Test>,
setEnum: Setter<Test>
};
fields == JSX.Element[];
```
https://primitives.solidjs.community/playground/controlled-props/
See [CHANGELOG.md](./CHANGELOG.md)