@ttoss/forms 0.19.0 → 0.21.0

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.
@@ -0,0 +1,36 @@
1
+ import { FormField } from './FormField';
2
+ import { Input } from '@ttoss/ui';
3
+ import { PatternFormat, PatternFormatProps } from 'react-number-format';
4
+
5
+ export type FormFieldPatternFormatProps = {
6
+ label?: string;
7
+ name: string;
8
+ } & PatternFormatProps;
9
+
10
+ export const FormFieldPatternFormat = ({
11
+ label,
12
+ name,
13
+ ...patternFormatProps
14
+ }: FormFieldPatternFormatProps) => {
15
+ return (
16
+ <FormField
17
+ name={name}
18
+ label={label}
19
+ render={({ field, fieldState }) => {
20
+ return (
21
+ <PatternFormat
22
+ name={field.name}
23
+ value={field.value}
24
+ onBlur={field.onBlur}
25
+ onValueChange={(values) => {
26
+ field.onChange(values.value);
27
+ }}
28
+ customInput={Input}
29
+ aria-invalid={Boolean(fieldState.error).valueOf()}
30
+ {...patternFormatProps}
31
+ />
32
+ );
33
+ }}
34
+ />
35
+ );
36
+ };
@@ -1,110 +1,57 @@
1
- import { FieldPath, FieldPathValue, FieldValues } from 'react-hook-form';
1
+ import { FieldPath, FieldValues } from 'react-hook-form';
2
2
  import { FormField, FormFieldProps } from './FormField';
3
- import { Select, type SelectProps } from '@ttoss/ui';
4
-
5
- type FormRadioOption = {
6
- value: string | number;
7
- label: string;
8
- };
9
-
10
- type SelectSwitchProps =
11
- | (SelectProps & { placeholder?: never })
12
- | (SelectProps & { defaultValue?: never });
13
-
14
- const checkDefaultValue = <
15
- TFieldValues extends FieldValues = FieldValues,
16
- TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>
17
- >({
18
- options,
19
- defaultValue,
20
- placeholder,
21
- }: {
22
- options: Array<FormRadioOption>;
23
- defaultValue?: FieldPathValue<TFieldValues, TName>;
24
- placeholder?: string;
25
- }): FieldPathValue<TFieldValues, TName> => {
26
- if (defaultValue) {
27
- return defaultValue;
28
- }
29
-
30
- const hasEmptyValue = options.some((opt) => {
31
- return opt.value === '' || opt.value === 0;
32
- });
33
-
34
- const EMPTY_VALUE = '' as FieldPathValue<TFieldValues, TName>;
35
-
36
- if (placeholder && hasEmptyValue) {
37
- return EMPTY_VALUE;
38
- }
39
-
40
- if (placeholder && !hasEmptyValue) {
41
- options.unshift({
42
- label: placeholder,
43
- value: '',
44
- });
45
- return EMPTY_VALUE;
46
- }
47
-
48
- if (!placeholder && defaultValue) return EMPTY_VALUE;
49
- if (options.length === 0) return EMPTY_VALUE;
50
-
51
- return options?.[0]?.value as FieldPathValue<TFieldValues, TName>;
52
- };
3
+ import { Select, SelectOption, type SelectProps } from '@ttoss/ui';
53
4
 
54
5
  type FormFieldSelectProps<
55
- TFieldValues extends FieldValues,
56
- TName extends FieldPath<TFieldValues>
57
- > = SelectSwitchProps &
58
- FormFieldProps & {
59
- label?: string;
60
- name: FieldPath<TFieldValues>;
61
- options: FormRadioOption[];
62
- defaultValue?: FieldPathValue<TFieldValues, TName>;
63
- };
6
+ TFieldValues extends FieldValues = FieldValues,
7
+ TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,
8
+ > = Omit<SelectProps, 'defaultValue'> & FormFieldProps<TFieldValues, TName>;
64
9
 
65
10
  export const FormFieldSelect = <
66
11
  TFieldValues extends FieldValues = FieldValues,
67
- TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>
68
12
  >({
69
13
  label,
70
14
  name,
71
- options,
15
+ id,
16
+ defaultValue,
72
17
  sx,
18
+ css,
19
+ disabled,
20
+ tooltip,
21
+ onTooltipClick,
73
22
  ...selectProps
74
- }: FormFieldSelectProps<TFieldValues, TName>) => {
75
- const { defaultValue, placeholder } = selectProps;
76
-
77
- const checkedDefaultValue = checkDefaultValue<TFieldValues, TName>({
78
- options,
79
- defaultValue,
80
- placeholder,
81
- });
82
-
23
+ }: FormFieldSelectProps<TFieldValues>) => {
83
24
  return (
84
25
  <FormField
85
26
  name={name}
86
27
  label={label}
87
- disabled={selectProps.disabled}
88
- tooltip={selectProps.tooltip}
89
- onTooltipClick={selectProps.onTooltipClick}
28
+ id={id}
29
+ defaultValue={defaultValue}
30
+ disabled={disabled}
31
+ tooltip={tooltip}
32
+ onTooltipClick={onTooltipClick}
90
33
  sx={sx}
91
- defaultValue={checkedDefaultValue}
34
+ css={css}
92
35
  render={({ field, fieldState }) => {
36
+ const value = selectProps.options?.find((option) => {
37
+ if ('value' in option) {
38
+ return option.value === field.value;
39
+ }
40
+
41
+ return false;
42
+ }) as SelectOption | undefined;
43
+
93
44
  return (
94
45
  <Select
95
46
  {...selectProps}
96
47
  {...field}
97
- {...{ ...selectProps, defaultValue: undefined }}
48
+ defaultValue={value}
49
+ value={value}
50
+ onChange={(value) => {
51
+ field.onChange(value?.value);
52
+ }}
98
53
  aria-invalid={fieldState.error ? 'true' : undefined}
99
- >
100
- {options.map((option: FormRadioOption) => {
101
- return (
102
- <option key={option.label} value={option.value}>
103
- {option.label}
104
- </option>
105
- );
106
- })}
107
- </Select>
54
+ />
108
55
  );
109
56
  }}
110
57
  />
package/src/index.ts CHANGED
@@ -7,7 +7,10 @@ export * as yup from 'yup';
7
7
  export { Form } from './Form';
8
8
  export { FormField } from './FormField';
9
9
  export { FormFieldCheckbox } from './FormFieldCheckbox';
10
+ export { FormFieldCreditCardNumber } from './FormFieldCreditCardNumber';
10
11
  export { FormFieldNumericFormat } from './FormFieldNumericFormat';
12
+ export { FormFieldPatternFormat } from './FormFieldPatternFormat';
13
+ export { FormFieldCurrencyInput } from './FormFieldCurrencyInput';
11
14
  export { FormFieldInput } from './FormFieldInput';
12
15
  export { FormFieldPassword } from './FormFieldPassword';
13
16
  export { FormFieldRadio } from './FormFieldRadio';