@workday/canvas-kit-docs 7.3.15 → 7.3.17

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.
@@ -3,6 +3,6 @@ import SelectWithFormik from './examples/SelectWithFormik';
3
3
 
4
4
  # Canvas Kit Examples
5
5
 
6
- ## Select With Formik
6
+ ## Select Using Formik
7
7
 
8
8
  <ExampleCodeBlock code={SelectWithFormik} />
@@ -3,6 +3,6 @@ import TextInputWithFormik from './examples/TextInputWithFormik';
3
3
 
4
4
  # Canvas Kit Examples
5
5
 
6
- ## Text Input With Formik
6
+ ## Controlled Text Inputs Using Formik
7
7
 
8
8
  <ExampleCodeBlock code={TextInputWithFormik} />
@@ -0,0 +1,8 @@
1
+ import TextInputWithReactHookForm from './examples/TextInputWithReactHookForm';
2
+
3
+
4
+ # Canvas Kit Examples
5
+
6
+ ## Uncontrolled Text Inputs Using React Hook Form
7
+
8
+ <ExampleCodeBlock code={TextInputWithReactHookForm} />
@@ -6,14 +6,19 @@ import {
6
6
  useFormFieldInput,
7
7
  useFormFieldModel,
8
8
  } from '@workday/canvas-kit-preview-react/form-field';
9
+ import {VStack} from '@workday/canvas-kit-react/layout';
10
+ import {PrimaryButton} from '@workday/canvas-kit-react/button';
9
11
 
