@axinom/mosaic-ui 0.49.0-rc.8 → 0.49.0-rc.9

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": "@axinom/mosaic-ui",
3
- "version": "0.49.0-rc.8",
3
+ "version": "0.49.0-rc.9",
4
4
  "description": "UI components for building Axinom Mosaic applications",
5
5
  "author": "Axinom",
6
6
  "license": "PROPRIETARY",
@@ -32,7 +32,7 @@
32
32
  "build-storybook": "storybook build"
33
33
  },
34
34
  "dependencies": {
35
- "@axinom/mosaic-core": "^0.4.22-rc.8",
35
+ "@axinom/mosaic-core": "^0.4.22-rc.9",
36
36
  "@faker-js/faker": "^7.4.0",
37
37
  "@popperjs/core": "^2.11.8",
38
38
  "clsx": "^1.1.0",
@@ -105,5 +105,5 @@
105
105
  "publishConfig": {
106
106
  "access": "public"
107
107
  },
108
- "gitHead": "7ce675d991f9ec931f00c32473a8fbf52136e6f3"
108
+ "gitHead": "a448af71faaf175908d9228bc1c1d32be65f9e2d"
109
109
  }
@@ -21,6 +21,7 @@ import { SearcheableOptionsFilter } from '../SelectionTypes/SearcheableOptionsFi
21
21
  import classes from './Filter.scss';
22
22
 
23
23
  export interface FilterProps<T extends Data> {
24
+ selectOnFocus?: boolean;
24
25
  options: FilterType<T>;
25
26
  value?: FilterValue;
26
27
  index?: number;
@@ -42,6 +43,7 @@ export const Filter = <T extends Data>({
42
43
  onFilterChange,
43
44
  onFilterClicked,
44
45
  onValidate,
46
+ selectOnFocus = true,
45
47
  }: PropsWithChildren<FilterProps<T>>): JSX.Element => {
46
48
  const [isExpanded, setIsExpanded] = useState<boolean>(false);
47
49
  const [hasError, setHasError] = useState<boolean>(false);
@@ -116,6 +118,7 @@ export const Filter = <T extends Data>({
116
118
  }
117
119
  onError={onError}
118
120
  onValidate={onValidate}
121
+ selectOnFocus={selectOnFocus}
119
122
  />
120
123
  );
121
124
 
@@ -1,4 +1,4 @@
1
- import { shallow } from 'enzyme';
1
+ import { mount, shallow } from 'enzyme';
2
2
  import React from 'react';
3
3
  import { noop } from '../../../../helpers/utils';
4
4
  import { FreeTextFilter } from './FreeTextFilter';
@@ -42,4 +42,19 @@ describe('FreeTextFilter', () => {
42
42
 
43
43
  expect(error).toBeDefined();
44
44
  });
45
+
46
+ it('selects text on focus when selectOnFocus is true and there is a value', () => {
47
+ const mockValue = 'test value';
48
+ const spy = jest.fn();
49
+
50
+ const wrapper = mount(
51
+ <FreeTextFilter onSelect={spy} value={mockValue} selectOnFocus={true} />,
52
+ );
53
+ const input = wrapper.find('input');
54
+ input.simulate('focus');
55
+
56
+ const inputElement = input.getDOMNode<HTMLInputElement>();
57
+ expect(inputElement.selectionStart).toBe(0);
58
+ expect(inputElement.selectionEnd).toBe(mockValue.length);
59
+ });
45
60
  });
@@ -1,5 +1,5 @@
1
1
  import clsx from 'clsx';
2
- import React, { KeyboardEventHandler, useState } from 'react';
2
+ import React, { KeyboardEventHandler, useRef, useState } from 'react';
3
3
  import { noop } from '../../../../helpers/utils';
4
4
  import { FilterValidationResult, FilterValue } from '../../Filters.model';
5
5
  import classes from './FreeTextFilter.scss';
@@ -17,6 +17,8 @@ export interface FreeTextFilterProps {
17
17
 
18
18
  /** CSS Class name for additional styles */
19
19
  className?: string;
20
+ /** Select text on focus if true (default: true) */
21
+ selectOnFocus?: boolean;
20
22
  }
21
23
 
22
24
  export const FreeTextFilter: React.FC<FreeTextFilterProps> = ({
@@ -25,11 +27,19 @@ export const FreeTextFilter: React.FC<FreeTextFilterProps> = ({
25
27
  onError = noop,
26
28
  onValidate: customValidate,
27
29
  className = '',
30
+ selectOnFocus = true,
28
31
  }) => {
29
32
  const [errorMsg, setErrorMsg] = useState<string>();
30
33
  const ENTER_KEY = 'Enter';
31
34
 
32
35
  const [valueLocal, setValue] = useState(value || '');
36
+ const inputRef = useRef<HTMLInputElement>(null);
37
+
38
+ const onFocusWrapper = (): void => {
39
+ if (selectOnFocus && valueLocal) {
40
+ inputRef.current?.select();
41
+ }
42
+ };
33
43
 
34
44
  const handleKeyDown: KeyboardEventHandler<HTMLInputElement> = (e) => {
35
45
  if (e.key === ENTER_KEY) {
@@ -55,11 +65,13 @@ export const FreeTextFilter: React.FC<FreeTextFilterProps> = ({
55
65
  )}
56
66
  >
57
67
  <input
68
+ ref={inputRef}
58
69
  autoFocus
59
70
  className={clsx(classes.inputValue, errorMsg && classes.hasError)}
60
71
  onKeyDown={handleKeyDown}
61
72
  value={valueLocal as string}
62
73
  onChange={(e) => setValue(e.target.value)}
74
+ onFocus={onFocusWrapper}
63
75
  />
64
76
  {errorMsg !== undefined && <small>{errorMsg}</small>}
65
77
  </div>