Taroify Documentation

repository·main·Indexed 22 days ago

https://github.com/mallfoundry/taroify

A lightweight, multi-terminal Taro React UI component library based on the Vant design language for Mini Programs and H5 applications. Includes @taroify/core for base components like ActionSheet and Transition, @taroify/commerce for specialized components like ActionBar, and @taroify/icons for font and image-based icons. Features a CLI (@taroify/cli) that provides offline component knowledge, API references, and MCP server integration for AI agents.

Tokens
206.1K
Snippets
657
Records
949
Agent score
77%

What's inside taroify

  1. Format the areaList data

    main

    The areaList prop must be an object containing three keys: province_list, city_list, and county_list.

    Each list is an object where the key is a 6-digit region code (representing province, city, and county respectively, padded with zeros) and the value is the name of the region. For example, Beijing's code is 110000.

    const areaList = {
      province_list: {
        110000: "北京市",
        120000: "天津市",
      },
      city_list: {
        110100: "北京市",
        120100: "天津市",
      },
      county_list: {
        110101: "东城区",
        110102: "西城区",
      },
    }
  2. Disable Form components and hierarchy

    main

    Setting the disabled prop on a Form component will automatically propagate the disabled state to its child components, including Input, Textarea, Checkbox, Switch, Checkbox.Group, Radio.Group, Rate, Slider, Stepper, and Uploader.

    There is a priority hierarchy for the disabled state:

    1. Form (Highest priority)
    2. Form Item / Field
    3. Individual Component (Lowest priority)

    This means if a Form is disabled, you cannot re-enable a specific Field or Component within it. However, if the Form is enabled, you can still disable specific items or components.

  3. Understand the Taroify repository structure

    main

    The repository is organized into several key directories:

    • packages/core: Contains the source code for individual components. Each component resides in its own folder.
    • packages/: Contains various sub-packages.
    • site/: Contains the source code for the documentation website. You can run the documentation site locally using pnpm --dir site run site:develop.
    • bundles/: Contains build artifacts.
    • scripts/: Contains utility scripts.
    taroify
    ├─ bundles         # 构建
    ├─ packages/core   # 组件
    ├─ packages        # 子包
    ├─ site            # 文档
    └─ scripts         # 脚本
  4. Configure NoticeBar scrolling and wrapping

    main

    You can control how the notification text is displayed using the following properties:

    • scrollable: Controls whether the content scrolls when it overflows. It defaults to false. Set to true to enable automatic scrolling.
    • wordwrap: Enables text wrapping for multiple lines. This property only takes effect when scrollable is set to false.
    • delay: The animation delay time in milliseconds (default: 1000).
    • speed: The scrolling speed in px/s (default: 60).
    // Enable scrolling for short text
    <NoticeBar scrollable>技术是开发它的人的共同灵魂。</NoticeBar>
    
    // Disable scrolling for long text
    <NoticeBar scrollable={false}>
      在代码阅读过程中人们说脏话的频率是衡量代码质量的唯一标准。
    </NoticeBar>
    
    // Enable multi-line wrapping (requires scrollable={false})
    <NoticeBar wordwrap scrollable={false}>
      在代码阅读过程中人们说脏话的频率是衡量代码质量的唯一标准。
    </NoticeBar>
  5. Switch Calendar view mode

    main

    By default, the calendar tiles all months within the specified range. For large date ranges, use switchMode="year-month" to render only the current month and provide buttons to switch between months or years. This is more efficient for large spans.

    <Calendar
      min={new Date(2020, 0, 1)}
      max={new Date(2030, 11, 31)}
      switchMode="year-month"
    />
  6. Use Toast for notifications

    main

    The Toast component provides lightweight, semi-transparent popups in the center of the screen for messages, loading states, and operation results. You can use it in two ways:

    1. Imperative Call (Recommended): Call Toast.open() or specialized methods like Toast.success() directly. This is available in versions >= v0.6.0-alpha.0.
    2. Component Call: Manually mount the <Toast /> component in your JSX and control its visibility via the open prop.

    Note: For versions older than v0.6.0-alpha.0, you must manually mount a <Toast id="toast" /> component in your page to enable imperative calls.

    import { Toast } from "@taroify/core"
    
    // Imperative usage
    Toast.open("Text message")
    
    // Component usage
    function BasicToast() {
      const [open, setOpen] = useState(false)
      return (
        <>
          <Toast open={open} onClose={setOpen}>
            Text message
          </Toast>
          <button onClick={() => setOpen(true)}>Show Toast</button>
        </>
      )
    }
  7. Trigger validation using dependencies and shouldUpdate

    main

    To create reactive forms where one field's value affects another, use these two mechanisms:

    1. dependencies

    Use the dependencies prop on a Field or Form.Item to specify which other field names should trigger a re-validation of the current field. For example, a "Confirm Password" field should depend on the "Password" field.

    2. shouldUpdate and noStyle

    To conditionally render parts of the form based on other field values, use shouldUpdate on a Form.Item.

    • Requirement: The child of a shouldUpdate item must be a function that returns a React node. If you pass a static component, shouldUpdate will not trigger a re-render.
    • noStyle: Use this prop to prevent the Form.Item from adding extra layout wrappers when you only want it to act as a logic controller.

    Note: Field components do not currently support shouldUpdate logic; use Form.Item for conditional rendering.

    <Form.Item
      name="confirmPassword"
      dependencies={["password"]}
      rules={[
        {
          validator: (val) => {
            return formRef.current?.getValues<any>()?.password === val ? true : "密码不一致";
          },
        },
      ]}.
    >
      <Input />
    </Form.Item>
    
    // Conditional rendering with shouldUpdate
    <Form.Item
      noStyle
      shouldUpdate={(prev, cur) => prev.type !== cur.type}
    >
      {() => (
        <Field name="conditionalField" label="Conditional">
          <Input />
        </Field>
      )}
    </Form.Item>
  8. Implement Single and Multi-select modes in TreeSelect

    main

    TreeSelect supports both single and multiple selection modes based on the type of the value prop provided:

    • Single Selection: Pass a single number or string to the value prop.
    • Multi-selection: Pass an array of number or string to the value prop. The max prop can be used to limit the number of items that can be selected.

    tabValue controls the currently active index in the left-side navigation, while value controls the selected item(s) in the right-side content area.

    // Multi-select example
    const [value, setValue] = useState([0, 1])
    
    <TreeSelect value={value} onChange={setValue}>
      <TreeSelect.Tab title="Group">
        <TreeSelect.Option value={0}>Option 0</TreeSelect.Option>
        <TreeSelect.Option value={1}>Option 1</TreeSelect.Option>
      </TreeSelect.Tab>
    </TreeSelect>
  9. Match Tabs by custom identifiers

    main

    By default, Tabs uses the index of the panel. If you provide a value prop to Tabs.TabPanel, you can use Tabs.defaultValue to set the initial active tab based on that specific identifier (string or number) instead of an index.

    <Tabs defaultValue="a">
      <Tabs.TabPanel value="a" title="标签 1">
        内容 1
      </Tabs.TabPanel>
      <Tabs.TabPanel value="b" title="标签 2">
        内容 2
      </Tabs.TabPanel>
    </Tabs>
  10. Build a fully custom Timeline

    main

    For complete control over the timeline structure, you can bypass the standard Timeline.Item shorthand and manually compose the timeline using sub-components. This allows you to place custom icons, connectors, and content alignment anywhere in the sequence.

    Sub-components:

    • Timeline.Content: Wraps the event content. Supports align (center, start, end) and direction (column).
    • Timeline.Separator: A container for the vertical elements.
    • Timeline.Connector: The vertical line connecting the dots.
    • Timeline.Dot: The visual marker for the event. Can contain icons or custom elements.
    • Timeline.Item: The top-level wrapper for a single timeline entry.
    <Timeline>
      <Timeline.Item>
        <Timeline.Content align="center">9:30 am</Timeline.Content>
        <Timeline.Separator>
          <Timeline.Connector />
          <Timeline.Dot>
            <FireOutlined size="24" />
          </Timeline.Dot>
          <Timeline.Connector />
        </Timeline.Separator>
        <Timeline.Content direction="column" align="start">
          <View className="timeline-title">Eat</View>
          <View>Because you need strength</View>
        </Timeline.Content>
      </Timeline.Item>
    </Timeline>