@rhc-shared-components/form-multi-select-component 0.0.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/README.md ADDED
@@ -0,0 +1,30 @@
1
+ # @rhc-shared-components/sample-project
2
+
3
+ > project description
4
+
5
+ [![NPM](https://img.shields.io/npm/v/@rhc-shared-components/sample-project.svg)](https://www.npmjs.com/package/@rhc-shared-components/sample-project) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
6
+
7
+ ## Install
8
+
9
+ ```bash
10
+ npm install --save @rhc-shared-components/sample-project
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ```tsx
16
+ import React, { Component } from 'react'
17
+
18
+ import MyComponent from '@rhc-shared-components/sample-project'
19
+ import '@rhc-shared-components/sample-project/dist/index.css'
20
+
21
+ class Example extends Component {
22
+ render() {
23
+ return <MyComponent />
24
+ }
25
+ }
26
+ ```
27
+
28
+ ## License
29
+
30
+ MIT © [authorGithubUsername](https://github.com/authorGithubUsername)
@@ -0,0 +1,19 @@
1
+ import * as React from 'react';
2
+ import './styles.scss';
3
+ export interface IMultiSelectItem {
4
+ value: string;
5
+ description?: string;
6
+ }
7
+ export interface FormMultiSelectInputProps {
8
+ name: string;
9
+ label: string;
10
+ isRequired?: boolean;
11
+ placeholder?: string;
12
+ selectOptions: IMultiSelectItem[];
13
+ ariaLabel?: string;
14
+ helperText?: string;
15
+ maxItems?: number;
16
+ maxHeight?: string | number;
17
+ }
18
+ declare const FormMultiSelectInput: React.FC<FormMultiSelectInputProps>;
19
+ export default FormMultiSelectInput;
@@ -0,0 +1,2 @@
1
+ import FormMultiSelectInput from './FormMultiSelectInput';
2
+ export { FormMultiSelectInput };
package/dist/index.js ADDED
@@ -0,0 +1,137 @@
1
+ var React = require('react');
2
+ var formik = require('formik');
3
+ var reactCore = require('@patternfly/react-core');
4
+ var formGroupContainer = require('@rhc-shared-components/form-group-container');
5
+ var lodash = require('lodash');
6
+
7
+ function _interopNamespace(e) {
8
+ if (e && e.__esModule) return e;
9
+ var n = Object.create(null);
10
+ if (e) {
11
+ Object.keys(e).forEach(function (k) {
12
+ if (k !== 'default') {
13
+ var d = Object.getOwnPropertyDescriptor(e, k);
14
+ Object.defineProperty(n, k, d.get ? d : {
15
+ enumerable: true,
16
+ get: function () { return e[k]; }
17
+ });
18
+ }
19
+ });
20
+ }
21
+ n["default"] = e;
22
+ return n;
23
+ }
24
+
25
+ var React__namespace = /*#__PURE__*/_interopNamespace(React);
26
+
27
+ function _objectWithoutPropertiesLoose(source, excluded) {
28
+ if (source == null) return {};
29
+ var target = {};
30
+ var sourceKeys = Object.keys(source);
31
+ var key, i;
32
+
33
+ for (i = 0; i < sourceKeys.length; i++) {
34
+ key = sourceKeys[i];
35
+ if (excluded.indexOf(key) >= 0) continue;
36
+ target[key] = source[key];
37
+ }
38
+
39
+ return target;
40
+ }
41
+
42
+ var _excluded = ["label", "isRequired", "children", "selectOptions", "ariaLabel", "placeholder", "helperText", "maxItems", "maxHeight"];
43
+
44
+ var FormMultiSelectInput = function FormMultiSelectInput(_ref) {
45
+ var label = _ref.label,
46
+ isRequired = _ref.isRequired,
47
+ selectOptions = _ref.selectOptions,
48
+ _ref$ariaLabel = _ref.ariaLabel,
49
+ ariaLabel = _ref$ariaLabel === void 0 ? 'Select Input' : _ref$ariaLabel,
50
+ placeholder = _ref.placeholder,
51
+ helperText = _ref.helperText,
52
+ maxItems = _ref.maxItems,
53
+ maxHeight = _ref.maxHeight,
54
+ rest = _objectWithoutPropertiesLoose(_ref, _excluded);
55
+
56
+ var _useField = formik.useField(rest),
57
+ meta = _useField[1];
58
+
59
+ var value = meta.value;
60
+
61
+ var _useFormikContext = formik.useFormikContext(),
62
+ isSubmitting = _useFormikContext.isSubmitting,
63
+ setFieldValue = _useFormikContext.setFieldValue,
64
+ validateField = _useFormikContext.validateField;
65
+
66
+ var _React$useState = React__namespace.useState(false),
67
+ isOpen = _React$useState[0],
68
+ setIsOpen = _React$useState[1];
69
+
70
+ var _React$useState2 = React__namespace.useState([]),
71
+ selected = _React$useState2[0],
72
+ setSelected = _React$useState2[1];
73
+
74
+ var onSelect = function onSelect(_event, selection) {
75
+ if (value.includes(selection)) {
76
+ setFieldValue(rest.name, value.filter(function (item) {
77
+ return item !== selection;
78
+ }));
79
+ validateField(rest.name);
80
+ } else {
81
+ if (maxItems && value.length === maxItems) {
82
+ return;
83
+ }
84
+
85
+ setFieldValue(rest.name, [].concat(value, [selection]));
86
+ validateField(rest.name);
87
+ }
88
+ };
89
+
90
+ React__namespace.useEffect(function () {
91
+ setSelected(value);
92
+ }, [value]);
93
+
94
+ var onClear = function onClear() {
95
+ setFieldValue(rest.name, []);
96
+ validateField(rest.name);
97
+ setIsOpen(false);
98
+ };
99
+
100
+ var onToggle = function onToggle() {
101
+ setIsOpen(!isOpen);
102
+ };
103
+
104
+ return React__namespace.createElement(React__namespace.Fragment, null, React__namespace.createElement(formGroupContainer.FormGroupContainer, {
105
+ validated: meta.error ? reactCore.ValidatedOptions.error : reactCore.ValidatedOptions["default"],
106
+ helperTextInvalid: meta.error,
107
+ isRequired: isRequired,
108
+ fieldId: rest.name,
109
+ label: label,
110
+ helperText: helperText
111
+ }, React__namespace.createElement(reactCore.Select, Object.assign({
112
+ variant: reactCore.SelectVariant.typeaheadMulti,
113
+ "aria-labelledby": rest.name,
114
+ typeAheadAriaLabel: placeholder || 'Select the options',
115
+ placeholderText: placeholder || 'Select the options',
116
+ onToggle: onToggle,
117
+ onSelect: onSelect,
118
+ onClear: onClear,
119
+ selections: selected,
120
+ isOpen: isOpen
121
+ }, maxHeight && {
122
+ maxHeight: maxHeight
123
+ }, {
124
+ isDisabled: isSubmitting,
125
+ "aria-label": ariaLabel
126
+ }, rest), lodash.map(selectOptions, function (option, index) {
127
+ return React__namespace.createElement(reactCore.SelectOption, Object.assign({
128
+ key: index,
129
+ value: option.value,
130
+ label: option.value
131
+ }, option.description && {
132
+ description: option.description
133
+ }));
134
+ }))));
135
+ };
136
+
137
+ exports.FormMultiSelectInput = FormMultiSelectInput;
@@ -0,0 +1,108 @@
1
+ import * as React from 'react';
2
+ import { useField, useFormikContext } from 'formik';
3
+ import { ValidatedOptions, Select, SelectVariant, SelectOption } from '@patternfly/react-core';
4
+ import { FormGroupContainer } from '@rhc-shared-components/form-group-container';
5
+ import { map } from 'lodash';
6
+
7
+ function _objectWithoutPropertiesLoose(source, excluded) {
8
+ if (source == null) return {};
9
+ var target = {};
10
+ var sourceKeys = Object.keys(source);
11
+ var key, i;
12
+
13
+ for (i = 0; i < sourceKeys.length; i++) {
14
+ key = sourceKeys[i];
15
+ if (excluded.indexOf(key) >= 0) continue;
16
+ target[key] = source[key];
17
+ }
18
+
19
+ return target;
20
+ }
21
+
22
+ const _excluded = ["label", "isRequired", "children", "selectOptions", "ariaLabel", "placeholder", "helperText", "maxItems", "maxHeight"];
23
+
24
+ const FormMultiSelectInput = _ref => {
25
+ let {
26
+ label,
27
+ isRequired,
28
+ selectOptions,
29
+ ariaLabel = 'Select Input',
30
+ placeholder,
31
+ helperText,
32
+ maxItems,
33
+ maxHeight
34
+ } = _ref,
35
+ rest = _objectWithoutPropertiesLoose(_ref, _excluded);
36
+
37
+ const [, meta] = useField(rest);
38
+ const {
39
+ value
40
+ } = meta;
41
+ const {
42
+ isSubmitting,
43
+ setFieldValue,
44
+ validateField
45
+ } = useFormikContext();
46
+ const [isOpen, setIsOpen] = React.useState(false);
47
+ const [selected, setSelected] = React.useState([]);
48
+
49
+ const onSelect = (_event, selection) => {
50
+ if (value.includes(selection)) {
51
+ setFieldValue(rest.name, value.filter(item => item !== selection));
52
+ validateField(rest.name);
53
+ } else {
54
+ if (maxItems && value.length === maxItems) {
55
+ return;
56
+ }
57
+
58
+ setFieldValue(rest.name, [...value, selection]);
59
+ validateField(rest.name);
60
+ }
61
+ };
62
+
63
+ React.useEffect(() => {
64
+ setSelected(value);
65
+ }, [value]);
66
+
67
+ const onClear = () => {
68
+ setFieldValue(rest.name, []);
69
+ validateField(rest.name);
70
+ setIsOpen(false);
71
+ };
72
+
73
+ const onToggle = () => {
74
+ setIsOpen(!isOpen);
75
+ };
76
+
77
+ return React.createElement(React.Fragment, null, React.createElement(FormGroupContainer, {
78
+ validated: meta.error ? ValidatedOptions.error : ValidatedOptions.default,
79
+ helperTextInvalid: meta.error,
80
+ isRequired: isRequired,
81
+ fieldId: rest.name,
82
+ label: label,
83
+ helperText: helperText
84
+ }, React.createElement(Select, Object.assign({
85
+ variant: SelectVariant.typeaheadMulti,
86
+ "aria-labelledby": rest.name,
87
+ typeAheadAriaLabel: placeholder || 'Select the options',
88
+ placeholderText: placeholder || 'Select the options',
89
+ onToggle: onToggle,
90
+ onSelect: onSelect,
91
+ onClear: onClear,
92
+ selections: selected,
93
+ isOpen: isOpen
94
+ }, maxHeight && {
95
+ maxHeight
96
+ }, {
97
+ isDisabled: isSubmitting,
98
+ "aria-label": ariaLabel
99
+ }, rest), map(selectOptions, (option, index) => React.createElement(SelectOption, Object.assign({
100
+ key: index,
101
+ value: option.value,
102
+ label: option.value
103
+ }, option.description && {
104
+ description: option.description
105
+ }))))));
106
+ };
107
+
108
+ export { FormMultiSelectInput };
@@ -0,0 +1 @@
1
+ export {};
package/package.json ADDED
@@ -0,0 +1,80 @@
1
+ {
2
+ "name": "@rhc-shared-components/form-multi-select-component",
3
+ "version": "0.0.0",
4
+ "description": "project description",
5
+ "author": "shkale",
6
+ "license": "MIT",
7
+ "repository": "https://gitlab.cee.redhat.com/customer-platform/rhc-shared-components/",
8
+ "main": "dist/index.js",
9
+ "module": "dist/index.modern.js",
10
+ "source": "src/index.tsx",
11
+ "engines": {
12
+ "node": ">=10"
13
+ },
14
+ "scripts": {
15
+ "build": "microbundle --css inline --no-sourcemap --no-compress --format modern,cjs --jsx React.createElement",
16
+ "start": "microbundle watch --css inline --no-compress --format modern,cjs --jsx React.createElement",
17
+ "clean": "rimraf dist",
18
+ "prepare": "run-s clean build",
19
+ "test": "run-s test:unit test:lint test:build",
20
+ "test:build": "run-s build",
21
+ "test:lint": "eslint .",
22
+ "test:unit": "cross-env CI=1 react-scripts test --env=jsdom",
23
+ "test:watch": "react-scripts test --env=jsdom",
24
+ "predeploy": "cd example && yarn install && yarn run build"
25
+ },
26
+ "peerDependencies": {
27
+ "@patternfly/react-core": ">=4.101.3",
28
+ "formik": ">=2.1.4",
29
+ "react": ">=16.13.1",
30
+ "react-dom": ">=16.13.1"
31
+ },
32
+ "devDependencies": {
33
+ "@rhc-shared-components/form-group-container": "^0.2.9",
34
+ "@testing-library/jest-dom": "^4.2.4",
35
+ "@testing-library/react": "^9.5.0",
36
+ "@testing-library/user-event": "^7.2.1",
37
+ "@types/jest": "^25.1.4",
38
+ "@types/node": "^12.12.38",
39
+ "@types/react": "^16.9.27",
40
+ "@types/react-dom": "^16.9.7",
41
+ "@typescript-eslint/eslint-plugin": "^2.26.0",
42
+ "@typescript-eslint/parser": "^2.26.0",
43
+ "babel-eslint": "^10.0.3",
44
+ "cross-env": "^7.0.2",
45
+ "eslint": "^6.8.0",
46
+ "eslint-config-prettier": "^6.7.0",
47
+ "eslint-config-standard": "^14.1.0",
48
+ "eslint-config-standard-react": "^9.2.0",
49
+ "eslint-plugin-import": "^2.18.2",
50
+ "eslint-plugin-node": "^11.0.0",
51
+ "eslint-plugin-prettier": "^3.1.1",
52
+ "eslint-plugin-promise": "^4.2.1",
53
+ "eslint-plugin-react": "^7.17.0",
54
+ "eslint-plugin-standard": "^4.0.1",
55
+ "microbundle": "^0.13.3",
56
+ "node-sass": "^4.0.0",
57
+ "npm-run-all": "^4.1.5",
58
+ "prettier": "^2.0.4",
59
+ "react": "^16.13.1",
60
+ "react-dom": "^16.13.1",
61
+ "react-scripts": "^3.4.1",
62
+ "rimraf": "^3.0.2",
63
+ "typescript": "^3.7.5"
64
+ },
65
+ "dependencies": {
66
+ "@patternfly/react-core": "^4.101.3",
67
+ "@rhc-shared-components/form-group-container": "^0.2.9",
68
+ "@types/lodash": "^4.14.177",
69
+ "formik": "^2.1.4",
70
+ "lodash": "^4.17.21",
71
+ "react": "^16.13.1",
72
+ "react-dom": "^16.13.1"
73
+ },
74
+ "files": [
75
+ "dist"
76
+ ],
77
+ "publishConfig": {
78
+ "access": "public"
79
+ }
80
+ }