Skip to content
ReferenceVersion 6.0.2

RCollapseGrid ​

Use this component for search forms with several filters. It renders an NGrid and requires NGridItem children. Configure it with cols, responsive, collapsedRows, xGap, and yGap. The default gap is 12, and an explicit 0 is preserved.

Interactive example ​

View example source
vue
<template>
  <DemoFrame>
    <div class="ray-demo">
      <RCollapseGrid
        v-model:open="open"
        :cols="2"
        :collapsedRows="1"
        :collapseToggleText="copy.toggle"
      >
        <NGridItem v-for="field in fields" :key="field">
          <NInput
            v-model:value="filters[field]"
            :placeholder="copy.fields[field]"
            :aria-label="copy.fields[field]"
          />
        </NGridItem>
        <template #action>
          <NButton type="primary" @click="submitted = { ...filters }">{{
            copy.search
          }}</NButton>
        </template>
      </RCollapseGrid>
      <output class="ray-demo-result">{{ submitted ?? copy.hint }}</output>
    </div>
  </DemoFrame>
</template>

<script setup lang="ts">
import { RCollapseGrid } from '@ray-template/ui/collapse-grid'
import { NButton, NGridItem, NInput } from 'naive-ui'
import { useData } from 'vitepress'
import { computed, reactive, ref } from 'vue'
import DemoFrame from './DemoFrame.vue'

const fields = ['name', 'department', 'role', 'id'] as const
const filters = reactive({ name: '', department: '', role: '', id: '' })
const { lang } = useData()
const copy = computed(() =>
  lang.value.startsWith('en')
    ? {
        fields: {
          name: 'Name',
          department: 'Department',
          role: 'Role',
          id: 'ID',
        },
        toggle: ['Expand', 'Collapse'] as [string, string],
        search: 'Search',
        hint: 'Expand to show more filters.',
      }
    : {
        fields: { name: '姓名', department: '部门', role: '岗位', id: '编号' },
        toggle: ['展开', '收起'] as [string, string],
        search: '查询',
        hint: '展开后可填写更多条件',
      },
)
const open = ref(false)
const submitted = ref<typeof filters>()
</script>
PropDefaultDescription
openfalseExpansion state. true expands and false collapses.
actionSpan1Grid columns occupied by the action area.
actionAlign'end'CSS alignment for the actions. Use auto, end, center, or start.
collapseToggleText['展开', '收起'] (Expand, Collapse)Order is the label shown when collapsed, then the label shown when expanded.
borderedtrueOuter card border.

Use default for filter fields, action for the search action, and collapse(collapsed) to replace the toggle. The custom collapse slot receives a boolean. Wire its button to update open.

Controlled state and update events

onUpdateOpen and onUpdate:open receive the expanded state and work with v-model:open. Remove any inversion added for an older reversed event. Binding both listeners invokes each once. The default button supports keyboard activation and exposes state through aria-expanded.

tsx
import { RCollapseGrid } from '@/components'
import { NButton, NGridItem, NInput } from 'naive-ui'
import { defineComponent, ref } from 'vue'

export default defineComponent({
  setup() {
    const open = ref(false)
    return () => (
      <RCollapseGrid
        open={open.value}
        cols={3}
        collapsedRows={1}
        collapseToggleText={['Expand', 'Collapse']}
        onUpdateOpen={(value) => {
          open.value = value
        }}
      >
        {{
          default: () => [
            <NGridItem>
              <NInput placeholder="Order number" />
            </NGridItem>,
            <NGridItem>
              <NInput placeholder="Customer" />
            </NGridItem>,
            <NGridItem>
              <NInput placeholder="City" />
            </NGridItem>,
          ],
          action: () => <NButton type="primary">Search</NButton>,
        }}
      </RCollapseGrid>
    )
  },
})

Component implementation · Props