Skip to content

Commit

Permalink
Add createComponentFromTemplate
Browse files Browse the repository at this point in the history
  • Loading branch information
tassoevan committed Feb 20, 2020
1 parent a0dbbeb commit 9e3574c
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 35 deletions.
67 changes: 33 additions & 34 deletions client/components/admin/settings/inputs/RoomPickSettingInput.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { Box, Field, Flex, Icon } from '@rocket.chat/fuselage';
import { Blaze } from 'meteor/blaze';
import { Template } from 'meteor/templating';
import React, { useRef, useEffect, useLayoutEffect } from 'react';

import { ResetSettingButton } from '../ResetSettingButton';
import { createComponentFromTemplate } from '../../../../lib/createComponentFromTemplate';

const InputAutocomplete = createComponentFromTemplate(
Template.inputAutocomplete,
(ref) => <div style={{ position: 'relative' }} ref={ref} />,
);

export function RoomPickSettingInput({
_id,
Expand Down Expand Up @@ -31,15 +36,31 @@ export function RoomPickSettingInput({
});

useEffect(() => {
const view = Blaze.renderWithData(Template.inputAutocomplete, {
id: _id,
name: _id,
class: 'search autocomplete rc-input__element',
autocomplete: autocomplete === false ? 'off' : undefined,
readOnly: readonly,
placeholder,
disabled,
settings: {
$('.autocomplete', wrapperRef.current).on('autocompleteselect', (event, doc) => {
const { current: value } = valueRef;
onChangeValue([...value.filter(({ _id }) => _id !== doc._id), doc]);
event.currentTarget.value = '';
event.currentTarget.focus();
});
}, []);

return <>
<Flex.Container>
<Box>
<Field.Label htmlFor={_id} title={_id}>{label}</Field.Label>
{hasResetButton && <ResetSettingButton data-qa-reset-setting-id={_id} onClick={onResetButtonClick} />}
</Box>
</Flex.Container>
<InputAutocomplete
ref={wrapperRef}
id={_id}
name={_id}
class='search autocomplete rc-input__element'
autocomplete={autocomplete === false ? 'off' : undefined}
readOnly={readonly}
placeholder={placeholder}
disabled={disabled}
settings={{
limit: 10,
// inputDelay: 300
rules: [
Expand All @@ -55,30 +76,8 @@ export function RoomPickSettingInput({
sort: 'name',
},
],
},

}, wrapperRef.current);

$('.autocomplete', wrapperRef.current).on('autocompleteselect', (event, doc) => {
const { current: value } = valueRef;
onChangeValue([...value.filter(({ _id }) => _id !== doc._id), doc]);
event.currentTarget.value = '';
event.currentTarget.focus();
});

return () => {
Blaze.remove(view);
};
}, [valueRef]);

return <>
<Flex.Container>
<Box>
<Field.Label htmlFor={_id} title={_id}>{label}</Field.Label>
{hasResetButton && <ResetSettingButton data-qa-reset-setting-id={_id} onClick={onResetButtonClick} />}
</Box>
</Flex.Container>
<div style={{ position: 'relative' }} ref={wrapperRef} />
}}
/>
<ul className='selected-rooms'>
{value.map(({ _id, name }) =>
<li key={_id} className='remove-room' onClick={handleRemoveRoomButtonClick(_id)}>
Expand Down
33 changes: 33 additions & 0 deletions client/lib/createComponentFromTemplate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { useMergedRefs } from '@rocket.chat/fuselage-hooks';
import { Blaze } from 'meteor/blaze';
import { ReactiveVar } from 'meteor/reactive-var';
import { memo, useRef, useLayoutEffect, createElement, forwardRef } from 'react';

export const createComponentFromTemplate = (
template,
renderContainerElement = (ref) => createElement('div', { ref }),
) => {
const component = memo(forwardRef(function(props, ref) {
const stateRef = useRef(new ReactiveVar(props));
const wrapperRef = useRef();
const mergedRef = useMergedRefs(ref, wrapperRef);

useLayoutEffect(() => {
stateRef.current.set(props);
});

useLayoutEffect(() => {
const view = Blaze.renderWithData(template, () => stateRef.current.get(), wrapperRef.current);

return () => {
Blaze.remove(view);
};
}, []);

return renderContainerElement(mergedRef);
}));

component.displayName = template.viewName;

return component;
};
1 change: 0 additions & 1 deletion client/lib/renderComponentIntoLayout.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Meteor } from 'meteor/meteor';
import { BlazeLayout } from 'meteor/kadira:blaze-layout';
import { Template } from 'meteor/templating';

Expand Down

0 comments on commit 9e3574c

Please sign in to comment.