@weareconceptstudio/form 0.1.6 → 0.1.7

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.
@@ -1,12 +1,5 @@
1
1
  export default ErrorMessage;
2
- declare function ErrorMessage({ as, errors, name, message, render, ...rest }: {
3
- [x: string]: any;
4
- as: any;
5
- errors: any;
6
- name: any;
7
- message: any;
8
- render: any;
9
- }): any;
2
+ declare function ErrorMessage(props: any): any;
10
3
  declare namespace ErrorMessage {
11
4
  let displayName: string;
12
5
  }
@@ -1,23 +1,24 @@
1
1
  import React, { Fragment, isValidElement, cloneElement, createElement } from 'react';
2
2
  import { useFormContext, get } from 'react-hook-form';
3
- const ErrorMessage = ({ as, errors, name, message, render, ...rest }) => {
3
+ const ErrorMessage = (props) => {
4
+ const { as, errors, name, message, render, ...rest } = props;
4
5
  const methods = useFormContext();
5
6
  const error = get(errors || methods.formState.errors, name);
6
7
  if (!error) {
7
8
  return null;
8
9
  }
9
10
  const { message: messageFromRegister, types } = error;
10
- const props = Object.assign({}, rest, {
11
+ const inlineProps = Object.assign({}, rest, {
11
12
  children: messageFromRegister || message,
12
13
  });
13
14
  return isValidElement(as)
14
- ? cloneElement(as, props)
15
+ ? cloneElement(as, inlineProps)
15
16
  : render
16
17
  ? render({
17
18
  message: messageFromRegister || message,
18
19
  messages: types,
19
20
  })
20
- : createElement(as || Fragment, props);
21
+ : createElement(as || Fragment, inlineProps);
21
22
  };
22
23
  if (process.env.NODE_ENV !== 'production') {
23
24
  ErrorMessage.displayName = 'ErrorMessage';
@@ -1,11 +1,15 @@
1
- import React, { cloneElement, isValidElement } from 'react';
1
+ import React, { cloneElement, isValidElement, useRef } from 'react';
2
2
  import lodashGet from 'lodash.get';
3
3
  import classNames from 'classnames';
4
- import { useTranslation } from '@weareconceptstudio/core';
4
+ import { isForwardRefChild, useTranslation } from '@weareconceptstudio/core';
5
5
  import ErrorMessage from '../../error-message';
6
6
  import { useInput } from '../hooks/useInput';
7
7
  import { useRules } from '../hooks/rules';
8
8
  const FormItem = ({ label, name, children, className, errorKey, required = true, requiredMessage, rules = [] }) => {
9
+ if (!isValidElement(children)) {
10
+ return null;
11
+ }
12
+ const childRef = useRef(null);
9
13
  const { translate } = useTranslation();
10
14
  const { formState } = useInput({
11
15
  name,
@@ -15,7 +19,7 @@ const FormItem = ({ label, name, children, className, errorKey, required = true,
15
19
  requiredMessage,
16
20
  errorKey: errorKey || label,
17
21
  // @ts-ignore
18
- childType: children?.type?.displayName || children?.type?.name || '',
22
+ childType: childRef?.current?.childType || '',
19
23
  }),
20
24
  errorKey: errorKey || label,
21
25
  });
@@ -23,15 +27,18 @@ const FormItem = ({ label, name, children, className, errorKey, required = true,
23
27
  const classString = classNames('form-item', {
24
28
  [className]: className,
25
29
  'has-error': hasError,
26
- disabled: isValidElement(children) && children.props.disabled,
30
+ // @ts-ignore
31
+ disabled: children.props.disabled,
27
32
  });
33
+ // @ts-ignore
34
+ const inlineProps = isForwardRefChild(children) ? { ref: childRef, name } : { name };
28
35
  return (React.createElement("div", { className: classString },
29
36
  React.createElement("div", { className: 'form-item-row' },
30
37
  label ? (React.createElement("div", { className: 'form-item-label' },
31
38
  React.createElement("label", { htmlFor: name, "data-required": required },
32
39
  translate(label),
33
40
  required ? React.createElement("span", { className: 'required-symbol' }, "*") : null))) : null,
34
- React.createElement("div", { className: 'form-item-control' }, isValidElement(children) && cloneElement(children, { name })),
41
+ React.createElement("div", { className: 'form-item-control' }, cloneElement(children, inlineProps)),
35
42
  React.createElement(ErrorMessage, { name: name, as: React.createElement("div", { className: 'error-message' }) }))));
36
43
  };
37
44
  export default FormItem;
@@ -1,6 +1,3 @@
1
1
  export default Input;
2
- declare function Input(props: any): React.JSX.Element;
3
- declare namespace Input {
4
- let displayName: string;
5
- }
2
+ declare const Input: React.ForwardRefExoticComponent<React.RefAttributes<any>>;
6
3
  import React from 'react';
@@ -1,8 +1,9 @@
1
- import React from 'react';
1
+ import React, { forwardRef, useImperativeHandle } from 'react';
2
2
  import BaseInput from './BaseInput';
3
3
  import { useInput } from '../form/hooks/useInput';
4
4
  import { useTranslation } from '@weareconceptstudio/core';
5
- const Input = (props) => {
5
+ const Input = forwardRef((props, ref) => {
6
+ useImperativeHandle(ref, () => ({ childType: 'input' }));
6
7
  const { name, autoComplete = 'new-password', onChange, onFocus, onBlur, onKeyDown, onKeyUp, disabled, className, type = 'text', placeholder } = props;
7
8
  const { translate } = useTranslation();
8
9
  const { field } = useInput({
@@ -13,7 +14,7 @@ const Input = (props) => {
13
14
  });
14
15
  return (React.createElement(BaseInput, { ...props, value: field.value, disabled: disabled },
15
16
  React.createElement("input", { type: type, onFocus: onFocus, onKeyUp: onKeyUp, disabled: disabled, className: className, onKeyDown: onKeyDown, placeholder: translate(placeholder), autoComplete: autoComplete, ...field })));
16
- };
17
+ });
17
18
  if (process.env.NODE_ENV !== 'production') {
18
19
  Input.displayName = 'Input';
19
20
  }
@@ -1,6 +1,3 @@
1
1
  export default TextArea;
2
- declare function TextArea(props: any): React.JSX.Element;
3
- declare namespace TextArea {
4
- let displayName: string;
5
- }
2
+ declare const TextArea: React.ForwardRefExoticComponent<React.RefAttributes<any>>;
6
3
  import React from 'react';
@@ -1,8 +1,9 @@
1
- import React from 'react';
1
+ import React, { forwardRef, useImperativeHandle } from 'react';
2
2
  import BaseInput from '../BaseInput';
3
3
  import { useInput } from '../../form/hooks/useInput';
4
4
  import { useTranslation } from '@weareconceptstudio/core';
5
- const TextArea = (props) => {
5
+ const TextArea = forwardRef((props, ref) => {
6
+ useImperativeHandle(ref, () => ({ childType: 'textarea' }));
6
7
  const { translate } = useTranslation();
7
8
  const { name, placeholder, rows = 5, onChange, onFocus, onBlur, onKeyDown, onKeyUp, disabled, className, autoComplete } = props;
8
9
  const input = name ? useInput({ name, onChange, onBlur }) : null;
@@ -12,7 +13,7 @@ const TextArea = (props) => {
12
13
  };
13
14
  return (React.createElement(BaseInput, { ...props },
14
15
  React.createElement("textarea", { ...field, rows: rows, onFocus: onFocus, onKeyUp: onKeyUp, disabled: disabled, className: className, onKeyDown: onKeyDown, autoComplete: autoComplete, placeholder: translate(placeholder) })));
15
- };
16
+ });
16
17
  if (process.env.NODE_ENV !== 'production') {
17
18
  TextArea.displayName = 'TextArea';
18
19
  }
@@ -395,6 +395,11 @@ const FormStyle = createGlobalStyle `${css `
395
395
  font-weight: 400;
396
396
  color: var(--form_errorMessageColor);
397
397
  transform: translateY(var(--form_errorDistance));
398
+ text-transform: lowercase;
399
+
400
+ &::first-letter {
401
+ text-transform: uppercase;
402
+ }
398
403
  }
399
404
 
400
405
  /* //! Disabled Styles */
@@ -3,7 +3,7 @@ import { rawFileToAsset } from '../utils';
3
3
  import UploadButtonStyle from './style';
4
4
  const UploadButton = (props) => {
5
5
  const { accept, disabled, multiple, setFileList } = props;
6
- const inputRef = useRef();
6
+ const inputRef = useRef(null);
7
7
  const handleFiles = async (files) => {
8
8
  const newFiles = [];
9
9
  for (let i = 0; i < files.length; i++) {
@@ -1,4 +1,4 @@
1
- export function typeFromMime(mime: any): "document" | "image" | "video" | "audio" | "pdf";
1
+ export function typeFromMime(mime: any): "audio" | "video" | "image" | "document" | "pdf";
2
2
  export function rawFileToAsset({ rawFile, isReplace }: {
3
3
  rawFile: any;
4
4
  isReplace?: boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@weareconceptstudio/form",
3
- "version": "0.1.6",
3
+ "version": "0.1.7",
4
4
  "description": "Concept Studio Form",
5
5
  "author": "Concept Studio",
6
6
  "license": "ISC",