Skip to content

Hooks API

Composables provided by Trix Admin.

useSchemaMethods

Schema built-in methods hook, providing navigation, tab, and window methods that can be called in JSON Schema.

Import

typescript
import { useSchemaMethods } from '@/hooks'

Usage

typescript
const { $nav, $tab, $window, schemaMethods } = useSchemaMethods()

// Use in DynamicPage
<VSchema :schema="schema" :methods="schemaMethods" />

Returns

MethodParametersDescription
pushpath: string | objectNavigate to specified path
replacepath: string | objectReplace current page (no history)
backdelta?: numberGo back, default 1 step
forwarddelta?: numberGo forward, default 1 step

$tab - Tab Methods

MethodParametersDescription
closetabId?: stringClose tab, default current tab
closeAndGopath?: stringClose current tab and navigate
closeOtherstabId?: stringClose other tabs
closeLefttabId?: stringClose tabs on the left
closeRighttabId?: stringClose tabs on the right
openpath: string, title?: stringOpen new tab and navigate
openIframeurl: string, title: stringOpen iframe tab
refresh-Refresh current tab
fixtabId?: stringPin tab
unfixtabId?: stringUnpin tab
isFixedtabId?: stringCheck if tab is pinned

$window - Window Methods

MethodParametersDescription
openurl: string, target?: string, features?: stringOpen URL in new window
close-Close current window
print-Print current page

Usage in Schema

Access these built-in methods via $methods:

json
{
  "com": "NButton",
  "events": {
    "click": { "call": "$methods.$nav.push", "args": ["/user/profile"] }
  },
  "children": "Go to Profile"
}
json
{
  "com": "NButton",
  "events": {
    "click": { "call": "$methods.$tab.close" }
  },
  "children": "Close Current Tab"
}
json
{
  "com": "NButton",
  "events": {
    "click": { "call": "$methods.$window.open", "args": ["https://example.com"] }
  },
  "children": "Open New Window"
}

useSchemaLoader

Hook for loading JSON Schema.

Import

typescript
import { useSchemaLoader } from '@/hooks'

Usage

typescript
const {
  schema,
  loading,
  error,
  errorStatus,
  retryCount,
  maxRetries,
  retry
} = useSchemaLoader({
  source: '/mock/schema/page.json',
  watchSource: true,
  immediate: true,
  requireSource: true,
  missingSourceMessage: 'Schema source not configured',
  maxRetries: 3,
  initialLoading: true,
  onLoading: () => console.log('Loading'),
  onLoaded: (schema) => console.log('Loaded', schema),
  onError: (err) => console.error('Error', err)
})

Parameters

ParameterTypeDefaultDescription
sourceRef<string> | string-Schema source URL
watchSourcebooleanfalseWatch source changes
immediatebooleantrueLoad immediately
requireSourceRef<boolean> | booleantrueWhether source is required
missingSourceMessagestring-Error message when source is missing
maxRetriesnumber3Max retry count
initialLoadingbooleantrueInitial loading state
onLoading() => void-Loading callback
onLoaded(schema) => void-Loaded callback
onError(error) => void-Error callback

Returns

PropertyTypeDescription
schemaRef<JsonNode | null>Loaded schema
loadingRef<boolean>Loading state
errorRef<string | null>Error message
errorStatusRef<number | null>HTTP error status code
retryCountRef<number>Current retry count
maxRetriesnumberMax retry count
retry() => voidRetry method

useBoolean

Boolean state management hook.

Import

typescript
import { useBoolean } from '@/hooks'

Usage

typescript
const { bool, setTrue, setFalse, toggle, set } = useBoolean(false)

setTrue()   // Set to true
setFalse()  // Set to false
toggle()    // Toggle value
set(true)   // Set specific value

Parameters

ParameterTypeDefaultDescription
initialValuebooleanfalseInitial value

Returns

PropertyTypeDescription
boolRef<boolean>Boolean value
setTrue() => voidSet to true
setFalse() => voidSet to false
toggle() => voidToggle value
set(value: boolean) => voidSet specific value

useLoading

Loading state management hook.

Import

typescript
import { useLoading } from '@/hooks'

Usage

typescript
const { loading, startLoading, endLoading } = useLoading()

startLoading()  // Start loading
endLoading()    // End loading

// With delay
const { loading, startLoading, endLoading } = useLoading(true, 300)

Parameters

ParameterTypeDefaultDescription
initialValuebooleanfalseInitial value
delaynumber0Delay time (milliseconds)

Returns

PropertyTypeDescription
loadingRef<boolean>Loading state
startLoading() => voidStart loading
endLoading() => voidEnd loading

useContext

Context hook.

Import

typescript
import { useContext } from '@/hooks'

Usage

typescript
const { isMobile, locale, darkMode } = useContext()

Returns

PropertyTypeDescription
isMobileComputedRef<boolean>Is mobile device
localeComputedRef<string>Current locale
darkModeComputedRef<boolean>Is dark mode

usePermission

Permission check hook.

Import

typescript
import { usePermission } from '@/hooks'

Usage

typescript
const { hasPermission, hasAnyPermission, hasAllPermissions } = usePermission()

// Check single permission
if (hasPermission('user:write')) { }

// Check any permission
if (hasAnyPermission(['user:write', 'user:admin'])) { }

// Check all permissions
if (hasAllPermissions(['user:read', 'user:write'])) { }

Returns

MethodParametersReturnsDescription
hasPermissionstringbooleanCheck single permission
hasAnyPermissionstring[]booleanCheck any permission
hasAllPermissionsstring[]booleanCheck all permissions

Released under the MIT License