Skip to content

Commit 92b5b43

Browse files
committed
add custom delay in text and custom step
1 parent 1cfdb5c commit 92b5b43

File tree

6 files changed

+61
-26
lines changed

6 files changed

+61
-26
lines changed

lib/ChatBot.jsx

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ class ChatBot extends Component {
3030
opened: props.opened || !props.floating,
3131
inputValue: '',
3232
inputInvalid: false,
33-
defaulBotSettings: {},
34-
defaulUserSettings: {},
33+
defaultBotSettings: {},
34+
defaultUserSettings: {},
3535
};
3636

3737
this.renderStep = this.renderStep.bind(this);
@@ -41,21 +41,32 @@ class ChatBot extends Component {
4141
}
4242

4343
componentWillMount() {
44-
const { botDelay, botAvatar, userDelay, userAvatar } = this.props;
44+
const { botDelay, botAvatar, customDelay, userDelay, userAvatar } = this.props;
4545
const steps = {};
4646

47-
const defaulBotSettings = {
47+
const defaultBotSettings = {
4848
delay: botDelay,
4949
avatar: botAvatar,
5050
};
51-
const defaulUserSettings = {
51+
const defaultUserSettings = {
5252
delay: userDelay,
5353
avatar: userAvatar,
5454
};
55+
const defaultCustomSettings = {
56+
delay: customDelay,
57+
};
5558

5659
for (let i = 0, len = this.props.steps.length; i < len; i += 1) {
5760
const step = this.props.steps[i];
58-
const settings = step.user ? defaulUserSettings : defaulBotSettings;
61+
let settings = {};
62+
63+
if (step.user || step.asMessage) {
64+
settings = defaultUserSettings;
65+
} else if (step.message) {
66+
settings = defaultBotSettings;
67+
} else if (step.component) {
68+
settings = defaultCustomSettings;
69+
}
5970

6071
steps[step.id] = Object.assign(
6172
{},
@@ -69,8 +80,8 @@ class ChatBot extends Component {
6980
const previousSteps = [steps[currentStep.id]];
7081

7182
this.setState({
72-
defaulBotSettings,
73-
defaulUserSettings,
83+
defaultBotSettings,
84+
defaultUserSettings,
7485
steps,
7586
currentStep,
7687
renderedSteps,
@@ -118,7 +129,7 @@ class ChatBot extends Component {
118129
renderedSteps,
119130
previousSteps,
120131
steps,
121-
defaulUserSettings,
132+
defaultUserSettings,
122133
} = this.state;
123134
let { currentStep, previousStep } = this.state;
124135
const isEnd = currentStep.end;
@@ -140,7 +151,7 @@ class ChatBot extends Component {
140151
{},
141152
currentStep,
142153
option,
143-
defaulUserSettings,
154+
defaultUserSettings,
144155
{
145156
user: true,
146157
trigger: option.trigger,
@@ -271,7 +282,7 @@ class ChatBot extends Component {
271282
renderedSteps,
272283
previousSteps,
273284
inputValue,
274-
defaulUserSettings,
285+
defaultUserSettings,
275286
} = this.state;
276287
let { currentStep } = this.state;
277288

@@ -285,7 +296,7 @@ class ChatBot extends Component {
285296

286297
currentStep = Object.assign(
287298
{},
288-
defaulUserSettings,
299+
defaultUserSettings,
289300
currentStep,
290301
step,
291302
);
@@ -358,7 +369,6 @@ class ChatBot extends Component {
358369
avatarStyle,
359370
bubbleStyle,
360371
customStyle,
361-
customDelay,
362372
hideBotAvatar,
363373
hideUserAvatar,
364374
} = this.props;
@@ -381,7 +391,6 @@ class ChatBot extends Component {
381391
return (
382392
<CustomStep
383393
key={index}
384-
delay={customDelay}
385394
step={step}
386395
steps={steps}
387396
style={customStyle}

lib/schemas/customSchema.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ export default [
2929
types: ['string', 'number'],
3030
required: false,
3131
},
32+
{
33+
key: 'delay',
34+
types: ['number'],
35+
required: false,
36+
},
3237
{
3338
key: 'end',
3439
types: ['boolean'],

lib/schemas/textSchema.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ export default [
1919
types: ['string', 'number'],
2020
required: false,
2121
},
22+
{
23+
key: 'delay',
24+
types: ['number'],
25+
required: false,
26+
},
2227
{
2328
key: 'end',
2429
types: ['boolean'],

lib/steps/custom/CustomStep.jsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ class CustomStep extends Component {
1616
}
1717

1818
componentDidMount() {
19-
const { delay } = this.props;
20-
const { waitAction } = this.props.step;
19+
const { step } = this.props;
20+
const { delay, waitAction } = step;
21+
2122
setTimeout(() => {
2223
this.setState({ loading: false }, () => {
2324
if (!waitAction) {
@@ -47,16 +48,17 @@ class CustomStep extends Component {
4748
className="rsc-cs"
4849
style={style}
4950
>
50-
{ loading ? (
51-
<Loading />
52-
) : this.renderComponent() }
51+
{
52+
loading ? (
53+
<Loading />
54+
) : this.renderComponent()
55+
}
5356
</CustomStepContainer>
5457
);
5558
}
5659
}
5760

5861
CustomStep.propTypes = {
59-
delay: PropTypes.number.isRequired,
6062
step: PropTypes.object.isRequired,
6163
steps: PropTypes.object.isRequired,
6264
style: PropTypes.object.isRequired,

lib/steps/options/OptionElement.jsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,12 @@ import styled from 'styled-components';
22
import defaultTheme from '../../theme';
33

44
const OptionElement = styled.a`
5-
background: ${props => props.user
6-
? props.theme.userBubbleColor
7-
: props.theme.botBubbleColor};
5+
background: ${({ theme }) => theme.botBubbleColor};
86
border-radius: 22px;
97
box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.15);
10-
color: ${props => props.user
11-
? props.theme.userFontColor
12-
: props.theme.botFontColor};
8+
color: ${({ theme }) => theme.botFontColor};
139
display: inline-block;
10+
font-size: 14px;
1411
padding: 12px;
1512
1613
&:hover { opacity: .7; }

src/components/Example.jsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,27 @@ const otherFontTheme = {
1313
userFontColor: '#4a4a4a',
1414
};
1515

16+
const Test = () =>
17+
<div>Hello</div>;
18+
1619
const steps = [
1720
{
1821
id: '1',
1922
message: 'Hello World',
23+
delay: 1000,
24+
trigger: '2',
25+
},
26+
{
27+
id: '2',
28+
options: [
29+
{ value: 'a', label: 'aaa', trigger: '3' },
30+
],
31+
},
32+
{
33+
id: '3',
34+
component: <Test />,
35+
// asMessage: true,
36+
// delay: 0,
2037
end: true,
2138
},
2239
];

0 commit comments

Comments
 (0)