Skip to content

Commit

Permalink
Add custom copy value resolver (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
sixlive authored Jan 21, 2019
1 parent de6e395 commit 5560027
Show file tree
Hide file tree
Showing 6 changed files with 12,934 additions and 5 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,23 @@ TextCopy::make('Some Field', 'some_field')
->copyButtonTitle('Some alternative title')
```

### Alternative copy value
You can choose to mutate and that is copied to the users clipboard. You can either pass a value or a Closure.

```php
TextCopy::make('Some Secret String', 'some_long_string')
->copyValue(function ($value) {
return substr($value, -6);
})
```

or

```php
TextCopy::make('Some Secret String', 'some_long_string')
->copyValue('some fixed copy value')
```

## Screenshots
### Default State
![default](.docs/default.png)
Expand Down
12,892 changes: 12,891 additions & 1 deletion dist/js/field.js

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions resources/js/components/DetailField.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<div slot="value" class="flex">
<div class="flex-no-shrink">{{ fieldDisplayValue }}</div>
<copy-button
:value="field.value"
:value="copyFieldValue"
:title="copyButtonTitleValue"
class="w-4 mx-3" />
</div>
Expand All @@ -12,7 +12,7 @@

<script>
import CopyButton from './CopyButton'
import { filterField, copyButtonTitle } from '../utilities'
import { filterField, copyButtonTitle, copyValue } from '../utilities'
export default {
props: ['resource', 'resourceName', 'resourceId', 'field'],
Expand All @@ -25,6 +25,9 @@ export default {
},
copyButtonTitleValue() {
return copyButtonTitle(this.field)
},
copyFieldValue() {
return copyValue(this.field)
}
}
}
Expand Down
7 changes: 5 additions & 2 deletions resources/js/components/IndexField.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
<span class="flex">
<div class="flex-no-shrink">{{ fieldDisplayValue }}</div>
<copy-button
:value="field.value"
:value="copyFieldValue"
:title="copyButtonTitleValue"
class="w-4 mx-3" />
</span>
</template>

<script>
import CopyButton from './CopyButton'
import { filterField, copyButtonTitle } from '../utilities'
import { filterField, copyButtonTitle, copyValue } from '../utilities'
export default {
components: { CopyButton },
Expand All @@ -21,6 +21,9 @@ export default {
},
copyButtonTitleValue() {
return copyButtonTitle(this.field)
},
copyFieldValue() {
return copyValue(this.field)
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions resources/js/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@ export function copyButtonTitle (field) {
return field.copy_button_title || `Copy ${field.name}`
}

export function copyValue (field) {
return field.copy_value || field.value
}

export default filterField
12 changes: 12 additions & 0 deletions src/TextCopy.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Sixlive\TextCopy;

use Closure;
use Laravel\Nova\Fields\Field;

class TextCopy extends Field
Expand Down Expand Up @@ -50,4 +51,15 @@ public function copyButtonTitle($title = '')

return $this;
}

public function copyValue($value)
{
$this->withMeta([
'copy_value' => $value instanceof Closure
? $value($this->value)
: $value,
]);

return $this;
}
}

0 comments on commit 5560027

Please sign in to comment.