NpConfigProvider

Global defaults for components, used to change the default value of component props in one place.

Priority: component prop > NpConfigProvider > component built-in default.

Basic Usage

Use component-options to set default props of components. The key is the component name (PascalCase without the Np prefix).

浏览器不支持,请手动复制 basic.en.vue

Props

NameDescriptionTypeDefault
componentOptionsDefault props overrides of componentsNpComponentOptions- (inherited from the outer provider; treated as {} without one)
localeLanguage of the built-in texts, supports the built-in 'zh' / 'en' and a custom locale messages object; inherited from the outer NpConfigProvider when omittedNpLocaleOption ('zh' | 'en' | NpLocaleMessagesInput)'zh' (without outer provider)

NpComponentOptions

Only display-related props are exposed (Button is the exception: all props except click / onClick are configurable; Form's column.props is passed through per control type). The configurable fields of each component are listed below, and every type is defined and exported by the component itself.

NameConfigurableType
ButtonAll props except click / onClickNpButtonConfigProps
Cellhover / arrow / bordered / sizeNpCellConfigProps
CellGroupsize / borderedNpCellGroupConfigProps
CheckboxloadingDelay / propsNpCheckboxConfigProps
Radiotype / loadingDelay / propsNpRadioConfigProps
UploadpropsNpUploadConfigProps
Filtertrigger / placementNpFilterConfigProps
FormenterSubmitMode / grid / props / column / footerNpFormConfigProps
TablerowKey / props / formProps / column / operationNpTableConfigProps

NpFormConfigProps

NameDescriptionType
enterSubmitModeEnter-to-submit mode'last' | 'all' | false
gridGrid layout configPartial<GridProps>
propsProps passed to n-formFormOption['props']
columnDefaults of each form itemNpFormColumnConfigProps
column.propsDefault control props, choose valid ones by typeAnyObject
column.formItemPropsDefault form item propsFormGridItemProps
footerDefaults of the form footerNpFormFooterConfigProps

NpFormFooterConfigProps

NameDescriptionType
formItemPropsProps of the footer form item, span controls its grid spanFormGridItemProps
styleStyle of the footer content containerCSSProperties
alignFooter alignment, validation feedback aligns right when right'left' | 'right'
submitErrorFeedbackWhether to show the submit error in the footer feedbackboolean
submitBtnSubmit button config, false to hide itfalse | { text?, props? }
resetBtnReset button config, false to hide itfalse | { text?, props? }

NpTableConfigProps

NameDescriptionType
rowKeyRow key field namestring
propsProps passed to n-data-tableTableOption['props']
formPropsProps of the form inside the CRUD dialogFormOption['props']
columnDefaults of each columnNpTableColumnConfigProps
column.propsDefault naive-ui column props (children excluded, it belongs to the column structure)Omit<TableColumn['props'], 'children'>
column.formPropsDefault form props of the CRUD dialog column, without content / data source / data contract / state / event fieldsNpTableColumnFormConfigProps
column.formProps.addPropsDefault form props for the add dialog, same-name fields take priority over column.formPropsNpTableColumnFormConfigProps['addProps']
column.formProps.editPropsDefault form props for the edit dialog, same-name fields take priority over column.formPropsNpTableColumnFormConfigProps['editProps']
operationDefaults of the operation columnNpTableOperationConfigProps

NpTableOperationConfigProps

NameDescriptionType
labelTitle of the operation column, defaults to Table.operation.label in the localestring
defaultOptionsDefault config of the built-in edit / delete items, merged into the built-in defaults item by itemTableOperationDefaultOptions
othersNTableColumn props except key / title / render / type (e.g. width / align / fixed / ellipsis), used as the defaults of the operation columnOmit<NTableColumn<T>, 'key' | 'title' | 'render' | 'type'>

The same config in option.operation takes priority; option.operation: false still hides the whole operation column.

Locale

Use locale to change the texts hard-coded inside components. The built-in 'zh' (default) and 'en' are supported, and a custom locale messages object (NpLocaleMessagesInput) can be passed directly to support any language.

浏览器不支持,请手动复制 locale.en.vue

Structure of the locale messages (NpLocaleMessages):

FieldDescriptionChinese default
Form.footer.submitBtnTextSubmit button text提交
Form.footer.resetBtnTextReset button text重置
Form.uploadPendingTipValidation message when the file upload is not finished文件上传未完成
Upload.uploadFailedTipPrefix of the upload failure message上传失败
Upload.unknownErrorText shown when the upload error has no message未知错误
Upload.uploadBtnTextText of the default upload button上传文件
Filter.clearAllTextText of the clear-all-filters item清除所有筛选
Table.operation.labelTitle of the operation column操作
Table.operation.editLabelText of the edit item编辑
Table.operation.delLabelText of the delete item删除
Table.display.titleTitle of the column display popover显示设置
Table.display.keepOneTipTip shown when hiding the last visible column至少保留一列显示
Table.search.placeholderPlaceholder of the search input请输入关键词搜索
Table.addBtnTextText of the header add button添加
Table.dialog.addTitleTitle of the create dialog新增
Table.dialog.editTitleTitle of the edit dialog编辑
Table.dialog.confirmTextConfirm button text of the create / edit dialog确认
Table.dialog.cancelTextCancel button text of the create / edit dialog取消
Table.deleteDialog.titleTitle of the delete confirm dialog确认删除
Table.deleteDialog.contentContent of the delete confirm dialog确定要删除该条数据吗?此操作不可恢复。
Table.deleteDialog.confirmTextConfirm button text of the delete confirm dialog确认删除
Table.deleteDialog.cancelTextCancel button text of the delete confirm dialog取消

Priority: component config (e.g. option.footer.submitBtn.text / option.operation.options) > componentOptions > locale.

The locale messages and types are exported for customization or direct reading:

import { zhCN, enUS, npLocales } from 'naive-plus'
import type {
  NpLocale,
  NpLocaleMessages,
  NpLocaleMessagesInput,
  NpLocaleOption,
} from 'naive-plus'

For other languages, pass a custom locale messages object to locale (NpLocaleMessagesInput only requires the fields to override, the fields left out keep the built-in texts):

import type { NpLocaleMessagesInput } from 'naive-plus'

const jaJP: NpLocaleMessagesInput = {
  Form: {
    footer: { submitBtnText: '送信', resetBtnText: 'リセット' },
  },
}
<NpConfigProvider :locale="jaJP">
  <!-- children -->
</NpConfigProvider>

zh / en are built in. Full localized texts are provided by the locale messages (NpLocaleMessages); componentOptions can also override texts through the exposed fields such as Form.footer.submitBtn.text, but prefer locale when you need complete localization.

Notes

  • The following kinds of props cannot be set globally, they must be specified at the usage site:
    • Content: label / value / description / icon / title (including the footer slot content of NpCell; except the title of the NpTable operation column, see Table.operation.label)
    • State: modelValue / checked / keys / params
    • Data source: options / items
    • Pagination: pagination (pagination state is managed inside NpTable)
    • Data contract: labelField / valueField / urlOnly
    • Event callbacks: click / onClick / onChange
  • The props passed to naive-ui (and Button's style) are merged with a single level (shallow merge), so both provider and component settings take effect, and the component's own value wins on conflicts; nested objects are not deep merged and are taken from the component itself as a whole.
  • NpForm's column is merged into every form item (props into control props, formItemProps into form item props).
  • NpForm's footer is merged with option.footer layer by layer (formItemProps / style / submitBtn / resetBtn are unioned), while option.footer === false still hides the whole footer.
  • NpTable's column is merged into every column (props into column props, formProps into the column config of the CRUD form, where formProps.props / formProps.formItemProps (including the ones inside addProps / editProps) are merged with a single level); column-own fields (content / data source / data contract / state / event) are stripped and never inherited from the global config.
  • Size / border of cells inside NpCellGroup is controlled by NpCellGroup's own props, not by the Cell options.
  • Nested NpConfigProvider inherits the outer componentOptions when the inner one does not pass componentOptions; passing it explicitly replaces the outer config instead of merging it. locale is inherited from the outer provider when omitted, and the inner value wins when specified.

Custom Components

When a custom component needs to read the global configuration, inject npConfigProviderInjectionKey (its value is Ref<NpComponentOptions | undefined>):

import { computed, inject } from 'vue'
import { npConfigProviderInjectionKey } from 'naive-plus'

const componentOptions = inject(npConfigProviderInjectionKey, undefined)
// Read the global Cell config and fall back to the built-in defaults
const cellOptions = computed(() => ({
  size: 'medium',
  ...componentOptions?.value?.Cell,
}))

To read the locale messages, inject npLocaleInjectionKey (its value is Ref<NpLocaleMessages>); it falls back to Chinese when not provided:

import { computed, inject } from 'vue'
import { npLocaleInjectionKey, zhCN } from 'naive-plus'

const locale = inject(npLocaleInjectionKey, undefined)
const messages = computed(() => locale?.value ?? zhCN)