> ## Documentation Index
> Fetch the complete documentation index at: https://anvil.servicetitan.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Select Card – Code

> Select Cards are a card-like checkbox or radio with customizable inner content.

export const LiveCode = ({children, customHeight, clickToLoad, example, fullWidth, fullHeight, hideCodeInLiveCode, screenshot, screenshotOnly, showCode: showCodeProp}) => {
  const SCREENSHOTS_BASE = "https://servicetitan.github.io/anvil2-docs-live-code/screenshots";
  const STACKBLITZ_BASE = "https://stackblitz.com/github/servicetitan/anvil2-docs-live-code/tree/main/examples";
  const [showCodeBlock, setShowCodeBlock] = useState(showCodeProp ?? false);
  const [isLocalOverride, setIsLocalOverride] = useState(false);
  useEffect(() => {
    const examplePath = `/images/live-code-screenshots-tmp/${example}.png`;
    fetch(examplePath, {
      method: "HEAD"
    }).then(r => {
      if (r.ok) setIsLocalOverride(true);
    }).catch(() => {});
  }, [example]);
  const screenshotBase = isLocalOverride ? "/images/live-code-screenshots-tmp" : SCREENSHOTS_BASE;
  if (screenshotOnly) {
    return <Frame className="flex flex-col">
        <div className="flex dark:hidden" style={{
      justifyContent: "center",
      alignItems: "center",
      width: fullWidth ? "100%" : "50%",
      minHeight: fullHeight ? "284px" : undefined,
      background: "#FFFFFF"
    }}>
          <img srcset={`${screenshotBase}/${example}.png, ${screenshotBase}/${example}-2x.png 2x`} src={`${screenshotBase}/${example}.png`} alt={example} noZoom />
        </div>
        <div className="hidden dark:flex" style={{
      justifyContent: "center",
      alignItems: "center",
      width: fullWidth ? "100%" : "50%",
      minHeight: fullHeight ? "284px" : undefined,
      background: "#141414"
    }}>
          <img srcset={`${screenshotBase}/${example}-dark.png, ${screenshotBase}/${example}-dark-2x.png 2x`} src={`${screenshotBase}/${example}-dark.png`} alt={example} noZoom />
        </div>
      </Frame>;
  }
  if (screenshot) {
    return <Frame className="flex flex-col -mb-2">
        <div className="flex dark:hidden bg-white dark:bg-codeblock border border-gray-950/10 dark:border-white/10 dark:twoslash-dark rounded-2xl overflow-hidden" style={{
      justifyContent: "center",
      alignItems: "center",
      width: fullWidth ? "100%" : "50%",
      minHeight: fullHeight ? "284px" : undefined
    }}>
          <img srcset={`${screenshotBase}/${example}.png, ${screenshotBase}/${example}-2x.png 2x`} src={`${screenshotBase}/${example}.png`} alt={example} noZoom />
        </div>

        <div className="hidden dark:flex bg-white dark:bg-codeblock border border-gray-950/10 dark:border-white/10 dark:twoslash-dark rounded-2xl overflow-hidden" style={{
      background: "#141414",
      justifyContent: "center",
      alignItems: "center",
      width: fullWidth ? "100%" : "50%",
      minHeight: fullHeight ? "284px" : undefined
    }}>
          <img srcset={`${screenshotBase}/${example}-dark.png, ${screenshotBase}/${example}-dark-2x.png 2x`} src={`${screenshotBase}/${example}-dark.png`} alt={example} noZoom />
        </div>

        <div className="flex justify-end items-center text-xs py-2 px-1 gap-4">
          {!showCodeProp ? <button className="inline-flex justify-end items-center text-gray-700 dark:text-gray-50 hover:text-blue-500 dark:hover:text-blue-300 transition-colors group self-end gap-1 cursor-pointer" onClick={() => setShowCodeBlock(!showCodeBlock)} style={{
      appearance: "none"
    }}>
              <Icon icon="code" size="12px" className="group-hover:bg-blue-500 dark:group-hover:bg-blue-300" />
              <span>{showCodeBlock ? "Hide code" : "Show code"}</span>
            </button> : null}

          <a className="inline-flex justify-end items-center hover:text-blue-500 dark:hover:text-blue-300 transition-colors group self-end gap-1" href={`${STACKBLITZ_BASE}/${example}?file=src/App.tsx`} target="_blank" rel="noreferrer">
            <Icon icon="bolt" size="12px" className="group-hover:bg-blue-500 dark:group-hover:bg-blue-300" />
            <span>StackBlitz demo</span>
          </a>
        </div>

        <div className="grid transition-[grid-template-rows] duration-300 ease-in-out overflow-auto overflow-y-hidden overflow-x-auto" style={showCodeBlock ? {
      gridTemplateRows: "1fr"
    } : {
      gridTemplateRows: "0fr"
    }}>
          <div style={{
      minHeight: 0,
      overflowX: "auto",
      overflowY: "hidden",
      marginBlockStart: "-1.25rem",
      marginBlockEnd: "-1.5rem"
    }}>
            {children}
          </div>
        </div>
      </Frame>;
  } else {
    return <div style={{
      display: "flex",
      width: fullWidth ? "100%" : "50%",
      minHeight: customHeight ? customHeight : "316px",
      resize: "vertical",
      overflow: "auto"
    }}>
        <iframe title={example} style={{
      flex: 1,
      width: fullWidth ? "100%" : "50%",
      minHeight: customHeight ? customHeight : "316px"
    }} src={`${STACKBLITZ_BASE}/${example}?embed=1&hideNavigation=1&hideExplorer=1&terminalHeight=0&file=src/App.tsx${clickToLoad ? "&ctl=1" : ""}${hideCodeInLiveCode ? "&view=preview" : ""}`} allow="accelerometer; ambient-light-sensor; camera; encrypted-media; geolocation; gyroscope; hid; microphone; midi; payment; usb; vr; xr-spatial-tracking" sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts" />
      </div>;
  }
};

