S
SolidJS•13mo ago
akerbeltz

checkbox controlled from outside

I want to control a checkbox from the outside and expose either a function onChange and the props.checked that will be reactive. import { createSignal } from 'solid-js'; import c from './Switch.module.scss'; function SwitchButton(props) { return ( <div style={{ display: 'flex', }} > <div style={{ 'align-self': 'center' }}>{props.label1}</div> <label class={c.switch}> <input type={'checkbox'} class={c.input} checked={props.checked} onChange={(e) => props?.onChange?.(e)} /> <span class={c.slider}></span> </label> <div style={{ 'align-self': 'center' }}>{props.label2}</div> </div> ); } export default SwitchButton;
19 Replies
high1
high1•13mo ago
The code looks OK. In the parent component, which controls the checkbox, a signal could control it like this:
const [isChecked, setChecked] = createSignal(false);

<SwitchButton checked={isChecked()} onChange={() => setChecked((checked) => !checked)} />
const [isChecked, setChecked] = createSignal(false);

<SwitchButton checked={isChecked()} onChange={() => setChecked((checked) => !checked)} />
If the question is how to expose either checked or set function, I don't think you can do that - you need to have both.
akerbeltz
akerbeltz•13mo ago
props.checked is not reactive when I use it like that and i change it with another button it's strange i think maybe has to do someting with the styles
high1
high1•13mo ago
It doesn't have to do anything with styles, that's certain. The reactivity is broken, but since you did not destructure, I don't see an obvious reason.
high1
high1•13mo ago
Solid Playground
Quickly discover what the solid compiler will generate from your JSX template
akerbeltz
akerbeltz•13mo ago
there's the whole component ( itried to import css but somewhat don't get correctly that import) https://playground.solidjs.com/anonymous/82507011-d034-48f7-82b2-a154f2ff9e93
Solid Playground
Quickly discover what the solid compiler will generate from your JSX template
high1
high1•13mo ago
And what's wrong with that component? Ah, you are missing onChange={(e) => props?.onChange?.(e.currentTarget.checked)}
high1
high1•13mo ago
Solid Playground
Quickly discover what the solid compiler will generate from your JSX template
high1
high1•13mo ago
TypeScript would catch the error
akerbeltz
akerbeltz•13mo ago
do you know how to put the classes with that playground
high1
high1•13mo ago
classes? Solid is pretty much agnostic about CSS, if that's what you mean. You can use whatever you like, provided that it's not framework specific.
akerbeltz
akerbeltz•13mo ago
with the playground page i mean haha
high1
high1•13mo ago
you should be able to use plain css Not sure about modules
akerbeltz
akerbeltz•13mo ago
but in the playground i send before i used plain css and it seems it don't work correctly
high1
high1•13mo ago
There's probably an error in the CSS or the way how it's applied. It's a playground, not meant to be used as a dev tool.
akerbeltz
akerbeltz•12mo ago
@.high1 I see.. thanks
high1
high1•12mo ago
Don't mention it. If you need help with setting up CSS, Solid is quite agnostic there, but you can use pretty much anything non-framework specific.
akerbeltz
akerbeltz•12mo ago
I solved 😄 the checked property of the input that is part of the switch was interfering somehow with the reactivity of the singal passed by props
high1
high1•12mo ago
Good to know. Haven't seen any issues with your examples.
elite174
elite174•10mo ago
Hey guys, I noticed that checkbox can't be controlled. For instance, if I pass <Switch checked={false} onChange={console.log} /> I still will be able to change checked state. How to deal with this?
Want results from more Discord servers?
Add your server
More Posts
Effects not running after a unhandled promise rejection eventWhen an unhandled error event occurs (e.g. by throwing `new Error()`), the `ErrorBoundary` is catchiWhy is this page with `useParams` and `createResource` not reloading?```jsx export default function ArticleDetail() { const [article] = createResource(useParams().id, call `createServerData$` outside Route?Can I call `createServerData$` outside of a Route? I want to put it in a header that is outside of `solid-start` site opens multiple `about:blank` on LoadMy `solid-start` page is opening multiple `about:blank` tabs when loading `localhost:3000` . Is theHow to keep/reuse DOM elements across routes?So, when using `solid-start` with routes, what is the most idiosyncratic way of keeping one element Disabling transition on load?I have a sticky navbar lower in the page which uses an intersection observer to transition to anothesetStore function replace object valuehttps://playground.solidjs.com/anonymous/f0b1602b-24a8-48db-a37d-e60db6eb3f85 ```tsx function CounteSolit-start routing works for a bit after `run dev`, but then only shows 404. Why?After starting dev `npm run dev`, I get the overview: ``` ** ➜ Page Routes: ┌─ http://localhConditional fetching with createResourceHi! Alright, this might be a little bit silly but here's my problem: I have a context provider compoIs there a polyfills.ts for `solid-start`? Or is there a way of modyfing the produced `index.html` ?So, in the `aws amplify` framework there is a common bug, which all frameworks have to fix somehow: