Skip to content

Commit 73f94c6

Browse files
Merge pull request LucasBassetti#208 from digitalWestie/extra-control
Add an option for providing a custom control
2 parents 15c3192 + a85f02a commit 73f94c6

File tree

9 files changed

+110
-18
lines changed

9 files changed

+110
-18
lines changed

dist/react-simple-chatbot.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/react-simple-chatbot.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/ChatBot.jsx

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,12 @@ class ChatBot extends Component {
7272
const chatSteps = {};
7373

7474
const defaultBotSettings = { delay: botDelay, avatar: botAvatar };
75-
const defaultUserSettings = { delay: userDelay, avatar: userAvatar, hideInput: false };
75+
const defaultUserSettings = {
76+
delay: userDelay,
77+
avatar: userAvatar,
78+
hideInput: false,
79+
hideExtraControl: false
80+
};
7681
const defaultCustomSettings = { delay: customDelay };
7782

7883
for (let i = 0, len = steps.length; i < len; i += 1) {
@@ -246,6 +251,9 @@ class ChatBot extends Component {
246251
if (data && data.hideInput) {
247252
currentStep.hideInput = data.hideInput;
248253
}
254+
if (data && data.hideExtraControl) {
255+
currentStep.hideExtraControl = data.hideExtraControl;
256+
}
249257
if (data && data.trigger) {
250258
currentStep.trigger = this.getTriggeredStep(data.trigger, data.value);
251259
}
@@ -592,6 +600,8 @@ class ChatBot extends Component {
592600
const {
593601
className,
594602
contentStyle,
603+
extraControl,
604+
controlStyle,
595605
floating,
596606
floatingIcon,
597607
floatingStyle,
@@ -621,6 +631,15 @@ class ChatBot extends Component {
621631
</Header>
622632
);
623633

634+
let customControl;
635+
if (extraControl !== undefined) {
636+
customControl = React.cloneElement(extraControl, {
637+
disabled,
638+
speaking,
639+
invalid: inputInvalid
640+
});
641+
}
642+
624643
const icon =
625644
(this.isInputValueEmpty() || speaking) && recognitionEnable ? <MicIcon /> : <SubmitIcon />;
626645

@@ -680,18 +699,21 @@ class ChatBot extends Component {
680699
{...inputAttributesOverride}
681700
/>
682701
)}
683-
{!currentStep.hideInput && !hideSubmitButton && (
684-
<SubmitButton
685-
className="rsc-submit-button"
686-
style={submitButtonStyle}
687-
onClick={this.handleSubmitButton}
688-
invalid={inputInvalid}
689-
disabled={disabled}
690-
speaking={speaking}
691-
>
692-
{icon}
693-
</SubmitButton>
694-
)}
702+
<div style={controlStyle} className="rsc-controls">
703+
{!currentStep.hideInput && !currentStep.hideExtraControl && customControl}
704+
{!currentStep.hideInput && !hideSubmitButton && (
705+
<SubmitButton
706+
className="rsc-submit-button"
707+
style={submitButtonStyle}
708+
onClick={this.handleSubmitButton}
709+
invalid={inputInvalid}
710+
disabled={disabled}
711+
speaking={speaking}
712+
>
713+
{icon}
714+
</SubmitButton>
715+
)}
716+
</div>
695717
</Footer>
696718
</ChatBotContainer>
697719
</div>
@@ -711,8 +733,10 @@ ChatBot.propTypes = {
711733
contentStyle: PropTypes.objectOf(PropTypes.any),
712734
customDelay: PropTypes.number,
713735
customStyle: PropTypes.objectOf(PropTypes.any),
736+
controlStyle: PropTypes.objectOf(PropTypes.any),
714737
enableMobileAutoFocus: PropTypes.bool,
715738
enableSmoothScroll: PropTypes.bool,
739+
extraControl: PropTypes.objectOf(PropTypes.element),
716740
floating: PropTypes.bool,
717741
floatingIcon: PropTypes.oneOfType([PropTypes.string, PropTypes.element]),
718742
floatingStyle: PropTypes.objectOf(PropTypes.any),
@@ -759,9 +783,11 @@ ChatBot.defaultProps = {
759783
className: '',
760784
contentStyle: {},
761785
customStyle: {},
786+
controlStyle: { position: 'absolute', right: '0', top: '0' },
762787
customDelay: 1000,
763788
enableMobileAutoFocus: false,
764789
enableSmoothScroll: false,
790+
extraControl: undefined,
765791
floating: false,
766792
floatingIcon: <ChatIcon />,
767793
floatingStyle: {},

lib/components/SubmitButton.jsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ const SubmitButton = styled.button`
2121
opacity: ${props => (props.disabled && !props.invalid ? '.5' : '1')};
2222
outline: none;
2323
padding: 14px 16px 12px 16px;
24-
position: absolute;
25-
right: 0;
26-
top: 0;
2724
&:before {
2825
content: '';
2926
position: absolute;

lib/schemas/customSchema.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ export default [
5454
types: ['boolean'],
5555
required: false
5656
},
57+
{
58+
key: 'hideExtraControl',
59+
types: ['boolean'],
60+
required: false
61+
},
5762
{
5863
key: 'inputAttributes',
5964
types: ['object'],

lib/schemas/optionsSchema.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ export default [
2424
types: ['boolean'],
2525
required: false
2626
},
27+
{
28+
key: 'hideExtraControl',
29+
types: ['boolean'],
30+
required: false
31+
},
2732
{
2833
key: 'inputAttributes',
2934
types: ['object'],

lib/schemas/textSchema.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ export default [
3939
types: ['boolean'],
4040
required: false
4141
},
42+
{
43+
key: 'hideExtraControl',
44+
types: ['boolean'],
45+
required: false
46+
},
4247
{
4348
key: 'inputAttributes',
4449
types: ['object'],

lib/schemas/userSchema.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ export default [
99
types: ['boolean'],
1010
required: true
1111
},
12+
{
13+
key: 'hideExtraControl',
14+
types: ['boolean'],
15+
required: false
16+
},
1217
{
1318
key: 'trigger',
1419
types: ['string', 'number', 'function'],

tests/lib/ChatBot.spec.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,4 +371,53 @@ describe('ChatBot', () => {
371371
expect(wrapper.find('input.rsc-input').props().autoComplete).to.be.equal('firstname');
372372
});
373373
});
374+
375+
describe('Extra control', () => {
376+
const CustomControl = () => (
377+
<button className="my-button">custom</button>
378+
);
379+
const wrapper = mount(
380+
<ChatBot
381+
botDelay={10}
382+
userDelay={10}
383+
customDelay={10}
384+
extraControl={<CustomControl />}
385+
steps={[
386+
{
387+
id: '1',
388+
user: true,
389+
hideExtraControl: false,
390+
trigger: '2'
391+
},
392+
{
393+
id: '2',
394+
user: true,
395+
hideExtraControl: true,
396+
trigger: '3'
397+
},
398+
{
399+
id: '3',
400+
message: 'end',
401+
end: true
402+
}
403+
]}
404+
/>,
405+
);
406+
407+
it('should be rendered with an extra control beside submit button', () => {
408+
expect(wrapper.find('div.rsc-controls button.my-button')).to.have.length(1);
409+
});
410+
411+
it('the extra control should be hidden', () => {
412+
console.log("Setting input value");
413+
wrapper.setState({ inputValue: 'test' });
414+
console.log("Simulate key press");
415+
wrapper.find('input.rsc-input').simulate('keyPress', { key: 'Enter' });
416+
setTimeout(() => {
417+
console.log("testing hidden");
418+
expect(wrapper.find('div.rsc-controls button.my-button')).to.have.length(0);
419+
}, 500);
420+
});
421+
422+
});
374423
});

0 commit comments

Comments
 (0)