Comments (10)
You can create a custom component for the other dropdown and filter out results based on props.record.params
. props.record
holds the state of the form
from adminjs.
@dziraf, can you give me a sample code on how can I achieve this?
from adminjs.
is it possible to create custom component for specific dropdown instead of creating custom component for the complete screen in Admin js?
Please help me as I am new to the admin js & node js.
from adminjs.
Yes, I'll help you tomorrow morning when I'm at work. Currently on a phone and it's inconvenient to write code
from adminjs.
Sure @dziraf, Thanks!!
from adminjs.
This is the default Reference component for edit
action:
Options are loaded on these lines:
const loadOptions = async (inputValue: string): Promise<SelectRecordEnhanced[]> => {
const api = new ApiClient()
const optionRecords = await api.searchRecords({
resourceId,
query: inputValue,
})
return optionRecords.map((optionRecord: RecordJSON) => ({
value: optionRecord.id,
label: optionRecord.title,
record: optionRecord,
}))
}
Let's say you have 2 dropdowns:
- Category
- Subcategory
Subcategory should only display categories belonging to chosen Category. The above function would look this way then:
const loadOptions = async (inputValue: string): Promise<SelectRecordEnhanced[]> => {
const api = new ApiClient()
const optionRecords = await api.searchRecords({
resourceId,
query: inputValue,
params: {
'filters.categoryId': props.record?.params?.categoryId,
}
})
return optionRecords.map((optionRecord: RecordJSON) => ({
value: optionRecord.id,
label: optionRecord.title,
record: optionRecord,
}))
}
Full component:
import React, { FC, useState, useEffect, useMemo, memo } from 'react'
import { FormGroup, FormMessage, SelectAsync, Label } from '@adminjs/design-system'
import { ApiClient, EditPropertyProps, RecordJSON, flat, useTranslation } from 'adminjs'
type CombinedProps = EditPropertyProps
type SelectRecordEnhanced = {
record: RecordJSON;
label: string;
value: any;
}
const EditReference: FC<CombinedProps> = (props) => {
const { tp } = useTranslation()
const { onChange, property, record } = props
const { reference: resourceId } = property
if (!resourceId) {
throw new Error(`Cannot reference resource in property '${property.path}'`)
}
const handleChange = (selected: SelectRecordEnhanced): void => {
if (selected) {
onChange(property.path, selected.value, selected.record)
} else {
onChange(property.path, null)
}
}
const loadOptions = async (inputValue: string): Promise<SelectRecordEnhanced[]> => {
const api = new ApiClient()
const optionRecords = await api.searchRecords({
resourceId,
query: inputValue,
params: {
'filters.categoryId': props.record?.params?.categoryId,
}
})
return optionRecords.map((optionRecord: RecordJSON) => ({
value: optionRecord.id,
label: optionRecord.title,
record: optionRecord,
}))
}
const error = record?.errors[property.path]
const selectedId = useMemo(
() => flat.get(record?.params, property.path) as string | undefined,
[record],
)
const [loadedRecord, setLoadedRecord] = useState<RecordJSON | undefined>()
const [loadingRecord, setLoadingRecord] = useState(0)
useEffect(() => {
if (selectedId) {
setLoadingRecord((c) => c + 1)
const api = new ApiClient()
api.recordAction({
actionName: 'show',
resourceId,
recordId: selectedId,
}).then(({ data }: any) => {
setLoadedRecord(data.record)
}).finally(() => {
setLoadingRecord((c) => c - 1)
})
}
}, [selectedId, resourceId])
const selectedValue = loadedRecord
const selectedOption = (selectedId && selectedValue) ? {
value: selectedValue.id,
label: selectedValue.title,
} : {
value: '',
label: '',
}
return (
<FormGroup error={Boolean(error)}>
<Label property={property} >{tp('property.path'), property.resourceId}</Label>
<SelectAsync
cacheOptions
value={selectedOption}
defaultOptions
loadOptions={loadOptions}
onChange={handleChange}
isClearable
isDisabled={property.isDisabled}
isLoading={!!loadingRecord}
{...property.props}
/>
<FormMessage>{error?.message}</FormMessage>
</FormGroup>
)
}
export default EditReference
To replace the default component you should use ComponentLoader: https://docs.adminjs.co/ui-customization/writing-your-own-components
from adminjs.
Getting this error with your code:
Object literal may only specify known properties, and 'params' does not exist in type '{ resourceId: string; query: string; searchProperty?: string; }'.
`
const loadOptions = async (inputValue: string): Promise<SelectRecordEnhanced[]> => {
const api = new ApiClient()
const optionRecords = await api.searchRecords({
resourceId,
query: inputValue,
params: {
'filters.categoryId': props.record?.params?.categoryId,
}
})
return optionRecords.map((optionRecord: RecordJSON) => ({
value: optionRecord.id,
label: optionRecord.title,
record: optionRecord,
}))
}
const error = record?.errors[property.path]
`
from adminjs.
@dziraf please help me with the error.
from adminjs.
Can anyone help me with this issue?
Thanks!!
from adminjs.
change
const loadOptions = async (inputValue: string): Promise<SelectRecordEnhanced[]> => {
const api = new ApiClient()
const optionRecords = await api.searchRecords({
resourceId,
query: inputValue,
params: {
'filters.categoryId': props.record?.params?.categoryId,
}
})
return optionRecords.map((optionRecord: RecordJSON) => ({
value: optionRecord.id,
label: optionRecord.title,
record: optionRecord,
}))
}
to
const loadOptions = async (inputValue: string): Promise<SelectRecordEnhanced[]> => {
const api = new ApiClient()
const response = await api.resourceAction({
resourceId,
actionName: 'search',
query: inputValue,
params: {
'filters.categoryId': props.record?.params?.categoryId,
}
})
const { records: optionRecords } = response.data
return optionRecords.map((optionRecord: RecordJSON) => ({
value: optionRecord.id,
label: optionRecord.title,
record: optionRecord,
}))
}
from adminjs.
Related Issues (20)
- [Bug]: Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports. Check the render method of `Edit`. HOT 1
- [Bug]: @adminjs/relations many-to-many with Prisma HOT 2
- [Bug]: Filter UI issue HOT 1
- [Feature]: @adminjs/relations remove "delete record" option HOT 2
- [Bug]: AdminJs/Relations One to Many TypeError: Cannot read properties of null (reading 'type') HOT 5
- [Bug]: AdminJs/relations and Prisma Many to Many with undefined references causes `Argument in: Invalid value provided` HOT 1
- [Feature]: 2FA on login? HOT 2
- [Bug]: inputs in Modals lose focus every key press HOT 5
- [Bug]: Bulk action doesn't trigger a list refresh HOT 1
- [Bug]: edit page does not shows the files I uploaded previously HOT 5
- [Bug]: Image in Rich Text Editor not showing when I try re-editing
- [Bug]: adminjs/cli `unable to resolve dependency tree` project init falied for nestjs with typeorm and postgres HOT 2
- [Bug]: Not able to build after adding admin-bro HOT 1
- [Feature]: how i can redirect to url from actions handler HOT 1
- [Bug]: Errors during transpile HOT 1
- Want to add new list option without url and want on click event on that HOT 1
- How to update mongoDB with custom component in list HOT 1
- [Feature]: Auto refresh table data based perdiocally HOT 1
- Can't see console.log HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from adminjs.