11import { RelationEditorComponentProps , SelectValue } from "cdm/ComponentsModel" ;
22import React , { useEffect } from "react" ;
33import { useState } from "react" ;
4- import Select from "react-select" ;
54import CustomTagsStyles from "components/styles/TagsStyles" ;
65import { c } from "helpers/StylesHelper" ;
7- import { obtainInfoFromRelation } from "helpers/RelationHelper" ;
86import { TableColumn } from "cdm/FolderModel" ;
97import { Link } from "obsidian-dataview" ;
10- import { OnChangeValue } from "react-select" ;
11- import { StyleVariables } from "helpers/Constants" ;
8+ import { ActionMeta , OnChangeValue } from "react-select" ;
129import ClickAwayListener from "@mui/material/ClickAwayListener" ;
10+ import CreatableSelect from "react-select/creatable" ;
11+ import { RelationalService } from "services/RelationalService" ;
1312
1413const RelationEditor = ( props : RelationEditorComponentProps ) => {
1514 const { defaultCell, persistChange, relationCell } = props ;
@@ -21,18 +20,32 @@ const RelationEditor = (props: RelationEditorComponentProps) => {
2120 ? relationCell . map ( ( link : Link ) => ( {
2221 label : link . fileName ( ) ,
2322 value : link . path ,
24- color : StyleVariables . TEXT_NORMAL ,
23+ color : tableColumn . config . relation_color ,
2524 } ) )
2625 : [ ]
2726 ) ;
2827 const [ relationOptions , setRelationOptions ] = useState ( [ ] ) ;
2928
3029 // onChange handler
31- const handleOnChange = async ( newValue : OnChangeValue < SelectValue , true > ) => {
30+ const handleOnChange = async (
31+ newValue : OnChangeValue < SelectValue , true > ,
32+ actionMeta : ActionMeta < SelectValue >
33+ ) => {
34+ switch ( actionMeta . action ) {
35+ case "create-option" :
36+ await RelationalService . createNoteIntoRelation (
37+ tableColumn . config . related_note_path ,
38+ actionMeta . option . value
39+ ) ;
40+ break ;
41+ default :
42+ // Do nothing
43+ }
44+
3245 const arrayTags = newValue . map ( ( tag ) => ( {
3346 label : tag . label ,
3447 value : tag . value ,
35- color : StyleVariables . TEXT_NORMAL ,
48+ color : tableColumn . config . relation_color ,
3649 } ) ) ;
3750 setRelationValue ( arrayTags ) ;
3851 } ;
@@ -46,14 +59,14 @@ const RelationEditor = (props: RelationEditorComponentProps) => {
4659
4760 useEffect ( ( ) => {
4861 setTimeout ( async ( ) => {
49- const { recordRows } = await obtainInfoFromRelation (
62+ const { recordRows } = await RelationalService . obtainInfoFromRelation (
5063 tableColumn . config . related_note_path
5164 ) ;
5265
5366 const multiOptions = Object . entries ( recordRows ) . map ( ( [ key , value ] ) => ( {
5467 label : value ,
5568 value : key ,
56- color : StyleVariables . TEXT_NORMAL ,
69+ color : tableColumn . config . relation_color ,
5770 } ) ) ;
5871
5972 setRelationOptions ( multiOptions ) ;
@@ -63,7 +76,7 @@ const RelationEditor = (props: RelationEditorComponentProps) => {
6376 return (
6477 < ClickAwayListener onClickAway = { handleClickAway } >
6578 < div className = { c ( "relation" ) } >
66- < Select
79+ < CreatableSelect
6780 defaultValue = { relationValue }
6881 components = { {
6982 DropdownIndicator : ( ) => null ,
0 commit comments