Skip to content

Commit 6cdec77

Browse files
authored
fix: Input state (#142)
1 parent 967593b commit 6cdec77

File tree

5 files changed

+29
-25
lines changed

5 files changed

+29
-25
lines changed

packages/fuselage-ui-kit/src/Input.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
import { Block } from './Block';
1313
import { useBlockContext } from './hooks';
1414

15-
export const Input = ({ label, element, parser, index, hint, context }) => {
15+
export const Input = React.memo(({ label, element, parser, index, hint, context }) => {
1616
const [{ error }] = useBlockContext(element, context);
1717
return (
1818
<Block>
@@ -26,9 +26,9 @@ export const Input = ({ label, element, parser, index, hint, context }) => {
2626
</FieldGroup>
2727
</Block>
2828
);
29-
};
29+
});
3030

31-
export const PlainInput = ({ element, context, index, parser }) => {
31+
export const PlainInput = React.memo(({ element, context, index, parser }) => {
3232
const [{ loading, value, error }, action] = useBlockContext(element, context);
3333
const { multiline, actionId, placeholder } = element;
3434
const Component = multiline ? TextAreaInput : TextInput;
@@ -41,9 +41,9 @@ export const PlainInput = ({ element, context, index, parser }) => {
4141
rows={6}
4242
error={error}
4343
value={value}
44-
onInput={action}
45-
onChange={() => {}}
44+
// onInput={action}
45+
onChange={action}
4646
placeholder={parser.plainText(placeholder)}
4747
/>
4848
);
49-
};
49+
});

packages/fuselage-ui-kit/src/hooks.js

+16-14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useContext, useState } from 'react';
1+
import React, { useContext, useState, useCallback } from 'react';
22
import {
33
BLOCK_CONTEXT,
44
} from '@rocket.chat/ui-kit';
@@ -14,26 +14,28 @@ export const kitContext = React.createContext(defaultContext);
1414

1515
export const useBlockContext = ({ blockId, actionId, appId, initialValue }, context) => {
1616
const { action, appId: appIdFromContext, viewId, state, errors, values = {} } = useContext(kitContext);
17-
const { value = initialValue } = values[actionId] || {};
18-
// const [value, setValue] = useState(initialValue);
17+
const { value: _value = initialValue } = values[actionId] || {};
18+
const [value, setValue] = useState(_value);
1919
const [loading, setLoading] = useState(false);
2020

2121
const error = errors && actionId && errors[actionId];
2222

23+
const actionFunction = useCallback(async ({ target: { value } }) => {
24+
setLoading(true);
25+
await action({ blockId, appId: appId || appIdFromContext, actionId, value, viewId });
26+
setLoading(false);
27+
}, []);
28+
29+
const stateFunction = useCallback(async ({ target: { value } }) => {
30+
setValue(value);
31+
await state({ blockId, appId, actionId, value });
32+
}, []);
33+
2334
if ([BLOCK_CONTEXT.SECTION, BLOCK_CONTEXT.ACTION].includes(context)) {
24-
return [{ loading, setLoading, error }, async ({ target: { value } }) => {
25-
setLoading(true);
26-
await action({ blockId, appId: appId || appIdFromContext, actionId, value, viewId });
27-
setLoading(false);
28-
}];
35+
return [{ loading, setLoading, error }, actionFunction];
2936
}
3037

31-
return [{ loading, setLoading, value, error }, async ({ target: { value } }) => {
32-
// setValue(value);
33-
setLoading(true);
34-
await state({ blockId, appId, actionId, value });
35-
setLoading(false);
36-
}];
38+
return [{ loading, setLoading, value, error }, stateFunction];
3739
};
3840

3941
export const getStyle = (style) => {

packages/fuselage/src/components/Select/MultiSelect.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const Container = Box.extend('rcx-select', 'div');
1111

1212
const SelectedOptions = React.memo((props) => <Chip {...props}/>);
1313

14-
const prevent = (e) => e.preventDefault() & e.stopPropagation() & e.stopImmediatePropagation();
14+
const prevent = (e) => e.preventDefault() & e.stopPropagation() & e.nativeEvent.stopImmediatePropagation();
1515
export const MultiSelect = ({
1616
value,
1717
filter,
@@ -71,7 +71,7 @@ export const MultiSelect = ({
7171
<Box is='div'>
7272
<Margins all='neg-x8'>
7373
<Chip.Wrapper role='listbox'>
74-
<Anchor disabled={disabled} ref={ref} aria-haspopup='listbox' onClick={show} onBlur={hide} onKeyUp={handleKeyUp} onKeyDown={handleKeyDown} style={{ order: 1 }} mod-undecorated children={option || placeholder}/>
74+
<Anchor disabled={disabled} ref={ref} aria-haspopup='listbox' onClick={show} onBlur={hide} onKeyUp={handleKeyUp} onKeyDown={handleKeyDown} style={{ order: 1 }} mod-undecorated children={!value ? option || placeholder : null}/>
7575
{currentValue.map((value) => <SelectedOptions tabIndex={-1} role='option' key={value} onMouseDown={(e) => prevent(e) & internalChanged([value]) && false} children={getLabel(options.find(([val]) => val === value))}/>)}
7676
</Chip.Wrapper>
7777
</Margins>

packages/fuselage/src/components/Select/Select.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export const Select = ({
8181
<Flex.Item>
8282
<Flex.Container>
8383
<Margins inline='neg-x4'>
84-
<Wrapper>
84+
<Wrapper mod-hidden={!!visibleText}>
8585
{ visibleText && <Flex.Item grow={1}>
8686
<Margins inline='x4'><Box is='span' textStyle='p2' textColor='hint' className='rcx-select__placeholder'>{visibleText}</Box></Margins>
8787
</Flex.Item>}
@@ -101,6 +101,6 @@ export const SelectFiltered = ({
101101
...props
102102
}) => {
103103
const [filter, setFilter] = useState('');
104-
const anchor = useCallback(React.forwardRef(({ children, filter, ...props }, ref) => <Margins inline='x4'><Flex.Item grow={1}><InputBox.Input ref={ref} placeholder={placeholder} value={filter} onChange={() => {}} onInput={(e) => setFilter(e.currentTarget.value)} {...props} mod-undecorated={true}/></Flex.Item></Margins>), []);
104+
const anchor = useCallback(React.forwardRef(({ children, filter, ...props }, ref) => <Margins inline='x4'><Flex.Item grow={1}><InputBox.Input className='rcx-select__focus' ref={ref} placeholder={placeholder} value={filter} onChange={() => {}} onInput={(e) => setFilter(e.currentTarget.value)} {...props} mod-undecorated={true}/></Flex.Item></Margins>), []);
105105
return <Select filter={filter} options={options} {...props} anchor={anchor}/>;
106106
};

packages/fuselage/src/components/Select/styles.scss

+3-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@
3737
flex-grow: 1;
3838

3939
&--hidden {
40-
opacity: 0;
40+
.rcx-select__focus {
41+
opacity: 0;
42+
}
4143
}
4244
}
4345

0 commit comments

Comments
 (0)