<Tabs>
  <Tab title="Implementation">
    <LiveCode showCode example="selectcard-playground" fullWidth screenshot>
      ```tsx lines expandable theme={null}
      import { SelectCard, Grid, Text, Chip } from "@servicetitan/anvil2";

      function App() {
        return (
          <SelectCard id="1" onChange={() => console.log("clicked")}>
            <Grid templateColumns="repeat(2, 1fr)" gap="4">
              <Text>Cooling Tune Up</Text>

              <Text style={{ textAlign: "right" }}>#4567-1</Text>

              <Text>10:30 AM</Text>

              <Chip label="Customer name" />
            </Grid>
          </SelectCard>
        );
      }

      export default App;
      ```
    </LiveCode>

    ## Common Examples

    ```tsx theme={null}
    import { Chip, Grid, SelectCard, Text } from "@servicetitan/anvil2";

    const data = [/*...data for select cards...*/]

    function ExampleComponent() {
      return (
        <SelectCard.Group
          legend="Example multiple selection"
          required={false}
          selectionMode="multiple"
          onChange={(e) => {
            console.log(e);
          }}
        >
          <Grid gap="4" templateColumns="repeat(3, 1fr)">
            {data.map((item) => (
              <SelectCard key={item.id} id={item.id}>
                <Grid templateColumns="repeat(2, 1fr)" gap="4">
                  <Text>{item.label}</Text>
                  <Text style={{ textAlign: "right" }}>{item.cost}</Text>
                  <Text>{item.timeEstimate}</Text>
                  <Chip label={item.customerName} />
                </Grid>
              </SelectCard>
            ))}
        </SelectCard.Group>
      );
    }
    ```

    ### SelectCard Components

    The Anvil2 `SelectCard` is typically used within a `SelectCard.Group`. Under the hood SelectCard can either be a radio or a checkbox, and accepts all of the props associated with those HTML inputs.

    * `SelectCard.Group`: the parent component used to configure a group of `SelectCard` items.
    * `SelectCard`: used to create a card input to use with a group.

    ### Validation

    `SelectCards` have an `errored` state, which can be triggered with an `onChange`.

    <LiveCode showCode example="selectcard-errored" screenshot fullWidth>
      ```tsx lines expandable theme={null}
      import { SelectCard, Grid, Text, Chip } from "@servicetitan/anvil2";

      function App() {
        return (
          <SelectCard id="2" onChange={() => console.log("clicked")} errored>
            <Grid templateColumns="repeat(2, 1fr)" gap="4">
              <Text>Cooling Tune Up</Text>

              <Text style={{ textAlign: "right" }}>#4567-1</Text>

              <Text>10:30 AM</Text>

              <Chip label="Customer name" />
            </Grid>
          </SelectCard>
        );
      }

      export default App;
      ```
    </LiveCode>

    ### Select Indicator Visual

    Use the `showSelectIndicator` prop to display a checkbox or radio indicator in a separate column on the left side of the card. The indicator type is automatically determined by the `selectionMode` from `SelectCard.Group`.

    <LiveCode showCode example="selectcard-group-selectindicator" screenshot fullWidth>
      ```tsx lines expandable theme={null}
      import { Chip, Grid, SelectCard, Text } from "@servicetitan/anvil2";

      function App() {
        return (
          <SelectCard.Group
            legend="Choose an option"
            selectionMode="single"
            showSelectIndicator
          >
            <Grid gap="4" templateColumns="repeat(3, 1fr)">
              {[
                {
                  id: "1",
                  label: "Cooling Tune Up",
                  cost: "$100",
                  timeEstimate: "1 hour",
                },
                {
                  id: "2",
                  label: "Heating Tune Up",
                  cost: "$150",
                  timeEstimate: "2 hours",
                },
                {
                  id: "3",
                  label: "Air Duct Cleaning",
                  cost: "$200",
                  timeEstimate: "3 hours",
                },
              ].map((item) => (
                <SelectCard key={item.id} id={item.id}>
                  <Grid templateColumns="repeat(2, 1fr)" gap="4">
                    <Text variant="headline" size="small" el="h2">
                      {item.label}
                    </Text>
                    <Chip label={item.cost} justifySelf="end" />
                    <Text variant="body" size="small" subdued>
                      ~{item.timeEstimate}
                    </Text>
                  </Grid>
                </SelectCard>
              ))}
            </Grid>
          </SelectCard.Group>
        );
      }

      export default App;
      ```
    </LiveCode>

    <Note>
      The `showSelectIndicator` prop is available for both `SelectCard` and `SelectCard.Group`, but in most cases it is recommended to use it with `SelectCard.Group` to show the indicator on all cards in the group.
    </Note>

    ### Controlled State

    Use `value` on `SelectCard.Group` to control the selection externally. The component will not update its selection on its own — you must update `value` in the `onChange` callback.

    <LiveCode showCode example="selectcard-group-controlled" screenshot fullWidth>
      ```tsx lines expandable theme={null}
      import { useState } from "react";
      import { SelectCard, Flex, Text } from "@servicetitan/anvil2";

      function App() {
        const [selected, setSelected] = useState<Array<string | number>>(["A"]);

        return (
          <SelectCard.Group
            legend="Which method of travel do you prefer?"
            selectionMode="single"
            value={selected}
            onChange={(_e, state) => {
              if (!state?.id) return;
              setSelected(state.checked ? [state.id] : []);
            }}
          >
            <Flex alignItems="center" gap="2">
              <SelectCard id="A">
                <Text>Walking</Text>
              </SelectCard>
              <SelectCard id="B">
                <Text>Biking</Text>
              </SelectCard>
              <SelectCard id="C">
                <Text>Driving</Text>
              </SelectCard>
            </Flex>
          </SelectCard.Group>
        );
      }

      export default App;
      ```
    </LiveCode>

    ### Default Selection

    Use `defaultValue` to pre-select cards when the group is uncontrolled. The selection can still be changed by the user after the initial render.

    <LiveCode showCode example="selectcard-group-defaultvalue" screenshot fullWidth>
      ```tsx lines expandable theme={null}
      import { SelectCard, Flex, Text } from "@servicetitan/anvil2";

      function App() {
        return (
          <SelectCard.Group
            legend="Which method(s) of travel do you prefer?"
            selectionMode="multiple"
            defaultValue={["A", "C"]}
            onChange={(e) => {
              console.log(e);
            }}
          >
            <Flex alignItems="center" gap="2">
              <SelectCard id="A">
                <Text>Walking</Text>
              </SelectCard>
              <SelectCard id="B">
                <Text>Biking</Text>
              </SelectCard>
              <SelectCard id="C">
                <Text>Driving</Text>
              </SelectCard>
            </Flex>
          </SelectCard.Group>
        );
      }

      export default App;
      ```
    </LiveCode>
  </Tab>

  <Tab title="SelectCard Props">
    ```tsx theme={null}
    import { SelectCard } from "@servicetitan/anvil2";

    function ExampleComponent() {
    return (

    <SelectCard
      id="card-1"
      onChange={(value) => console.log(value)}
      defaultChecked={false}
      disabled={false}
      errored={false}
    >
      Card content
    </SelectCard>
    ); }

    ```

    ## `SelectCard` Props

    <ParamField path="id" type="string | number" required>
      A unique identifier for the card, used to track the selection state.
    </ParamField>

    <ParamField path="checked" type="boolean">
      Controlled checked state.
    </ParamField>

    <ParamField path="checkboxProps" type={`Omit<CheckboxProps, "label">`}>
      Passes props to the underlying `Checkbox` component.
    </ParamField>

    <ParamField path="defaultChecked" type="boolean">
      Uncontrolled initial checked state. Ignored when inside a `SelectCard.Group` that sets `defaultValue`.
    </ParamField>

    <ParamField path="disabled" type="boolean">
      When `true`, disables the card.
    </ParamField>

    <ParamField path="errored" type="boolean">
      When `true`, sets error styling on the card.
    </ParamField>

    <ParamField path="onChange" type="(value: boolean) => void">
      Function called on selection state change.
    </ParamField>

    <ParamField path="radioProps" type={`Omit<RadioProps, "label">`}>
      Passes props to the underlying `Radio` component.
    </ParamField>

    <ParamField path="removeDropShadow" type="boolean">
      Removes the drop shadow effect on the card.
    </ParamField>

    <ParamField path="showSelectIndicator" type="boolean" default="false">
      When `true`, displays a checkbox or radio indicator in a separate column on the left side of the card. The indicator type (checkbox or radio) is automatically determined by the `selectionMode` prop on the `SelectCard.Group`, and defaults to checkbox if no `SelectCard.Group` is used.
    </ParamField>
  </Tab>

  <Tab title="SelectCard.Group Props">
    ```tsx theme={null}
    import { SelectCard } from "@servicetitan/anvil2";

    function ExampleComponent() {
      return (
        <SelectCard.Group
          legend="Group Legend"
          selectionMode="multiple"
          defaultValue={["card-1"]}
          required={false}
          ariaLabelledBy="group-label"
        >
          <SelectCard id="card-1" onChange={() => {}}>
            Card content
          </SelectCard>
        </SelectCard.Group>
      );
    }
    ```

    ## `SelectCard.Group` Props

    <ParamField path="legend" type="ReactNode" required>
      Sets the legend on the group.
    </ParamField>

    <ParamField path="selectionMode" type={`"single" | "multiple"`} required>
      Sets how many cards the user can select: `"single"` uses radio buttons, `"multiple"` uses checkboxes.
    </ParamField>

    <ParamField path="ariaLabelledBy" type="string">
      Associates the legend with content below.
    </ParamField>

    <ParamField path="children" type="ReactNode" />

    <ParamField path="defaultValue" type="Array<string | number>">
      Initial selection state for uncontrolled usage. Array of `SelectCard` ids that are selected on first render. Takes precedence over individual `SelectCard` `defaultChecked` props.
    </ParamField>

    <ParamField path="legendProps" type={`Omit<FieldLabelProps, "el">`}>
      Additional props to pass to the legend [FieldLabel](/docs/web/components/field-label/code) component. Supports the `aiMark` prop for displaying [AI-powered field indicators](/docs/web/utilities/ai-marks).
    </ParamField>

    <ParamField path="onChange" type="(e: ChangeEvent, state: { id: string | number; checked: boolean }) => void">
      Callback function triggered when the selection state changes.
    </ParamField>

    <ParamField path="required" type="boolean" default="false">
      When `true`, sets "required" text in label. This does not enforce error when
      nothing is selected.
    </ParamField>

    <ParamField path="showSelectIndicator" type="boolean" default="false">
      When `true`, displays a checkbox or radio indicator in a separate column on the left side of the each of the cards in the group. The indicator type (checkbox or radio) is automatically determined by the `selectionMode` setting.
    </ParamField>

    <ParamField path="value" type="Array<string | number>">
      Controlled selection state. Array of `SelectCard` ids that are currently selected. When provided, the group becomes controlled and selection must be managed via `onChange`.
    </ParamField>
  </Tab>
</Tabs>
