|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +import { DtmfTone } from '@azure/communication-calling'; |
| 5 | +import { Dialpad, FluentThemeProvider } from '@azure/communication-react'; |
| 6 | +import { Stack } from '@fluentui/react'; |
| 7 | +import React, { useState } from 'react'; |
| 8 | + |
| 9 | +export const CustomDialpadExample: (props: { isTouchOnlyDevice?: boolean }) => JSX.Element = (props: { |
| 10 | + isTouchOnlyDevice?: boolean; |
| 11 | +}) => { |
| 12 | + const [dtmftone, setDtmftone] = useState(''); |
| 13 | + const [buttonValue, setButtonValue] = useState(''); |
| 14 | + const [buttonIndex, setButtonIndex] = useState(''); |
| 15 | + const [textFieldValue, setTextFieldValue] = useState(''); |
| 16 | + |
| 17 | + const onSendDtmfTone = (dtmfTone: DtmfTone): Promise<void> => { |
| 18 | + setDtmftone(dtmfTone); |
| 19 | + return Promise.resolve(); |
| 20 | + }; |
| 21 | + |
| 22 | + const onClickDialpadButton = (buttonValue: string, buttonIndex: number): void => { |
| 23 | + setButtonValue(buttonValue); |
| 24 | + setButtonIndex(buttonIndex.toString()); |
| 25 | + }; |
| 26 | + |
| 27 | + const onChange = (input: string): void => { |
| 28 | + // if there is already a plus sign at the front remove it |
| 29 | + if (input[0] === '+') { |
| 30 | + input = input.slice(1, input.length); |
| 31 | + } |
| 32 | + // add + sign and brackets to format phone number |
| 33 | + if (input.length < 4 && input.length > 0) { |
| 34 | + // store the new value in textFieldValue and pass it back to dialpad textfield |
| 35 | + setTextFieldValue(`+ ${input}`); |
| 36 | + } else if (input.length >= 4) { |
| 37 | + // store the new value in textFieldValue and pass it back to dialpad textfield |
| 38 | + setTextFieldValue(`+ (${input.slice(0, 3)}) ${input.slice(3, input.length)}`); |
| 39 | + } else { |
| 40 | + // store the new value in textFieldValue and pass it back to dialpad textfield |
| 41 | + setTextFieldValue(input); |
| 42 | + } |
| 43 | + }; |
| 44 | + |
| 45 | + return ( |
| 46 | + <FluentThemeProvider> |
| 47 | + <Stack> |
| 48 | + <div style={{ fontSize: '1.5rem', marginBottom: '1rem', fontFamily: 'Segoe UI' }}>DTMF Tone: {dtmftone}</div> |
| 49 | + <div style={{ fontSize: '1.5rem', marginBottom: '1rem', fontFamily: 'Segoe UI' }}> |
| 50 | + Button Clicked: {buttonValue} index at {buttonIndex} |
| 51 | + </div> |
| 52 | + |
| 53 | + <Dialpad |
| 54 | + onSendDtmfTone={onSendDtmfTone} |
| 55 | + textFieldValue={textFieldValue} |
| 56 | + onClickDialpadButton={onClickDialpadButton} |
| 57 | + onChange={onChange} |
| 58 | + disableDtmfPlayback={true} |
| 59 | + longPressTrigger={props.isTouchOnlyDevice ? 'touch' : 'mouseAndTouch'} |
| 60 | + /> |
| 61 | + </Stack> |
| 62 | + </FluentThemeProvider> |
| 63 | + ); |
| 64 | +}; |
0 commit comments