JFIFxxC      C  " }!1AQa"q2#BR$3br %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz w!1AQaq"2B #3Rbr{ gilour

File "json-chip-field.tsx"

Full Path: /home/markqprx/iniasli.pro/client/admin/settings/json-chip-field.tsx
File size: 1.05 KB
MIME-type: text/plain
Charset: utf-8

import {useController} from 'react-hook-form';
import React, {useMemo} from 'react';
import {mergeProps} from '@react-aria/utils';
import {
  ChipField,
  ChipValue,
} from '../../ui/forms/input-field/chip-field/chip-field';
import {FormChipFieldProps} from '../../ui/forms/input-field/chip-field/form-chip-field';

export function JsonChipField({children, ...props}: FormChipFieldProps<any>) {
  const {
    field: {onChange, onBlur, value = [], ref},
    fieldState: {invalid, error},
  } = useController({
    name: props.name,
  });

  const arrayValue = useMemo(() => {
    const mixedValue = value as string | string[];
    return typeof mixedValue === 'string' ? JSON.parse(mixedValue) : mixedValue;
  }, [value]);

  const formProps: Partial<FormChipFieldProps<ChipValue>> = {
    onChange: newValue => {
      const jsonValue = JSON.stringify(newValue.map(chip => chip.name));
      onChange(jsonValue);
    },
    onBlur,
    value: arrayValue,
    invalid,
    errorMessage: error?.message,
  };

  return <ChipField ref={ref} {...mergeProps(formProps, props)} />;
}