Skip to content

Commit 12ebd74

Browse files
Support passing a Signal for Select initialValue prop.
Enable the `initialValue` to be re-set reactively in response to a signal changing. This is useful for providing 'reset form' functionality for example. For #12
1 parent 4591b5c commit 12ebd74

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,21 @@
88
displayed when there are no options available. Defaults to `No options`.
99
Thanks to [@raskyer](https://github.com/raskyer) for this contribution.
1010

11+
- The `initialValue` prop of the `Select` component can now be a Signal in order
12+
to support reactively re-setting the initial value of the component. This is
13+
useful for providing 'reset form' functionality for example.
14+
15+
```jsx
16+
const [initialValue, setInitialValue] = createSignal(null, { equals: false });
17+
18+
<Select
19+
initialValue={initialValue}
20+
options={["apple", "banana", "pear", "pineapple", "kiwi"]}
21+
/>
22+
23+
<button onClick={() => setInitialValue(null)}>Reset</button>
24+
```
25+
1126
## [0.9.0] - 2022-04-09
1227

1328
### Added

src/select.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
Component,
77
JSXElement,
88
createEffect,
9+
on,
910
} from "solid-js";
1011

1112
import {
@@ -52,7 +53,6 @@ const Select = (props: SelectProps) => {
5253
"options",
5354
"optionToValue",
5455
"isOptionDisabled",
55-
"initialValue",
5656
"multiple",
5757
"disabled",
5858
"onInput",
@@ -62,6 +62,14 @@ const Select = (props: SelectProps) => {
6262
);
6363
const select = createSelect(selectProps);
6464

65+
if (local.initialValue !== undefined) {
66+
if (typeof local.initialValue === "function") {
67+
createEffect(on(local.initialValue, (value) => select.setValue(value)));
68+
} else {
69+
select.setValue(local.initialValue);
70+
}
71+
}
72+
6573
return (
6674
<Container
6775
class={local.class}

0 commit comments

Comments
 (0)