SolidJSS
SolidJSโ€ข3y agoโ€ข
5 replies
CaptainDaylight

Passing For Id between InputGroup and Input using children

I'm trying to pass create a JSX structure like:
<InputGroup
  label="Email"
  error="that's not an email"
>
  <Input 
    placeholder="like@this.com"
  />
</InputGroup>


My InputGroup component looks like:

export default function InputGroup(props: InputGroupProps) {
  const { label, error, children: childrenProp } = props;
  const [forId] = createSignal(`input-${Math.random().toString(36).substr(2, 9)}`);
  const renderedChildren = children(() => childrenProp);

  createEffect(() => {
    const child = renderedChildren();
    if (child?.props) {
      child.props.id = forId();
    }
  });

  return (
    <>
      <label for={forId()}>{label}</label>
      {renderedChildren()}
      <Show when={!!error}>
        <p>{error}</p>
      </Show>
    </>
  );
}


This is if (child?.props) { is giving me the type error:
Property 'props' does not exist on type 'number | boolean | Node | (string & {}) | ResolvedJSXElement[]'.


Does anyone have ideas how to achieve what I'm trying to do with the children helper? Or is there a better approach?
Was this page helpful?