10
12
  export default () => {
11
13
  const formik = useFormik({
12
14
  initialValues: {
13
15
  selectedBook: '',
14
16
  },
15
- onSubmit: data => {
16
- console.log(data);
17
+ onSubmit: values => {
18
+ // Send data to server
19
+ setTimeout(() => {
20
+ alert(JSON.stringify(values, null, 2));
21
+ }, 0);
17
22
  },
18
23
  });
19
24
 
@@ -21,18 +26,21 @@ export default () => {
21
26
  const formFieldInputProps = useFormFieldInput(model);
22
27
 
23
28
  return (
24
- <form onSubmit={formik.handleSubmit}>
25
- <FormField orientation="vertical" alignItems="stretch">
26
- <FormField.Label>Choose a book</FormField.Label>
27
- <Select
28
- name="selectedBook"
29
- options={bookList}
30
- onChange={event => formik.setFieldValue('selectedBook', event.currentTarget.value)}
31
- value={formik.values.selectedBook}
32
- grow
33
- {...formFieldInputProps}
34
- />
35
- </FormField>
29
+ <form onSubmit={formik.handleSubmit} action=".">
30
+ <VStack spacing="xs" alignItems="flex-start">
31
+ <FormField orientation="vertical" alignSelf="stretch" alignItems="normal">
32
+ <FormField.Label>Choose a book</FormField.Label>
33
+ <Select
34
+ name="selectedBook"
35
+ options={bookList}
36
+ onChange={event => formik.setFieldValue('selectedBook', event.currentTarget.value)}
37
+ value={formik.values.selectedBook}
38
+ grow
39
+ {...formFieldInputProps}
40
+ />
41
+ </FormField>
42
+ <PrimaryButton type="submit">Submit</PrimaryButton>
43
+ </VStack>
36
44
  </form>
37
45
  );
38
46
  };
@@ -1,7 +1,7 @@
1
1
  import React from 'react';
2
2
 
3
3
  import {useFormik} from 'formik';
4
- import * as yup from 'yup';
4
+ import {object, SchemaOf, string} from 'yup';
5
5
 
6
6
  import {TextInput} from '@workday/canvas-kit-preview-react/text-input';
7
7
  import {HStack, VStack} from '@workday/canvas-kit-react/layout';
@@ -9,23 +9,26 @@ import {TertiaryButton, PrimaryButton} from '@workday/canvas-kit-react/button';
9
9
  import {visibleIcon, invisibleIcon} from '@workday/canvas-system-icons-web';
10
10
  import {useUniqueId} from '@workday/canvas-kit-react/common';
11
11
 
12
- export default () => {
13
- const passwordMinimum = 8;
14
- const passwordHint = `Password should be of minimum ${passwordMinimum} characters length`;
15
- const emailRequired = 'Email is required';
16
- const passwordRequired = 'Password is required';
12
+ interface LoginSchema {
13
+ email: string;
14
+ password: string;
15
+ }
17
16
 
18
- const validationSchema = yup.object({
19
- email: yup
20
- .string()
21
- .email('Enter a valid email')
22
- .required(emailRequired),
23
- password: yup
24
- .string()
25
- .min(passwordMinimum, passwordHint)
26
- .required(passwordRequired),
27
- });
17
+ const passwordMinimum = 8;
18
+ const passwordHint = `Password should be of minimum ${passwordMinimum} characters length`;
19
+ const emailRequired = 'Email is required';
20
+ const passwordRequired = 'Password is required';
21
+
22
+ const validationSchema: SchemaOf<LoginSchema> = object({
23
+ email: string()
24
+ .email('Enter a valid email')
25
+ .required(emailRequired),
26
+ password: string()
27
+ .min(passwordMinimum, passwordHint)
28
+ .required(passwordRequired),
29
+ });
28
30
 
31
+ export default () => {
29
32
  const passwordRef = React.useRef<HTMLInputElement>(null);
30
33
  const [showPassword, setShowPassword] = React.useState(false);
31
34
  const passwordId = useUniqueId();
@@ -37,8 +40,7 @@ export default () => {
37
40
  },
38
41
  validationSchema: validationSchema,
39
42
  onSubmit: values => {
40
- passwordRef.current.type = 'password';
41
-
43
+ setShowPassword(false);
42
44
  // Send data to server
43
45
  setTimeout(() => {
44
46
  alert(JSON.stringify(values, null, 2));
@@ -90,7 +92,7 @@ export default () => {
90
92
  aria-controls={`input-${passwordId}`}
91
93
  onClick={() => {
92
94
  setShowPassword(state => !state);
93
- passwordRef.current.focus();
95
+ passwordRef.current?.focus();
94
96
  }}
95
97
  />
96
98
  </HStack>
@@ -0,0 +1,137 @@
1
+ import React from 'react';
2
+
3
+ import {useForm, FieldErrorsImpl} from 'react-hook-form';
4
+ import {object, SchemaOf, string} from 'yup';
5
+
6
+ import {TextInput} from '@workday/canvas-kit-preview-react/text-input';
7
+ import {HStack, VStack} from '@workday/canvas-kit-react/layout';
8
+ import {TertiaryButton, PrimaryButton} from '@workday/canvas-kit-react/button';
9
+ import {visibleIcon, invisibleIcon} from '@workday/canvas-system-icons-web';
10
+ import {useUniqueId} from '@workday/canvas-kit-react/common';
11
+
12
+ type YupValidationResolver = <T extends {}>(
13
+ validationSchema: SchemaOf<T>
14
+ ) => (data: T) => Promise<{values: T; errors: {}} | {values: {}; errors: FieldErrorsImpl<T>}>;
15
+
16
+ const useYupValidationResolver: YupValidationResolver = validationSchema => {
17
+ return React.useCallback(
18
+ async data => {
19
+ try {
20
+ const values = await validationSchema.validate(data, {abortEarly: false});
21
+ return {values, errors: {}};
22
+ } catch (errors) {
23
+ return {
24
+ values: {},
25
+ errors: errors.inner.reduce(
26
+ (allErrors, currentError) => ({
27
+ ...allErrors,
28
+ [currentError.path]: {
29
+ type: currentError.type ?? 'validation',
30
+ message: currentError.message,
31
+ },
32
+ }),
33
+ {}
34
+ ),
35
+ };
36
+ }
37
+ },
38
+ [validationSchema]
39
+ );
40
+ };
41
+
42
+ interface LoginSchema {
43
+ email: string;
44
+ password: string;
45
+ }
46
+
47
+ const passwordMinimum = 8;
48
+ const passwordHint = `Password should be of minimum ${passwordMinimum} characters length`;
49
+ const emailRequired = 'Email is required';
50
+ const passwordRequired = 'Password is required';
51
+
52
+ const validationSchema: SchemaOf<LoginSchema> = object({
53
+ email: string()
54
+ .email('Enter a valid email')
55
+ .required(emailRequired),
56
+ password: string()
57
+ .min(passwordMinimum, passwordHint)
58
+ .required(passwordRequired),
59
+ });
60
+
61
+ export default () => {
62
+ const {
63
+ handleSubmit,
64
+ register,
65
+ formState: {errors},
66
+ } = useForm<LoginSchema>({
67
+ defaultValues: {
68
+ email: 'example@baz.com',
69
+ password: 'foobarbaz',
70
+ },
71
+ resolver: useYupValidationResolver(validationSchema),
72
+ mode: 'onTouched',
73
+ });
74
+
75
+ const onSubmit = handleSubmit(values => {
76
+ setShowPassword(false);
77
+ // Send data to server
78
+ setTimeout(() => {
79
+ alert(JSON.stringify(values, null, 2));
80
+ }, 0);
81
+ });
82
+
83
+ const [showPassword, setShowPassword] = React.useState(false);
84
+ const passwordId = useUniqueId();
85
+ const passwordRef = React.useRef<HTMLInputElement | null>(null);
86
+ const {ref: passwordCallbackRef, ...passwordRegistration} = register('password');
87
+ const combinePasswordRef = (ref: HTMLInputElement | null) => {
88
+ passwordCallbackRef(ref);
89
+ passwordRef.current = ref;
90
+ };
91
+
92
+ return (
93
+ <form onSubmit={onSubmit} action=".">
94
+ <VStack spacing="xs" alignItems="flex-start">
95
+ <TextInput orientation="vertical" isRequired={true} hasError={!!errors.email}>
96
+ <TextInput.Label>Email</TextInput.Label>
97
+ <TextInput.Field
98
+ {...register('email')}
99
+ autoComplete="username"
100
+ placeholder="yourName@example.com"
101
+ />
102
+ <TextInput.Hint>{errors.email?.message}</TextInput.Hint>
103
+ </TextInput>
104
+ <TextInput
105
+ orientation="vertical"
106
+ id={passwordId}
107
+ isRequired={true}
108
+ hasError={!!errors.password}
109
+ >
110
+ <TextInput.Label>Password</TextInput.Label>
111
+ <HStack spacing="xxs">
112
+ <TextInput.Field
113
+ {...passwordRegistration}
114
+ type={showPassword ? 'text' : 'password'}
115
+ autoComplete="current-password"
116
+ spellCheck={false}
117
+ ref={combinePasswordRef}
118
+ />
119
+ <TertiaryButton
120
+ type="button"
121
+ icon={showPassword ? invisibleIcon : visibleIcon}
122
+ aria-label={showPassword ? 'Hide Password' : 'Show Password'}
123
+ aria-controls={`input-${passwordId}`}
124
+ onClick={() => {
125
+ setShowPassword(state => !state);
126
+ passwordRef.current?.focus();
127
+ }}
128
+ />
129
+ </HStack>
130
+ <TextInput.Hint>{errors.password?.message || passwordHint}</TextInput.Hint>
131
+ </TextInput>
132
+
133
+ <PrimaryButton type="submit">Submit</PrimaryButton>
134
+ </VStack>
135
+ </form>
136
+ );
137
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@workday/canvas-kit-docs",
3
- "version": "7.3.15",
3
+ "version": "7.3.17",
4
4
  "description": "Documentation components of Canvas Kit components",
5
5
  "author": "Workday, Inc. (https://www.workday.com)",
6
6
  "license": "Apache-2.0",
@@ -42,7 +42,7 @@
42
42
  ],
43
43
  "dependencies": {
44
44
  "@storybook/csf": "0.0.1",
45
- "@workday/canvas-kit-react": "^7.3.15"
45
+ "@workday/canvas-kit-react": "^7.3.17"
46
46
  },
47
47
  "devDependencies": {
48
48
  "fs-extra": "^10.0.0",
@@ -50,5 +50,5 @@
50
50
  "mkdirp": "^1.0.3",
51
51
  "typescript": "4.1"
52
52
  },
53
- "gitHead": "fd5ab8a8e931a200a15f7fa2640f917805b044e9"
53
+ "gitHead": "f10663f4193a94f3110470f28aa467a724562e85"
54
54
  }