nutui-uniapp

repository·main·Indexed 19 days ago

https://github.com/nutui-uniapp/nutui-uniapp

A lightweight, JD-style Vue-based component library adapted for the uni-app framework. It provides over 80 high-quality components for mobile H5 and mini-program development, featuring TypeScript support, dynamic theming, dark mode, and internationalization for English, Indonesian, Simplified Chinese, and Traditional Chinese.

Tokens
235.6K
Snippets
745
Records
923
Agent score
67%

What's inside nutui-uniapp

  1. Overview of NutUI-uniapp

    main

    NutUI-uniapp is a lightweight mobile UI component library designed in the style of JD (Jingdong). It is built on Vue.js and is specifically adapted for the uni-app framework, enabling developers to build high-quality mini-programs efficiently.

    Key features include:

    • Component Design: Based on NutUI 4.x and adapted for uni-app, providing an out-of-the-box experience for rapid UI development.
    • On-demand Loading: Supports automatic importing of only the components being used via a dedicated parser to optimize bundle size.
    • TypeScript Support: Full support for TypeScript, including type checking and type inference.
    • Theme Configuration: Flexible styling customization, allowing for global primary color changes and specific component visual adjustments to meet brand requirements.
  2. Use the Address component for four-level address selection

    main

    The nut-address component allows users to select addresses through four levels: province, city, country, and town. It supports different modes including custom address selection and selecting from existing saved addresses.

    <nut-address
      v-model:visible="visible"
      :province="address.province"
      :city="address.city"
      :country="address.country"
      :town="address.town"
      @change="onChange"
      @close="onClose"
    ></nut-address>
  3. Use the Comment component for product reviews

    main

    The nut-comment component is used to display a list of product reviews. It supports single-row image scrolling by default, multi-row image layouts, and follow-up reviews (追评).

    <template>
      <nut-comment
        :images="cmt.images"
        :videos="cmt.videos"
        :info="cmt.info"
        :operation="['replay']"
        @click="onClick"
        @clickImages="onImagesClick"
      >
        <template #commentLabels>
          <image src="https://example.com/label.jpg"></image>
        </template>
      </nut-comment>
    </template>
  4. Control TabBar selection via name or index

    main

    You can control the active tab in nut-tabbar using v-model in two ways:

    1. By Name: Assign a unique name prop to each nut-tabbar-item. The v-model should then be a string matching one of those names.
    2. By Index: The v-model is a number representing the zero-based index of the active item.

    Example: Matching by Name

    <template>
      <nut-tabbar v-model="activeName">
        <nut-tabbar-item
          v-for="item in list"
          :key="item.name"
          :name="item.name"
          :tab-title="item.title"
          :icon="item.icon"
        ></nut-tabbar-item>
      </nut-tabbar>
    </template>
    const activeName = ref("category");
    const list = [
      { title: "标签", icon: "home", name: "home" },
      { title: "标签", icon: "category", name: "category" },
      { title: "标签", icon: "find", name: "find" },
      { title: "标签", icon: "cart", name: "cart" },
      { title: "标签", icon: "my", name: "my" }
    ];

    Example: Selecting by Index

    <template>
      <nut-tabbar v-model="active">
        <nut-tabbar-item
          v-for="item in list"
          :tab-title="item.title"
          :icon="item.icon"
        ></nut-tabbar-item>
      </nut-tabbar>
    </template>
    const active = ref(2);
    const list = [
      { title: "标签", icon: "home" },
      { title: "标签", icon: "category" },
      { title: "标签", icon: "find" },
      { title: "标签", icon: "cart" },
      { title: "标签", icon: "my" }
    ];
  5. Configure Tabs auto-height, scrolling, and layout

    main

    The nut-tabs component supports several layout and behavior configurations:

    • auto-height: When true, the height of nut-tabs and nut-tabs__content adjusts automatically to match the height of the current nut-tab-pane.
    • title-scroll: Enables scrolling for the tab bar when there are many tabs.
    • title-gutter: Sets the spacing between tabs when scrolling is enabled.
    • direction: Changes the orientation of the tab bar. Use vertical for a side-bar layout or horizontal (default) for a top-bar layout.
    <template>
      <!-- Auto height -->
      <nut-tabs v-model="value" auto-height>
        <nut-tab-pane title="Tab 1">
          <view>Tall Content</view>
          <view>More Content</view>
        </nut-tab-pane>
        <nut-tab-pane title="Tab 2">Short Content</nut-tab-pane>
      </nut-tabs>
    
      <!-- Scrolling and Vertical layout -->
      <nut-tabs
        v-model="value"
        direction="vertical"
        title-scroll
        title-gutter="10"
      >
        <nut-tab-pane v-for="item in list" :key="item" :title="`Tab ${item}`">
          Content
        </nut-tab-pane>
      </nut-tabs>
    </template>
  6. How SwipeGroup works for mutual exclusion

    main

    When you have multiple nut-swipe components in a list and want to ensure only one is open at a time, wrap them in a nut-swipe-group with the lock prop set to true.

    Important: When using nut-swipe-group, every nut-swipe child must have a unique name prop.

    When lock is enabled, opening one nut-swipe will automatically trigger the close method on all other active nut-swipe components in the group.

    Example: Mutual Exclusion

    <template>
      <nut-swipe-group lock>
        <nut-swipe name="swipe1">
          <nut-cell title="Swipe 1" round-radius="0"></nut-cell>
          <template #right>
            <nut-button type="danger" shape="square">Delete</nut-button>
          </template>
        </nut-swipe>
    
        <nut-swipe name="swipe2">
          <nut-cell title="Swipe 2" round-radius="0"></nut-cell>
          <template #right>
            <nut-button type="danger" shape="square">Delete</nut-button>
          </template>
        </nut-swipe>
      </nut-swipe-group>
    </template>
    <template>
      <nut-swipe-group lock>
        <nut-swipe name="swipe1">
          <nut-cell title="左滑删除" round-radius="0"></nut-cell>
    
          <template #right>
            <nut-button type="danger" shape="square">删除</utton>
          </template>
        </nut-swipe>
    
        <nut-swipe name="swipe2">
          <nut-cell title="左滑删除" round-radius="0"></nut-cell>
    
          <template #right>
            <nut-button type="danger" shape="square">删除</utton>
          </template>
        </nut-swipe>
    
        <nut-swipe name="swipe3">
          <nut-cell title="左滑删除" round-radius="0"></nut-cell>
    
          <template #right>
            <nut-button type="danger" shape="square">删除</utton>
          </template>
        </nut-swipe>
      </nut-swipe-group>
    </template>
  7. Configure multi-column and cascading selections in Picker

    main

    The nut-picker supports two main data structures via the columns prop:

    1. Multi-column display: Pass a two-dimensional array (an array of arrays) where each inner array represents a separate column of options.
    2. Cascading (Multi-level) selection: Pass a single array of objects where each object can contain a children field. This creates a hierarchical relationship where selecting an item in one column updates the options in the next.

    To use custom field names for your data (e.g., if your API returns name instead of text), use the field-names prop.

    // Multi-column example
    const columns = ref([
      [{ text: 'Monday', value: 'Mon' }, { text: 'Tuesday', value: 'Tue' }],
      [{ text: 'Morning', value: 'AM' }, { text: 'Afternoon', value: 'PM' }]
    ]);
    
    // Cascading example
    const columns = ref([
      {
        text: 'Zhejiang',
        value: 'ZheJiang',
        children: [
          { text: 'Hangzhou', value: 'HangZhou', children: [{ text: 'West Lake', value: 'XiHu' }] }
        ]
      }
    ]);
    
    // Custom field names
    const fieldNames = {
      text: 'name',
      value: 'code',
      children: 'list'
    };
  8. Configure NumberKeyboard modes and layouts

    main

    You can customize the layout and behavior of the NumberKeyboard using the following patterns:

    Right Column Layout

    Use type="rightColumn" to add a sidebar for extra keys (e.g., a decimal point) and customize the confirm button text.

    Random Key Positions

    Use the random-keys prop to shuffle the key positions, which is useful for security in payment scenarios.

    Title Bar

    Use the title prop to display a header at the top of the keyboard.

    ID Card Mode

    Use custom-key to add specific characters like 'X' to the keypad.

    <!-- Right Column with custom key and confirm text -->
    <nut-number-keyboard
      v-model="value"
      v-model:visible="visible"
      type="rightColumn"
      :custom-key="['.']"
      confirm-text="支付"
      @confirm="onConfirm"
    ></nut-number-keyboard>
    
    <!-- Random keys for security -->
    <nut-number-keyboard
      v-model="value"
      v-model:visible="visible"
      random-keys
    ></nut-number-keyboard>
    
    <!-- With a title bar -->
    <nut-number-keyboard
      v-model="value"
      v-model:visible="visible"
      title="默认键盘"
    ></nut-number-keyboard>
    
    <!-- ID Card style with 'X' key -->
    <nut-number-keyboard
      v-model="value"
      v-model:visible="visible"
      :custom-key="['X']"
    ></nut-number-keyboard>
  9. Reference Component Props, Events, Slots, and Exposes

    main

    This template defines how component APIs should be documented.

    Props

    Document properties using a table containing: 参数 (Parameter), 说明 (Description), 类型 (Type), 可选值 (Allowed Values), and 默认值 (Default Value).

    Events

    Document emitted events using a table containing: 事件名 (Event Name), 说明 (Description), and 类型 (Callback Type).

    Slots

    Document available slots using a table containing: 名称 (Name), 说明 (Description), 类型 (Type), and 子标签 (Sub-tags/Child components).

    Exposes

    Document methods or properties that can be accessed via a Vue ref on the component instance.