@dev-fastn-ai/react-core 2.4.2 → 2.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +61 -7
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -603,7 +603,15 @@ For fields of type `select` or `multi-select`, use the `useFieldOptions` hook to
603
603
  ```tsx
604
604
  import { useFieldOptions } from "@fastn-ai/react-core";
605
605
 
606
- function SelectField({ field, value, onChange, isMulti = false }) {
606
+ interface SelectFieldProps {
607
+ field: any;
608
+ value: any;
609
+ onChange: (value: any) => void;
610
+ isMulti?: boolean;
611
+ context?: any;
612
+ }
613
+
614
+ function SelectField({ field, value, onChange, isMulti = false, context }: SelectFieldProps) {
607
615
  const {
608
616
  options,
609
617
  loading,
@@ -613,9 +621,9 @@ function SelectField({ field, value, onChange, isMulti = false }) {
613
621
  error,
614
622
  search,
615
623
  totalLoadedOptions,
616
- } = useFieldOptions(field);
624
+ } = useFieldOptions(field, context);
617
625
 
618
- function handleInputChange(e) {
626
+ function handleInputChange(e: React.ChangeEvent<HTMLInputElement>) {
619
627
  search(e.target.value);
620
628
  }
621
629
 
@@ -623,7 +631,7 @@ function SelectField({ field, value, onChange, isMulti = false }) {
623
631
  if (hasNext && !loadingMore) loadMore();
624
632
  }
625
633
 
626
- function handleSelectChange(selectedOptions) {
634
+ function handleSelectChange(selectedOptions: Array<{ label: string; value: any }>) {
627
635
  if (isMulti) {
628
636
  // For multi-select, value should be an array of { label, value } objects
629
637
  const selectedValues = selectedOptions.map(option => ({
@@ -664,12 +672,12 @@ function SelectField({ field, value, onChange, isMulti = false }) {
664
672
 
665
673
  <select
666
674
  multiple={isMulti}
667
- value={isMulti ? (value || []).map(v => v.value) : (value?.value || '')}
668
- onChange={(e) => {
675
+ value={isMulti ? (value || []).map((v: any) => v.value) : (value?.value || '')}
676
+ onChange={(e: React.ChangeEvent<HTMLSelectElement>) => {
669
677
  if (isMulti) {
670
678
  const selectedOptions = Array.from(e.target.selectedOptions).map(option => {
671
679
  const opt = options.find(o => o.value === option.value);
672
- return { label: opt.label, value: opt.value };
680
+ return { label: opt?.label || '', value: opt?.value };
673
681
  });
674
682
  handleSelectChange(selectedOptions);
675
683
  } else {
@@ -713,6 +721,52 @@ function SelectField({ field, value, onChange, isMulti = false }) {
713
721
  }
714
722
  ```
715
723
 
724
+ #### **Using Context with Select Fields**
725
+
726
+ The `useFieldOptions` hook now supports passing context data to the `getOptions` and `loadMore` methods. This is useful when you need to pass additional form data or parameters to filter or customize the options:
727
+
728
+ ```tsx
729
+ function SelectFieldWithContext({ field, value, onChange, formData }) {
730
+ // Pass form data as context to the hook
731
+ const context = formData; // This will be passed to getOptions and loadMore
732
+
733
+ const {
734
+ options,
735
+ loading,
736
+ loadingMore,
737
+ hasNext,
738
+ loadMore,
739
+ error,
740
+ search,
741
+ totalLoadedOptions,
742
+ } = useFieldOptions(field, context);
743
+
744
+ // ... rest of the component logic remains the same
745
+ }
746
+ ```
747
+
748
+ **When to use context:**
749
+ - When options depend on other form field values
750
+ - When you need to pass authentication tokens or user-specific data
751
+ - When implementing cascading dropdowns (e.g., country → state → city)
752
+ - When options need to be filtered based on current form state
753
+
754
+ **Example with cascading dropdowns:**
755
+ ```tsx
756
+ function CascadingSelectField({ field, value, onChange, parentValue }) {
757
+ // Create context with parent field value
758
+ const context = {
759
+ parentField: parentValue,
760
+ // Add other relevant form data
761
+ };
762
+
763
+ const { options, loading, error, search } = useFieldOptions(field, context);
764
+
765
+ // The getOptions method in your field.optionsSource will receive this context
766
+ // and can use it to filter options based on the parent selection
767
+ }
768
+ ```
769
+
716
770
  ### **Google Drive Picker Fields**
717
771
 
718
772
  For Google Drive file picker fields, handle the file selection flow. These fields also work with `{ label, value }` objects:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dev-fastn-ai/react-core",
3
- "version": "2.4.2",
3
+ "version": "2.4.3",
4
4
  "description": "React hooks and components for integrating Fastn AI connector marketplace into your applications. Built on top of @fastn-ai/core with React Query for optimal performance.",
5
5
  "main": "dist/index.cjs.js",
6
6
  "module": "dist/index.esm.js",