Skip to content

Commit

Permalink
Updates to click to copy
Browse files Browse the repository at this point in the history
  • Loading branch information
julianguyen committed Mar 4, 2019
1 parent fa482cd commit 70c294e
Show file tree
Hide file tree
Showing 19 changed files with 103 additions and 2,990 deletions.
8 changes: 0 additions & 8 deletions app/assets/stylesheets/secret_share/secret_share_content.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,3 @@
background: $white-80;
@include dashboardSectionStyles();
}

.secretShare {
#secretShareLink {
background-color: $white;
color: $mulberry;
font-size: 1em;
}
}
29 changes: 11 additions & 18 deletions app/helpers/moments_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,17 @@ def moments_stats
result + '</div>'
end

def secret_share_url_props(moment)
secret_share_url_input(moment)
end

private

def secret_share_url_input(moment)
{ inputs: [
{
id: 'secretShareLink',
type: 'text',
name: 'secretShareInput',
readOnly: true,
value: secret_share_url(moment.secret_share_identifier) || nil,
dark: true,
copyOnClick: true
}
], action: moment_path(moment) }
def secret_share_props(moment)
{ inputs: [{
id: 'secretShare',
type: 'text',
name: 'secretShare',
label: t('moments.secret_share.singular'),
readOnly: true,
value: secret_share_url(moment.secret_share_identifier) || nil,
dark: true,
copyOnClick: t('moments.secret_share.link_copied')
}], action: moment_path(moment) }
end

private
Expand Down
5 changes: 1 addition & 4 deletions app/views/moments/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@
<% if @moment.owned_by?(current_user) && @moment.shared? %>
<div class="smallMarginTop">
<div class="label" id="secretShare">
<%= label_tag t('moments.secret_share.singular') %>
</div>
<%= react_component('Form', props: secret_share_url_props) %>
<%= react_component('Form', props: secret_share_link_props(@moment)) %>
</div>
<% end %>
Expand Down
70 changes: 36 additions & 34 deletions client/app/components/Input/InputDefault.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// @flow
import React, { Fragment } from 'react';
import { ToastContainer, toast } from 'react-toastify';
import React from 'react';
import css from './Input.scss';
import 'react-toastify/dist/ReactToastify.css';

export const REQUIRES_DEFAULT = ['text', 'number', 'time', 'date', 'hidden'];
export const DEFAULT_WITH_LABEL = ['text', 'number', 'time', 'date'];
Expand All @@ -23,15 +21,17 @@ export type Props = {
hasError?: Function,
myRef?: any,
label?: string,
onClick?: Function,
copyOnClick?: boolean,
copyOnClick?: string,
};

const copyToClipBoard = (e: SyntheticEvent<HTMLInputElement>) => {
const copyToClipBoard = (
e: SyntheticEvent<HTMLInputElement>,
copyOnClick: string,
) => {
const { document, alert } = window;
e.currentTarget.select();
document.execCommand('copy');
// TODO: Once i18n and react are playing nicely this can be changed
toast('Secret Share Link Copied!', { autoClose: 5000 });
alert(copyOnClick);
};

const onFocus = (required: ?boolean, hasError: ?Function) => {
Expand Down Expand Up @@ -72,31 +72,33 @@ export const InputDefault = (props: Props) => {
} = props;
if (!REQUIRES_DEFAULT.includes(type)) return null;
return (
<Fragment>
<input
className={css.default}
id={id}
type={type}
name={name}
defaultValue={value}
placeholder={placeholder}
readOnly={readOnly}
disabled={disabled}
required={required}
minLength={minLength}
maxLength={maxLength}
min={min}
max={max}
onFocus={() => onFocus(required, hasError)}
onBlur={(e: SyntheticEvent<HTMLInputElement>) => onBlur(e, required, hasError)
}
ref={myRef}
aria-label={label}
aria-invalid={hasError}
onClick={copyOnClick ? (e: SyntheticEvent<HTMLInputElement>) => { copyToClipBoard(e); }
: () => {}}
/>
<ToastContainer />
</Fragment>
<input
className={css.default}
id={id}
type={type}
name={name}
defaultValue={value}
placeholder={placeholder}
readOnly={readOnly}
disabled={disabled}
required={required}
minLength={minLength}
maxLength={maxLength}
min={min}
max={max}
onFocus={() => onFocus(required, hasError)}
onBlur={(e: SyntheticEvent<HTMLInputElement>) => onBlur(e, required, hasError)
}
ref={myRef}
aria-label={label}
aria-invalid={hasError}
onClick={
copyOnClick
? (e: SyntheticEvent<HTMLInputElement>) => {
copyToClipBoard(e, copyOnClick);
}
: () => {}
}
/>
);
};
29 changes: 14 additions & 15 deletions client/app/components/Input/__tests__/InputDefault.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,28 +59,27 @@ describe('InputDefault', () => {
});
});

describe('copyOnClick prop type', () => {
it('adds onClick prop ', () => {
describe('has valid copyOnClick prop', () => {
it('copies to clipboard when input is clicked', () => {
jest.spyOn(window, 'alert');
jest.spyOn(window.document, 'execCommand');
const copyOnClick = 'Some message';
const wrapper = shallow(
<InputDefault
id={id}
type="text"
name={name}
copyOnClick="true"
copyOnClick={copyOnClick}
/>,
);
expect(wrapper.find('input').prop('onClick').onClick).to.equal();
});

it('does not add onClick prop', () => {
const wrapper = shallow(
<InputDefault
id={id}
type="text"
name={name}
/>,
);
expect(wrapper.props().onClick).to.equal(10);
wrapper.find('input').simulate('click', {
currentTarget: {
value: 'test',
select: () => {},
},
});
expect(window.document.execCommand).toHaveBeenCalledWith('copy');
expect(window.alert).toHaveBeenCalledWith(copyOnClick);
});
});
});
1 change: 1 addition & 0 deletions client/app/components/Input/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export type Props = {
accordionOpen?: boolean,
formNoValidate?: boolean,
googleAPIKey?: string,
copyOnClick?: string,
};

export type State = {
Expand Down
5 changes: 2 additions & 3 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@
"react-html-parser": "^2.0.2",
"react-lazyload": "^2.3.0",
"react-on-rails": "11.2.2",
"react-render-html": "^0.6.0",
"react-toastify": "^4.5.2"
"react-render-html": "^0.6.0"
},
"devDependencies": {
"@babel/core": "^7.1.2",
Expand Down Expand Up @@ -105,7 +104,7 @@
},
"jest": {
"moduleNameMapper": {
"\\.(css|scss)$": "identity-obj-proxy"
"\\.scss$": "identity-obj-proxy"
},
"setupFiles": [
"./app/libs/testHelper.js"
Expand Down
Loading

0 comments on commit 70c294e

Please sign in to comment.