> ## 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.

# Days Of The Week – Code

> Days of the Weeks are form elements that allow users to select days of the week.

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="daysoftheweek-playground" fullWidth screenshot>
      ```tsx lines expandable theme={null}
      import { DaysOfTheWeek } from "@servicetitan/anvil2";
      import { useState } from "react";

      function App() {
        const [selectedDays, setSelectedDays] = useState<number[]>([]);

        return (
          <div style={{ minWidth: "20.5rem" }}>
            <DaysOfTheWeek
              selectedDays={selectedDays}
              onChange={setSelectedDays}
              label="Label text"
              hint="Hint text"
              description="Description text"
              // errorMessage="Error message"
              errorAriaLive="assertive"
              disabled={false}
              firstDay={7}
              required={false}
              moreInfo="More info"
            />
          </div>
        );
      }

      export default App;
      ```
    </LiveCode>

    ## Common Examples

    ### Labeling

    `DaysOfTheWeek` can be given optional labeling props to help users understand the context around the component.

    ```tsx theme={null}
    <DaysOfTheWeek
      label="Label text"
      hint="Hint text"
      description="Description text"
      moreInfo="More info"
    />
    ```

    For more information on how to use these these props, check out our [forms pattern documentation](/docs/web/patterns/forms#what-help-elements-should-we-use).

    ### Disabled

    #### Fully disabled

    The `DaysOfTheWeek` can be marked `disabled` to disable all of the days.

    ```tsx theme={null}
    () => {
      const [selectedDays, setSelectedDays] = useState([]);
      return (
        <DaysOfTheWeek
          selectedDays={selectedDays}
          onChange={setSelectedDays}
          disabled
        />
      );
    };
    ```

    #### Partially disabled

    The `DaysOfTheWeek` can be marked `disabled={number[]}` to disable specific days of the week.

    ```tsx theme={null}
    () => {
      const [selectedDays, setSelectedDays] = useState([]);
      return (
        <DaysOfTheWeek
          selectedDays={selectedDays}
          onChange={setSelectedDays}
          disabled={[6, 7]} // 6 = Saturday, 7 = Sunday
          hint="Select a weekday"
        />
      );
    };
    ```

    ### Alternative Starting Day

    Depending on your needs, the `DaysOfTheWeek` component can accept an alternative starting day via the `firstDay` prop. Here's an example showing Monday as the first day of the week:

    <LiveCode showCode example="daysoftheweek-firstday" screenshot fullWidth>
      ```tsx lines expandable theme={null}
      import { DaysOfTheWeek } from "@servicetitan/anvil2";
      import { useState } from "react";

      function App() {
        const [selectedDays, setSelectedDays] = useState<number[]>([]);

        return (
          <div style={{ minWidth: "20.5rem" }}>
            <DaysOfTheWeek
              selectedDays={selectedDays}
              onChange={setSelectedDays}
              firstDay={1}
            />
          </div>
        );
      }

      export default App;
      ```
    </LiveCode>

    #### Static day numbers

    When using the `firstDay` prop, it's important to recognize that the numbers used to index days remain static. This prop only adjusts how the buttons are layout out visually.

    This means that props like `selectedDays` and `disabled` continue to treat Monday as `1`, Tuesday as `2`, and so on.

    <LiveCode showCode example="daysoftheweek-map" screenshot fullWidth>
      ```tsx lines expandable theme={null}
      import { DaysOfTheWeek } from "@servicetitan/anvil2";

      function App() {
        return (
          <div style={{ minWidth: "20.5rem" }}>
            <DaysOfTheWeek
              selectedDays={[2]} // Tuesday is selected
              disabled={[6]} // Saturday is disabled
              firstDay={1} // Monday is the first day shown
            />
          </div>
        );
      }

      export default App;
      ```
    </LiveCode>

    ## Best Practices

    ### Day Numbers

    Take notice of the numbers used for `selectedDays`, `onChange`, `firstDay`, and `disabled`. If you are familiar with the `Date` object's `getDay` method, you may find them surprising at first.

    We use numbers 1-7 to number days, beginning with Monday as 1 proceeding through Sunday as 7. This is an intentional decision based on a goal to remain consistent the `Intl.Locale.prototype.getWeekInfo()` and `Temporal.PlainDate.prototype.dayOfWeek` APIs.

    This is a implementation choice rooted in consideration of the ever evolving and improving web standards. We acknowledge that `Date.prototype.getDay()` uses 0-6 for Sunday-Saturday but we do not plan on matching this API. We prefer the more powerful and complete `Intl` and `Temporal` APIs.
  </Tab>

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

    function ExampleComponent() {
    const [selectedDays, setSelectedDays] = useState([]);
    return (

    <DaysOfTheWeek
      selectedDays={selectedDays}
      onChange={setSelectedDays}
      firstDay={7}
      disabled={false}
      required={false}
    />
    ); }

    ```

    ## `DaysOfTheWeek` Props

    <ParamField path="description" type="ReactElement | string">
      Description text or element to display below the component.
    </ParamField>

    <ParamField path="disabled" type="boolean | number[]" default="false">
      Whether the component is disabled. Can be a boolean to disable all days, or an
      array of day numbers (1-7) to disable specific days.
    </ParamField>

    <ParamField path="errorAriaLive" type={`"polite" | "assertive" | "off"`} default="assertive">
      ARIA live setting for the error message.
    </ParamField>

    <ParamField path="errorMessage" type="ReactElement | string">
      Error message to display when there is an error.
    </ParamField>

    <ParamField path="firstDay" type="number" default="7">
      The first day of the week (1-7, where 1 is Monday and 7 is Sunday).
    </ParamField>

    <ParamField path="hint" type="ReactElement | string">
      Hint text or element to display above the component.
    </ParamField>

    <ParamField path="id" type="string">
      ID for the component.
    </ParamField>

    <ParamField path="label" type="ReactElement | string">
      Label text or element.
    </ParamField>

    <ParamField path="labelProps" type="FieldLabelProps">
      Additional props passed to the [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="moreInfo" type="ReactElement | string">
      Additional info to display in a tooltip next to the label.
    </ParamField>

    <ParamField path="onChange" type="(selectedDays: number[]) => void">
      Callback fired when the selection changes.
    </ParamField>

    <ParamField path="required" type="boolean">
      Whether the field is required.
    </ParamField>

    <ParamField path="selectedDays" type="number[]" default="[]">
      The selected days (1-7, where 1 is Monday and 7 is Sunday).
    </ParamField>
  </Tab>
</Tabs>
