@hyphen/hyphen-components 5.1.0 → 5.2.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hyphen/hyphen-components",
3
- "version": "5.1.0",
3
+ "version": "5.2.0",
4
4
  "license": "MIT",
5
5
  "author": {
6
6
  "name": "@hyphen"
@@ -19,6 +19,8 @@ import { FormikToggleGroup } from './FormikToggleGroup/FormikToggleGroup';
19
19
  import { ToggleGroupItem } from '../ToggleGroup/ToggleGroup';
20
20
  import { Tooltip, TooltipContent, TooltipProvider } from '../Tooltip/Tooltip';
21
21
  import { TooltipTrigger } from '@radix-ui/react-tooltip';
22
+ import { FormikToggleGroupMulti } from './FormikToggleGroupMulti/FormikToggleGroupMulti';
23
+ import { Icon } from '../Icon/Icon';
22
24
 
23
25
  const meta = {
24
26
  title: 'Patterns/Formik Form',
@@ -55,6 +57,12 @@ export const FormikForm = () =>
55
57
  { id: 'medium', value: 'medium', label: 'Medium' },
56
58
  { id: 'large', value: 'large', label: 'Large' },
57
59
  ];
60
+ const locationOptions = [
61
+ { id: 'ca', value: 'ca', label: 'Canada' },
62
+ { id: 'mx', value: 'mx', label: 'Mexico' },
63
+ { id: 'us', value: 'us', label: 'United States' },
64
+ ];
65
+
58
66
  const availabilityOptions = [
59
67
  {
60
68
  id: '99',
@@ -136,6 +144,9 @@ export const FormikForm = () =>
136
144
  if (!values.availability) {
137
145
  errors.availability = 'required';
138
146
  }
147
+ if (values.locations.length < 1) {
148
+ errors.locations = 'required';
149
+ }
139
150
  return errors;
140
151
  };
141
152
  const handleSubmit = (
@@ -178,9 +189,9 @@ export const FormikForm = () =>
178
189
  message: '',
179
190
  country: '',
180
191
  availability: '99',
192
+ locations: [] as string[],
181
193
  }}
182
194
  validate={handleValidation}
183
- validateOnChange={false}
184
195
  onSubmit={handleSubmit}
185
196
  >
186
197
  {({ isSubmitting, values, setFieldValue, handleBlur, errors }) => (
@@ -344,6 +355,23 @@ export const FormikForm = () =>
344
355
  </Field>
345
356
  </TooltipProvider>
346
357
 
358
+ <FormikToggleGroupMulti
359
+ name="locations"
360
+ label="Locations"
361
+ helpText="Select all that apply"
362
+ options={locationOptions}
363
+ variant="outline"
364
+ >
365
+ {locationOptions.map((option) => (
366
+ <ToggleGroupItem key={option.id} value={option.value}>
367
+ {values.locations.includes(option.value) && (
368
+ <Icon name="check" />
369
+ )}{' '}
370
+ {option.label}
371
+ </ToggleGroupItem>
372
+ ))}
373
+ </FormikToggleGroupMulti>
374
+
347
375
  <DateInput
348
376
  datePickerProps={{
349
377
  onChange: (date) => {
@@ -18,7 +18,7 @@ export interface FormikToggleGroupProps {
18
18
  setFieldValue: (field: string, value: any) => void;
19
19
  };
20
20
  options: Array<{
21
- id: string | number;
21
+ id: string;
22
22
  value: string;
23
23
  label: React.ReactNode;
24
24
  disabled?: boolean;
@@ -58,6 +58,7 @@ export const FormikToggleGroup: React.FC<FormikToggleGroupProps> = ({
58
58
  ? children
59
59
  : options.map((option) => (
60
60
  <ToggleGroupItem
61
+ id={option.id}
61
62
  value={option.value}
62
63
  key={option.id}
63
64
  disabled={!!option.disabled}
@@ -0,0 +1,74 @@
1
+ import React from 'react';
2
+ import { FieldArray, useField } from 'formik';
3
+ import {
4
+ ToggleGroup,
5
+ ToggleGroupItem,
6
+ ToggleVariant,
7
+ } from '../../ToggleGroup/ToggleGroup';
8
+ import { Box } from '../../Box/Box';
9
+ import { HelpText } from '../../HelpText/HelpText';
10
+
11
+ export interface FormikToggleGroupMultiProps {
12
+ name: string;
13
+ options: Array<{
14
+ id: string;
15
+ value: string;
16
+ label: React.ReactNode;
17
+ disabled?: boolean;
18
+ }>;
19
+ helpText?: string;
20
+ label?: string;
21
+ children?: React.ReactNode;
22
+ variant: ToggleVariant;
23
+ }
24
+
25
+ export const FormikToggleGroupMulti = ({
26
+ name,
27
+ options,
28
+ helpText,
29
+ label,
30
+ children,
31
+ variant,
32
+ ...props
33
+ }: FormikToggleGroupMultiProps) => {
34
+ const [field, meta, helpers] = useField(name);
35
+ return (
36
+ <Box gap="sm">
37
+ {(label || helpText) && (
38
+ <div>
39
+ {label && <Box fontSize="sm">{label}</Box>}
40
+ {helpText && <HelpText>{helpText}</HelpText>}
41
+ </div>
42
+ )}
43
+ <FieldArray name={name}>
44
+ {() => (
45
+ <ToggleGroup
46
+ {...props}
47
+ name={name}
48
+ onValueChange={(selectedValues: string[]) => {
49
+ helpers.setValue(selectedValues);
50
+ helpers.setTouched(true);
51
+ }}
52
+ value={field.value}
53
+ error={meta.error}
54
+ type="multiple"
55
+ variant={variant}
56
+ >
57
+ {children
58
+ ? children
59
+ : options.map((option) => (
60
+ <ToggleGroupItem
61
+ key={option.id}
62
+ id={option.id}
63
+ value={option.value}
64
+ disabled={!!option.disabled}
65
+ >
66
+ {option.label}
67
+ </ToggleGroupItem>
68
+ ))}
69
+ </ToggleGroup>
70
+ )}
71
+ </FieldArray>
72
+ </Box>
73
+ );
74
+ };
@@ -7,7 +7,7 @@ import { InputValidationMessage } from '../InputValidationMessage/InputValidatio
7
7
  import { BaseSpacing, ResponsiveProp } from '../../types';
8
8
  import { cssShorthandToClasses } from '../../lib';
9
9
 
10
- type ToggleVariant = 'default' | 'outline';
10
+ export type ToggleVariant = 'default' | 'outline';
11
11
 
12
12
  const ToggleGroupContext = createContext<{ variant: ToggleVariant }>({
13
13
  variant: 'default',